forked from dymensionxyz/dymint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
91 lines (81 loc) · 3.01 KB
/
validator.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package p2p
import (
"context"
"errors"
"github.com/dymensionxyz/dymint/log"
"github.com/dymensionxyz/dymint/mempool"
nodemempool "github.com/dymensionxyz/dymint/node/mempool"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/pubsub"
corep2p "github.com/tendermint/tendermint/p2p"
)
// GossipValidator is a callback function type.
type GossipValidator func(*GossipMessage) bool
// IValidator is an interface for implementing validators of messages gossiped in the p2p network.
type IValidator interface {
// TxValidator creates a pubsub validator that uses the node's mempool to check the
// transaction. If the transaction is valid, then it is added to the mempool
TxValidator(mp mempool.Mempool, mpoolIDS *nodemempool.MempoolIDs) GossipValidator
}
// Validator is a validator for messages gossiped in the p2p network.
type Validator struct {
logger log.Logger
localPubsubServer *pubsub.Server
}
var _ IValidator = (*Validator)(nil)
// NewValidator creates a new Validator.
func NewValidator(logger log.Logger, pusbsubServer *pubsub.Server) *Validator {
return &Validator{
logger: logger,
localPubsubServer: pusbsubServer,
}
}
// TxValidator creates a pubsub validator that uses the node's mempool to check the
// transaction. If the transaction is valid, then it is added to the mempool.
func (v *Validator) TxValidator(mp mempool.Mempool, mpoolIDS *nodemempool.MempoolIDs) GossipValidator {
return func(txMessage *GossipMessage) bool {
v.logger.Debug("transaction received", "bytes", len(txMessage.Data))
checkTxResCh := make(chan *abci.Response, 1)
err := mp.CheckTx(txMessage.Data, func(resp *abci.Response) {
checkTxResCh <- resp
}, mempool.TxInfo{
SenderID: mpoolIDS.GetForPeer(txMessage.From),
SenderP2PID: corep2p.ID(txMessage.From),
})
switch {
case errors.Is(err, mempool.ErrTxInCache):
return true
case errors.Is(err, mempool.ErrMempoolIsFull{}):
return true
case errors.Is(err, mempool.ErrTxTooLarge{}):
return false
case errors.Is(err, mempool.ErrPreCheck{}):
return false
default:
}
res := <-checkTxResCh
checkTxResp := res.GetCheckTx()
return checkTxResp.Code == abci.CodeTypeOK
}
}
// BlockValidator runs basic checks on the gossiped block
func (v *Validator) BlockValidator() GossipValidator {
return func(blockMsg *GossipMessage) bool {
v.logger.Debug("block event received", "from", blockMsg.From, "bytes", len(blockMsg.Data))
var gossipedBlock GossipedBlock
if err := gossipedBlock.UnmarshalBinary(blockMsg.Data); err != nil {
v.logger.Error("failed to deserialize gossiped block", "error", err)
return false
}
if err := gossipedBlock.Validate(); err != nil {
v.logger.Error("Invalid gossiped block", "error", err)
return false
}
err := v.localPubsubServer.PublishWithEvents(context.Background(), gossipedBlock, map[string][]string{EventTypeKey: {EventNewGossipedBlock}})
if err != nil {
v.logger.Error("Error publishing event", "err", err)
return false
}
return true
}
}