-
Notifications
You must be signed in to change notification settings - Fork 199
/
transaction.go
116 lines (102 loc) · 3.08 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
//go:generate protoc -I=proto -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf --gogoslick_out=. transaction.proto
package transaction
import (
"encoding/json"
"math/big"
"github.com/ElrondNetwork/elrond-go/data"
)
var _ = data.TransactionHandler(&Transaction{})
// IsInterfaceNil verifies if underlying object is nil
func (tx *Transaction) IsInterfaceNil() bool {
return tx == nil
}
// SetValue sets the value of the transaction
func (tx *Transaction) SetValue(value *big.Int) {
tx.Value = value
}
// SetData sets the data of the transaction
func (tx *Transaction) SetData(data []byte) {
tx.Data = data
}
// SetRcvAddr sets the receiver address of the transaction
func (tx *Transaction) SetRcvAddr(addr []byte) {
tx.RcvAddr = addr
}
// SetSndAddr sets the sender address of the transaction
func (tx *Transaction) SetSndAddr(addr []byte) {
tx.SndAddr = addr
}
// MarshalJSON converts the Transaction data type into its corresponding equivalent in byte slice.
// Note that Value data type is converted in a string
func (tx *Transaction) MarshalJSON() ([]byte, error) {
valAsString := "nil"
if tx.Value != nil {
valAsString = tx.Value.String()
}
return json.Marshal(&struct {
Nonce uint64 `json:"nonce"`
Value string `json:"value"`
RcvAddr []byte `json:"receiver"`
SndAddr []byte `json:"sender"`
GasPrice uint64 `json:"gasPrice,omitempty"`
GasLimit uint64 `json:"gasLimit,omitempty"`
Data []byte `json:"data,omitempty"`
Signature []byte `json:"signature,omitempty"`
}{
Nonce: tx.Nonce,
Value: valAsString,
RcvAddr: tx.RcvAddr,
SndAddr: tx.SndAddr,
GasPrice: tx.GasPrice,
GasLimit: tx.GasLimit,
Data: tx.Data,
Signature: tx.Signature,
})
}
// UnmarshalJSON converts the provided bytes into a Transaction data type.
func (tx *Transaction) UnmarshalJSON(dataBuff []byte) error {
aux := &struct {
Nonce uint64 `json:"nonce"`
Value string `json:"value"`
RcvAddr []byte `json:"receiver"`
SndAddr []byte `json:"sender"`
GasPrice uint64 `json:"gasPrice,omitempty"`
GasLimit uint64 `json:"gasLimit,omitempty"`
Data []byte `json:"data,omitempty"`
Signature []byte `json:"signature,omitempty"`
}{}
if err := json.Unmarshal(dataBuff, &aux); err != nil {
return err
}
tx.Nonce = aux.Nonce
tx.RcvAddr = aux.RcvAddr
tx.SndAddr = aux.SndAddr
tx.GasPrice = aux.GasPrice
tx.GasLimit = aux.GasLimit
tx.Data = aux.Data
tx.Signature = aux.Signature
var ok bool
tx.Value, ok = big.NewInt(0).SetString(aux.Value, 10)
if !ok {
tx.Value = nil
}
return nil
}
// TrimSlicePtr creates a copy of the provided slice without the excess capacity
func TrimSlicePtr(in []*Transaction) []*Transaction {
if len(in) == 0 {
return []*Transaction{}
}
ret := make([]*Transaction, len(in))
copy(ret, in)
return ret
}
// TrimSliceHandler creates a copy of the provided slice without the excess capacity
func TrimSliceHandler(in []data.TransactionHandler) []data.TransactionHandler {
if len(in) == 0 {
return []data.TransactionHandler{}
}
ret := make([]data.TransactionHandler, len(in))
copy(ret, in)
return ret
}