Skip to content

Commit

Permalink
Add structs for capella fork (#126)
Browse files Browse the repository at this point in the history
ferranbt authored Mar 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 1523d19 commit ea61f0e
Showing 14 changed files with 2,711 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

.PHONY:
build-spec-tests:
go run github.com/ferranbt/fastssz/sszgen --path ./spectests/structs.go --exclude-objs Hash
go run github.com/ferranbt/fastssz/sszgen --path ./spectests/structs.go --exclude-objs Hash,Uint256
go run github.com/ferranbt/fastssz/sszgen --path ./tests

.PHONY:
get-spec-tests:
./scripts/download-spec-tests.sh v1.1.10
./scripts/download-spec-tests.sh v1.3.0-rc.0

.PHONY:
generate-testcases:
121 changes: 121 additions & 0 deletions spectests/structs.go
Original file line number Diff line number Diff line change
@@ -318,3 +318,124 @@ type ExecutionPayloadHeader struct {
BlockHash []byte `json:"block_hash" ssz-size:"32"`
TransactionsRoot []byte `json:"transactions_root" ssz-size:"32"`
}

// Capella types

type Uint256 [32]byte

type ExecutionPayloadCapella struct {
ParentHash [32]byte `ssz-size:"32" json:"parent_hash"`
FeeRecipient [20]byte `ssz-size:"20" json:"fee_recipient"`
StateRoot [32]byte `ssz-size:"32" json:"state_root"`
ReceiptsRoot [32]byte `ssz-size:"32" json:"receipts_root"`
LogsBloom [256]byte `ssz-size:"256" json:"logs_bloom"`
PrevRandao [32]byte `ssz-size:"32" json:"prev_randao"`
BlockNumber uint64 `json:"block_number"`
GasLimit uint64 `json:"gas_limit"`
GasUsed uint64 `json:"gas_used"`
Timestamp uint64 `json:"timestamp"`
ExtraData []byte `ssz-max:"32" json:"extra_data"`
BaseFeePerGas Uint256 `ssz-size:"32" json:"base_fee_per_gas"`
BlockHash [32]byte `ssz-size:"32" json:"block_hash"`
Transactions [][]byte `ssz-max:"1048576,1073741824" ssz-size:"?,?" json:"transactions"`
Withdrawals []*Withdrawal `json:"withdrawals" ssz-max:"16"`
}

type ExecutionPayloadHeaderCapella struct {
ParentHash [32]byte `json:"parent_hash" ssz-size:"32"`
FeeRecipient [20]byte `json:"fee_recipient" ssz-size:"20"`
StateRoot [32]byte `json:"state_root" ssz-size:"32"`
ReceiptsRoot [32]byte `json:"receipts_root" ssz-size:"32"`
LogsBloom [256]byte `json:"logs_bloom" ssz-size:"256"`
PrevRandao [32]byte `json:"prev_randao" ssz-size:"32"`
BlockNumber uint64 `json:"block_number"`
GasLimit uint64 `json:"gas_limit"`
GasUsed uint64 `json:"gas_used"`
Timestamp uint64 `json:"timestamp"`
ExtraData []byte `json:"extra_data" ssz-max:"32"`
BaseFeePerGas Uint256 `json:"base_fee_per_gas" ssz-size:"32"`
BlockHash [32]byte `json:"block_hash" ssz-size:"32"`
TransactionsRoot [32]byte `json:"transactions_root" ssz-size:"32"`
WithdrawalRoot [32]byte `json:"withdrawals_root" ssz-size:"32"`
}

type BLSToExecutionChange struct {
ValidatorIndex uint64 `json:"validator_index"`
FromBLSPubKey [48]byte `json:"from_bls_pubkey" ssz-size:"48"`
ToExecutionAddress [20]byte `json:"to_execution_address" ssz-size:"20"`
}

type HistoricalSummary struct {
BlockSummaryRoot [32]byte `json:"block_summary_root" ssz-size:"32"`
StateSummaryRoot [32]byte `json:"state_summary_root" ssz-size:"32"`
}

type SignedBLSToExecutionChange struct {
Message *BLSToExecutionChange `json:"message"`
Signature [96]byte `json:"signature" ssz-size:"96"`
}

type Withdrawal struct {
Index uint64 `json:"index"`
ValidatorIndex uint64 `json:"validator_index"`
Address [20]byte `json:"address" ssz-size:"20"`
Amount uint64 `json:"amount"`
}

type BeaconStateCapella struct {
GenesisTime uint64 `json:"genesis_time"`
GenesisValidatorsRoot [32]byte `json:"genesis_validators_root" ssz-size:"32"`
Slot uint64 `json:"slot"`
Fork *Fork `json:"fork"`
LatestBlockHeader *BeaconBlockHeader `json:"latest_block_header"`
BlockRoots [8192][32]byte `json:"block_roots" ssz-size:"8192,32"`
StateRoots [8192][32]byte `json:"state_roots" ssz-size:"8192,32"`
HistoricalRoots [][]byte `json:"historical_roots" ssz-max:"16777216" ssz-size:"?,32"`
Eth1Data *Eth1Data `json:"eth1_data"`
Eth1DataVotes []*Eth1Data `json:"eth1_data_votes" ssz-max:"2048"`
Eth1DepositIndex uint64 `json:"eth1_deposit_index"`
Validators []*Validator `json:"validators" ssz-max:"1099511627776"`
Balances []uint64 `json:"balances" ssz-max:"1099511627776"`
RandaoMixes [65536][32]byte `json:"randao_mixes" ssz-size:"65536,32"`
Slashings []uint64 `json:"slashings" ssz-size:"8192"`
PreviousEpochParticipation []byte `json:"previous_epoch_participation" ssz-max:"1099511627776"`
CurrentEpochParticipation []byte `json:"current_epoch_participation" ssz-max:"1099511627776"`
JustificationBits [1]byte `json:"justification_bits" ssz-size:"1"`
PreviousJustifiedCheckpoint *Checkpoint `json:"previous_justified_checkpoint"`
CurrentJustifiedCheckpoint *Checkpoint `json:"current_justified_checkpoint"`
FinalizedCheckpoint *Checkpoint `json:"finalized_checkpoint"`
InactivityScores []uint64 `json:"inactivity_scores" ssz-max:"1099511627776"`
CurrentSyncCommittee *SyncCommittee `json:"current_sync_committee"`
NextSyncCommittee *SyncCommittee `json:"next_sync_committee"`
LatestExecutionPayloadHeader *ExecutionPayloadHeaderCapella `json:"latest_execution_payload_header"`
NextWithdrawalIndex uint64 `json:"next_withdrawal_index"`
NextWithdrawalValidatorIndex uint64 `json:"next_withdrawal_validator_index"`
HistoricalSummaries []*HistoricalSummary `json:"historical_summaries" ssz-max:"16777216"`
}

type SignedBeaconBlockCapella struct {
Block *BeaconBlockCapella `json:"message"`
Signature []byte `json:"signature" ssz-size:"96"`
}

type BeaconBlockCapella struct {
Slot uint64 `json:"slot"`
ProposerIndex uint64 `json:"proposer_index"`
ParentRoot [32]byte `json:"parent_root" ssz-size:"32"`
StateRoot [32]byte `json:"state_root" ssz-size:"32"`
Body *BeaconBlockBodyCapella `json:"body"`
}

type BeaconBlockBodyCapella struct {
RandaoReveal []byte `json:"randao_reveal" ssz-size:"96"`
Eth1Data *Eth1Data `json:"eth1_data"`
Graffiti [32]byte `json:"graffiti" ssz-size:"32"`
ProposerSlashings []*ProposerSlashing `json:"proposer_slashings" ssz-max:"16"`
AttesterSlashings []*AttesterSlashing `json:"attester_slashings" ssz-max:"2"`
Attestations []*Attestation `json:"attestations" ssz-max:"128"`
Deposits []*Deposit `json:"deposits" ssz-max:"16"`
VoluntaryExits []*SignedVoluntaryExit `json:"voluntary_exits" ssz-max:"16"`
SyncAggregate *SyncAggregate `json:"sync_aggregate"`
ExecutionPayload *ExecutionPayloadCapella `json:"execution_payload"`
BlsToExecutionChanges []*SignedBLSToExecutionChange `json:"bls_to_execution_changes" ssz-max:"16"`
}
2,519 changes: 2,517 additions & 2 deletions spectests/structs_encoding.go

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions spectests/structs_test.go
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ const (
phase0 fork = "phase0"
altair fork = "altair"
bellatrix fork = "bellatrix"
capella fork = "capella"
)

type testCallback func(fork fork) codec
@@ -45,12 +46,16 @@ var codecs = map[string]testCallback{
return new(BeaconStateAltair)
} else if fork == bellatrix {
return new(BeaconStateBellatrix)
} else if fork == capella {
return new(BeaconStateCapella)
}
return nil
},
"BeaconBlock": func(fork fork) codec {
if fork == phase0 {
return new(BeaconBlock)
} else if fork == capella {
return new(BeaconBlockCapella)
}
return nil
},
@@ -61,6 +66,8 @@ var codecs = map[string]testCallback{
return new(BeaconBlockBodyAltair)
} else if fork == bellatrix {
return new(BeaconBlockBodyBellatrix)
} else if fork == capella {
return new(BeaconBlockBodyCapella)
}
return nil
},
@@ -78,6 +85,8 @@ var codecs = map[string]testCallback{
"SignedBeaconBlock": func(fork fork) codec {
if fork == phase0 {
return new(SignedBeaconBlock)
} else if fork == capella {
return new(SignedBeaconBlockCapella)
}
return nil
},
@@ -94,8 +103,21 @@ var codecs = map[string]testCallback{
return new(SyncAggregate)
},
"ExecutionPayload": func(fork fork) codec {
if fork == capella {
return new(ExecutionPayloadCapella)
}
return new(ExecutionPayload)
},
"ExecutionPayloadHeader": func(fork fork) codec {
if fork == capella {
return new(ExecutionPayloadHeaderCapella)
}
return new(ExecutionPayloadHeader)
},
"BLSToExecutionChange": func(f fork) codec { return new(BLSToExecutionChange) },
"HistoricalSummary": func(f fork) codec { return new(HistoricalSummary) },
"SignedBLSToExecutionChange": func(f fork) codec { return new(SignedBLSToExecutionChange) },
"Withdrawal": func(f fork) codec { return new(Withdrawal) },
}

func testSpecFork(t *testing.T, fork fork) {
@@ -130,6 +152,10 @@ func TestSpec_Bellatrix(t *testing.T) {
testSpecFork(t, bellatrix)
}

func TestSpec_Capella(t *testing.T) {
testSpecFork(t, capella)
}

func checkSSZEncoding(t *testing.T, fork fork, fileName, structName string, base testCallback) {
obj := base(fork)
if obj == nil {
36 changes: 36 additions & 0 deletions spectests/structs_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package spectests

import (
"fmt"
"math/big"
)

func (u *Uint256) UnmarshalText(text []byte) error {
x := new(big.Int)
if err := x.UnmarshalText(text); err != nil {
return err
}
if x.BitLen() > 256 {
return fmt.Errorf("too big")
}

buf := reverse(x.Bytes())
copy(u[:], buf)
return nil
}

func (u Uint256) MarshalText() (text []byte, err error) {
buf := reverse(u[:])
x := new(big.Int).SetBytes(buf[:])
return []byte(x.String()), nil
}

func reverse(in []byte) (out []byte) {
out = make([]byte, len(in))
copy(out[:], in[:])

for i, j := 0, len(out)-1; i < j; i, j = i+1, j-1 {
out[i], out[j] = out[j], out[i]
}
return
}
2 changes: 1 addition & 1 deletion sszgen/testcases/case1_encoding.go
2 changes: 1 addition & 1 deletion sszgen/testcases/case2_encoding.go
2 changes: 1 addition & 1 deletion sszgen/testcases/case3_encoding.go
2 changes: 1 addition & 1 deletion sszgen/testcases/case4_encoding.go
2 changes: 1 addition & 1 deletion sszgen/testcases/case5_encoding.go
2 changes: 1 addition & 1 deletion sszgen/testcases/case6_encoding.go
2 changes: 1 addition & 1 deletion sszgen/testcases/case7_encoding.go
2 changes: 1 addition & 1 deletion sszgen/testcases/other/case3_encoding.go
2 changes: 1 addition & 1 deletion tests/codetrie_encoding.go

0 comments on commit ea61f0e

Please sign in to comment.