forked from ava-labs/coreth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
73 lines (60 loc) · 2.67 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// (c) 2021-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package handlers
import (
"context"
"github.com/MetalBlockchain/metalgo/codec"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/coreth/core/state/snapshot"
"github.com/MetalBlockchain/coreth/core/types"
"github.com/MetalBlockchain/coreth/ethdb"
"github.com/MetalBlockchain/coreth/plugin/evm/message"
"github.com/MetalBlockchain/coreth/sync/handlers/stats"
"github.com/MetalBlockchain/coreth/trie"
"github.com/ethereum/go-ethereum/common"
)
var _ message.RequestHandler = &syncHandler{}
type BlockProvider interface {
GetBlock(common.Hash, uint64) *types.Block
}
type SnapshotProvider interface {
Snapshots() *snapshot.Tree
}
type SyncDataProvider interface {
BlockProvider
SnapshotProvider
}
type syncHandler struct {
stateTrieLeafsRequestHandler *LeafsRequestHandler
atomicTrieLeafsRequestHandler *LeafsRequestHandler
blockRequestHandler *BlockRequestHandler
codeRequestHandler *CodeRequestHandler
}
// NewSyncHandler constructs the handler for serving state sync.
func NewSyncHandler(
provider SyncDataProvider,
diskDB ethdb.KeyValueReader,
evmTrieDB *trie.Database,
atomicTrieDB *trie.Database,
networkCodec codec.Manager,
stats stats.HandlerStats,
) message.RequestHandler {
return &syncHandler{
stateTrieLeafsRequestHandler: NewLeafsRequestHandler(evmTrieDB, provider, networkCodec, stats),
atomicTrieLeafsRequestHandler: NewLeafsRequestHandler(atomicTrieDB, nil, networkCodec, stats),
blockRequestHandler: NewBlockRequestHandler(provider, networkCodec, stats),
codeRequestHandler: NewCodeRequestHandler(diskDB, networkCodec, stats),
}
}
func (s *syncHandler) HandleStateTrieLeafsRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, leafsRequest message.LeafsRequest) ([]byte, error) {
return s.stateTrieLeafsRequestHandler.OnLeafsRequest(ctx, nodeID, requestID, leafsRequest)
}
func (s *syncHandler) HandleAtomicTrieLeafsRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, leafsRequest message.LeafsRequest) ([]byte, error) {
return s.atomicTrieLeafsRequestHandler.OnLeafsRequest(ctx, nodeID, requestID, leafsRequest)
}
func (s *syncHandler) HandleBlockRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, blockRequest message.BlockRequest) ([]byte, error) {
return s.blockRequestHandler.OnBlockRequest(ctx, nodeID, requestID, blockRequest)
}
func (s *syncHandler) HandleCodeRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, codeRequest message.CodeRequest) ([]byte, error) {
return s.codeRequestHandler.OnCodeRequest(ctx, nodeID, requestID, codeRequest)
}