Skip to content

Commit a04720f

Browse files
committed
Add broadcast tests
1 parent 85c23e7 commit a04720f

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

broadcast_test.go

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package faucet
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
8+
coreTypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
9+
"github.com/gnolang/gno/tm2/pkg/std"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
13+
tm2Errors "github.com/gnolang/gno/tm2/pkg/bft/abci/example/errors"
14+
)
15+
16+
func TestBroadcastTransaction(t *testing.T) {
17+
t.Parallel()
18+
19+
t.Run("invalid broadcast", func(t *testing.T) {
20+
t.Parallel()
21+
22+
var (
23+
sendErr = errors.New("unable to send transaction")
24+
capturedTx *std.Tx
25+
26+
mockClient = &mockClient{
27+
sendTransactionCommitFn: func(tx *std.Tx) (*coreTypes.ResultBroadcastTxCommit, error) {
28+
capturedTx = tx
29+
30+
return nil, sendErr
31+
},
32+
}
33+
)
34+
35+
// Broadcast the transaction, and capture the error
36+
tx := &std.Tx{Memo: "dummy tx"}
37+
require.ErrorIs(t, broadcastTransaction(mockClient, tx), sendErr)
38+
39+
// Make sure the correct transaction
40+
// broadcast was attempted
41+
assert.Equal(t, tx, capturedTx)
42+
})
43+
44+
t.Run("initial tx validation error (CheckTx)", func(t *testing.T) {
45+
t.Parallel()
46+
47+
var (
48+
capturedTx *std.Tx
49+
50+
checkTxErr = tm2Errors.UnauthorizedError{}
51+
response = &coreTypes.ResultBroadcastTxCommit{
52+
CheckTx: abci.ResponseCheckTx{
53+
ResponseBase: abci.ResponseBase{
54+
Error: checkTxErr,
55+
},
56+
},
57+
}
58+
59+
mockClient = &mockClient{
60+
sendTransactionCommitFn: func(tx *std.Tx) (*coreTypes.ResultBroadcastTxCommit, error) {
61+
capturedTx = tx
62+
63+
return response, nil
64+
},
65+
}
66+
)
67+
68+
// Broadcast the transaction, and capture the error
69+
tx := &std.Tx{Memo: "dummy tx"}
70+
require.ErrorIs(t, broadcastTransaction(mockClient, tx), checkTxErr)
71+
72+
// Make sure the correct transaction
73+
// broadcast was attempted
74+
assert.Equal(t, tx, capturedTx)
75+
})
76+
77+
t.Run("execute tx error (DeliverTx)", func(t *testing.T) {
78+
t.Parallel()
79+
80+
var (
81+
capturedTx *std.Tx
82+
83+
deliverTxErr = tm2Errors.BadNonceError{}
84+
response = &coreTypes.ResultBroadcastTxCommit{
85+
DeliverTx: abci.ResponseDeliverTx{
86+
ResponseBase: abci.ResponseBase{
87+
Error: deliverTxErr,
88+
},
89+
},
90+
}
91+
92+
mockClient = &mockClient{
93+
sendTransactionCommitFn: func(tx *std.Tx) (*coreTypes.ResultBroadcastTxCommit, error) {
94+
capturedTx = tx
95+
96+
return response, nil
97+
},
98+
}
99+
)
100+
101+
// Broadcast the transaction, and capture the error
102+
tx := &std.Tx{Memo: "dummy tx"}
103+
require.ErrorIs(t, broadcastTransaction(mockClient, tx), deliverTxErr)
104+
105+
// Make sure the correct transaction
106+
// broadcast was attempted
107+
assert.Equal(t, tx, capturedTx)
108+
})
109+
110+
t.Run("valid broadcast", func(t *testing.T) {
111+
t.Parallel()
112+
113+
var (
114+
capturedTx *std.Tx
115+
116+
response = &coreTypes.ResultBroadcastTxCommit{
117+
DeliverTx: abci.ResponseDeliverTx{
118+
ResponseBase: abci.ResponseBase{
119+
Error: nil, // no error
120+
},
121+
},
122+
CheckTx: abci.ResponseCheckTx{
123+
ResponseBase: abci.ResponseBase{
124+
Error: nil, // no error
125+
},
126+
},
127+
}
128+
129+
mockClient = &mockClient{
130+
sendTransactionCommitFn: func(tx *std.Tx) (*coreTypes.ResultBroadcastTxCommit, error) {
131+
capturedTx = tx
132+
133+
return response, nil
134+
},
135+
}
136+
)
137+
138+
// Broadcast the transaction, and capture the error
139+
tx := &std.Tx{Memo: "dummy tx"}
140+
require.NoError(t, broadcastTransaction(mockClient, tx))
141+
142+
// Make sure the correct transaction
143+
// broadcast was attempted
144+
assert.Equal(t, tx, capturedTx)
145+
})
146+
}

mock_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package faucet
22

33
import (
4+
coreTypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
45
"github.com/gnolang/gno/tm2/pkg/crypto"
56
"github.com/gnolang/gno/tm2/pkg/std"
67
)
@@ -131,3 +132,39 @@ func (m *mockEstimator) EstimateGasWanted(tx *std.Tx) int64 {
131132

132133
return 0
133134
}
135+
136+
type (
137+
getAccountDelegate func(crypto.Address) (std.Account, error)
138+
sendTransactionSyncDelegate func(tx *std.Tx) (*coreTypes.ResultBroadcastTx, error)
139+
sendTransactionCommitDelegate func(tx *std.Tx) (*coreTypes.ResultBroadcastTxCommit, error)
140+
)
141+
142+
type mockClient struct {
143+
getAccountFn getAccountDelegate
144+
sendTransactionSyncFn sendTransactionSyncDelegate
145+
sendTransactionCommitFn sendTransactionCommitDelegate
146+
}
147+
148+
func (m *mockClient) GetAccount(address crypto.Address) (std.Account, error) {
149+
if m.getAccountFn != nil {
150+
return m.getAccountFn(address)
151+
}
152+
153+
return nil, nil
154+
}
155+
156+
func (m *mockClient) SendTransactionSync(tx *std.Tx) (*coreTypes.ResultBroadcastTx, error) {
157+
if m.sendTransactionSyncFn != nil {
158+
return m.sendTransactionSyncFn(tx)
159+
}
160+
161+
return nil, nil
162+
}
163+
164+
func (m *mockClient) SendTransactionCommit(tx *std.Tx) (*coreTypes.ResultBroadcastTxCommit, error) {
165+
if m.sendTransactionCommitFn != nil {
166+
return m.sendTransactionCommitFn(tx)
167+
}
168+
169+
return nil, nil
170+
}

0 commit comments

Comments
 (0)