diff --git a/encoder.go b/encoder.go index 5db55c1..7e9e6cb 100644 --- a/encoder.go +++ b/encoder.go @@ -12,6 +12,13 @@ import ( "github.com/holiman/uint256" ) +// Some helpers to avoid occasional allocations +var ( + boolFalse = []byte{0x00} + boolTrue = []byte{0x01} + uint256Zero = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} +) + // Encoder is a wrapper around an io.Writer or a []byte buffer to implement SSZ // encoding in a streaming or buffered way. It has the following behaviors: // @@ -74,9 +81,9 @@ func EncodeBool[T ~bool](enc *Encoder, v T) { return } if !v { - _, enc.err = enc.outWriter.Write([]byte{0x00}) + _, enc.err = enc.outWriter.Write(boolFalse) } else { - _, enc.err = enc.outWriter.Write([]byte{0x01}) + _, enc.err = enc.outWriter.Write(boolTrue) } } else { if !v { @@ -114,13 +121,13 @@ func EncodeUint256(enc *Encoder, n *uint256.Int) { n.MarshalSSZInto(enc.buf[:32]) _, enc.err = enc.outWriter.Write(enc.buf[:32]) } else { - _, enc.err = enc.outWriter.Write([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) + _, enc.err = enc.outWriter.Write(uint256Zero) } } else { if n != nil { n.MarshalSSZInto(enc.outBuffer) } else { - copy(enc.outBuffer, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) + copy(enc.outBuffer, uint256Zero) } enc.outBuffer = enc.outBuffer[32:] } diff --git a/tests/consensus_specs_test.go b/tests/consensus_specs_test.go index 979723f..32272b0 100644 --- a/tests/consensus_specs_test.go +++ b/tests/consensus_specs_test.go @@ -77,10 +77,8 @@ func TestConsensusSpecs(t *testing.T) { testConsensusSpecType[*types.SignedBeaconBlockHeader](t, "SignedBeaconBlockHeader") testConsensusSpecType[*types.SignedBLSToExecutionChange](t, "SignedBLSToExecutionChange") testConsensusSpecType[*types.SignedVoluntaryExit](t, "SignedVoluntaryExit") - testConsensusSpecType[*types.SigningRoot](t, "SigningRoot") testConsensusSpecType[*types.SyncAggregate](t, "SyncAggregate") testConsensusSpecType[*types.SyncCommittee](t, "SyncCommittee") - testConsensusSpecType[*types.Transfer](t, "Transfer") testConsensusSpecType[*types.Validator](t, "Validator") testConsensusSpecType[*types.VoluntaryExit](t, "VoluntaryExit") testConsensusSpecType[*types.Withdrawal](t, "Withdrawal") @@ -221,19 +219,29 @@ func BenchmarkConsensusSpecs(b *testing.B) { benchmarkConsensusSpecType[*types.AttestationData](b, "deneb", "AttestationData") benchmarkConsensusSpecType[*types.AttesterSlashing](b, "deneb", "AttesterSlashing") benchmarkConsensusSpecType[*types.BeaconBlock](b, "phase0", "BeaconBlock") - benchmarkConsensusSpecType[*types.BeaconBlockBody](b, "phase0", "BeaconBlockBody") + benchmarkConsensusSpecType[*types.BeaconBlockBodyDeneb](b, "deneb", "BeaconBlockBody") benchmarkConsensusSpecType[*types.BeaconBlockHeader](b, "deneb", "BeaconBlockHeader") + benchmarkConsensusSpecType[*types.BeaconState](b, "phase0", "BeaconState") + benchmarkConsensusSpecType[*types.BLSToExecutionChange](b, "deneb", "BLSToExecutionChange") benchmarkConsensusSpecType[*types.Checkpoint](b, "deneb", "Checkpoint") benchmarkConsensusSpecType[*types.Deposit](b, "deneb", "Deposit") benchmarkConsensusSpecType[*types.DepositData](b, "deneb", "DepositData") + benchmarkConsensusSpecType[*types.DepositMessage](b, "deneb", "DepositMessage") + benchmarkConsensusSpecType[*types.Eth1Block](b, "deneb", "Eth1Block") benchmarkConsensusSpecType[*types.Eth1Data](b, "deneb", "Eth1Data") - benchmarkConsensusSpecType[*types.ExecutionPayloadCapella](b, "capella", "ExecutionPayload") + benchmarkConsensusSpecType[*types.ExecutionPayloadDeneb](b, "deneb", "ExecutionPayload") + benchmarkConsensusSpecType[*types.ExecutionPayloadHeaderDeneb](b, "deneb", "ExecutionPayloadHeader") + benchmarkConsensusSpecType[*types.Fork](b, "deneb", "Fork") benchmarkConsensusSpecType[*types.HistoricalBatch](b, "deneb", "HistoricalBatch") - benchmarkConsensusSpecType[*types.HistoricalBatchVariation](b, "deneb", "HistoricalBatch") + benchmarkConsensusSpecType[*types.HistoricalSummary](b, "deneb", "HistoricalSummary") benchmarkConsensusSpecType[*types.IndexedAttestation](b, "deneb", "IndexedAttestation") + benchmarkConsensusSpecType[*types.PendingAttestation](b, "deneb", "PendingAttestation") benchmarkConsensusSpecType[*types.ProposerSlashing](b, "deneb", "ProposerSlashing") benchmarkConsensusSpecType[*types.SignedBeaconBlockHeader](b, "deneb", "SignedBeaconBlockHeader") + benchmarkConsensusSpecType[*types.SignedBLSToExecutionChange](b, "deneb", "SignedBLSToExecutionChange") benchmarkConsensusSpecType[*types.SignedVoluntaryExit](b, "deneb", "SignedVoluntaryExit") + benchmarkConsensusSpecType[*types.SyncAggregate](b, "deneb", "SyncAggregate") + benchmarkConsensusSpecType[*types.SyncCommittee](b, "deneb", "SyncCommittee") benchmarkConsensusSpecType[*types.Validator](b, "deneb", "Validator") benchmarkConsensusSpecType[*types.VoluntaryExit](b, "deneb", "VoluntaryExit") benchmarkConsensusSpecType[*types.Withdrawal](b, "deneb", "Withdrawal") diff --git a/tests/testtypes/consensus-spec-tests/gen_signing_root_ssz.go b/tests/testtypes/consensus-spec-tests/gen_signing_root_ssz.go deleted file mode 100644 index 2ccee43..0000000 --- a/tests/testtypes/consensus-spec-tests/gen_signing_root_ssz.go +++ /dev/null @@ -1,16 +0,0 @@ -// Code generated by github.com/karalabe/ssz. DO NOT EDIT. - -package consensus_spec_tests - -import "github.com/karalabe/ssz" - -// SizeSSZ returns the total size of the static ssz object. -func (obj *SigningRoot) SizeSSZ() uint32 { - return 32 + 8 -} - -// DefineSSZ defines how an object is encoded/decoded. -func (obj *SigningRoot) DefineSSZ(codec *ssz.Codec) { - ssz.DefineStaticBytes(codec, obj.ObjectRoot[:]) // Field (0) - ObjectRoot - 32 bytes - ssz.DefineStaticBytes(codec, obj.Domain[:]) // Field (1) - Domain - 8 bytes -} diff --git a/tests/testtypes/consensus-spec-tests/gen_transfer_ssz.go b/tests/testtypes/consensus-spec-tests/gen_transfer_ssz.go deleted file mode 100644 index 3201fbb..0000000 --- a/tests/testtypes/consensus-spec-tests/gen_transfer_ssz.go +++ /dev/null @@ -1,21 +0,0 @@ -// Code generated by github.com/karalabe/ssz. DO NOT EDIT. - -package consensus_spec_tests - -import "github.com/karalabe/ssz" - -// SizeSSZ returns the total size of the static ssz object. -func (obj *Transfer) SizeSSZ() uint32 { - return 8 + 8 + 8 + 8 + 8 + 48 + 96 -} - -// DefineSSZ defines how an object is encoded/decoded. -func (obj *Transfer) DefineSSZ(codec *ssz.Codec) { - ssz.DefineUint64(codec, &obj.Sender) // Field (0) - Sender - 8 bytes - ssz.DefineUint64(codec, &obj.Recipient) // Field (1) - Recipient - 8 bytes - ssz.DefineUint64(codec, &obj.Amount) // Field (2) - Amount - 8 bytes - ssz.DefineUint64(codec, &obj.Fee) // Field (3) - Fee - 8 bytes - ssz.DefineUint64(codec, &obj.Slot) // Field (4) - Slot - 8 bytes - ssz.DefineStaticBytes(codec, obj.Pubkey[:]) // Field (5) - Pubkey - 48 bytes - ssz.DefineStaticBytes(codec, obj.Signature[:]) // Field (6) - Signature - 96 bytes -} diff --git a/tests/testtypes/consensus-spec-tests/types_consensus.go b/tests/testtypes/consensus-spec-tests/types_consensus.go index e9c12b1..1605c23 100644 --- a/tests/testtypes/consensus-spec-tests/types_consensus.go +++ b/tests/testtypes/consensus-spec-tests/types_consensus.go @@ -29,10 +29,8 @@ import "github.com/holiman/uint256" //go:generate go run ../../../cmd/sszgen -type SignedBeaconBlockHeader -out gen_signed_beacon_block_header_ssz.go //go:generate go run ../../../cmd/sszgen -type SignedBLSToExecutionChange -out gen_signed_bls_to_execution_change_ssz.go //go:generate go run ../../../cmd/sszgen -type SignedVoluntaryExit -out gen_signed_voluntary_exit_ssz.go -//go:generate go run ../../../cmd/sszgen -type SigningRoot -out gen_signing_root_ssz.go //go:generate go run ../../../cmd/sszgen -type SyncAggregate -out gen_sync_aggregate_ssz.go //go:generate go run ../../../cmd/sszgen -type SyncCommittee -out gen_sync_committee_ssz.go -//go:generate go run ../../../cmd/sszgen -type Transfer -out gen_transfer_ssz.go //go:generate go run ../../../cmd/sszgen -type VoluntaryExit -out gen_voluntary_exit_ssz.go //go:generate go run ../../../cmd/sszgen -type Validator -out gen_validator_ssz.go //go:generate go run ../../../cmd/sszgen -type Withdrawal -out gen_withdrawal_ssz.go @@ -389,11 +387,6 @@ type SignedVoluntaryExit struct { Signature [96]byte } -type SigningRoot struct { - ObjectRoot [32]byte - Domain [8]byte -} - type SyncAggregate struct { SyncCommiteeBits [64]byte SyncCommiteeSignature [96]byte @@ -404,16 +397,6 @@ type SyncCommittee struct { AggregatePubKey [48]byte } -type Transfer struct { - Sender uint64 - Recipient uint64 - Amount uint64 - Fee uint64 - Slot uint64 - Pubkey [48]byte - Signature [96]byte -} - type VoluntaryExit struct { Epoch uint64 ValidatorIndex uint64