Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

reduce the amount of inout usages #23

Open
decanus opened this issue Jan 18, 2019 · 2 comments
Open

reduce the amount of inout usages #23

decanus opened this issue Jan 18, 2019 · 2 comments
Milestone

Comments

@decanus
Copy link
Member

decanus commented Jan 18, 2019

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

@decanus decanus added this to the refactoring and cleanup milestone Jan 19, 2019
@apbendi
Copy link

apbendi commented Mar 10, 2019

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!

@apbendi
Copy link

apbendi commented Mar 10, 2019

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants