diff --git a/cl/cltypes/aggregate.go b/cl/cltypes/aggregate.go index 4c7bcb5b9..c6c8e6694 100644 --- a/cl/cltypes/aggregate.go +++ b/cl/cltypes/aggregate.go @@ -57,8 +57,8 @@ func (a *SignedAggregateAndProof) EncodingSizeSSZ() int { * and signature is the aggregate BLS signature of the committee. */ type SyncAggregate struct { - SyncCommiteeBits [64]byte - SyncCommiteeSignature [96]byte + SyncCommiteeBits [64]byte `json:"sync_commitee_bits"` + SyncCommiteeSignature libcommon.Bytes96 `json:"signature"` } // return sum of the committee bits diff --git a/cl/cltypes/beacon_block.go b/cl/cltypes/beacon_block.go index 2f6a266f8..818e6d376 100644 --- a/cl/cltypes/beacon_block.go +++ b/cl/cltypes/beacon_block.go @@ -23,46 +23,46 @@ const ( ) type SignedBeaconBlock struct { - Signature libcommon.Bytes96 - Block *BeaconBlock + Signature libcommon.Bytes96 `json:"signature"` + Block *BeaconBlock `json:"message"` } type BeaconBlock struct { - Slot uint64 - ProposerIndex uint64 - ParentRoot libcommon.Hash - StateRoot libcommon.Hash - Body *BeaconBody + Slot uint64 `json:"slot"` + ProposerIndex uint64 `json:"proposer_index"` + ParentRoot libcommon.Hash `json:"parent_root"` + StateRoot libcommon.Hash `json:"state_root"` + Body *BeaconBody `json:"body"` } type BeaconBody struct { // A byte array used for randomness in the beacon chain - RandaoReveal libcommon.Bytes96 + RandaoReveal libcommon.Bytes96 `json:"randao_reveal"` // Data related to the Ethereum 1.0 chain - Eth1Data *Eth1Data + Eth1Data *Eth1Data `json:"eth1_data"` // A byte array used to customize validators' behavior - Graffiti libcommon.Hash + Graffiti libcommon.Hash `json:"graffit"` // A list of slashing events for validators who included invalid blocks in the chain - ProposerSlashings *solid.ListSSZ[*ProposerSlashing] + ProposerSlashings *solid.ListSSZ[*ProposerSlashing] `json:"proposer_slashings"` // A list of slashing events for validators who included invalid attestations in the chain - AttesterSlashings *solid.ListSSZ[*AttesterSlashing] + AttesterSlashings *solid.ListSSZ[*AttesterSlashing] `json:"attester_slashings"` // A list of attestations included in the block - Attestations *solid.ListSSZ[*solid.Attestation] + Attestations *solid.ListSSZ[*solid.Attestation] `json:"attestations"` // A list of deposits made to the Ethereum 1.0 chain - Deposits *solid.ListSSZ[*Deposit] + Deposits *solid.ListSSZ[*Deposit] `json:"deposits"` // A list of validators who have voluntarily exited the beacon chain - VoluntaryExits *solid.ListSSZ[*SignedVoluntaryExit] + VoluntaryExits *solid.ListSSZ[*SignedVoluntaryExit] `json:"voluntary_exits"` // A summary of the current state of the beacon chain - SyncAggregate *SyncAggregate + SyncAggregate *SyncAggregate `json:"sync_aggregate,omitempty"` // Data related to crosslink records and executing operations on the Ethereum 2.0 chain - ExecutionPayload *Eth1Block + ExecutionPayload *Eth1Block `json:"execution_payload,omitempty"` // Withdrawals Diffs for Execution Layer - ExecutionChanges *solid.ListSSZ[*SignedBLSToExecutionChange] + ExecutionChanges *solid.ListSSZ[*SignedBLSToExecutionChange] `json:"execution_changes,omitempty"` // The commitments for beacon chain blobs // With a max of 4 per block - BlobKzgCommitments *solid.ListSSZ[*KZGCommitment] + BlobKzgCommitments *solid.ListSSZ[*KZGCommitment] `json:"blob_kzg_commitments,omitempty"` // The version of the beacon chain - Version clparams.StateVersion + Version clparams.StateVersion `json:"-"` beaconCfg *clparams.BeaconChainConfig } diff --git a/cl/cltypes/beacon_kzgcommitment.go b/cl/cltypes/beacon_kzgcommitment.go index 7b42d0b3b..f6a5d5f38 100644 --- a/cl/cltypes/beacon_kzgcommitment.go +++ b/cl/cltypes/beacon_kzgcommitment.go @@ -1,7 +1,10 @@ package cltypes import ( + "encoding/json" + gokzg4844 "github.com/crate-crypto/go-kzg-4844" + libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon/cl/merkle_tree" ssz2 "github.com/ledgerwatch/erigon/cl/ssz" ) @@ -16,6 +19,14 @@ const BYTES_PER_BLOB = uint64(BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB) type KZGCommitment gokzg4844.KZGCommitment +func (b KZGCommitment) MarshalJSON() ([]byte, error) { + return json.Marshal(libcommon.Bytes48(b)) +} + +func (b *KZGCommitment) UnmarshalJSON(data []byte) error { + return json.Unmarshal(data, (*libcommon.Bytes48)(b)) +} + func (b *KZGCommitment) Copy() *KZGCommitment { copy := *b return © diff --git a/cl/cltypes/bls_to_execution_change.go b/cl/cltypes/bls_to_execution_change.go index af9a0a027..676154f15 100644 --- a/cl/cltypes/bls_to_execution_change.go +++ b/cl/cltypes/bls_to_execution_change.go @@ -11,9 +11,9 @@ import ( // Change to EL engine type BLSToExecutionChange struct { - ValidatorIndex uint64 - From libcommon.Bytes48 - To libcommon.Address + ValidatorIndex uint64 `json:"validator_index"` + From libcommon.Bytes48 `json:"from"` + To libcommon.Address `json:"to"` } func (b *BLSToExecutionChange) EncodeSSZ(buf []byte) ([]byte, error) { @@ -43,8 +43,8 @@ func (*BLSToExecutionChange) Static() bool { } type SignedBLSToExecutionChange struct { - Message *BLSToExecutionChange - Signature libcommon.Bytes96 + Message *BLSToExecutionChange `json:"message"` + Signature libcommon.Bytes96 `json:"signature"` } func (s *SignedBLSToExecutionChange) EncodeSSZ(buf []byte) ([]byte, error) { diff --git a/cl/cltypes/eth1_block.go b/cl/cltypes/eth1_block.go index 13dd43edf..f9009fe7a 100644 --- a/cl/cltypes/eth1_block.go +++ b/cl/cltypes/eth1_block.go @@ -16,24 +16,24 @@ import ( // ETH1Block represents a block structure CL-side. type Eth1Block struct { - ParentHash libcommon.Hash - FeeRecipient libcommon.Address - StateRoot libcommon.Hash - ReceiptsRoot libcommon.Hash - LogsBloom types.Bloom - PrevRandao libcommon.Hash - BlockNumber uint64 - GasLimit uint64 - GasUsed uint64 - Time uint64 - Extra *solid.ExtraData - BaseFeePerGas libcommon.Hash + ParentHash libcommon.Hash `json:"parent_hash"` + FeeRecipient libcommon.Address `json:"fee_recipient"` + StateRoot libcommon.Hash `json:"state_root"` + ReceiptsRoot libcommon.Hash `json:"receipts_root"` + LogsBloom types.Bloom `json:"logs_bloom"` + PrevRandao libcommon.Hash `json:"prev_randao"` + BlockNumber uint64 `json:"block_number"` + GasLimit uint64 `json:"gas_limit"` + GasUsed uint64 `json:"gas_used"` + Time uint64 `json:"timestamp"` + Extra *solid.ExtraData `json:"extra_data"` + BaseFeePerGas libcommon.Hash `json:"base_fee_per_gas"` // Extra fields - BlockHash libcommon.Hash - Transactions *solid.TransactionsSSZ - Withdrawals *solid.ListSSZ[*types.Withdrawal] - BlobGasUsed uint64 - ExcessBlobGas uint64 + BlockHash libcommon.Hash `json:"block_hash"` + Transactions *solid.TransactionsSSZ `json:"transactions"` + Withdrawals *solid.ListSSZ[*types.Withdrawal] `json:"withdrawals,omitempty"` + BlobGasUsed uint64 `json:"blob_gas_used,omitempty"` + ExcessBlobGas uint64 `json:"excess_blob_gas,omitempty"` // internals version clparams.StateVersion beaconCfg *clparams.BeaconChainConfig diff --git a/cl/cltypes/eth1_data.go b/cl/cltypes/eth1_data.go index 165560f8b..92b45be76 100644 --- a/cl/cltypes/eth1_data.go +++ b/cl/cltypes/eth1_data.go @@ -10,9 +10,9 @@ import ( ) type Eth1Data struct { - Root libcommon.Hash - DepositCount uint64 - BlockHash libcommon.Hash + Root libcommon.Hash `json:"deposit_root"` + DepositCount uint64 `json:"deposit_count"` + BlockHash libcommon.Hash `json:"block_hash"` } func (e *Eth1Data) Copy() *Eth1Data { diff --git a/cl/cltypes/indexed_attestation.go b/cl/cltypes/indexed_attestation.go index b9eb1b0dc..e1690c0f7 100644 --- a/cl/cltypes/indexed_attestation.go +++ b/cl/cltypes/indexed_attestation.go @@ -11,9 +11,9 @@ import ( * IndexedAttestation are attestantions sets to prove that someone misbehaved. */ type IndexedAttestation struct { - AttestingIndices solid.Uint64ListSSZ - Data solid.AttestationData - Signature libcommon.Bytes96 + AttestingIndices solid.Uint64ListSSZ `json:"attesting_indicies"` + Data solid.AttestationData `json:"data"` + Signature libcommon.Bytes96 `json:"signature"` } func (i *IndexedAttestation) Static() bool { diff --git a/cl/cltypes/slashings.go b/cl/cltypes/slashings.go index 8a74b071a..f849b7b50 100644 --- a/cl/cltypes/slashings.go +++ b/cl/cltypes/slashings.go @@ -6,8 +6,8 @@ import ( ) type ProposerSlashing struct { - Header1 *SignedBeaconBlockHeader - Header2 *SignedBeaconBlockHeader + Header1 *SignedBeaconBlockHeader `json:"signed_header_1"` + Header2 *SignedBeaconBlockHeader `json:"signed_header_2"` } func (p *ProposerSlashing) EncodeSSZ(dst []byte) ([]byte, error) { @@ -29,8 +29,8 @@ func (p *ProposerSlashing) HashSSZ() ([32]byte, error) { } type AttesterSlashing struct { - Attestation_1 *IndexedAttestation - Attestation_2 *IndexedAttestation + Attestation_1 *IndexedAttestation `json:"attestation_1"` + Attestation_2 *IndexedAttestation `json:"attestation_2"` } func (a *AttesterSlashing) EncodeSSZ(dst []byte) ([]byte, error) { diff --git a/cl/cltypes/solid/attestation.go b/cl/cltypes/solid/attestation.go index baf2354b3..12ca81b07 100644 --- a/cl/cltypes/solid/attestation.go +++ b/cl/cltypes/solid/attestation.go @@ -1,6 +1,10 @@ package solid import ( + "encoding/json" + + libcommon "github.com/ledgerwatch/erigon-lib/common" + "github.com/ledgerwatch/erigon-lib/common/hexutility" "github.com/ledgerwatch/erigon-lib/common/length" "github.com/ledgerwatch/erigon-lib/types/clonable" "github.com/ledgerwatch/erigon-lib/types/ssz" @@ -44,6 +48,33 @@ func NewAttestionFromParameters( return a } +func (a Attestation) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + AggregationBits hexutility.Bytes + Signature libcommon.Bytes96 + Data AttestationData + }{ + AggregationBits: a.aggregationBitsBuffer, + Signature: a.Signature(), + Data: a.AttestantionData(), + }) +} + +func (a *Attestation) UnmarshalJSON(buf []byte) error { + var tmp struct { + AggregationBits hexutility.Bytes + Signature libcommon.Bytes96 + Data AttestationData + } + if err := json.Unmarshal(buf, &tmp); err != nil { + return err + } + a.SetAggregationBits(tmp.AggregationBits) + a.SetSignature(tmp.Signature) + a.SetAttestationData(tmp.Data) + return nil +} + // AggregationBits returns the aggregation bits buffer of the Attestation instance. func (a *Attestation) AggregationBits() []byte { return a.aggregationBitsBuffer diff --git a/cl/cltypes/solid/attestation_data.go b/cl/cltypes/solid/attestation_data.go index 65cad27b7..82cbebec8 100644 --- a/cl/cltypes/solid/attestation_data.go +++ b/cl/cltypes/solid/attestation_data.go @@ -3,6 +3,7 @@ package solid import ( "bytes" "encoding/binary" + "encoding/json" libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/common/length" @@ -37,6 +38,41 @@ func NewAttestionDataFromParameters( return a } +func (a AttestationData) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + Slot uint64 + Index uint64 + BeaconBlockRoot libcommon.Hash + Source Checkpoint + Target Checkpoint + }{ + Slot: a.Slot(), + BeaconBlockRoot: a.BeaconBlockRoot(), + Index: a.ValidatorIndex(), + Source: a.Source(), + Target: a.Target(), + }) +} + +func (a AttestationData) UnmarshalJSON(buf []byte) error { + var tmp struct { + Slot uint64 + Index uint64 + BeaconBlockRoot libcommon.Hash + Source Checkpoint + Target Checkpoint + } + if err := json.Unmarshal(buf, &tmp); err != nil { + return err + } + a.SetSlot(tmp.Slot) + a.SetValidatorIndex(tmp.Index) + a.SetBeaconBlockRoot(tmp.BeaconBlockRoot) + a.SetSource(tmp.Source) + a.SetTarget(tmp.Target) + return nil +} + func NewAttestationData() AttestationData { return make([]byte, attestationDataBufferSize) } diff --git a/cl/cltypes/solid/checkpoint.go b/cl/cltypes/solid/checkpoint.go index aa17c6019..66b1c96ad 100644 --- a/cl/cltypes/solid/checkpoint.go +++ b/cl/cltypes/solid/checkpoint.go @@ -3,6 +3,7 @@ package solid import ( "bytes" "encoding/binary" + "encoding/json" libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/common/length" @@ -32,6 +33,26 @@ func NewCheckpoint() Checkpoint { return make([]byte, checkpointSize) } +func (c Checkpoint) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + Epoch uint64 + Root libcommon.Hash + }{Epoch: c.Epoch(), Root: c.BlockRoot()}) +} + +func (c Checkpoint) UnmarshalJSON(buf []byte) error { + var tmp struct { + Epoch uint64 + Root libcommon.Hash + } + if err := json.Unmarshal(buf, &tmp); err != nil { + return err + } + c.SetEpoch(tmp.Epoch) + c.SetBlockRoot(tmp.Root) + return nil +} + // SetBlockRoot copies the given blockRoot into the correct location within the Checkpoint func (c Checkpoint) SetBlockRoot(blockRoot libcommon.Hash) { copy(c[8:], blockRoot[:]) // copy the blockRoot into the Checkpoint starting at index 8 diff --git a/cl/cltypes/solid/extra_data.go b/cl/cltypes/solid/extra_data.go index abaa04075..4ef2c78ec 100644 --- a/cl/cltypes/solid/extra_data.go +++ b/cl/cltypes/solid/extra_data.go @@ -2,8 +2,10 @@ package solid import ( "encoding/binary" + "encoding/json" "github.com/ledgerwatch/erigon-lib/common" + "github.com/ledgerwatch/erigon-lib/common/hexutility" "github.com/ledgerwatch/erigon-lib/common/length" "github.com/ledgerwatch/erigon-lib/types/clonable" "github.com/ledgerwatch/erigon/cl/merkle_tree" @@ -22,6 +24,18 @@ func NewExtraData() *ExtraData { } } +func (e *ExtraData) UnmarshalJSON(buf []byte) error { + if err := json.Unmarshal(buf, (*hexutility.Bytes)(&e.data)); err != nil { + return err + } + e.l = len(e.data) + return nil +} + +func (e ExtraData) MarshalJSON() ([]byte, error) { + return json.Marshal(hexutility.Bytes(e.data[:e.l])) +} + // Clone creates a new instance of ExtraData. func (*ExtraData) Clone() clonable.Clonable { return NewExtraData() diff --git a/cl/cltypes/solid/hash_list.go b/cl/cltypes/solid/hash_list.go index bc6726784..35f430265 100644 --- a/cl/cltypes/solid/hash_list.go +++ b/cl/cltypes/solid/hash_list.go @@ -1,6 +1,8 @@ package solid import ( + "encoding/json" + "github.com/ledgerwatch/erigon-lib/common" libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/common/length" @@ -23,6 +25,28 @@ func NewHashList(c int) HashListSSZ { } } +func (arr *hashList) MarshalJSON() ([]byte, error) { + list := make([]libcommon.Hash, arr.l) + for i := 0; i < arr.l; i++ { + list[0] = arr.Get(i) + } + return json.Marshal(list) +} + +func (arr *hashList) UnmarshalJSON(buf []byte) error { + var list []libcommon.Hash + + if err := json.Unmarshal(buf, &list); err != nil { + return err + } + arr.Clear() + arr.l = len(list) + for _, elem := range list { + arr.Append(elem) + } + return nil +} + func (h *hashList) Append(val libcommon.Hash) { offset := h.l * length.Hash if offset == len(h.u) { diff --git a/cl/cltypes/solid/hash_vector.go b/cl/cltypes/solid/hash_vector.go index 95f5fc3eb..e91642d7f 100644 --- a/cl/cltypes/solid/hash_vector.go +++ b/cl/cltypes/solid/hash_vector.go @@ -1,6 +1,8 @@ package solid import ( + "encoding/json" + libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/common/length" "github.com/ledgerwatch/erigon-lib/types/clonable" @@ -26,6 +28,14 @@ func (h *hashVector) Append(val libcommon.Hash) { panic("not implmented") } +func (h hashVector) MarshalJSON() ([]byte, error) { + return json.Marshal(h.u) +} + +func (h *hashVector) UnmarshalJSON(buf []byte) error { + return json.Unmarshal(buf, h.u) +} + func (h *hashVector) Cap() int { return h.u.l } diff --git a/cl/cltypes/solid/interfaces.go b/cl/cltypes/solid/interfaces.go index b5373e20a..b6b42d2b0 100644 --- a/cl/cltypes/solid/interfaces.go +++ b/cl/cltypes/solid/interfaces.go @@ -1,6 +1,8 @@ package solid import ( + "encoding/json" + "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/types/ssz" ssz2 "github.com/ledgerwatch/erigon/cl/ssz" @@ -23,7 +25,26 @@ type IterableSSZ[T any] interface { ssz.HashableSSZ } -type Uint64VectorSSZ IterableSSZ[uint64] -type Uint64ListSSZ IterableSSZ[uint64] -type HashListSSZ IterableSSZ[common.Hash] -type HashVectorSSZ IterableSSZ[common.Hash] +type Uint64ListSSZ interface { + IterableSSZ[uint64] + json.Marshaler + json.Unmarshaler +} + +type Uint64VectorSSZ interface { + IterableSSZ[uint64] + json.Marshaler + json.Unmarshaler +} + +type HashListSSZ interface { + IterableSSZ[common.Hash] + json.Marshaler + json.Unmarshaler +} + +type HashVectorSSZ interface { + IterableSSZ[common.Hash] + json.Marshaler + json.Unmarshaler +} diff --git a/cl/cltypes/solid/list_ssz.go b/cl/cltypes/solid/list_ssz.go index 11450d8f9..22234ebb8 100644 --- a/cl/cltypes/solid/list_ssz.go +++ b/cl/cltypes/solid/list_ssz.go @@ -1,6 +1,8 @@ package solid import ( + "encoding/json" + libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/types/clonable" "github.com/ledgerwatch/erigon-lib/types/ssz" @@ -43,6 +45,14 @@ func NewStaticListSSZ[T encodableHashableSSZ](limit int, bytesPerElement int) *L } } +func (l ListSSZ[T]) MarshalJSON() ([]byte, error) { + return json.Marshal(l.list) +} + +func (l *ListSSZ[T]) UnmarshalJSON(data []byte) error { + return json.Unmarshal(data, &l.list) +} + func NewDynamicListSSZFromList[T encodableHashableSSZ](list []T, limit int) *ListSSZ[T] { return &ListSSZ[T]{ list: list, diff --git a/cl/cltypes/solid/transactions.go b/cl/cltypes/solid/transactions.go index 73ba6c81a..40651ac85 100644 --- a/cl/cltypes/solid/transactions.go +++ b/cl/cltypes/solid/transactions.go @@ -1,7 +1,10 @@ package solid import ( + "encoding/json" + libcommon "github.com/ledgerwatch/erigon-lib/common" + "github.com/ledgerwatch/erigon-lib/common/hexutility" "github.com/ledgerwatch/erigon-lib/types/clonable" "github.com/ledgerwatch/erigon-lib/types/ssz" "github.com/ledgerwatch/erigon/cl/merkle_tree" @@ -12,6 +15,27 @@ type TransactionsSSZ struct { root libcommon.Hash // root } +func (t *TransactionsSSZ) UnmarshalJSON(buf []byte) error { + tmp := []hexutility.Bytes{} + t.root = libcommon.Hash{} + if err := json.Unmarshal(buf, &tmp); err != nil { + return err + } + t.underlying = nil + for _, tx := range tmp { + t.underlying = append(t.underlying, tx) + } + return nil +} + +func (t TransactionsSSZ) MarshalJSON() ([]byte, error) { + tmp := []hexutility.Bytes{} + for _, tx := range t.underlying { + tmp = append(tmp, tx) + } + return json.Marshal(tmp) +} + func (*TransactionsSSZ) Clone() clonable.Clonable { return &TransactionsSSZ{} } diff --git a/cl/cltypes/solid/uint64_list.go b/cl/cltypes/solid/uint64_list.go index b57b5ae0d..a18eeec03 100644 --- a/cl/cltypes/solid/uint64_list.go +++ b/cl/cltypes/solid/uint64_list.go @@ -1,6 +1,8 @@ package solid import ( + "encoding/json" + "github.com/ledgerwatch/erigon-lib/types/clonable" ) @@ -14,6 +16,14 @@ func NewUint64ListSSZ(limit int) Uint64ListSSZ { } } +func (h uint64ListSSZ) MarshalJSON() ([]byte, error) { + return json.Marshal(h.u) +} + +func (h uint64ListSSZ) UnmarshalJSON(buf []byte) error { + return json.Unmarshal(buf, h.u) +} + func (h *uint64ListSSZ) Static() bool { return false } diff --git a/cl/cltypes/solid/uint64_vector.go b/cl/cltypes/solid/uint64_vector.go index 446d6cdbf..8ddb7f067 100644 --- a/cl/cltypes/solid/uint64_vector.go +++ b/cl/cltypes/solid/uint64_vector.go @@ -1,6 +1,8 @@ package solid import ( + "encoding/json" + "github.com/ledgerwatch/erigon-lib/types/clonable" ) @@ -19,6 +21,14 @@ func NewUint64VectorSSZ(size int) Uint64VectorSSZ { } } +func (h uint64VectorSSZ) MarshalJSON() ([]byte, error) { + return json.Marshal(h.u) +} + +func (h *uint64VectorSSZ) UnmarshalJSON(buf []byte) error { + return json.Unmarshal(buf, h.u) +} + func (arr *uint64VectorSSZ) Clear() { arr.u.Clear() } diff --git a/cl/cltypes/solid/uint64slice_byte.go b/cl/cltypes/solid/uint64slice_byte.go index 5b5e85d5d..00abb1785 100644 --- a/cl/cltypes/solid/uint64slice_byte.go +++ b/cl/cltypes/solid/uint64slice_byte.go @@ -2,6 +2,7 @@ package solid import ( "encoding/binary" + "encoding/json" "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/common/length" @@ -56,9 +57,26 @@ func (arr *byteBasedUint64Slice) CopyTo(target *byteBasedUint64Slice) { copy(target.u, arr.u) } -// depth returns the depth of the Merkle tree representing the slice. -func (arr *byteBasedUint64Slice) depth() int { - return int(GetDepth(uint64(arr.c) / 4)) +func (arr *byteBasedUint64Slice) MarshalJSON() ([]byte, error) { + list := make([]uint64, arr.l) + for i := 0; i < arr.l; i++ { + list[0] = arr.Get(i) + } + return json.Marshal(list) +} + +func (arr *byteBasedUint64Slice) UnmarshalJSON(buf []byte) error { + var list []uint64 + + if err := json.Unmarshal(buf, &list); err != nil { + return err + } + arr.Clear() + arr.l = len(list) + for _, elem := range list { + arr.Append(elem) + } + return nil } // Range iterates over the slice and applies a provided function to each element. diff --git a/cl/cltypes/validator.go b/cl/cltypes/validator.go index bfd99c9fe..83b5e04db 100644 --- a/cl/cltypes/validator.go +++ b/cl/cltypes/validator.go @@ -16,11 +16,10 @@ const ( ) type DepositData struct { - PubKey [48]byte - WithdrawalCredentials libcommon.Hash - Amount uint64 - Signature libcommon.Bytes96 - Root libcommon.Hash // Ignored if not for hashing + PubKey libcommon.Bytes48 `json:"pubkey"` + WithdrawalCredentials libcommon.Hash `json:"withdrawal_credentials"` + Amount uint64 `json:"amount"` + Signature libcommon.Bytes96 `json:"signature"` } func (d *DepositData) EncodeSSZ(dst []byte) ([]byte, error) { @@ -49,8 +48,8 @@ func (*DepositData) Static() bool { type Deposit struct { // Merkle proof is used for deposits - Proof solid.HashVectorSSZ // 33 X 32 size. - Data *DepositData + Proof solid.HashVectorSSZ `json:"proof"` // 33 X 32 size. + Data *DepositData `json:"data"` } func (d *Deposit) EncodeSSZ(dst []byte) ([]byte, error) { @@ -73,8 +72,8 @@ func (d *Deposit) HashSSZ() ([32]byte, error) { } type VoluntaryExit struct { - Epoch uint64 - ValidatorIndex uint64 + Epoch uint64 `json:"epoch"` + ValidatorIndex uint64 `json:"validator_index"` } func (e *VoluntaryExit) EncodeSSZ(buf []byte) ([]byte, error) { @@ -102,8 +101,8 @@ func (*VoluntaryExit) EncodingSizeSSZ() int { } type SignedVoluntaryExit struct { - VolunaryExit *VoluntaryExit - Signature libcommon.Bytes96 + VolunaryExit *VoluntaryExit `json:"message"` + Signature libcommon.Bytes96 `json:"signature"` } func (e *SignedVoluntaryExit) EncodeSSZ(dst []byte) ([]byte, error) { diff --git a/cl/cltypes/validator_test.go b/cl/cltypes/validator_test.go index c798a19c8..6ee34a39f 100644 --- a/cl/cltypes/validator_test.go +++ b/cl/cltypes/validator_test.go @@ -47,7 +47,6 @@ func TestDepositData(t *testing.T) { WithdrawalCredentials: [32]byte{4, 5, 6}, Amount: 100, Signature: [96]byte{7, 8, 9}, - Root: [32]byte{10, 11, 12}, } // Encode DepositData to SSZ diff --git a/cl/phase1/stages/stage_history_download.go b/cl/phase1/stages/stage_history_download.go index 43e68056f..a95d80d75 100644 --- a/cl/phase1/stages/stage_history_download.go +++ b/cl/phase1/stages/stage_history_download.go @@ -3,6 +3,7 @@ package stages import ( "context" "fmt" + "sync/atomic" "time" libcommon "github.com/ledgerwatch/erigon-lib/common" @@ -96,8 +97,13 @@ func SpawnStageHistoryDownload(cfg StageHistoryReconstructionCfg, ctx context.Co foundLatestEth1ValidHash = true // skip this if we are not using an engine supporting direct insertion } + var currEth1Progress atomic.Int64 + // Set up onNewBlock callback cfg.downloader.SetOnNewBlock(func(blk *cltypes.SignedBeaconBlock) (finished bool, err error) { + if blk.Version() >= clparams.BellatrixVersion { + currEth1Progress.Store(int64(blk.Block.Body.ExecutionPayload.BlockNumber)) + } if !foundLatestEth1ValidHash { payload := blk.Block.Body.ExecutionPayload encodedPayload, err := payload.EncodeSSZ(nil) @@ -136,7 +142,8 @@ func SpawnStageHistoryDownload(cfg StageHistoryReconstructionCfg, ctx context.Co return } logArgs = append(logArgs, - "progress", currProgress, + "slot", currProgress, + "blockNumber", currEth1Progress.Load(), "blk/sec", fmt.Sprintf("%.1f", speed), "peers", peerCount) logger.Info("Downloading History", logArgs...)