-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnode.go
64 lines (55 loc) · 1.28 KB
/
node.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
package chainload
import (
"context"
"math/big"
"time"
"github.com/gochain/gochain/v3/accounts"
"github.com/gochain/gochain/v3/common"
"github.com/gochain/gochain/v3/core/types"
"github.com/gochain/gochain/v3/goclient"
"go.uber.org/zap"
)
type Node struct {
lgr *zap.Logger
Number int
gas uint64
*goclient.Client
*AccountStore
SeedCh chan SeedReq
}
func (n *Node) refund(ctx context.Context, acct accounts.Account, nonce uint64, seed common.Address) (*big.Int, error) {
t := time.Now()
bal, err := n.PendingBalanceAt(ctx, acct.Address)
if err != nil {
return nil, err
}
pendingBalanceAtTimer.UpdateSince(t)
t = time.Now()
gasPrice, err := n.SuggestGasPrice(ctx)
if err != nil {
return nil, err
}
suggestGasPriceTimer.UpdateSince(t)
gas := randBetween(n.gas, 2*n.gas)
var amount big.Int
amount.Mul(new(big.Int).SetUint64(gas), gasPrice)
amount.Sub(bal, &amount)
if amount.Cmp(new(big.Int)) < 1 {
return nil, nil
}
tx := types.NewTransaction(nonce, seed, &amount, gas, gasPrice, nil)
t = time.Now()
tx, err = n.SignTx(acct, tx)
if err != nil {
return nil, err
}
signTxTimer.UpdateSince(t)
t = time.Now()
err = n.SendTransaction(ctx, tx)
if err != nil {
sendTxErrMeter.Mark(1)
return nil, err
}
sendTxTimer.UpdateSince(t)
return &amount, nil
}