forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helpers.go
105 lines (90 loc) · 2.32 KB
/
test_helpers.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
package types
// Helper functions for testing
import (
"github.com/tendermint/go-crypto"
cmn "github.com/tendermint/tmlibs/common"
)
// Creates a PrivAccount from secret.
// The amount is not set.
func PrivAccountFromSecret(secret string) PrivAccount {
privKey :=
crypto.GenPrivKeyEd25519FromSecret([]byte(secret)).Wrap()
privAccount := PrivAccount{
PrivKey: privKey,
Account: Account{
PubKey: privKey.PubKey(),
},
}
return privAccount
}
// Make `num` random accounts
func RandAccounts(num int, minAmount int64, maxAmount int64) []PrivAccount {
privAccs := make([]PrivAccount, num)
for i := 0; i < num; i++ {
balance := minAmount
if maxAmount > minAmount {
balance += cmn.RandInt64() % (maxAmount - minAmount)
}
privKey := crypto.GenPrivKeyEd25519().Wrap()
pubKey := privKey.PubKey()
privAccs[i] = PrivAccount{
PrivKey: privKey,
Account: Account{
PubKey: pubKey,
Balance: Coins{Coin{"", balance}},
},
}
}
return privAccs
}
/////////////////////////////////////////////////////////////////
//func MakeAccs(secrets ...string) (accs []PrivAccount) {
// for _, secret := range secrets {
// privAcc := PrivAccountFromSecret(secret)
// privAcc.Account.Balance = Coins{{"mycoin", 7}}
// accs = append(accs, privAcc)
// }
// return
//}
func MakeAcc(secret string) PrivAccount {
privAcc := PrivAccountFromSecret(secret)
privAcc.Account.Balance = Coins{{"mycoin", 7}}
return privAcc
}
func Accs2TxInputs(seq int, accs ...PrivAccount) []TxInput {
var txs []TxInput
for _, acc := range accs {
tx := NewTxInput(
acc.Account.PubKey,
Coins{{"mycoin", 5}},
seq)
txs = append(txs, tx)
}
return txs
}
//turn a list of accounts into basic list of transaction outputs
func Accs2TxOutputs(accs ...PrivAccount) []TxOutput {
var txs []TxOutput
for _, acc := range accs {
tx := TxOutput{
acc.Account.PubKey.Address(),
Coins{{"mycoin", 4}}}
txs = append(txs, tx)
}
return txs
}
func MakeSendTx(seq int, accOut PrivAccount, accsIn ...PrivAccount) *SendTx {
tx := &SendTx{
Gas: 0,
Fee: Coin{"mycoin", 1},
Inputs: Accs2TxInputs(seq, accsIn...),
Outputs: Accs2TxOutputs(accOut),
}
return tx
}
func SignTx(chainID string, tx *SendTx, accs ...PrivAccount) {
signBytes := tx.SignBytes(chainID)
for i, _ := range tx.Inputs {
tx.Inputs[i].Signature = accs[i].Sign(signBytes)
}
}