-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
334 lines (306 loc) · 10.2 KB
/
client.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/big"
"math/rand"
"os"
"path/filepath"
"time"
"github.com/panghalamit/go-ethereum/accounts"
"github.com/panghalamit/go-ethereum/accounts/keystore"
"github.com/panghalamit/go-ethereum/core/types"
"github.com/panghalamit/go-ethereum/ethclient"
)
type TestConfig struct {
PathToAccountsDir string
Scenario1 Scenario1Config
Scenario2 Scenario2Config
Scenario3 Scenario3Config
}
type Scenario1Config struct {
Value json.Number
GasLimit uint64
}
type Scenario2Config struct {
Value json.Number
GasLimit uint64
NumTxs int
RescueTxTimeout int
}
type Scenario3Config struct {
Value json.Number
GasLimit uint64
MinExecutablesPerAccount uint32
MaxExecutablesTotal uint32
MaxNonExecutablesPerAccount uint32
MaxNonExecutablesTotal uint32
NumTxs int
}
var config TestConfig
// InitTestConfig inititializes config parameters from json file
func InitTestConfig() (*TestConfig, error) {
jsonBytes, err := ioutil.ReadFile("config-test.json")
if err != nil {
return &config, err
}
err1 := json.Unmarshal(jsonBytes, &config)
if err1 != nil {
return &config, err1
}
return &config, nil
}
// ImportKsAccount imports accounts into keystore given path to account's file
func ImportKsAccount(ks *keystore.KeyStore, pathToAccount string) (*accounts.Account, error) {
jsonBytes, err := ioutil.ReadFile(pathToAccount)
if err != nil {
return &accounts.Account{}, err
}
pwd := ""
account, err := ks.Import(jsonBytes, pwd, pwd)
if err != nil {
return &accounts.Account{}, err
}
return &account, nil
}
// Init creates a keystore and imports test accounts from list of account files
func Init(accfiles []string, ks *keystore.KeyStore) ([]*accounts.Account, error) {
acclist := make([]*accounts.Account, len(accfiles))
var err error
for ind, acc := range accfiles {
acclist[ind], err = ImportKsAccount(ks, acc)
if err != nil {
return nil, err
}
}
return acclist, nil
}
// FilePathWalkDir returns all files present in a directory
func FilePathWalkDir(root string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return nil
})
return files, err
}
// CreateTransaction creates new transaction with given sender,receiver, nonce, value, gasPrice, chainId
func CreateTransaction(ks *keystore.KeyStore, sender *accounts.Account, receiver *accounts.Account, passphrase string, nonce uint64, value *big.Int, data []byte, gasPrice *big.Int, gasLimit uint64, chainId *big.Int) (*types.Transaction, error) {
tx := types.NewTransaction(nonce, receiver.Address, value, gasLimit, gasPrice, data)
signedTx, err := ks.SignTxWithPassphrase(*sender, passphrase, tx, chainId)
if err != nil {
return nil, err
}
return signedTx, nil
}
// Scenario1 sends a single transaction from a account
func Scenario1(cl *ethclient.Client, acclist []*accounts.Account, ks *keystore.KeyStore) {
fmt.Printf("\n -------------------------Scenario1 : Sending a single transfer transaction-------------------------\n")
sender := acclist[0]
receiver := acclist[1]
nonce, err1 := cl.PendingNonceAt(context.Background(), sender.Address)
if err1 != nil {
fmt.Printf("err1\n")
log.Fatal(err1)
}
balance, err2 := cl.BalanceAt(context.Background(), sender.Address, nil)
if err2 != nil {
log.Fatal(err2)
}
fmt.Printf("Balance of account %s : %v \n", sender.Address.Hex(), balance)
fmt.Printf("Nonce value for account %s : %v\n", sender.Address.Hex(), nonce)
value, ok := new(big.Int).SetString(config.Scenario1.Value.String(), 10)
if !ok {
value = big.NewInt(1000000000000000000)
}
gasLimit := config.Scenario1.GasLimit
gasPrice, err3 := cl.SuggestGasPrice(context.Background())
if err3 != nil {
fmt.Printf("err3\n")
log.Fatal(err3)
}
fmt.Printf("Suggested gas price %v\n", gasPrice)
chainID, err4 := cl.NetworkID(context.Background())
if err4 != nil {
fmt.Printf("err4\n")
log.Fatal(err4)
}
fmt.Printf("ChainId = %v\n", chainID)
var data []byte
signedTx, err5 := CreateTransaction(ks, sender, receiver, "", nonce, value, data, gasPrice, gasLimit, chainID)
if err5 != nil {
fmt.Printf("err5\n")
log.Fatal(err5)
}
fmt.Printf("Signed transaction successfully :%v \n", signedTx.Hash().Hex())
err6 := cl.SendTransaction(context.Background(), signedTx)
if err6 != nil {
fmt.Printf("err6\n")
log.Fatal(err6)
}
fmt.Printf("tx sent: %s", signedTx.Hash().Hex())
}
// Scenario2 sends multiple transactions from a accounts with random nonce gap
func Scenario2(cl *ethclient.Client, acclist []*accounts.Account, ks *keystore.KeyStore) {
fmt.Printf("\n-------------------------Scenario2 : Sending multiple transfer transactions with gap in nonce values-------------------------\n")
sender := acclist[2]
receiver := acclist[0]
rand.Seed(time.Now().UTC().UnixNano())
nonce, err1 := cl.PendingNonceAt(context.Background(), sender.Address)
if err1 != nil {
log.Fatal(err1)
}
chainID, err2 := cl.NetworkID(context.Background())
if err2 != nil {
log.Fatal(err2)
}
value, ok := new(big.Int).SetString(config.Scenario2.Value.String(), 10)
if !ok {
value = big.NewInt(1000000000000000000)
}
gasLimit := config.Scenario2.GasLimit
gasPrice, err3 := cl.SuggestGasPrice(context.Background())
var data []byte
if err3 != nil {
log.Fatal(err3)
}
for i := 0; i < config.Scenario2.NumTxs; i++ {
toss := rand.Float32()
if toss < 0.3 {
gasPrice = big.NewInt(2)
} else if toss > 0.7 {
gasPrice = big.NewInt(3)
}
fmt.Printf("Gas price %v\n", gasPrice)
fmt.Printf("ChainId = %v\n", chainID)
signedTx, err4 := CreateTransaction(ks, sender, receiver, "", nonce+uint64(i+1), value, data, gasPrice, gasLimit, chainID)
if err4 != nil {
log.Fatal(err4)
}
fmt.Printf("Signed transaction successfully :%v \n", signedTx.Hash().Hex())
err5 := cl.SendTransaction(context.Background(), signedTx)
if err5 != nil {
fmt.Println(err5)
//log.Fatal(err6)
} else {
fmt.Printf("tx sent with hash: %s, nonce: %v, value: %v\n", signedTx.Hash().Hex(), nonce+uint64(i+1), value)
}
}
// get latest block
currBlockHeader, err6 := cl.HeaderByNumber(context.Background(), nil)
if err6 != nil {
fmt.Println(err6)
return
}
blockNumber := currBlockHeader.Number.Uint64()
fmt.Printf("Using block height as timeout to send rescue Tx (Tx with gap value as nonce)\n")
for curr := blockNumber; curr < blockNumber+uint64(config.Scenario2.RescueTxTimeout); {
// get latest block
currBlockHeader, err6 = cl.HeaderByNumber(context.Background(), nil)
if err6 != nil {
fmt.Println(err6)
return
}
curr = currBlockHeader.Number.Uint64()
fmt.Printf("Block height %v \n", curr)
time.Sleep(15 * time.Second)
}
// wait for config.Scenario2.RescueTxTimeout block times before sending rescue tx
signedRescueTx, err7 := CreateTransaction(ks, sender, receiver, "", nonce, value, data, gasPrice, gasLimit, chainID)
if err7 != nil {
log.Fatal(err7)
}
fmt.Printf("Signed transaction successfully :%v \n", signedRescueTx.Hash().Hex())
err8 := cl.SendTransaction(context.Background(), signedRescueTx)
if err8 != nil {
log.Fatal(err8)
} else {
fmt.Printf("Rescue tx with pending nonce sent with hash: %s, nonce: %v, value: %v\n", signedRescueTx.Hash().Hex(), nonce, value)
}
}
// Scenario3 sends multiple transactions from multiple accounts
func Scenario3(cl *ethclient.Client, acclist []*accounts.Account, ks *keystore.KeyStore) {
fmt.Printf("\n-------------------------Scenario3 : Sending multiple transfer transactions from different accounts-------------------------\n")
//set the same value in txpool config
minExecutablesPerAccount := config.Scenario3.MinExecutablesPerAccount
maxExecutablesTotal := config.Scenario3.MaxExecutablesTotal
maxNonExecutablesPerAccount := config.Scenario3.MaxNonExecutablesPerAccount
maxNonExecutablesTotal := config.Scenario3.MaxNonExecutablesTotal
fmt.Printf("Testing for txpool config values as, accountslots : %v, globalslots : %v, accountqueue : %v, globalqueue : %v \n", minExecutablesPerAccount, maxExecutablesTotal, maxNonExecutablesPerAccount, maxNonExecutablesTotal)
gasPrice, err3 := cl.SuggestGasPrice(context.Background())
if err3 != nil {
log.Fatal(err3)
}
chainID, err4 := cl.NetworkID(context.Background())
if err4 != nil {
log.Fatal(err4)
}
fmt.Printf("ChainId = %v\n", chainID)
value, ok := new(big.Int).SetString(config.Scenario3.Value.String(), 10)
if !ok {
value = big.NewInt(1000000000000000000)
}
gasLimit := config.Scenario3.GasLimit
size := uint32(len(acclist))
for i := uint32(0); i < uint32(config.Scenario3.NumTxs); i++ {
sender := acclist[i%size]
receiver := acclist[(i+1)%size]
nonce, err1 := cl.PendingNonceAt(context.Background(), sender.Address)
if err1 != nil {
log.Fatal(err1)
}
gasPrice = big.NewInt(int64(i))
fmt.Printf("Gas price %v\n", gasPrice)
var data []byte
signedTx, err5 := CreateTransaction(ks, sender, receiver, "", nonce, value, data, gasPrice, gasLimit, chainID)
if err5 != nil {
log.Fatal(err5)
}
fmt.Printf("Signed transaction successfully :%v \n", signedTx.Hash().Hex())
err6 := cl.SendTransaction(context.Background(), signedTx)
if err6 != nil {
fmt.Println(err6)
//log.Fatal(err6)
}
}
}
func main() {
if len(os.Args) != 3 {
log.Fatal("Usage: ./TxLifeCycle <NodeRpcAddr> <ScenarioToRun>")
}
_, err3 := InitTestConfig()
if err3 != nil {
log.Fatal(err3)
}
fmt.Println("Successfully Read Test Configuration from config file")
client, err := ethclient.Dial(os.Args[1])
if err != nil {
log.Fatal(err)
}
fmt.Println("We connected to a local node")
accfiles, err1 := FilePathWalkDir(config.PathToAccountsDir)
if err1 != nil {
log.Fatal(err1)
}
if len(accfiles) < 3 {
log.Fatal("Need atleast 3 accounts to test all scenarios")
}
scenarioToRun := os.Args[2]
ks := keystore.NewKeyStore("./tmp", keystore.StandardScryptN, keystore.StandardScryptP)
acclist, err2 := Init(accfiles, ks)
if err2 != nil {
log.Fatal(err2)
}
if scenarioToRun == "1" {
Scenario1(client, acclist, ks)
} else if scenarioToRun == "2" {
Scenario2(client, acclist, ks)
} else if scenarioToRun == "3" {
Scenario3(client, acclist, ks)
}
}