/
transaction.go
303 lines (250 loc) · 6.59 KB
/
transaction.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package transaction
import (
"crypto/ecdsa"
"errors"
"fmt"
"github.com/MinterTeam/minter-go-node/core/commissions"
"github.com/MinterTeam/minter-go-node/core/state"
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/MinterTeam/minter-go-node/crypto"
"github.com/MinterTeam/minter-go-node/crypto/sha3"
"github.com/MinterTeam/minter-go-node/rlp"
"math/big"
)
type TxType byte
type SigType byte
const (
TypeSend TxType = 0x01
TypeSellCoin TxType = 0x02
TypeSellAllCoin TxType = 0x03
TypeBuyCoin TxType = 0x04
TypeCreateCoin TxType = 0x05
TypeDeclareCandidacy TxType = 0x06
TypeDelegate TxType = 0x07
TypeUnbond TxType = 0x08
TypeRedeemCheck TxType = 0x09
TypeSetCandidateOnline TxType = 0x0A
TypeSetCandidateOffline TxType = 0x0B
TypeCreateMultisig TxType = 0x0C
TypeMultisend TxType = 0x0D
TypeEditCandidate TxType = 0x0E
SigTypeSingle SigType = 0x01
SigTypeMulti SigType = 0x02
)
var (
ErrInvalidSig = errors.New("invalid transaction v, r, s values")
MaxCoinSupply = big.NewInt(0).Exp(big.NewInt(10), big.NewInt(15+18), nil) // 1,000,000,000,000,000 bips
)
type Transaction struct {
Nonce uint64
ChainID types.ChainID
GasPrice uint32
GasCoin types.CoinSymbol
Type TxType
Data RawData
Payload []byte
ServiceData []byte
SignatureType SigType
SignatureData []byte
decodedData Data
sig *Signature
multisig *SignatureMulti
sender *types.Address
}
type Signature struct {
V *big.Int
R *big.Int
S *big.Int
}
type SignatureMulti struct {
Multisig types.Address
Signatures []Signature
}
type RawData []byte
type TotalSpends []TotalSpend
func (tss *TotalSpends) Add(coin types.CoinSymbol, value *big.Int) {
for i, t := range *tss {
if t.Coin == coin {
(*tss)[i].Value.Add((*tss)[i].Value, big.NewInt(0).Set(value))
return
}
}
*tss = append(*tss, TotalSpend{
Coin: coin,
Value: big.NewInt(0).Set(value),
})
}
type TotalSpend struct {
Coin types.CoinSymbol
Value *big.Int
}
type Conversion struct {
FromCoin types.CoinSymbol
FromAmount *big.Int
FromReserve *big.Int
ToCoin types.CoinSymbol
ToAmount *big.Int
ToReserve *big.Int
}
type Data interface {
String() string
Gas() int64
TotalSpend(tx *Transaction, context *state.StateDB) (TotalSpends, []Conversion, *big.Int, *Response)
BasicCheck(tx *Transaction, context *state.StateDB) *Response
Run(tx *Transaction, context *state.StateDB, isCheck bool, rewardPool *big.Int, currentBlock uint64) Response
}
func (tx *Transaction) Serialize() ([]byte, error) {
return rlp.EncodeToBytes(tx)
}
func (tx *Transaction) Gas() int64 {
return tx.decodedData.Gas() + tx.payloadGas()
}
func (tx *Transaction) payloadGas() int64 {
return int64(len(tx.Payload)+len(tx.ServiceData)) * commissions.PayloadByte
}
func (tx *Transaction) CommissionInBaseCoin() *big.Int {
commissionInBaseCoin := big.NewInt(0).Mul(big.NewInt(int64(tx.GasPrice)), big.NewInt(tx.Gas()))
commissionInBaseCoin.Mul(commissionInBaseCoin, CommissionMultiplier)
return commissionInBaseCoin
}
func (tx *Transaction) String() string {
sender, _ := tx.Sender()
return fmt.Sprintf("TX nonce:%d from:%s payload:%s data:%s",
tx.Nonce, sender.String(), tx.Payload, tx.decodedData.String())
}
func (tx *Transaction) Sign(prv *ecdsa.PrivateKey) error {
h := tx.Hash()
sig, err := crypto.Sign(h[:], prv)
if err != nil {
return err
}
tx.SetSignature(sig)
return nil
}
func (tx *Transaction) SetSignature(sig []byte) {
switch tx.SignatureType {
case SigTypeSingle:
{
if tx.sig == nil {
tx.sig = &Signature{}
}
tx.sig.R = new(big.Int).SetBytes(sig[:32])
tx.sig.S = new(big.Int).SetBytes(sig[32:64])
tx.sig.V = new(big.Int).SetBytes([]byte{sig[64] + 27})
data, err := rlp.EncodeToBytes(tx.sig)
if err != nil {
panic(err)
}
tx.SignatureData = data
}
case SigTypeMulti:
{
if tx.multisig == nil {
tx.multisig = &SignatureMulti{
Multisig: types.Address{},
Signatures: []Signature{},
}
}
tx.multisig.Signatures = append(tx.multisig.Signatures, Signature{
V: new(big.Int).SetBytes([]byte{sig[64] + 27}),
R: new(big.Int).SetBytes(sig[:32]),
S: new(big.Int).SetBytes(sig[32:64]),
})
data, err := rlp.EncodeToBytes(tx.multisig)
if err != nil {
panic(err)
}
tx.SignatureData = data
}
}
}
func (tx *Transaction) Sender() (types.Address, error) {
if tx.sender != nil {
return *tx.sender, nil
}
switch tx.SignatureType {
case SigTypeSingle:
sender, err := RecoverPlain(tx.Hash(), tx.sig.R, tx.sig.S, tx.sig.V)
if err != nil {
return types.Address{}, err
}
tx.sender = &sender
return sender, nil
case SigTypeMulti:
return tx.multisig.Multisig, nil
}
return types.Address{}, errors.New("unknown signature type")
}
func (tx *Transaction) Hash() types.Hash {
return rlpHash([]interface{}{
tx.Nonce,
tx.ChainID,
tx.GasPrice,
tx.GasCoin,
tx.Type,
tx.Data,
tx.Payload,
tx.ServiceData,
tx.SignatureType,
})
}
func (tx *Transaction) SetDecodedData(data Data) {
tx.decodedData = data
}
func (tx *Transaction) GetDecodedData() Data {
return tx.decodedData
}
func (tx *Transaction) SetMultisigAddress(address types.Address) {
if tx.multisig == nil {
tx.multisig = &SignatureMulti{}
}
tx.multisig.Multisig = address
data, err := rlp.EncodeToBytes(tx.multisig)
if err != nil {
panic(err)
}
tx.SignatureData = data
}
func RecoverPlain(sighash types.Hash, R, S, Vb *big.Int) (types.Address, error) {
if Vb.BitLen() > 8 {
return types.Address{}, ErrInvalidSig
}
V := byte(Vb.Uint64() - 27)
if !crypto.ValidateSignatureValues(V, R, S, true) {
return types.Address{}, ErrInvalidSig
}
// encode the snature in uncompressed format
r, s := R.Bytes(), S.Bytes()
sig := make([]byte, 65)
copy(sig[32-len(r):32], r)
copy(sig[64-len(s):64], s)
sig[64] = V
// recover the public key from the snature
pub, err := crypto.Ecrecover(sighash[:], sig)
if err != nil {
return types.Address{}, err
}
if len(pub) == 0 || pub[0] != 4 {
return types.Address{}, errors.New("invalid public key")
}
var addr types.Address
copy(addr[:], crypto.Keccak256(pub[1:])[12:])
return addr, nil
}
func rlpHash(x interface{}) (h types.Hash) {
hw := sha3.NewKeccak256()
err := rlp.Encode(hw, x)
if err != nil {
panic(err)
}
hw.Sum(h[:0])
return h
}
func CheckForCoinSupplyOverflow(current *big.Int, delta *big.Int) error {
total := big.NewInt(0).Set(current)
total.Add(total, delta)
if total.Cmp(MaxCoinSupply) != -1 {
return errors.New("сoin supply overflow")
}
return nil
}