/
newFeeDelegatedValueTransferMemoTC.go
154 lines (125 loc) · 4.55 KB
/
newFeeDelegatedValueTransferMemoTC.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
package newFeeDelegatedValueTransferMemoTC
import (
"errors"
"fmt"
"log"
"math/big"
"math/rand"
"github.com/klaytn/klaytn-load-tester/klayslave/account"
"github.com/klaytn/klaytn-load-tester/klayslave/clipool"
"github.com/klaytn/klaytn/client"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/params"
"github.com/myzhan/boomer"
)
const Name = "newFeeDelegatedValueTransferMemoTC"
var (
endPoint string
nAcc int
accGrp []*account.Account
cliPool clipool.ClientPool
gasPrice *big.Int
// multinode tester
transferedValue *big.Int
expectedFee *big.Int
fromAccount *account.Account
prevBalanceFrom *big.Int
toAccount *account.Account
prevBalanceTo *big.Int
)
func Init(accs []*account.Account, endpoint string, gp *big.Int) {
gasPrice = gp
endPoint = endpoint
cliCreate := func() interface{} {
c, err := client.Dial(endPoint)
if err != nil {
log.Fatalf("Failed to connect RPC: %v", err)
}
return c
}
cliPool.Init(20, 300, cliCreate)
for _, acc := range accs {
accGrp = append(accGrp, acc)
}
nAcc = len(accGrp)
}
func Run() {
cli := cliPool.Alloc().(*client.Client)
from := accGrp[rand.Int()%nAcc]
to := accGrp[rand.Int()%nAcc]
value := big.NewInt(int64(rand.Int() % 3))
start := boomer.Now()
_, _, err := from.TransferNewFeeDelegatedValueTransferMemoTx(cli, to, value)
elapsed := boomer.Now() - start
if err == nil {
boomer.Events.Publish("request_success", "http", "transferNewFeeDelegatedValueTransferMemoTx"+" to "+endPoint, elapsed, int64(10))
cliPool.Free(cli)
} else {
boomer.Events.Publish("request_failure", "http", "transferNewFeeDelegatedValueTransferMemoTx"+" to "+endPoint, elapsed, err.Error())
}
}
func RunSingle() (txHash common.Hash, err error) {
cli := cliPool.Alloc().(*client.Client)
defer cliPool.Free(cli)
fromIdx := rand.Int() % nAcc
toIdx := (fromIdx + 1) % nAcc
from := accGrp[fromIdx]
to := accGrp[toIdx]
value := big.NewInt(int64(rand.Int() % 3))
fmt.Printf("[TC] transferSignedTc: %v, from:%v, to:%v, value:%v\n", endPoint, from.GetAddress().String(), to.GetAddress().String(), value)
transferedValue = big.NewInt(value.Int64())
expectedFee = big.NewInt(0).Mul(big.NewInt(25*params.Ston), big.NewInt(21000))
balance, err := from.GetBalance(cli)
if err != nil {
return common.Hash{}, err
}
fromAccount = from
prevBalanceFrom = big.NewInt(balance.Int64())
fmt.Printf("From:%v, balance:%v\n", fromAccount.GetAddress().String(), prevBalanceFrom.Int64())
balance, err = to.GetBalance(cli)
if err != nil {
return common.Hash{}, err
}
toAccount = to
prevBalanceTo = big.NewInt(balance.Int64())
fmt.Printf("To:%v, balance:%v\n", toAccount.GetAddress().String(), prevBalanceTo.Int64())
txHash, _, err = from.TransferNewFeeDelegatedValueTransferMemoTx(cli, to, value)
if err != nil {
return common.Hash{}, err
}
return txHash, err
}
// CheckResult returns true and nil error, if expected results are observed.
// Otherewise returns false and error.
func CheckResult() (bool, error) {
cli := cliPool.Alloc().(*client.Client)
defer cliPool.Free(cli)
balance, err := fromAccount.GetBalance(cli)
if err != nil {
return false, err
}
expectedBalance := big.NewInt(0)
expectedBalance.Sub(prevBalanceFrom, transferedValue)
expectedBalance.Sub(expectedBalance, expectedFee)
// fmt.Printf("prevBalanceFrom=%v, transferedValue=%v, expectedFee=%v\n", prevBalanceFrom.Int64(), transferedValue.Int64(), expectedFee.Int64())
if expectedBalance.Int64() != balance.Int64() {
fmt.Printf("[FAILED] From account address=%v, Expected balance=%v, Actual balance=%v\n", fromAccount.GetAddress().String(), expectedBalance.Int64(), balance.Int64())
return false, errors.New("Balance mismatched!")
} else {
fmt.Printf("[PASSED] From account address=%v, Expected balance=%v, Actual balance=%v\n", fromAccount.GetAddress().String(), expectedBalance.Int64(), balance.Int64())
}
balance, err = toAccount.GetBalance(cli)
if err != nil {
return false, err
}
expectedBalance = big.NewInt(0)
expectedBalance.Add(prevBalanceTo, transferedValue)
fmt.Printf("prevBalanceTo=%v, transferedValue=%v\n", prevBalanceTo.Int64(), transferedValue.Int64())
if expectedBalance.Int64() != balance.Int64() {
fmt.Printf("[FAILED] To account address=%v, Expected balance=%v, Actual balance=%v\n", toAccount.GetAddress().String(), expectedBalance.Int64(), balance.Int64())
return false, errors.New("Balance mismatched!")
} else {
fmt.Printf("[PASSED] To account address=%v, Expected balance=%v, Actual balance=%v\n", toAccount.GetAddress().String(), expectedBalance.Int64(), balance.Int64())
}
return true, err
}