-
Notifications
You must be signed in to change notification settings - Fork 375
/
tx.go
179 lines (153 loc) · 4.58 KB
/
tx.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
package std
import (
"bufio"
"context"
"fmt"
"io"
"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/crypto/multisig"
"github.com/gnolang/gno/tm2/pkg/errors"
)
var (
maxGasWanted = int64((1 << 60) - 1) // something smaller than math.MaxInt64
ErrTxsLoadingAborted = errors.New("transaction loading aborted")
)
// Tx is a standard way to wrap a Msg with Fee and Signatures.
// NOTE: the first signature is the fee payer (Signatures must not be nil).
type Tx struct {
Msgs []Msg `json:"msg" yaml:"msg"`
Fee Fee `json:"fee" yaml:"fee"`
Signatures []Signature `json:"signatures" yaml:"signatures"`
Memo string `json:"memo" yaml:"memo"`
}
func NewTx(msgs []Msg, fee Fee, sigs []Signature, memo string) Tx {
return Tx{
Msgs: msgs,
Fee: fee,
Signatures: sigs,
Memo: memo,
}
}
// GetMsgs returns the all the transaction's messages.
func (tx Tx) GetMsgs() []Msg { return tx.Msgs }
// ValidateBasic does a simple and lightweight validation check that doesn't
// require access to any other information.
func (tx Tx) ValidateBasic() error {
stdSigs := tx.GetSignatures()
if tx.Fee.GasWanted > maxGasWanted {
return ErrGasOverflow(fmt.Sprintf("invalid gas supplied; %d > %d", tx.Fee.GasWanted, maxGasWanted))
}
if !tx.Fee.GasFee.IsValid() {
return ErrInsufficientFee(fmt.Sprintf("invalid fee %s amount provided", tx.Fee.GasFee))
}
if len(stdSigs) == 0 {
return ErrNoSignatures("no signers")
}
if len(stdSigs) != len(tx.GetSigners()) {
return ErrUnauthorized("wrong number of signers")
}
return nil
}
// CountSubKeys counts the total number of keys for a multi-sig public key.
func CountSubKeys(pub crypto.PubKey) int {
v, ok := pub.(multisig.PubKeyMultisigThreshold)
if !ok {
return 1
}
numKeys := 0
for _, subkey := range v.PubKeys {
numKeys += CountSubKeys(subkey)
}
return numKeys
}
// GetSigners returns the addresses that must sign the transaction.
// Addresses are returned in a deterministic order.
// They are accumulated from the GetSigners method for each Msg
// in the order they appear in tx.GetMsgs().
// Duplicate addresses will be omitted.
func (tx Tx) GetSigners() []crypto.Address {
seen := map[string]bool{}
var signers []crypto.Address
for _, msg := range tx.GetMsgs() {
for _, addr := range msg.GetSigners() {
if !seen[addr.String()] {
signers = append(signers, addr)
seen[addr.String()] = true
}
}
}
return signers
}
// GetMemo returns the memo
func (tx Tx) GetMemo() string { return tx.Memo }
// GetSignatures returns the signature of signers who signed the Msg.
// GetSignatures returns the signature of signers who signed the Msg.
// CONTRACT: Length returned is same as length of
// pubkeys returned from MsgKeySigners, and the order
// matches.
// CONTRACT: If the signature is missing (ie the Msg is
// invalid), then the corresponding signature is
// .Empty().
func (tx Tx) GetSignatures() []Signature { return tx.Signatures }
func (tx Tx) GetSignBytes(chainID string, accountNumber uint64, sequence uint64) ([]byte, error) {
return GetSignaturePayload(SignDoc{
ChainID: chainID,
AccountNumber: accountNumber,
Sequence: sequence,
Fee: tx.Fee,
Msgs: tx.Msgs,
Memo: tx.Memo,
})
}
// __________________________________________________________
// Fee includes the amount of coins paid in fees and the maximum
// gas to be used by the transaction. The ratio yields an effective "gasprice",
// which must be above some miminum to be accepted into the mempool.
type Fee struct {
GasWanted int64 `json:"gas_wanted" yaml:"gas_wanted"`
GasFee Coin `json:"gas_fee" yaml:"gas_fee"`
}
// NewFee returns a new instance of Fee
func NewFee(gasWanted int64, gasFee Coin) Fee {
return Fee{
GasWanted: gasWanted,
GasFee: gasFee,
}
}
// Bytes for signing later
func (fee Fee) Bytes() []byte {
bz, err := amino.MarshalJSON(fee) // TODO
if err != nil {
panic(err)
}
return bz
}
func ParseTxs(ctx context.Context, reader io.Reader) ([]Tx, error) {
var txs []Tx
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
select {
case <-ctx.Done():
return nil, ErrTxsLoadingAborted
default:
// Parse the amino JSON
var tx Tx
if err := amino.UnmarshalJSON(scanner.Bytes(), &tx); err != nil {
return nil, fmt.Errorf(
"unable to unmarshal amino JSON, %w",
err,
)
}
txs = append(txs, tx)
}
}
// Check for scanning errors
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf(
"error encountered while reading file, %w",
err,
)
}
return txs, nil
}