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

Passing all finality consensus-spec-tests for altair/bellatrix #6894

Merged
merged 5 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd/ef-tests-cl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"os"
"path"

"github.com/ledgerwatch/log/v3"
)

func executeTest(p string) (bool, error) {

log.Root().SetHandler(log.LvlFilterHandler(log.LvlCrit, log.StderrHandler))
initialPath, err := os.Getwd()
if err != nil {
return false, err
Expand All @@ -18,5 +20,6 @@ func executeTest(p string) (bool, error) {
err = fn()
}
os.Chdir(initialPath)
log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StderrHandler))
return implemented, err
}
156 changes: 29 additions & 127 deletions cmd/erigon-cl/core/state/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,7 @@ func (b *BeaconState) HashSSZ() ([32]byte, error) {
return merkle_tree.MerkleRootFromLeaves(b.leaves[:])
}

// An hash component is a payload for a specific state leaves given base inner leaves.
type hashComponent struct {
hashF func() ([32]byte, error)
index int
}

// An hash component is a payload for a specific state leaves given base inner leaves.
type hashResult struct {
root libcommon.Hash
index int
err error
}

// computeRandaoMixesHash computes the randao mix hash in hopefully less time than required.
func (b *BeaconState) computeRandaoMixesHash() ([32]byte, error) {
mixes := utils.PreparateRootsForHashing(b.randaoMixes[:])
// Divide computation in 8 threads
hashComponents := []*hashComponent{}
numThreads := 16
branchSize := len(b.randaoMixes) / numThreads // should be 8192
for i := 0; i < numThreads; i++ {
leaves := make([][32]byte, branchSize)
copy(leaves, mixes[i*branchSize:])
hashComponents = append(hashComponents, &hashComponent{
hashF: func() ([32]byte, error) {
root, err := merkle_tree.ArraysRoot(leaves, uint64(branchSize))
if err != nil {
return [32]byte{}, err
}
return root, nil
},
index: i,
})
}
merkleLayer := make([][32]byte, numThreads)
resultCh := make(chan hashResult)
for _, component := range hashComponents {
go hashComponentWorker(component, resultCh)
}
for range hashComponents {
result := <-resultCh
if result.err != nil {
return [32]byte{}, result.err
}
merkleLayer[result.index] = result.root
}
return merkle_tree.ArraysRoot(merkleLayer, uint64(numThreads))
}

func (b *BeaconState) computeDirtyLeaves() error {
hashComponents := []*hashComponent{}
// Update all dirty leafs
// ----

Expand Down Expand Up @@ -106,46 +56,30 @@ func (b *BeaconState) computeDirtyLeaves() error {

// Field(5): BlockRoots
if b.isLeafDirty(BlockRootsLeafIndex) {
// Make the hash component.
hashComponents = append(hashComponents, &hashComponent{
hashF: func() ([32]byte, error) {
root, err := merkle_tree.ArraysRoot(utils.PreparateRootsForHashing(b.blockRoots[:]), state_encoding.BlockRootsLength)
if err != nil {
return [32]byte{}, err
}
return root, nil
},
index: int(BlockRootsLeafIndex),
})
root, err := merkle_tree.ArraysRoot(utils.PreparateRootsForHashing(b.blockRoots[:]), state_encoding.BlockRootsLength)
if err != nil {
return err
}
b.updateLeaf(BlockRootsLeafIndex, root)
}

// Field(6): StateRoots
if b.isLeafDirty(StateRootsLeafIndex) {
// Make the hash component.
hashComponents = append(hashComponents, &hashComponent{
hashF: func() ([32]byte, error) {
root, err := merkle_tree.ArraysRoot(utils.PreparateRootsForHashing(b.stateRoots[:]), state_encoding.StateRootsLength)
if err != nil {
return [32]byte{}, err
}
return root, nil
},
index: int(StateRootsLeafIndex),
})
root, err := merkle_tree.ArraysRoot(utils.PreparateRootsForHashing(b.stateRoots[:]), state_encoding.StateRootsLength)
if err != nil {
return err
}
b.updateLeaf(StateRootsLeafIndex, root)

}

// Field(7): HistoricalRoots
if b.isLeafDirty(HistoricalRootsLeafIndex) {
hashComponents = append(hashComponents, &hashComponent{
hashF: func() ([32]byte, error) {
root, err := merkle_tree.ArraysRootWithLimit(utils.PreparateRootsForHashing(b.historicalRoots), state_encoding.HistoricalRootsLength)
if err != nil {
return [32]byte{}, err
}
return root, nil
},
index: int(HistoricalRootsLeafIndex),
})
root, err := merkle_tree.ArraysRootWithLimit(utils.PreparateRootsForHashing(b.historicalRoots), state_encoding.HistoricalRootsLength)
if err != nil {
return err
}
b.updateLeaf(HistoricalRootsLeafIndex, root)
}

// Field(8): Eth1Data
Expand All @@ -159,16 +93,11 @@ func (b *BeaconState) computeDirtyLeaves() error {

// Field(9): Eth1DataVotes
if b.isLeafDirty(Eth1DataVotesLeafIndex) {
hashComponents = append(hashComponents, &hashComponent{
hashF: func() ([32]byte, error) {
root, err := state_encoding.Eth1DataVectorRoot(b.eth1DataVotes)
if err != nil {
return [32]byte{}, err
}
return root, nil
},
index: int(Eth1DataVotesLeafIndex),
})
root, err := state_encoding.Eth1DataVectorRoot(b.eth1DataVotes)
if err != nil {
return err
}
b.updateLeaf(Eth1DataVotesLeafIndex, root)
}

// Field(10): Eth1DepositIndex
Expand Down Expand Up @@ -197,7 +126,7 @@ func (b *BeaconState) computeDirtyLeaves() error {

// Field(13): RandaoMixes
if b.isLeafDirty(RandaoMixesLeafIndex) {
root, err := b.computeRandaoMixesHash()
root, err := merkle_tree.ArraysRoot(preparateRootsForHashing(b.randaoMixes[:]), state_encoding.RandaoMixesLength)
if err != nil {
return err
}
Expand All @@ -206,16 +135,12 @@ func (b *BeaconState) computeDirtyLeaves() error {

// Field(14): Slashings
if b.isLeafDirty(SlashingsLeafIndex) {
hashComponents = append(hashComponents, &hashComponent{
hashF: func() ([32]byte, error) {
root, err := state_encoding.SlashingsRoot(b.slashings[:])
if err != nil {
return [32]byte{}, err
}
return root, nil
},
index: int(SlashingsLeafIndex),
})
root, err := state_encoding.SlashingsRoot(b.slashings[:])
if err != nil {
return err
}
b.updateLeaf(SlashingsLeafIndex, root)

}
// Field(15): PreviousEpochParticipation
if b.isLeafDirty(PreviousEpochParticipationLeafIndex) {
Expand Down Expand Up @@ -330,31 +255,8 @@ func (b *BeaconState) computeDirtyLeaves() error {
b.updateLeaf(HistoricalSummariesLeafIndex, root)
}
}
// Execute hash components in parallel
resultCh := make(chan hashResult)
for _, component := range hashComponents {
go hashComponentWorker(component, resultCh)
}
for range hashComponents {
result := <-resultCh
if result.err != nil {
return result.err
}
b.updateLeaf(StateLeafIndex(result.index), result.root)
}
return nil
}

func hashComponentWorker(component *hashComponent, resultCh chan hashResult) {
root, err := component.hashF()
if err != nil {
resultCh <- hashResult{err: err}
return
}
resultCh <- hashResult{
root: root,
index: component.index,
}
return nil
}

func (b *BeaconState) updateLeaf(idx StateLeafIndex, leaf libcommon.Hash) {
Expand Down
38 changes: 21 additions & 17 deletions cmd/erigon-cl/core/transition/process_sync_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (s *StateTransistor) processSyncAggregate(sync *cltypes.SyncAggregate) ([][
if len(sync.SyncCommiteeBits)*8 > len(committeeKeys) {
return nil, errors.New("bits length exceeds committee length")
}
votedKeys := make([][]byte, 0, len(committeeKeys))
var votedKeys [][]byte

proposerReward, participantReward, err := s.state.SyncRewards()
if err != nil {
Expand All @@ -34,27 +34,31 @@ func (s *StateTransistor) processSyncAggregate(sync *cltypes.SyncAggregate) ([][
return nil, err
}

syncAggregateBits := sync.SyncCommiteeBits
earnedProposerReward := uint64(0)
for i := 0; i < len(sync.SyncCommiteeBits)*8; i++ {
vIdx, exists := s.state.ValidatorIndexByPubkey(committeeKeys[i])
// Impossible scenario.
if !exists {
return nil, errors.New("validator public key does not exist in state")
}
bit := i % 8
currByte := sync.SyncCommiteeBits[i/8]
if (currByte & (1 << bit)) > 0 {
votedKeys = append(votedKeys, committeeKeys[i][:])
if err := s.state.IncreaseBalance(vIdx, participantReward); err != nil {
return nil, err
currPubKeyIndex := 0
for i := range syncAggregateBits {
for bit := 1; bit <= 128; bit *= 2 {
vIdx, exists := s.state.ValidatorIndexByPubkey(committeeKeys[currPubKeyIndex])
// Impossible scenario.
if !exists {
return nil, errors.New("validator public key does not exist in state")
}
earnedProposerReward += proposerReward
} else {
if err := s.state.DecreaseBalance(vIdx, participantReward); err != nil {
return nil, err
if syncAggregateBits[i]&byte(bit) > 0 {
votedKeys = append(votedKeys, currentSyncCommittee.PubKeys[currPubKeyIndex][:])
if err := s.state.IncreaseBalance(vIdx, participantReward); err != nil {
return nil, err
}
earnedProposerReward += proposerReward
} else {
if err := s.state.DecreaseBalance(vIdx, participantReward); err != nil {
return nil, err
}
}
currPubKeyIndex++
}
}

return votedKeys, s.state.IncreaseBalance(proposerIndex, earnedProposerReward)
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
)

require (
github.com/Giulio2002/bls v0.0.0-20230210152409-898d831f215e
github.com/Giulio2002/bls v0.0.0-20230217173148-c87a29266b6c
github.com/RoaringBitmap/roaring v1.2.2
github.com/VictoriaMetrics/fastcache v1.12.0
github.com/VictoriaMetrics/metrics v1.23.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D
filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU=
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Giulio2002/bls v0.0.0-20230210152409-898d831f215e h1:L82i5amWncPiHr/nbCDn/lO8unE8fINWSpnmeUNYW6k=
github.com/Giulio2002/bls v0.0.0-20230210152409-898d831f215e/go.mod h1:xmItuVMbOARz49adX3QpSAjBf8yQei9XQHzKwp9YWgo=
github.com/Giulio2002/bls v0.0.0-20230217173148-c87a29266b6c h1:Die1JC9Eiec1trMccXTTVgdE11KYHPec1Z76AP3bSlE=
github.com/Giulio2002/bls v0.0.0-20230217173148-c87a29266b6c/go.mod h1:o6qWofeW8A1XImbo3eHbC/wXnw/dasu0YuHEtdrjYzw=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/RoaringBitmap/roaring v0.4.7/go.mod h1:8khRDP4HmeXns4xIj9oGrKSz7XTQiJx2zgh7AcNke4w=
github.com/RoaringBitmap/roaring v0.4.17/go.mod h1:D3qVegWTmfCaX4Bl5CrBE9hfrSrrXIr8KVNvRsDi1NI=
Expand Down