|
| 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 | +} |
0 commit comments