/
execution.go
185 lines (168 loc) · 5.05 KB
/
execution.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
// Copyright (c) 2019 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package bot
import (
"context"
"encoding/hex"
"fmt"
"math/big"
"strings"
"time"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/tools/bot/config"
"github.com/iotexproject/iotex-core/tools/bot/pkg/util/grpcutil"
)
const (
_multiSendSha3 = "e3b48f48"
_multiSendOffset = "0000000000000000000000000000000000000000000000000000000000000060"
_prefixZero = "000000000000000000000000"
_fixPayLoad = "00000000000000000000000000000000000000000000000000000000000000047465737400000000000000000000000000000000000000000000000000000000"
)
// Execution defines a execution
type Execution struct {
cfg config.Config
ctx context.Context
cancel context.CancelFunc
name string
}
// NewExecution make a new execution
func NewExecution(cfg config.Config, name string) (Service, error) {
return newExecution(cfg, name)
}
func newExecution(cfg config.Config, name string) (Service, error) {
svr := Execution{
cfg: cfg,
name: name,
}
return &svr, nil
}
// Start starts the server
func (s *Execution) Start(ctx context.Context) error {
s.ctx, s.cancel = context.WithCancel(ctx)
return s.start()
}
// Stop stops the server
func (s *Execution) Stop() {
s.cancel()
}
// Name returns name
func (s *Execution) Name() string {
return s.name
}
func (s *Execution) start() error {
sk, err := crypto.HexStringToPrivateKey(s.cfg.Xrc20.Signer)
if err != nil {
return err
}
hs, err := s.exec(sk)
if err != nil {
return err
}
// check if timeout
s.checkAndAlert(hs)
return nil
}
func (s *Execution) checkAndAlert(hs string) {
d := time.Duration(s.cfg.AlertThreshold) * time.Second
t := time.NewTicker(d)
defer t.Stop()
select {
case <-t.C:
err := grpcutil.GetReceiptByActionHash(s.cfg.API.URL, hs)
if err != nil {
log.L().Fatal("Execution timeout:", zap.String("Execution hash", hs), zap.Error(err))
return
}
log.L().Info("Execution success:", zap.String("Execution hash", hs))
case <-s.ctx.Done():
return
}
}
func (s *Execution) exec(pri crypto.PrivateKey) (txhash string, err error) {
addr := pri.PublicKey().Address()
if addr == nil {
err = errors.New("failed to get address")
return
}
nonce, err := grpcutil.GetNonce(s.cfg.API.URL, addr.String())
if err != nil {
return
}
gasprice := big.NewInt(0).SetUint64(s.cfg.GasPrice)
if len(s.cfg.Execution.To.Address) != len(s.cfg.Execution.To.Amount) {
err = errors.New("address len is not equal to amount len")
return
}
data := _multiSendSha3 + _multiSendOffset
params2Offset := 32*3 + 1*32 + len(s.cfg.Execution.To.Address)*32
params := fmt.Sprintf("%x", params2Offset)
data += strings.Repeat("0", 64-len(params)) + params
params3Offset := params2Offset + 1*32 + len(s.cfg.Execution.To.Address)*32
params = fmt.Sprintf("%x", params3Offset)
data += strings.Repeat("0", 64-len(params)) + params
lenOfAddress := fmt.Sprintf("%x", len(s.cfg.Execution.To.Address))
data += strings.Repeat("0", 64-len(lenOfAddress)) + lenOfAddress
for _, addr := range s.cfg.Execution.To.Address {
a, errs := address.FromString(addr)
if errs != nil {
err = errs
return
}
data += _prefixZero + hex.EncodeToString(a.Bytes())
}
data += strings.Repeat("0", 64-len(lenOfAddress)) + lenOfAddress
for _, amount := range s.cfg.Execution.To.Amount {
amo, ok := new(big.Int).SetString(amount, 10)
if !ok {
err = errors.New("amount convert error")
return
}
data += strings.Repeat("0", 64-len(amo.Text(16))) + amo.Text(16)
}
data += _fixPayLoad
dataBytes, err := hex.DecodeString(data)
if err != nil {
return
}
amount, ok := new(big.Int).SetString(s.cfg.Execution.Amount, 10)
if !ok {
err = errors.New("amount convert error")
return
}
tx, err := action.NewExecution(s.cfg.Execution.Contract, nonce, amount,
s.cfg.GasLimit, gasprice, dataBytes)
if err != nil {
return
}
tx, err = grpcutil.FixGasLimit(s.cfg.API.URL, addr.String(), tx)
if err != nil {
return
}
bd := &action.EnvelopeBuilder{}
elp := bd.SetNonce(nonce).
SetGasLimit(tx.GasLimit()).
SetGasPrice(gasprice).
SetAction(tx).Build()
selp, err := action.Sign(elp, pri)
if err != nil {
return
}
err = grpcutil.SendAction(s.cfg.API.URL, selp.Proto())
if err != nil {
return
}
shash := hash.Hash256b(byteutil.Must(proto.Marshal(selp.Proto())))
txhash = hex.EncodeToString(shash[:])
log.L().Info("Execution: ", zap.String("Execution hash", txhash), zap.Uint64("nonce", nonce), zap.String("from", addr.String()))
return
}