From 44968250aab6adaa7c6202dcef0a9d3a480c8002 Mon Sep 17 00:00:00 2001 From: shantichanal <158101918+shantichanal@users.noreply.github.com> Date: Mon, 7 Jul 2025 15:21:52 +0200 Subject: [PATCH] simplified proof builder structure --- internal/era2/builder2.go | 35 ++++++++++++++++++++++++++--------- internal/era2/era2.go | 1 - 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/internal/era2/builder2.go b/internal/era2/builder2.go index cfeb2bdb47..a94f0ae7cd 100644 --- a/internal/era2/builder2.go +++ b/internal/era2/builder2.go @@ -58,8 +58,23 @@ const ( headerSize uint64 = 8 ) +// ProofVariant is an idiomatic “enum”. type proofvar uint16 +const ( + ProofNone proofvar = 0 + ProofHHA proofvar = proofvar(TypeProofHistoricalHashesAccumulator) + ProofRoots proofvar = proofvar(TypeProofHistoricalRoots) + ProofCapella proofvar = proofvar(TypeProofHistoricalSummariesCapella) + ProofDeneb proofvar = proofvar(TypeProofHistoricalSummariesDeneb) +) + +// Proof bundles variant + compressed bytes. +type Proof struct { + Variant proofvar + Data []byte +} + type Builder struct { w *e2store.Writer buf *bytes.Buffer @@ -97,17 +112,21 @@ func NewBuilder(w io.Writer) *Builder { } } -func (b *Builder) Add(block *types.Block, receipts types.Receipts, td *big.Int, proofBytes []byte, proofty proofvar) error { +func (b *Builder) Add(block *types.Block, receipts types.Receipts, td *big.Int, proof *Proof) error { if len(b.headersRLP) >= MaxEraESize { return fmt.Errorf("exceeds MaxEraESize %d", MaxEraESize) } - if proofty != 0 && len(proofBytes) == 0 { - return fmt.Errorf("proof type %d requires proof bytes", proofty) + if proof != nil { + if proof.Variant == ProofNone || len(proof.Data) == 0 { + return fmt.Errorf("invalid proof: variant=%d len=%d", proof.Variant, len(proof.Data)) + } } - if len(b.headersRLP) != 0 && proofty != b.prooftype { - return fmt.Errorf("cannot mix proof types, expected %d, got %d", b.prooftype, proofty) + if len(b.headersRLP) == 0 { + b.prooftype = proof.Variant + } else if proof.Variant != b.prooftype { + return fmt.Errorf("cannot mix proof variants: have %d want %d", b.prooftype, proof.Variant) } hdr, err := rlp.EncodeToBytes(block.Header()) @@ -130,8 +149,8 @@ func (b *Builder) Add(block *types.Block, receipts types.Receipts, td *big.Int, b.tdsint = append(b.tdsint, new(big.Int).Set(td)) b.hashes = append(b.hashes, block.Hash()) - if b.prooftype != 0 { - b.proofsRLP = append(b.proofsRLP, proofBytes) + if proof.Variant != ProofNone { + b.proofsRLP = append(b.proofsRLP, proof.Data) } if b.startNum == nil { @@ -189,9 +208,7 @@ func (b *Builder) Finalize() error { b.writtenBytes += uint64(n) } } - return b.writeIndex() - } func uint256LE(v *big.Int) []byte { diff --git a/internal/era2/era2.go b/internal/era2/era2.go index dea5828566..a6c47d6912 100644 --- a/internal/era2/era2.go +++ b/internal/era2/era2.go @@ -305,7 +305,6 @@ func (e *Era2) loadIndex() error { off += 8 + int64(length) // headersize plus length to jump to next TLV header } return nil - } func (e *Era2) BatchRange(first, count uint64, wantHeaders, wantBodies, wantReceipts, wantProofs bool) (hdrs []*types.Header, bods []*types.Body, recs []types.Receipts, prfs [][]byte, err error) {