@@ -5,16 +5,44 @@ package message
55
66import (
77 "context"
8+ "errors"
89 "fmt"
910
1011 "github.com/ava-labs/libevm/common"
12+ "github.com/ava-labs/libevm/core/types"
1113 "github.com/ava-labs/libevm/crypto"
1214
1315 "github.com/ava-labs/avalanchego/ids"
1416 "github.com/ava-labs/avalanchego/snow/engine/snowman/block"
1517)
1618
17- var _ Syncable = (* BlockSyncSummary )(nil )
19+ var (
20+ _ Syncable = (* BlockSyncSummary )(nil )
21+ _ SyncableParser = (* BlockSyncSummaryParser )(nil )
22+
23+ // errInvalidBlockSyncSummary is returned when the provided bytes cannot be
24+ // parsed into a valid BlockSyncSummary.
25+ errInvalidBlockSyncSummary = errors .New ("invalid block sync summary" )
26+
27+ // errAcceptImplNotSpecified is returned when Accept is called on a BlockSyncSummary
28+ // that doesn't have an acceptImpl set.
29+ errAcceptImplNotSpecified = errors .New ("accept implementation not specified" )
30+ )
31+
32+ // Syncable extends [block.StateSummary] with EVM-specific block information.
33+ type Syncable interface {
34+ block.StateSummary
35+ GetBlockHash () common.Hash
36+ GetBlockRoot () common.Hash
37+ }
38+
39+ // SyncableParser parses raw bytes into a [Syncable] instance.
40+ type SyncableParser interface {
41+ Parse (summaryBytes []byte , acceptImpl AcceptImplFn ) (Syncable , error )
42+ }
43+
44+ // AcceptImplFn is a function that determines the state sync mode for a given [Syncable].
45+ type AcceptImplFn func (Syncable ) (block.StateSyncMode , error )
1846
1947// BlockSyncSummary provides the information necessary to sync a node starting
2048// at the given block.
@@ -28,14 +56,15 @@ type BlockSyncSummary struct {
2856 acceptImpl AcceptImplFn
2957}
3058
59+ // NewBlockSyncSummary creates a new [BlockSyncSummary] for the given block.
60+ // The acceptImpl is intentionally left unset and should be set by the parser.
3161func NewBlockSyncSummary (blockHash common.Hash , blockNumber uint64 , blockRoot common.Hash ) (* BlockSyncSummary , error ) {
32- // We intentionally do not use the acceptImpl here and leave it for the parser to set.
3362 summary := BlockSyncSummary {
3463 BlockNumber : blockNumber ,
3564 BlockHash : blockHash ,
3665 BlockRoot : blockRoot ,
3766 }
38- bytes , err := Codec .Marshal (Version , & summary )
67+ bytes , err := Codec () .Marshal (Version , & summary )
3968 if err != nil {
4069 return nil , fmt .Errorf ("failed to marshal syncable summary: %w" , err )
4170 }
@@ -76,7 +105,39 @@ func (s *BlockSyncSummary) String() string {
76105
77106func (s * BlockSyncSummary ) Accept (context.Context ) (block.StateSyncMode , error ) {
78107 if s .acceptImpl == nil {
79- return block .StateSyncSkipped , fmt . Errorf ( "accept implementation not specified for summary: %s" , s )
108+ return block .StateSyncSkipped , errAcceptImplNotSpecified
80109 }
81110 return s .acceptImpl (s )
82111}
112+
113+ // BlockSyncSummaryParser parses [BlockSyncSummary] instances from raw bytes.
114+ type BlockSyncSummaryParser struct {}
115+
116+ // NewBlockSyncSummaryParser creates a new [BlockSyncSummaryParser].
117+ func NewBlockSyncSummaryParser () * BlockSyncSummaryParser {
118+ return & BlockSyncSummaryParser {}
119+ }
120+
121+ func (* BlockSyncSummaryParser ) Parse (summaryBytes []byte , acceptImpl AcceptImplFn ) (Syncable , error ) {
122+ summary := BlockSyncSummary {}
123+ if _ , err := Codec ().Unmarshal (summaryBytes , & summary ); err != nil {
124+ return nil , fmt .Errorf ("%w: %w" , errInvalidBlockSyncSummary , err )
125+ }
126+
127+ summary .bytes = summaryBytes
128+ summaryID , err := ids .ToID (crypto .Keccak256 (summaryBytes ))
129+ if err != nil {
130+ return nil , fmt .Errorf ("failed to compute summary ID: %w" , err )
131+ }
132+ summary .summaryID = summaryID
133+ summary .acceptImpl = acceptImpl
134+ return & summary , nil
135+ }
136+
137+ // BlockSyncSummaryProvider provides state summaries for blocks.
138+ type BlockSyncSummaryProvider struct {}
139+
140+ // StateSummaryAtBlock returns the block state summary for the given block if valid.
141+ func (* BlockSyncSummaryProvider ) StateSummaryAtBlock (blk * types.Block ) (block.StateSummary , error ) {
142+ return NewBlockSyncSummary (blk .Hash (), blk .NumberU64 (), blk .Root ())
143+ }
0 commit comments