simplified proof builder structure

This commit is contained in:
shantichanal 2025-07-07 15:21:52 +02:00 committed by lightclient
parent 730bbb7244
commit 44968250aa
No known key found for this signature in database
GPG key ID: 657913021EF45A6A
2 changed files with 26 additions and 10 deletions

View file

@ -58,8 +58,23 @@ const (
headerSize uint64 = 8 headerSize uint64 = 8
) )
// ProofVariant is an idiomatic “enum”.
type proofvar uint16 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 { type Builder struct {
w *e2store.Writer w *e2store.Writer
buf *bytes.Buffer 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 { if len(b.headersRLP) >= MaxEraESize {
return fmt.Errorf("exceeds MaxEraESize %d", MaxEraESize) return fmt.Errorf("exceeds MaxEraESize %d", MaxEraESize)
} }
if proofty != 0 && len(proofBytes) == 0 { if proof != nil {
return fmt.Errorf("proof type %d requires proof bytes", proofty) 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 { if len(b.headersRLP) == 0 {
return fmt.Errorf("cannot mix proof types, expected %d, got %d", b.prooftype, proofty) 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()) 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.tdsint = append(b.tdsint, new(big.Int).Set(td))
b.hashes = append(b.hashes, block.Hash()) b.hashes = append(b.hashes, block.Hash())
if b.prooftype != 0 { if proof.Variant != ProofNone {
b.proofsRLP = append(b.proofsRLP, proofBytes) b.proofsRLP = append(b.proofsRLP, proof.Data)
} }
if b.startNum == nil { if b.startNum == nil {
@ -189,9 +208,7 @@ func (b *Builder) Finalize() error {
b.writtenBytes += uint64(n) b.writtenBytes += uint64(n)
} }
} }
return b.writeIndex() return b.writeIndex()
} }
func uint256LE(v *big.Int) []byte { func uint256LE(v *big.Int) []byte {

View file

@ -305,7 +305,6 @@ func (e *Era2) loadIndex() error {
off += 8 + int64(length) // headersize plus length to jump to next TLV header off += 8 + int64(length) // headersize plus length to jump to next TLV header
} }
return nil 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) { 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) {