yeeth/BeaconChain.swift

reduce the amount of `inout` usages

decanus opened this issue · 2 comments

StateTransitions uses inout everywhere, might be nicer to use returns of the state.

Hey @decanus, poking around on here! I agree it would be good to move away from inouts, as they're definitely not considered "Swifty" in most cases.

I see a couple ways this could be accomplished. One would be to simply have each static method return a mutated BeaconState, so the implementation of processBlock for example would look like this:

static func processBlock(state: BeaconState, block: BeaconBlock) -> BeaconState {
        assert(state.slot == block.slot)
        
        var state = state

        state = blockSignature(state: state, block: block)
        state = randao(state: state, block: block)
        state =  eth1data(state: state, block: block)
        state = proposerSlashings(state: state, block: block)
        state = attesterSlashings(state: state, block: block)
        state = attestations(state: state, block: block)
        state = deposits(state: state, block: block)
        state = voluntaryExits(state: state, block: block)

       return state
    }

This would be a more functional style.

Alternatively, I can see an argument for a more OO style by using an extension to implement these as instance methods on the BeaconState struct itself. That would end up looking more like this:

static func processBlock(state: BeaconState, block: BeaconBlock) -> BeaconState {
        assert(state.slot == block.slot)
        
        var state = state
        
        return state
                .blockSignature(block: block)
                .randao(block: block)
                .eth1data(block: block)
                .proposerSlashings(block: block)
                .attesterSlashings(block: block)
                .attestations(block: block)
                .deposits(block: block)
                .voluntaryExits(block: block)
    }

I think the latter approach is a little more idiomatic, but don't feel strongly either way. What do you think? I might take a stab at this!

Update: prototyping with mutating instance funcs. LMK what you think! #74