forked from celestiaorg/celestia-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutil.go
141 lines (116 loc) · 3.85 KB
/
testutil.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
package paytestutil
import (
"bytes"
"testing"
"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/celestia-app/x/payment/types"
"github.com/celestiaorg/nmt/namespace"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/require"
tmrand "github.com/tendermint/tendermint/libs/rand"
)
// GenerateManyRawWirePFD creates many raw WirePayForData transactions. Using
// negative numbers for count and size will randomize those values. count is
// capped at 5000 and size is capped at 3MB. Going over these caps will result
// in randomized values.
func GenerateManyRawWirePFD(t *testing.T, txConfig client.TxConfig, signer *types.KeyringSigner, count, size int) [][]byte {
// hardcode a maximum of 5000 transactions so that we can use this for fuzzing
if count > 5000 || count < 0 {
count = tmrand.Intn(5000)
}
txs := make([][]byte, count)
coin := sdk.Coin{
Denom: app.BondDenom,
Amount: sdk.NewInt(10),
}
opts := []types.TxBuilderOption{
types.SetFeeAmount(sdk.NewCoins(coin)),
types.SetGasLimit(10000000),
}
for i := 0; i < count; i++ {
if size < 0 || size > 3000000 {
size = tmrand.Intn(1000000)
}
wpfdTx := generateRawWirePFDTx(
t,
txConfig,
randomValidNamespace(),
tmrand.Bytes(size),
signer,
opts...,
)
txs[i] = wpfdTx
}
return txs
}
func GenerateManyRawSendTxs(t *testing.T, txConfig client.TxConfig, signer *types.KeyringSigner, count int) [][]byte {
txs := make([][]byte, count)
for i := 0; i < count; i++ {
txs[i] = generateRawSendTx(t, txConfig, signer, 100)
}
return txs
}
// this creates send transactions meant to help test encoding/prepare/process
// proposal, they are not meant to actually be executed by the state machine. If
// we want that, we have to update nonce, and send funds to someone other than
// the same account signing the transaction.
func generateRawSendTx(t *testing.T, txConfig client.TxConfig, signer *types.KeyringSigner, amount int64) (rawTx []byte) {
feeCoin := sdk.Coin{
Denom: app.BondDenom,
Amount: sdk.NewInt(1),
}
opts := []types.TxBuilderOption{
types.SetFeeAmount(sdk.NewCoins(feeCoin)),
types.SetGasLimit(1000000000),
}
amountCoin := sdk.Coin{
Denom: app.BondDenom,
Amount: sdk.NewInt(amount),
}
addr, err := signer.GetSignerInfo().GetAddress()
require.NoError(t, err)
builder := signer.NewTxBuilder(opts...)
msg := banktypes.NewMsgSend(addr, addr, sdk.NewCoins(amountCoin))
tx, err := signer.BuildSignedTx(builder, msg)
require.NoError(t, err)
rawTx, err = txConfig.TxEncoder()(tx)
require.NoError(t, err)
return rawTx
}
// generateRawWirePFDTx creates a tx with a single MsgWirePayForData message using the provided namespace and message
func generateRawWirePFDTx(t *testing.T, txConfig client.TxConfig, ns, message []byte, signer *types.KeyringSigner, opts ...types.TxBuilderOption) (rawTx []byte) {
// create a msg
msg := generateSignedWirePayForData(t, ns, message, signer, opts, types.AllSquareSizes(len(message))...)
builder := signer.NewTxBuilder(opts...)
tx, err := signer.BuildSignedTx(builder, msg)
require.NoError(t, err)
// encode the tx
rawTx, err = txConfig.TxEncoder()(tx)
require.NoError(t, err)
return rawTx
}
func generateSignedWirePayForData(t *testing.T, ns, message []byte, signer *types.KeyringSigner, options []types.TxBuilderOption, ks ...uint64) *types.MsgWirePayForData {
msg, err := types.NewWirePayForData(ns, message, ks...)
if err != nil {
t.Error(err)
}
err = msg.SignShareCommitments(signer, options...)
if err != nil {
t.Error(err)
}
return msg
}
const (
TestAccountName = "test-account"
)
func randomValidNamespace() namespace.ID {
for {
s := tmrand.Bytes(8)
if bytes.Compare(s, appconsts.MaxReservedNamespace) > 0 {
return s
}
}
}