forked from celestiaorg/celestia-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirect_tx_gen.go
235 lines (208 loc) · 6.4 KB
/
direct_tx_gen.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package util
import (
"math/rand"
"testing"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/celestiaorg/celestia-app/v2/app"
"github.com/celestiaorg/celestia-app/v2/pkg/appconsts"
"github.com/celestiaorg/celestia-app/v2/pkg/user"
"github.com/celestiaorg/celestia-app/v2/test/util/blobfactory"
"github.com/celestiaorg/celestia-app/v2/test/util/testfactory"
"github.com/celestiaorg/go-square/blob"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
coretypes "github.com/tendermint/tendermint/types"
)
// RandBlobTxsWithAccounts will create random blob transactions using the
// provided configuration. The account info is queried directly from the
// application. One blob transaction is generated per account provided.
func RandBlobTxsWithAccounts(
t *testing.T,
capp *app.App,
cfg client.TxConfig,
kr keyring.Keyring,
size int,
blobCount int,
randSize bool,
chainid string,
accounts []string,
extraOpts ...user.TxOption,
) []coretypes.Tx {
opts := append(blobfactory.DefaultTxOpts(), extraOpts...)
require.Greater(t, size, 0)
require.Greater(t, blobCount, 0)
txs := make([]coretypes.Tx, len(accounts))
for i := 0; i < len(accounts); i++ {
addr := testfactory.GetAddress(kr, accounts[i])
acc := DirectQueryAccount(capp, addr)
account := user.NewAccount(accounts[i], acc.GetAccountNumber(), acc.GetSequence())
signer, err := user.NewSigner(kr, cfg, chainid, capp.AppVersion(), account)
require.NoError(t, err)
randomizedSize := size
if randSize {
randomizedSize = rand.Intn(size)
if randomizedSize == 0 {
randomizedSize = 1
}
}
randomizedBlobCount := blobCount
if randSize {
randomizedBlobCount = rand.Intn(blobCount)
if randomizedBlobCount == 0 {
randomizedBlobCount = 1
}
}
_, blobs := blobfactory.RandMsgPayForBlobsWithSigner(tmrand.NewRand(), addr.String(), randomizedSize, randomizedBlobCount)
tx, _, err := signer.CreatePayForBlobs(account.Name(), blobs, opts...)
require.NoError(t, err)
txs[i] = tx
}
return txs
}
func DirectQueryAccount(app *app.App, addr sdk.AccAddress) authtypes.AccountI {
ctx := app.NewContext(true, tmproto.Header{})
return app.AccountKeeper.GetAccount(ctx, addr)
}
// RandBlobTxsWithManualSequence will create random blob transactions using the
// provided configuration. One blob transaction is generated per account
// provided. The sequence and account numbers are set manually using the provided values.
func RandBlobTxsWithManualSequence(
t *testing.T,
cfg client.TxConfig,
kr keyring.Keyring,
size int,
blobCount int,
randSize bool,
chainid string,
accounts []string,
sequence, accountNum uint64,
invalidSignature bool,
) []coretypes.Tx {
t.Helper()
require.Greater(t, size, 0)
require.Greater(t, blobCount, 0)
opts := blobfactory.DefaultTxOpts()
txs := make([]coretypes.Tx, len(accounts))
for i := 0; i < len(accounts); i++ {
addr := testfactory.GetAddress(kr, accounts[i])
acc := user.NewAccount(accounts[i], accountNum, sequence)
signer, err := user.NewSigner(kr, cfg, chainid, appconsts.LatestVersion, acc)
require.NoError(t, err)
randomizedSize := size
if randSize {
randomizedSize = rand.Intn(size)
if randomizedSize == 0 {
randomizedSize = 1
}
}
randomizedBlobCount := blobCount
if randSize {
randomizedBlobCount = rand.Intn(blobCount)
if randomizedBlobCount == 0 {
randomizedBlobCount = 1
}
}
msg, blobs := blobfactory.RandMsgPayForBlobsWithSigner(tmrand.NewRand(), addr.String(), randomizedSize, randomizedBlobCount)
tx, err := signer.CreateTx([]sdk.Msg{msg}, opts...)
require.NoError(t, err)
if invalidSignature {
builder := cfg.NewTxBuilder()
for _, opt := range opts {
builder = opt(builder)
}
require.NoError(t, builder.SetMsgs(msg))
err := builder.SetSignatures(signing.SignatureV2{
PubKey: acc.PubKey(),
Data: &signing.SingleSignatureData{
SignMode: signing.SignMode_SIGN_MODE_DIRECT,
Signature: []byte("invalid signature"),
},
Sequence: acc.Sequence(),
})
require.NoError(t, err)
tx, err = signer.EncodeTx(builder.GetTx())
require.NoError(t, err)
}
cTx, err := blob.MarshalBlobTx(tx, blobs...)
if err != nil {
panic(err)
}
txs[i] = cTx
}
return txs
}
// SendTxsWithAccounts will create a send transaction per account provided, and
// send all funds to the "toAccount". The account info is queried directly from
// the application.
func SendTxsWithAccounts(
t *testing.T,
capp *app.App,
enc client.TxConfig,
kr keyring.Keyring,
amount uint64,
toAccount string,
accounts []string,
chainid string,
extraOpts ...user.TxOption,
) []coretypes.Tx {
opts := append(blobfactory.DefaultTxOpts(), extraOpts...)
txs := make([]coretypes.Tx, len(accounts))
for i := 0; i < len(accounts); i++ {
signingAddr := getAddress(accounts[i], kr)
// update the account info in the signer so the signature is valid
acc := DirectQueryAccount(capp, signingAddr)
if acc == nil {
t.Fatalf("account %s not found", signingAddr)
}
txs[i] = SendTxWithManualSequence(
t,
enc,
kr,
accounts[i],
toAccount,
amount,
chainid,
acc.GetSequence(),
acc.GetAccountNumber(),
opts...,
)
}
return txs
}
// SendTxsWithAccounts will create a send transaction per account provided. The
// account info must be provided.
func SendTxWithManualSequence(
t *testing.T,
cfg client.TxConfig,
kr keyring.Keyring,
fromAcc, toAcc string,
amount uint64,
chainid string,
sequence, accountNum uint64,
opts ...user.TxOption,
) coretypes.Tx {
fromAddr, toAddr := getAddress(fromAcc, kr), getAddress(toAcc, kr)
signer, err := user.NewSigner(kr, cfg, chainid, appconsts.LatestVersion, user.NewAccount(fromAcc, accountNum, sequence))
require.NoError(t, err)
msg := banktypes.NewMsgSend(fromAddr, toAddr, sdk.NewCoins(sdk.NewCoin(app.BondDenom, sdk.NewIntFromUint64(amount))))
rawTx, err := signer.CreateTx([]sdk.Msg{msg}, opts...)
require.NoError(t, err)
return rawTx
}
func getAddress(account string, kr keyring.Keyring) sdk.AccAddress {
rec, err := kr.Key(account)
if err != nil {
panic(err)
}
addr, err := rec.GetAddress()
if err != nil {
panic(err)
}
return addr
}