forked from dymensionxyz/dymint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_test.go
103 lines (95 loc) · 2.1 KB
/
validator_test.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
92
93
94
95
96
97
98
99
100
101
102
103
package p2p_test
import (
"testing"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/assert"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/types"
"github.com/dymensionxyz/dymint/mempool"
nodemempool "github.com/dymensionxyz/dymint/node/mempool"
"github.com/dymensionxyz/dymint/p2p"
)
func TestValidator_TxValidator(t *testing.T) {
type args struct {
mp mempool.Mempool
numMsgs int
}
tests := []struct {
name string
args args
want bool
}{
{
name: "valid: tx already in cache",
args: args{
mp: &mockMP{err: mempool.ErrTxInCache},
numMsgs: 3,
},
want: true,
}, {
name: "valid: mempool is full",
args: args{
mp: &mockMP{err: mempool.ErrMempoolIsFull{}},
numMsgs: 3,
},
want: true,
}, {
name: "invalid: tx too large",
args: args{
mp: &mockMP{err: mempool.ErrTxTooLarge{}},
numMsgs: 3,
},
want: false,
}, {
name: "invalid: pre-check error",
args: args{
mp: &mockMP{err: mempool.ErrPreCheck{}},
numMsgs: 3,
},
want: false,
}, {
name: "valid: no error",
args: args{
mp: &mockMP{},
numMsgs: 3,
},
want: true,
}, {
name: "unknown error",
args: args{
mp: &mockMP{err: assert.AnError},
numMsgs: 3,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger := log.TestingLogger()
validateTx := p2p.NewValidator(logger, nil).TxValidator(tt.args.mp, nodemempool.NewMempoolIDs())
valid := validateTx(txMsg)
assert.Equalf(t, tt.want, valid, "validateTx() = %v, want %v", valid, tt.want)
})
}
}
type mockMP struct {
mempool.Mempool
err error
}
func (m *mockMP) CheckTx(_ types.Tx, cb func(*abci.Response), _ mempool.TxInfo) error {
if cb != nil {
code := abci.CodeTypeOK
if m.err != nil {
code = 1
}
cb(&abci.Response{
Value: &abci.Response_CheckTx{CheckTx: &abci.ResponseCheckTx{Code: code}},
})
}
return m.err
}
var txMsg = &p2p.GossipMessage{
Data: []byte("data"),
From: peer.ID("from"),
}