Skip to content

Commit

Permalink
Added working slot processing. (#6779)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 authored Feb 5, 2023
1 parent b5b79b8 commit ee4c8ed
Show file tree
Hide file tree
Showing 23 changed files with 523 additions and 192 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ erigon: go-version erigon.cmd
@rm -f $(GOBIN)/tg # Remove old binary to prevent confusion where users still use it because of the scripts

COMMANDS += devnet
COMMANDS += erigon-el-mock
COMMANDS += downloader
COMMANDS += erigon-cl
COMMANDS += hack
Expand Down
2 changes: 0 additions & 2 deletions cl/clparams/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ var GenesisConfigs map[NetworkType]GenesisConfig = map[NetworkType]GenesisConfig
// Trusted checkpoint sync endpoints: https://eth-clients.github.io/checkpoint-sync-endpoints/
var CheckpointSyncEndpoints = map[NetworkType][]string{
MainnetNetwork: {
"https://beaconstate.ethstaker.cc",
"https://sync.invis.tools/eth/v2/debug/beacon/states/finalized",
"https://mainnet-checkpoint-sync.attestant.io/eth/v2/debug/beacon/states/finalized",
"https://mainnet.checkpoint.sigp.io/eth/v2/debug/beacon/states/finalized",
Expand All @@ -201,7 +200,6 @@ var CheckpointSyncEndpoints = map[NetworkType][]string{
},
GoerliNetwork: {
"https://goerli.beaconstate.info/eth/v2/debug/beacon/states/finalized",
"https://goerli.beaconstate.ethstaker.cc/eth/v2/debug/beacon/states/finalized",
"https://goerli-sync.invis.tools/eth/v2/debug/beacon/states/finalized",
"https://goerli.checkpoint-sync.ethdevops.io/eth/v2/debug/beacon/states/finalized",
"https://prater-checkpoint-sync.stakely.io/eth/v2/debug/beacon/states/finalized",
Expand Down
3 changes: 1 addition & 2 deletions cl/cltypes/eth1_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
type Eth1Block struct {
Header *types.Header
// Transactions can be kept in bytes.
Body *types.RawBody
version clparams.StateVersion
Body *types.RawBody
}

func (b *Eth1Block) NumberU64() uint64 {
Expand Down
2 changes: 0 additions & 2 deletions cl/cltypes/lightclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ func (l *LightClientBootstrap) DecodeSSZWithVersion(buf []byte, version int) err
l.version = clparams.StateVersion(version)

if len(buf) < l.EncodingSizeSSZ() {
fmt.Println(len(buf))
fmt.Println(l.EncodingSizeSSZ())
return ssz_utils.ErrLowBufferSize
}
l.Header = new(LightClientHeader)
Expand Down
2 changes: 0 additions & 2 deletions cl/cltypes/network_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cltypes_test

import (
"fmt"
"testing"

libcommon "github.com/ledgerwatch/erigon-lib/common"
Expand Down Expand Up @@ -116,7 +115,6 @@ func TestMarshalNetworkTypes(t *testing.T) {
&cltypes.LightClientBootstrap{},
}
for i, tc := range cases {
fmt.Println(i)
marshalledBytes, err := tc.EncodeSSZ(nil)
require.NoError(t, err)
require.Equal(t, len(marshalledBytes), tc.EncodingSizeSSZ())
Expand Down
3 changes: 0 additions & 3 deletions cl/cltypes/types_encoding.go

This file was deleted.

1 change: 0 additions & 1 deletion cmd/erigon-cl/core/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func RetrieveBeaconState(ctx context.Context, beaconConfig *clparams.BeaconChain
epoch := utils.GetCurrentEpoch(genesisConfig.GenesisTime, beaconConfig.SecondsPerSlot, beaconConfig.SlotsPerEpoch)

beaconState := state.New(beaconConfig)
fmt.Println(int(beaconConfig.GetCurrentStateVersion(epoch)))
err = beaconState.DecodeSSZWithVersion(marshaled, int(beaconConfig.GetCurrentStateVersion(epoch)))
if err != nil {
return nil, fmt.Errorf("checkpoint sync failed %s", err)
Expand Down
12 changes: 6 additions & 6 deletions cmd/erigon-cl/core/rawdb/accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,21 @@ func WriteBeaconBlock(tx kv.RwTx, signedBlock *cltypes.SignedBeaconBlock) error
return tx.Put(kv.BeaconBlocks, key, value)
}

func ReadBeaconBlock(tx kv.RwTx, slot uint64) (*cltypes.SignedBeaconBlock, error) {
signedBlock, _, _, _, err := ReadBeaconBlockForStorage(tx, slot)
func ReadBeaconBlock(tx kv.RwTx, slot uint64) (*cltypes.SignedBeaconBlock, uint64, libcommon.Hash, error) {
signedBlock, eth1Number, eth1Hash, _, err := ReadBeaconBlockForStorage(tx, slot)
if err != nil {
return nil, err
return nil, 0, libcommon.Hash{}, err
}
if signedBlock == nil {
return nil, nil
return nil, 0, libcommon.Hash{}, err
}

attestations, err := ReadAttestations(tx, slot)
if err != nil {
return nil, err
return nil, 0, libcommon.Hash{}, err
}
signedBlock.Block.Body.Attestations = attestations
return signedBlock, err
return signedBlock, eth1Number, eth1Hash, err
}

func ReadBeaconBlockForStorage(tx kv.Getter, slot uint64) (block *cltypes.SignedBeaconBlock, eth1Number uint64, eth1Hash libcommon.Hash, eth2Hash libcommon.Hash, err error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/erigon-cl/core/rawdb/accessors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestBeaconBlock(t *testing.T) {
}

require.NoError(t, rawdb.WriteBeaconBlock(tx, signedBeaconBlock))
newBlock, err := rawdb.ReadBeaconBlock(tx, signedBeaconBlock.Block.Slot)
newBlock, _, _, err := rawdb.ReadBeaconBlock(tx, signedBeaconBlock.Block.Slot)
require.NoError(t, err)
newBlock.Block.Body.ExecutionPayload = emptyBlock
newRoot, err := newBlock.HashSSZ()
Expand Down
7 changes: 6 additions & 1 deletion cmd/erigon-cl/core/state/accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/binary"
"fmt"
"sort"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/cl/clparams"
Expand Down Expand Up @@ -327,7 +328,7 @@ func (b *BeaconState) GetAttestationParticipationFlagIndicies(data *cltypes.Atte
if err != nil {
return nil, err
}
headRoot, err := b.GetBlockRoot(data.Slot)
headRoot, err := b.GetBlockRootAtSlot(data.Slot)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -362,6 +363,10 @@ func (b *BeaconState) GetIndexedAttestation(attestation *cltypes.Attestation) (*
if err != nil {
return nil, err
}
// Sort the the attestation indicies.
sort.Slice(attestingIndicies, func(i, j int) bool {
return attestingIndicies[i] < attestingIndicies[j]
})
return &cltypes.IndexedAttestation{
AttestingIndices: attestingIndicies,
Data: attestation.Data,
Expand Down
86 changes: 86 additions & 0 deletions cmd/erigon-cl/core/transition/block_transition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package transition

import (
"errors"
"fmt"

"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
)

// processBlock takes a block and transition said block. Important: it assumes execution payload is correct.
func (s *StateTransistor) processBlock(signedBlock *cltypes.SignedBeaconBlock) error {
block := signedBlock.Block
if signedBlock.Version() != s.state.Version() {
return fmt.Errorf("wrong state version for block at slot %d", block.Slot)
}
if err := s.ProcessBlockHeader(block); err != nil {
return fmt.Errorf("ProcessBlockHeader: %s", err)
}
if s.state.Version() >= clparams.BellatrixVersion {
// Set execution header accordingly to state.
s.state.SetLatestExecutionPayloadHeader(block.Body.ExecutionPayload.Header)
}
if err := s.ProcessRandao(block.Body.RandaoReveal); err != nil {
return fmt.Errorf("ProcessRandao: %s", err)
}
if err := s.ProcessEth1Data(block.Body.Eth1Data); err != nil {
return fmt.Errorf("ProcessEth1Data: %s", err)
}
// Do operationns
if err := s.processOperations(block.Body); err != nil {
return fmt.Errorf("processOperations: %s", err)
}
// Process altair data
if s.state.Version() >= clparams.AltairVersion {
if err := s.ProcessSyncAggregate(block.Body.SyncAggregate); err != nil {
return fmt.Errorf("ProcessSyncAggregate: %s", err)
}
}
return nil
}

func (s *StateTransistor) processOperations(blockBody *cltypes.BeaconBody) error {
if len(blockBody.Deposits) != int(s.maximumDeposits()) {
return errors.New("outstanding deposits do not match maximum deposits")
}
// Process each proposer slashing
for _, slashing := range blockBody.ProposerSlashings {
if err := s.ProcessProposerSlashing(slashing); err != nil {
return fmt.Errorf("ProcessProposerSlashing: %s", err)
}
}
// Process each attester slashing
for _, slashing := range blockBody.AttesterSlashings {
if err := s.ProcessAttesterSlashing(slashing); err != nil {
return fmt.Errorf("ProcessAttesterSlashing: %s", err)
}
}
// Process each attestations
for _, att := range blockBody.Attestations {
if err := s.ProcessAttestation(att); err != nil {
return fmt.Errorf("ProcessAttestation: %s", err)
}
}
// Process each deposit
for _, dep := range blockBody.Deposits {
if err := s.ProcessDeposit(dep); err != nil {
return fmt.Errorf("ProcessDeposit: %s", err)
}
}
// Process each voluntary exit.
for _, exit := range blockBody.VoluntaryExits {
if err := s.ProcessVoluntaryExit(exit); err != nil {
return fmt.Errorf("ProcessVoluntaryExit: %s", err)
}
}
return nil
}

func (s *StateTransistor) maximumDeposits() (maxDeposits uint64) {
maxDeposits = s.state.Eth1Data().DepositCount - s.state.Eth1DepositIndex()
if maxDeposits > s.beaconConfig.MaxDeposits {
maxDeposits = s.beaconConfig.MaxDeposits
}
return
}
2 changes: 1 addition & 1 deletion cmd/erigon-cl/core/transition/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func GetSetIntersection(v1, v2 []uint64) []uint64 {
func isValidIndexedAttestation(state *state.BeaconState, att *cltypes.IndexedAttestation) (bool, error) {
inds := att.AttestingIndices
if len(inds) == 0 || !IsSortedSet(inds) {
return false, fmt.Errorf("invalid attesting indices")
return false, fmt.Errorf("isValidIndexedAttestation: attesting indices are not sorted or are null")
}

pks := [][]byte{}
Expand Down
2 changes: 0 additions & 2 deletions cmd/erigon-cl/core/transition/operations_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package transition

import (
"fmt"
"testing"

libcommon "github.com/ledgerwatch/erigon-lib/common"
Expand Down Expand Up @@ -367,7 +366,6 @@ func TestProcessVoluntaryExits(t *testing.T) {
ActivationEpoch: 0,
})
state.SetSlot((clparams.MainnetBeaconConfig.SlotsPerEpoch * 5) + (clparams.MainnetBeaconConfig.SlotsPerEpoch * clparams.MainnetBeaconConfig.ShardCommitteePeriod))
fmt.Println(state.Slot())
transitioner := New(state, &clparams.MainnetBeaconConfig, nil, true)

require.NoError(t, transitioner.ProcessVoluntaryExit(exit), "Could not process exits")
Expand Down
21 changes: 17 additions & 4 deletions cmd/erigon-cl/core/transition/process_slots.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package transition

import (
"errors"
"fmt"

"github.com/Giulio2002/bls"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/fork"
)

func (s *StateTransistor) transitionState(block *cltypes.SignedBeaconBlock) error {
func (s *StateTransistor) TransitionState(block *cltypes.SignedBeaconBlock) error {
currentBlock := block.Block
s.processSlots(currentBlock.Slot)
if !s.noValidate {
Expand All @@ -19,6 +21,10 @@ func (s *StateTransistor) transitionState(block *cltypes.SignedBeaconBlock) erro
return fmt.Errorf("block not valid")
}
}
// Transition block
if err := s.processBlock(block); err != nil {
return err
}
// TODO add logic to process block and update state.
if !s.noValidate {
expectedStateRoot, err := s.state.HashSSZ()
Expand Down Expand Up @@ -66,6 +72,10 @@ func (s *StateTransistor) processSlots(slot uint64) error {
if err != nil {
return fmt.Errorf("unable to process slot transition: %v", err)
}
// TODO(Someone): Add epoch transition.
if (stateSlot+1)%s.beaconConfig.SlotsPerEpoch == 0 {
return errors.New("cannot transition epoch: not implemented")
}
// TODO: add logic to process epoch updates.
stateSlot += 1
s.state.SetSlot(stateSlot)
Expand All @@ -78,10 +88,13 @@ func (s *StateTransistor) verifyBlockSignature(block *cltypes.SignedBeaconBlock)
if err != nil {
return false, err
}
sigRoot, err := block.Block.Body.HashSSZ()
domain, err := s.state.GetDomain(s.beaconConfig.DomainBeaconProposer, s.state.Epoch())
if err != nil {
return false, err
}
sigRoot, err := fork.ComputeSigningRoot(block.Block, domain)
if err != nil {
return false, err
}
sig := block.Signature
return bls.Verify(sig[:], sigRoot[:], proposer.PublicKey[:])
return bls.Verify(block.Signature[:], sigRoot[:], proposer.PublicKey[:])
}
Loading

0 comments on commit ee4c8ed

Please sign in to comment.