1717package blsync
1818
1919import (
20- "fmt"
21- "math/big"
22-
23- "github.com/ethereum/go-ethereum/beacon/engine"
2420 "github.com/ethereum/go-ethereum/beacon/light/request"
2521 "github.com/ethereum/go-ethereum/beacon/light/sync"
2622 "github.com/ethereum/go-ethereum/beacon/types"
2723 "github.com/ethereum/go-ethereum/common"
2824 "github.com/ethereum/go-ethereum/common/lru"
29- ctypes "github.com/ethereum/go-ethereum/core/types"
3025 "github.com/ethereum/go-ethereum/event"
3126 "github.com/ethereum/go-ethereum/log"
32- "github.com/ethereum/go-ethereum/trie"
33- "github.com/holiman/uint256"
34- "github.com/protolambda/zrnt/eth2/beacon/capella"
35- "github.com/protolambda/zrnt/eth2/configs"
36- "github.com/protolambda/ztyp/tree"
3727)
3828
3929// beaconBlockSync implements request.Module; it fetches the beacon blocks belonging
4030// to the validated and prefetch heads.
4131type beaconBlockSync struct {
42- recentBlocks * lru.Cache [common.Hash , * capella .BeaconBlock ]
32+ recentBlocks * lru.Cache [common.Hash , * types .BeaconBlock ]
4333 locked map [common.Hash ]request.ServerAndID
4434 serverHeads map [request.Server ]common.Hash
4535 headTracker headTracker
4636
4737 lastHeadInfo types.HeadInfo
48- chainHeadFeed * event.Feed
38+ chainHeadFeed event.FeedOf [types. ChainHeadEvent ]
4939}
5040
5141type headTracker interface {
@@ -55,16 +45,19 @@ type headTracker interface {
5545}
5646
5747// newBeaconBlockSync returns a new beaconBlockSync.
58- func newBeaconBlockSync (headTracker headTracker , chainHeadFeed * event. Feed ) * beaconBlockSync {
48+ func newBeaconBlockSync (headTracker headTracker ) * beaconBlockSync {
5949 return & beaconBlockSync {
60- headTracker : headTracker ,
61- chainHeadFeed : chainHeadFeed ,
62- recentBlocks : lru.NewCache [common.Hash , * capella.BeaconBlock ](10 ),
63- locked : make (map [common.Hash ]request.ServerAndID ),
64- serverHeads : make (map [request.Server ]common.Hash ),
50+ headTracker : headTracker ,
51+ recentBlocks : lru.NewCache [common.Hash , * types.BeaconBlock ](10 ),
52+ locked : make (map [common.Hash ]request.ServerAndID ),
53+ serverHeads : make (map [request.Server ]common.Hash ),
6554 }
6655}
6756
57+ func (s * beaconBlockSync ) SubscribeChainHead (ch chan <- types.ChainHeadEvent ) event.Subscription {
58+ return s .chainHeadFeed .Subscribe (ch )
59+ }
60+
6861// Process implements request.Module.
6962func (s * beaconBlockSync ) Process (requester request.Requester , events []request.Event ) {
7063 for _ , event := range events {
@@ -73,7 +66,7 @@ func (s *beaconBlockSync) Process(requester request.Requester, events []request.
7366 sid , req , resp := event .RequestInfo ()
7467 blockRoot := common .Hash (req .(sync.ReqBeaconBlock ))
7568 if resp != nil {
76- s .recentBlocks .Add (blockRoot , resp .(* capella .BeaconBlock ))
69+ s .recentBlocks .Add (blockRoot , resp .(* types .BeaconBlock ))
7770 }
7871 if s .locked [blockRoot ] == sid {
7972 delete (s .locked , blockRoot )
@@ -112,63 +105,11 @@ func (s *beaconBlockSync) tryRequestBlock(requester request.Requester, blockRoot
112105 }
113106}
114107
115- func blockHeadInfo (block * capella .BeaconBlock ) types.HeadInfo {
108+ func blockHeadInfo (block * types .BeaconBlock ) types.HeadInfo {
116109 if block == nil {
117110 return types.HeadInfo {}
118111 }
119- return types.HeadInfo {Slot : uint64 (block .Slot ), BlockRoot : beaconBlockHash (block )}
120- }
121-
122- // beaconBlockHash calculates the hash of a beacon block.
123- func beaconBlockHash (beaconBlock * capella.BeaconBlock ) common.Hash {
124- return common .Hash (beaconBlock .HashTreeRoot (configs .Mainnet , tree .GetHashFn ()))
125- }
126-
127- // getExecBlock extracts the execution block from the beacon block's payload.
128- func getExecBlock (beaconBlock * capella.BeaconBlock ) (* ctypes.Block , error ) {
129- payload := & beaconBlock .Body .ExecutionPayload
130- txs := make ([]* ctypes.Transaction , len (payload .Transactions ))
131- for i , opaqueTx := range payload .Transactions {
132- var tx ctypes.Transaction
133- if err := tx .UnmarshalBinary (opaqueTx ); err != nil {
134- return nil , fmt .Errorf ("failed to parse tx %d: %v" , i , err )
135- }
136- txs [i ] = & tx
137- }
138- withdrawals := make ([]* ctypes.Withdrawal , len (payload .Withdrawals ))
139- for i , w := range payload .Withdrawals {
140- withdrawals [i ] = & ctypes.Withdrawal {
141- Index : uint64 (w .Index ),
142- Validator : uint64 (w .ValidatorIndex ),
143- Address : common .Address (w .Address ),
144- Amount : uint64 (w .Amount ),
145- }
146- }
147- wroot := ctypes .DeriveSha (ctypes .Withdrawals (withdrawals ), trie .NewStackTrie (nil ))
148- execHeader := & ctypes.Header {
149- ParentHash : common .Hash (payload .ParentHash ),
150- UncleHash : ctypes .EmptyUncleHash ,
151- Coinbase : common .Address (payload .FeeRecipient ),
152- Root : common .Hash (payload .StateRoot ),
153- TxHash : ctypes .DeriveSha (ctypes .Transactions (txs ), trie .NewStackTrie (nil )),
154- ReceiptHash : common .Hash (payload .ReceiptsRoot ),
155- Bloom : ctypes .Bloom (payload .LogsBloom ),
156- Difficulty : common .Big0 ,
157- Number : new (big.Int ).SetUint64 (uint64 (payload .BlockNumber )),
158- GasLimit : uint64 (payload .GasLimit ),
159- GasUsed : uint64 (payload .GasUsed ),
160- Time : uint64 (payload .Timestamp ),
161- Extra : []byte (payload .ExtraData ),
162- MixDigest : common .Hash (payload .PrevRandao ), // reused in merge
163- Nonce : ctypes.BlockNonce {}, // zero
164- BaseFee : (* uint256 .Int )(& payload .BaseFeePerGas ).ToBig (),
165- WithdrawalsHash : & wroot ,
166- }
167- execBlock := ctypes .NewBlockWithHeader (execHeader ).WithBody (txs , nil ).WithWithdrawals (withdrawals )
168- if execBlockHash := execBlock .Hash (); execBlockHash != common .Hash (payload .BlockHash ) {
169- return execBlock , fmt .Errorf ("Sanity check failed, payload hash does not match (expected %x, got %x)" , common .Hash (payload .BlockHash ), execBlockHash )
170- }
171- return execBlock , nil
112+ return types.HeadInfo {Slot : block .Slot (), BlockRoot : block .Root ()}
172113}
173114
174115func (s * beaconBlockSync ) updateEventFeed () {
@@ -190,14 +131,16 @@ func (s *beaconBlockSync) updateEventFeed() {
190131 return
191132 }
192133 s .lastHeadInfo = headInfo
134+
193135 // new head block and finality info available; extract executable data and send event to feed
194- execBlock , err := getExecBlock ( headBlock )
136+ execBlock , err := headBlock . ExecutionPayload ( )
195137 if err != nil {
196138 log .Error ("Error extracting execution block from validated beacon block" , "error" , err )
197139 return
198140 }
199141 s .chainHeadFeed .Send (types.ChainHeadEvent {
200- HeadBlock : engine .BlockToExecutableData (execBlock , nil , nil ).ExecutionPayload ,
201- Finalized : common .Hash (finality .Finalized .PayloadHeader .BlockHash ),
142+ BeaconHead : head .Header ,
143+ Block : execBlock ,
144+ Finalized : finality .Finalized .PayloadHeader .BlockHash (),
202145 })
203146}
0 commit comments