forked from ava-labs/coreth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
75 lines (59 loc) · 2.91 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
74
75
// (c) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package message
import (
"context"
"github.com/ethereum/go-ethereum/log"
"github.com/kukrer/savannahnode/ids"
)
var (
_ GossipHandler = NoopMempoolGossipHandler{}
_ RequestHandler = NoopRequestHandler{}
)
// GossipHandler handles incoming gossip messages
type GossipHandler interface {
HandleAtomicTx(nodeID ids.NodeID, msg AtomicTxGossip) error
HandleEthTxs(nodeID ids.NodeID, msg EthTxsGossip) error
}
type NoopMempoolGossipHandler struct{}
func (NoopMempoolGossipHandler) HandleAtomicTx(nodeID ids.NodeID, msg AtomicTxGossip) error {
log.Debug("dropping unexpected AtomicTxGossip message", "peerID", nodeID)
return nil
}
func (NoopMempoolGossipHandler) HandleEthTxs(nodeID ids.NodeID, msg EthTxsGossip) error {
log.Debug("dropping unexpected EthTxsGossip message", "peerID", nodeID)
return nil
}
// RequestHandler interface handles incoming requests from peers
// Must have methods in format of handleType(context.Context, ids.ShortID, uint32, request Type) error
// so that the Request object of relevant Type can invoke its respective handle method
// on this struct.
// Also see GossipHandler for implementation style.
type RequestHandler interface {
HandleStateTrieLeafsRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, leafsRequest LeafsRequest) ([]byte, error)
HandleAtomicTrieLeafsRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, leafsRequest LeafsRequest) ([]byte, error)
HandleBlockRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, request BlockRequest) ([]byte, error)
HandleCodeRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, codeRequest CodeRequest) ([]byte, error)
}
// ResponseHandler handles response for a sent request
// Only one of OnResponse or OnFailure is called for a given requestID, not both
type ResponseHandler interface {
// OnResponse is invoked when the peer responded to a request
OnResponse(nodeID ids.NodeID, requestID uint32, response []byte) error
// OnFailure is invoked when there was a failure in processing a request
// The FailureReason outlines the underlying cause.
OnFailure(nodeID ids.NodeID, requestID uint32) error
}
type NoopRequestHandler struct{}
func (NoopRequestHandler) HandleStateTrieLeafsRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, leafsRequest LeafsRequest) ([]byte, error) {
return nil, nil
}
func (NoopRequestHandler) HandleAtomicTrieLeafsRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, leafsRequest LeafsRequest) ([]byte, error) {
return nil, nil
}
func (NoopRequestHandler) HandleBlockRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, request BlockRequest) ([]byte, error) {
return nil, nil
}
func (NoopRequestHandler) HandleCodeRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, codeRequest CodeRequest) ([]byte, error) {
return nil, nil
}