forked from ava-labs/spacesvm
/
helper.go
204 lines (172 loc) · 4.1 KB
/
helper.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package client
import (
"context"
"crypto/ecdsa"
"encoding/json"
"fmt"
"time"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/ethereum/go-ethereum/crypto"
"github.com/fatih/color"
"github.com/MetalBlockchain/spacesvm/chain"
"github.com/MetalBlockchain/spacesvm/tdata"
)
func PPInfo(info *chain.SpaceInfo) {
expiry := time.Unix(int64(info.Expiry), 0)
color.Cyan(
"raw space %s: units=%d expiry=%v (%v remaining)",
info.RawSpace, info.Units, expiry, time.Until(expiry),
)
}
func PPActivity(a []*chain.Activity) error {
if len(a) == 0 {
color.Cyan("no recent activity")
}
for _, item := range a {
b, err := json.Marshal(item)
if err != nil {
return err
}
color.Cyan(string(b))
}
return nil
}
// Signs and issues the transaction (node construction).
func SignIssueTx(
ctx context.Context,
cli Client,
input *chain.Input,
priv *ecdsa.PrivateKey,
opts ...OpOption,
) (txID ids.ID, cost uint64, err error) {
ret := &Op{}
ret.applyOpts(opts)
td, txCost, err := cli.SuggestedFee(ctx, input)
if err != nil {
return ids.Empty, 0, err
}
dh, err := tdata.DigestHash(td)
if err != nil {
return ids.Empty, 0, fmt.Errorf("%w: failed to compute digest hash", err)
}
sig, err := chain.Sign(dh, priv)
if err != nil {
return ids.Empty, 0, err
}
txID, err = cli.IssueTx(ctx, td, sig)
if err != nil {
return ids.Empty, 0, err
}
if err := handleConfirmation(ctx, ret, cli, txID, priv); err != nil {
return ids.Empty, 0, err
}
return txID, txCost, nil
}
// Signs and issues the transaction (local construction).
func SignIssueRawTx(
ctx context.Context,
cli Client,
utx chain.UnsignedTransaction,
priv *ecdsa.PrivateKey,
opts ...OpOption,
) (txID ids.ID, cost uint64, err error) {
ret := &Op{}
ret.applyOpts(opts)
g, err := cli.Genesis(ctx)
if err != nil {
return ids.Empty, 0, err
}
la, err := cli.Accepted(ctx)
if err != nil {
return ids.Empty, 0, err
}
price, blockCost, err := cli.SuggestedRawFee(ctx)
if err != nil {
return ids.Empty, 0, err
}
utx.SetBlockID(la)
utx.SetMagic(g.Magic)
utx.SetPrice(price + blockCost/utx.FeeUnits(g))
dh, err := chain.DigestHash(utx)
if err != nil {
return ids.Empty, 0, err
}
sig, err := chain.Sign(dh, priv)
if err != nil {
return ids.Empty, 0, err
}
tx := chain.NewTx(utx, sig)
if err := tx.Init(g); err != nil {
return ids.Empty, 0, err
}
color.Yellow(
"issuing tx %s (fee units=%d, load units=%d, price=%d, blkID=%s)",
tx.ID(), tx.FeeUnits(g), tx.LoadUnits(g), tx.GetPrice(), tx.GetBlockID(),
)
txID, err = cli.IssueRawTx(ctx, tx.Bytes())
if err != nil {
return ids.Empty, 0, err
}
if err := handleConfirmation(ctx, ret, cli, txID, priv); err != nil {
return ids.Empty, 0, err
}
return txID, utx.GetPrice() * utx.FeeUnits(g), nil
}
func handleConfirmation(
ctx context.Context, ret *Op, cli Client,
txID ids.ID, priv *ecdsa.PrivateKey,
) error {
if ret.pollTx {
color.Yellow("issued transaction %s (now polling)", txID)
confirmed, err := cli.PollTx(ctx, txID)
if err != nil {
return err
}
if !confirmed {
color.Yellow("transaction %s not confirmed", txID)
} else {
color.Yellow("transaction %s confirmed", txID)
}
}
if len(ret.space) > 0 {
info, _, err := cli.Info(ctx, ret.space)
if err != nil {
color.Red("cannot get space info %v", err)
return err
}
PPInfo(info)
}
if ret.balance {
addr := crypto.PubkeyToAddress(priv.PublicKey)
b, err := cli.Balance(ctx, addr)
if err != nil {
return err
}
color.Cyan("Address=%s Balance=%d", addr, b)
}
return nil
}
type Op struct {
pollTx bool
space string
balance bool
}
type OpOption func(*Op)
func (op *Op) applyOpts(opts []OpOption) {
for _, opt := range opts {
opt(op)
}
}
// "true" to poll transaction for its confirmation.
func WithPollTx() OpOption {
return func(op *Op) { op.pollTx = true }
}
// Non-empty to print out space information.
func WithInfo(space string) OpOption {
return func(op *Op) { op.space = space }
}
func WithBalance() OpOption {
return func(op *Op) { op.balance = true }
}