forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcast_test.go
70 lines (57 loc) · 1.8 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
package client
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/rpc/client/mock"
"github.com/tendermint/tendermint/rpc/coretypes"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/client/flags"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
type MockClient struct {
mock.Client
err error
}
func (c MockClient) BroadcastTxCommit(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTxCommit, error) {
return nil, c.err
}
func (c MockClient) BroadcastTxAsync(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
return nil, c.err
}
func (c MockClient) BroadcastTxSync(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
return nil, c.err
}
func CreateContextWithErrorAndMode(err error, mode string) Context {
return Context{
Client: MockClient{err: err},
BroadcastMode: mode,
}
}
// Test the correct code is returned when
func TestBroadcastError(t *testing.T) {
errors := map[error]uint32{
tmtypes.ErrTxInCache: sdkerrors.ErrTxInMempoolCache.ABCICode(),
tmtypes.ErrTxTooLarge{}: sdkerrors.ErrTxTooLarge.ABCICode(),
tmtypes.ErrMempoolIsFull{}: sdkerrors.ErrMempoolIsFull.ABCICode(),
}
modes := []string{
flags.BroadcastAsync,
flags.BroadcastBlock,
flags.BroadcastSync,
}
txBytes := []byte{0xA, 0xB}
txHash := fmt.Sprintf("%X", tmhash.Sum(txBytes))
for _, mode := range modes {
for err, code := range errors {
ctx := CreateContextWithErrorAndMode(err, mode)
resp, returnedErr := ctx.BroadcastTx(txBytes)
require.NoError(t, returnedErr)
require.Equal(t, code, resp.Code)
require.NotEmpty(t, resp.Codespace)
require.Equal(t, txHash, resp.TxHash)
}
}
}