-
Notifications
You must be signed in to change notification settings - Fork 2
/
case.go
385 lines (347 loc) · 10.8 KB
/
case.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/rlp"
"io/ioutil"
"main/base"
"main/node_manager"
"math/big"
"net/http"
"sort"
"sync"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/polynetwork/bridge-common/chains/eth"
"github.com/polynetwork/bridge-common/log"
)
type Context struct {
nodes *eth.SDK
sync.RWMutex
height uint64
getRewardsUrl string
getGasFeeUrl string
}
func (ctx *Context) Till(height uint64) {
pass := false
for {
ctx.RLock()
pass = ctx.height >= height
ctx.RUnlock()
if pass {
return
}
time.Sleep(time.Millisecond * 300)
}
}
type Case struct {
index int64
err error
actions []Action
plan []*ActionItem
}
func (c *Case) Run(ctx *Context) (err error) {
exit := make(chan struct{})
defer func() {
close(exit)
}()
go func() {
for {
select {
case <-exit:
return
default:
height, err := ctx.nodes.Node().GetLatestHeight()
if err != nil {
log.Error("Get chain latest height failed", "err", err)
} else {
ctx.Lock()
ctx.height = height
ctx.Unlock()
}
time.Sleep(time.Millisecond * 200)
}
}
}()
// Sort actions
bp := make(map[uint64][]Action)
for i, a := range c.actions {
a.SetIndex(i)
bp[a.StartAt()] = append(bp[a.StartAt()], a)
}
c.plan = make([]*ActionItem, 0, len(bp))
for b, actions := range bp {
c.plan = append(c.plan, &ActionItem{b, actions})
}
sort.Slice(c.plan, func(i, j int) bool { return c.plan[i].start < c.plan[j].start })
type result struct {
note string
err error
index int
}
res := make(chan result, len(c.actions))
for i, item := range c.plan {
log.Info("Scheduling plan", "index", i, "action_count", len(item.actions), "at", item.start)
for _, action := range item.actions {
go func(a Action) {
ctx.Till(a.StartAt())
log.Info("Running case action", "case_index", c.index, "action_index", a.Index())
note, e := a.Run(ctx)
res <- result{note, e, a.Index()}
}(action)
}
}
for j := 0; j < len(c.actions); j++ {
log.Info("Waiting case actions result", "case_index", c.index, "progress", j+1, "total", len(c.actions))
r := <-res
c.actions[r.index].SetError(r.err)
c.actions[r.index].SetNote(r.note)
if r.err != nil {
err = fmt.Errorf("action failure, err: %v, action_index: %v", r.err, r.index)
log.Error("Run case action failed", "case", c.index, "action", r.index, "err", err)
}
}
return
}
type ActionItem struct {
start uint64
actions []Action
}
type Action interface {
Run(*Context) (string, error)
StartAt() uint64
Before() uint64
SetIndex(int)
Index() int
Error() error
SetError(err error)
Note() string
SetNote(note string)
}
type ActionBase struct {
Epoch uint64
Block uint64
ShouldBefore uint64
index int
note string
err error
}
func (a *ActionBase) StartAt() uint64 { return a.Block + a.Epoch*uint64(CONFIG.BlocksPerEpoch) }
func (a *ActionBase) Before() uint64 {
return a.ShouldBefore + a.Epoch*uint64(CONFIG.BlocksPerEpoch)
}
func (a *ActionBase) SetIndex(index int) { a.index = index }
func (a *ActionBase) Index() int { return a.index }
func (a *ActionBase) SetError(err error) { a.err = err }
func (a *ActionBase) Error() error { return a.err }
func (a *ActionBase) SetNote(note string) { a.note = note }
func (a *ActionBase) Note() string { return a.note }
type SendTx struct {
ActionBase
Tx *types.Transaction
ShouldSucceed bool
}
func (a *SendTx) Run(ctx *Context) (note string, err error) {
err = ctx.nodes.Node().SendTransaction(context.Background(), a.Tx)
if err != nil {
return
}
currentHeight, _ := ctx.nodes.Node().GetLatestHeight()
log.Info("Sent tx", "current_height", currentHeight, "hash", a.Tx.Hash(), "index", a.Index())
for i := 0; i < 10; i++ {
time.Sleep(time.Second * 2)
height, _, pending, err := ctx.nodes.Node().Confirm(a.Tx.Hash(), 1, 10)
if err != nil {
return "", err
}
if height > 0 {
if height <= a.Before() {
rec, err := ctx.nodes.Node().TransactionReceipt(context.Background(), a.Tx.Hash())
if err != nil {
return "", err
}
if (rec.Status == 1) == a.ShouldSucceed {
return "", nil
}
return "", fmt.Errorf("transaction status error, status %v, wanted %v", rec.Status == 1, a.ShouldSucceed)
}
return "", fmt.Errorf("tx packed too late, height %v, expected before %v", height, a.Before())
} else if !pending {
return "", fmt.Errorf("possible tx lost %s", a.Tx.Hash())
}
}
return "", nil
}
type Query struct {
ActionBase
Request ethereum.CallMsg
Assertions []Assertion
}
func (a *Query) Run(ctx *Context) (note string, err error) {
output, err := ctx.nodes.Node().CallContract(context.Background(), a.Request, big.NewInt(int64(a.StartAt())))
if err != nil {
return
}
err = Assert(output, a.Assertions)
return
}
type CheckBalance struct {
ActionBase
Address common.Address
Validators []common.Address
NetStake *big.Int
}
func (a *CheckBalance) Run(ctx *Context) (note string, err error) {
checkHeight := a.StartAt() - 10
fmt.Println("action ", a.Index(), "check balance at height ", checkHeight)
balance, err := ctx.nodes.Node().BalanceAt(context.Background(), a.Address, big.NewInt(int64(checkHeight)))
if err != nil {
return "", err
}
fmt.Printf("account=%s balance=%s\n", a.Address.String(), balance)
initialBalance := new(big.Int)
if b, ok := base.InitialBalanceMap[a.Address.String()]; ok {
initialBalance.SetString(b, 10)
}
fmt.Printf("account=%s initialBalance=%s\n", a.Address.String(), initialBalance)
fmt.Printf("account=%s netStake=%s\n", a.Address.String(), a.NetStake)
expectedRewards, err := a.getExpectedRewards(ctx, a.Address, checkHeight)
if err != nil {
return
}
fmt.Printf("account=%s expectedRewards=%s\n", a.Address.String(), expectedRewards)
gasFee, err := a.getGasFee(ctx, a.Address, checkHeight)
if err != nil {
return
}
fmt.Printf("account=%s gasFee=%s\n", a.Address.String(), gasFee)
unArrivedRewards := big.NewInt(0)
for _, validator := range a.Validators {
input := &node_manager.GetStakeRewardsParam{ConsensusAddress: validator, StakeAddress: a.Address}
data, err := input.Encode()
if err != nil {
err = fmt.Errorf("encode failed, err: %v", err)
return "", err
}
request := ethereum.CallMsg{To: &NODE_MANAGER_CONTRACT, Data: data}
output, err := ctx.nodes.Node().CallContract(context.Background(), request, big.NewInt(int64(checkHeight)))
if err != nil {
log.Error("callContract failed", "err", err)
continue
}
unpacked, err := node_manager.ABI.Unpack(base.MethodGetStakeRewards, output)
if err != nil {
return "", fmt.Errorf("fail to unpack output: %v %x", err, output)
}
result := *abi.ConvertType(unpacked[0], new([]byte)).(*[]byte)
stakeWards := &node_manager.StakeRewards{}
err = rlp.DecodeBytes(result, stakeWards)
if err != nil {
return "", fmt.Errorf("fail to decode return value: %v %x", err, result)
}
fmt.Printf("stakeWards.Rewards%s\n", stakeWards.Rewards.BigInt())
unArrivedRewards = new(big.Int).Add(unArrivedRewards, stakeWards.Rewards.BigInt())
}
fmt.Printf("account=%s unArrivedRewards=%s\n", a.Address.String(), unArrivedRewards)
arrivedRewards := new(big.Int).Sub(balance, initialBalance)
arrivedRewards.Add(arrivedRewards, gasFee)
arrivedRewards.Sub(arrivedRewards, a.NetStake)
fmt.Printf("account=%s arrivedRewards=%s\n", a.Address.String(), arrivedRewards)
allRewards := new(big.Int).Add(unArrivedRewards, arrivedRewards)
fmt.Printf("account=%s allRewards=%s\n", a.Address.String(), allRewards)
maxDelta := new(big.Int).Mul(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil), big.NewInt(1))
delta := new(big.Int).Abs(new(big.Int).Sub(allRewards, expectedRewards))
note = fmt.Sprintf("delta %s", delta.String())
fmt.Printf("actionIndex=%d account=%s delta=%s\n", a.Index(), a.Address.String(), delta)
if delta.Cmp(maxDelta) == 1 {
return "", fmt.Errorf("actionIndex=%d account: %s balance check failure, allRewards %s, expectedRewards %s, delta %s", a.Index(), a.Address, allRewards, expectedRewards, delta)
}
return
}
type GetRewardsReq struct {
Id string `json:"Id"`
Addresses []string `json:"Addresses"`
EndHeight uint64 `json:"EndHeight"`
}
type GetRewardsRsp struct {
Action string `json:"action"`
Desc string `json:"desc"`
Error int `json:"error"`
Result struct {
Amount []string `json:"Amount"`
Id string `json:"Id"`
} `json:"result"`
}
type GetGasFeeReq struct {
Id string `json:"Id"`
Addresses []string `json:"Addresses"`
EndHeight uint64 `json:"EndHeight"`
}
type GetGasFeeRsp struct {
Action string `json:"action"`
Desc string `json:"desc"`
Error int `json:"error"`
Result struct {
Amount []string `json:"Amount"`
Id string `json:"Id"`
} `json:"result"`
}
func (a *CheckBalance) getExpectedRewards(ctx *Context, address common.Address, height uint64) (*big.Int, error) {
getRewardsReq := &GetRewardsReq{Addresses: []string{address.String()}, EndHeight: height}
getRewardsRsp := &GetRewardsRsp{}
err := PostJsonFor(ctx.getRewardsUrl, getRewardsReq, getRewardsRsp)
if err != nil || len(getRewardsRsp.Result.Amount) == 0 {
return nil, fmt.Errorf("getExpectedRewards post failed, err: %v", err)
}
expectedRewards, ok := new(big.Int).SetString(getRewardsRsp.Result.Amount[0], 10)
if !ok {
return nil, fmt.Errorf("getExpectedRewards convert %s to big.Int failed", getRewardsRsp.Result.Amount[0])
}
return expectedRewards, nil
}
func (a *CheckBalance) getGasFee(ctx *Context, address common.Address, height uint64) (*big.Int, error) {
getGasFeeReq := &GetGasFeeReq{Addresses: []string{address.String()}, EndHeight: height}
getGasFeeRsp := &GetGasFeeRsp{}
err := PostJsonFor(ctx.getGasFeeUrl, getGasFeeReq, getGasFeeRsp)
if err != nil || len(getGasFeeRsp.Result.Amount) == 0 {
return nil, fmt.Errorf("getGasFees post failed, err: %v", err)
}
gasFee, ok := new(big.Int).SetString(getGasFeeRsp.Result.Amount[0], 10)
if !ok {
return nil, fmt.Errorf("getExpectedRewards convert %s to big.Int failed", getGasFeeRsp.Result.Amount[0])
}
return gasFee, nil
}
func PostJsonFor(url string, payload interface{}, result interface{}) error {
data, err := json.Marshal(payload)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(respBody, result)
if err != nil {
log.Error("PostJson response", "Body", string(respBody))
} else {
log.Debug("PostJson response", "Body", string(respBody))
}
return err
}