-
Notifications
You must be signed in to change notification settings - Fork 3
/
broadcast_test.go
146 lines (115 loc) · 3.34 KB
/
broadcast_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package faucet
import (
"errors"
"testing"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
coreTypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tm2Errors "github.com/gnolang/gno/tm2/pkg/bft/abci/example/errors"
)
func TestBroadcastTransaction(t *testing.T) {
t.Parallel()
t.Run("invalid broadcast", func(t *testing.T) {
t.Parallel()
var (
sendErr = errors.New("unable to send transaction")
capturedTx *std.Tx
mockClient = &mockClient{
sendTransactionCommitFn: func(tx *std.Tx) (*coreTypes.ResultBroadcastTxCommit, error) {
capturedTx = tx
return nil, sendErr
},
}
)
// Broadcast the transaction, and capture the error
tx := &std.Tx{Memo: "dummy tx"}
require.ErrorIs(t, broadcastTransaction(mockClient, tx), sendErr)
// Make sure the correct transaction
// broadcast was attempted
assert.Equal(t, tx, capturedTx)
})
t.Run("initial tx validation error (CheckTx)", func(t *testing.T) {
t.Parallel()
var (
capturedTx *std.Tx
checkTxErr = tm2Errors.UnauthorizedError{}
response = &coreTypes.ResultBroadcastTxCommit{
CheckTx: abci.ResponseCheckTx{
ResponseBase: abci.ResponseBase{
Error: checkTxErr,
},
},
}
mockClient = &mockClient{
sendTransactionCommitFn: func(tx *std.Tx) (*coreTypes.ResultBroadcastTxCommit, error) {
capturedTx = tx
return response, nil
},
}
)
// Broadcast the transaction, and capture the error
tx := &std.Tx{Memo: "dummy tx"}
require.ErrorIs(t, broadcastTransaction(mockClient, tx), checkTxErr)
// Make sure the correct transaction
// broadcast was attempted
assert.Equal(t, tx, capturedTx)
})
t.Run("execute tx error (DeliverTx)", func(t *testing.T) {
t.Parallel()
var (
capturedTx *std.Tx
deliverTxErr = tm2Errors.BadNonceError{}
response = &coreTypes.ResultBroadcastTxCommit{
DeliverTx: abci.ResponseDeliverTx{
ResponseBase: abci.ResponseBase{
Error: deliverTxErr,
},
},
}
mockClient = &mockClient{
sendTransactionCommitFn: func(tx *std.Tx) (*coreTypes.ResultBroadcastTxCommit, error) {
capturedTx = tx
return response, nil
},
}
)
// Broadcast the transaction, and capture the error
tx := &std.Tx{Memo: "dummy tx"}
require.ErrorIs(t, broadcastTransaction(mockClient, tx), deliverTxErr)
// Make sure the correct transaction
// broadcast was attempted
assert.Equal(t, tx, capturedTx)
})
t.Run("valid broadcast", func(t *testing.T) {
t.Parallel()
var (
capturedTx *std.Tx
response = &coreTypes.ResultBroadcastTxCommit{
DeliverTx: abci.ResponseDeliverTx{
ResponseBase: abci.ResponseBase{
Error: nil, // no error
},
},
CheckTx: abci.ResponseCheckTx{
ResponseBase: abci.ResponseBase{
Error: nil, // no error
},
},
}
mockClient = &mockClient{
sendTransactionCommitFn: func(tx *std.Tx) (*coreTypes.ResultBroadcastTxCommit, error) {
capturedTx = tx
return response, nil
},
}
)
// Broadcast the transaction, and capture the error
tx := &std.Tx{Memo: "dummy tx"}
require.NoError(t, broadcastTransaction(mockClient, tx))
// Make sure the correct transaction
// broadcast was attempted
assert.Equal(t, tx, capturedTx)
})
}