-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask.go
83 lines (72 loc) · 2.22 KB
/
task.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
package main
import (
"context"
"fmt"
"strings"
"github.com/blockchain-tps-test/samples/theta/tps"
)
const (
Eth tps.TaskType = iota
TaskRetryLimit = 10
)
type EthTask struct {
to string
amount int64
tokenId int64
tryCount int
transfer_type string
}
func (t *EthTask) Type() tps.TaskType {
return Eth
}
func (t *EthTask) TryCount() int {
return t.tryCount
}
func (t *EthTask) IncrementTryCount() error {
t.tryCount += 1
if t.tryCount >= TaskRetryLimit {
return fmt.Errorf("err task retry limit, tryCount: %d", t.tryCount)
}
return nil
}
func (t *EthTask) Do(ctx context.Context, client *EthClient, priv string, nonce uint64, queue *tps.Queue, logger tps.Logger, contractAddress string) error {
//根据不同的model生成不同的发送交易的任务
var rootErr error
if t.transfer_type == "CrossChain" {
_, rootErr = client.CrossChainTNT20Transfer(ctx, priv, nonce, t.to, t.amount, contractAddress, 1) //链间交易
} else if t.transfer_type == "InChain" {
//_, rootErr = client.CrossSubChainTNT20Transfer(ctx, priv, nonce, t.to, t.amount, contractAddress, 1) //链内TNT20
_, rootErr = client.Erc20TransferFrom(ctx, priv, nonce, t.to, t.amount, contractAddress, 1)
} else {
logger.Fatal("err model")
}
if rootErr != nil { //根据错误捕捉,并返回错误类型
if strings.Contains(rootErr.Error(), "Invalid Transaction") {
//logger.Warn(fmt.Sprintf("nonce error, %s", rootErr.Error()))
logger.Fatal("err model")
return tps.ErrWrongNonce
}
if strings.Contains(rootErr.Error(), "Transaction already seen") {
//logger.Warn(fmt.Sprintf("nonce error, %s", rootErr.Error()))
logger.Fatal("err model")
return tps.ErrTaskRetry
}
if strings.Contains(rootErr.Error(), "ValidateInputAdvanced: Got") {
logger.Warn(fmt.Sprintf("nonce error, %s", rootErr.Error()))
logger.Fatal("err model")
return rootErr
}
logger.Warn(fmt.Sprintf("faild sending, err: %s", rootErr.Error()))
if err := t.IncrementTryCount(); err != nil {
logger.Fatal("err model")
return tps.ErrWrongNonce
}
// if strings.Contains(rootErr.Error(), "ValidateInputAdvanced:"){
// return tps.NonceWrong
// }
return tps.ErrWrongNonce
queue.Push(t)
return nil
}
return nil
}