Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor States To Allow for Single Cached Hasher #9922

Merged
merged 21 commits into from
Nov 29, 2021

Conversation

nisdas
Copy link
Member

@nisdas nisdas commented Nov 19, 2021

What type of PR is this?

Refactor

What does this PR do? Why is it needed?

  • Currently our cached state hasher is duplicated across 3 states. This PR instantiates it in the stateutil package so that
    only a single implementation of it exists. This reduces duplication as now it only has to be implemented once rather than multiple times for each state type.

Which issues(s) does this PR fix?

Fixes #

Other notes for review

@nisdas nisdas added the Ready For Review A pull request ready for code review label Nov 19, 2021
@nisdas nisdas requested a review from a team as a code owner November 19, 2021 09:57
rkapka
rkapka previously approved these changes Nov 22, 2021
Comment on lines +30 to +39
if len(prevLeaves) != len(leaves) {
res, err := h.merkleizeWithCache(leaves, length, fieldName, hashFunc)
if err != nil {
return [32]byte{}, err
}
if h.rootsCache != nil {
leavesCache[fieldName] = leaves
}
return res, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit out of scope for a refactor

@terencechain
Copy link
Member

can we merge v3 state PRs first before we merge this? it'll be less disparity and prone to error

Copy link
Contributor

@potuz potuz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went though it and didn't find anything, but seems hard to review this seriously without spending the whole day. If the Spec tests are comprehensive for all states, and this passes spec tests I'd be inclined to approve this.

Copy link
Member

@terencechain terencechain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

)

var (
leavesCache = make(map[string][][32]byte, params.BeaconConfig().BeaconStateMergeFieldCount)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we just always taking the highest field count here? if yes, could we please add a comment to clarify?


// RootsArrayHashTreeRoot computes the Merkle root of arrays of 32-byte hashes, such as [64][32]byte
// according to the Simple Serialize specification of Ethereum.
func RootsArrayHashTreeRoot(vals [][]byte, length uint64, fieldName string) ([32]byte, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only used in tests, can we delete this function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used in runtime as part of computeFieldRoots. This is why we continue using it, this should be removed later but I will need a bit more data to be sure on this.

return [32]byte{}, errors.New("nil pending attestation")
}
// Marshal attestation to determine if it exists in the cache.
enc := PendingAttEncKey(att)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PendingAttEncKey no longer needs to be exported

}
}

res, err := PendingAttRootWithHasher(hasher, att)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PendingAttRootWithHasher no longer needs to be exported

if eth1Data == nil {
return [32]byte{}, errors.New("nil eth1 data")
}

enc := stateutil.Eth1DataEncKey(eth1Data)
enc := Eth1DataEncKey(eth1Data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eth1DataEncKey no longer needs to be exported

@@ -59,15 +68,15 @@ func (h *stateRootHasher) validatorRoot(hasher ssz.HashFn, validator *ethpb.Vali
return [32]byte{}, errors.New("nil validator")
}

enc := stateutil.ValidatorEncKey(validator)
enc := ValidatorEncKey(validator)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValidatorEncKey no longer needs to be exported

var NocachedHasher *stateRootHasher

// CachedHasher references a hasher that will utilize a roots cache.
var CachedHasher *stateRootHasher
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor downside with this is we have to export these hashers to be shared with state pkgs

terencechain
terencechain previously approved these changes Nov 29, 2021
Copy link
Member

@terencechain terencechain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@prylabs-bulldozer prylabs-bulldozer bot merged commit 37bc407 into develop Nov 29, 2021
@delete-merged-branch delete-merged-branch bot deleted the refactorFieldRoot branch November 29, 2021 16:30
rkapka added a commit that referenced this pull request Dec 3, 2021
This reverts commit 37bc407.

# Conflicts:
#	beacon-chain/state/fieldtrie/field_trie_test.go
#	beacon-chain/state/stateutil/trie_helpers_test.go
#	beacon-chain/state/v1/BUILD.bazel
#	beacon-chain/state/v1/field_roots.go
#	beacon-chain/state/v1/getters_misc.go
#	beacon-chain/state/v1/state_trie.go
#	beacon-chain/state/v2/BUILD.bazel
#	beacon-chain/state/v2/field_roots.go
#	beacon-chain/state/v2/state_trie.go
@rkapka rkapka mentioned this pull request Dec 3, 2021
rkapka added a commit that referenced this pull request Dec 3, 2021
)""

This reverts commit 928e05e.

# Conflicts:
#	beacon-chain/state/v3/BUILD.bazel
#	beacon-chain/state/v3/field_roots.go
#	beacon-chain/state/v3/state_trie.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready For Review A pull request ready for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants