-
Notifications
You must be signed in to change notification settings - Fork 199
/
interceptedUnsignedTransaction.go
196 lines (165 loc) · 5.56 KB
/
interceptedUnsignedTransaction.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
package unsigned
import (
"fmt"
"math/big"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/data/smartContractResult"
"github.com/ElrondNetwork/elrond-go/hashing"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
)
var _ process.TxValidatorHandler = (*InterceptedUnsignedTransaction)(nil)
var _ process.InterceptedData = (*InterceptedUnsignedTransaction)(nil)
// InterceptedUnsignedTransaction holds and manages a transaction based struct with extended functionality
type InterceptedUnsignedTransaction struct {
uTx *smartContractResult.SmartContractResult
marshalizer marshal.Marshalizer
hasher hashing.Hasher
pubkeyConv core.PubkeyConverter
coordinator sharding.Coordinator
hash []byte
rcvShard uint32
sndShard uint32
isForCurrentShard bool
}
// NewInterceptedUnsignedTransaction returns a new instance of InterceptedUnsignedTransaction
func NewInterceptedUnsignedTransaction(
uTxBuff []byte,
marshalizer marshal.Marshalizer,
hasher hashing.Hasher,
pubkeyConv core.PubkeyConverter,
coordinator sharding.Coordinator,
) (*InterceptedUnsignedTransaction, error) {
if uTxBuff == nil {
return nil, process.ErrNilBuffer
}
if check.IfNil(marshalizer) {
return nil, process.ErrNilMarshalizer
}
if check.IfNil(hasher) {
return nil, process.ErrNilHasher
}
if check.IfNil(pubkeyConv) {
return nil, process.ErrNilPubkeyConverter
}
if check.IfNil(coordinator) {
return nil, process.ErrNilShardCoordinator
}
uTx, err := createUtx(marshalizer, uTxBuff)
if err != nil {
return nil, err
}
inUTx := &InterceptedUnsignedTransaction{
uTx: uTx,
marshalizer: marshalizer,
hasher: hasher,
pubkeyConv: pubkeyConv,
coordinator: coordinator,
}
err = inUTx.processFields(uTxBuff)
if err != nil {
return nil, err
}
return inUTx, nil
}
func createUtx(marshalizer marshal.Marshalizer, uTxBuff []byte) (*smartContractResult.SmartContractResult, error) {
uTx := &smartContractResult.SmartContractResult{}
err := marshalizer.Unmarshal(uTx, uTxBuff)
if err != nil {
return nil, err
}
return uTx, nil
}
// CheckValidity checks if the received transaction is valid (not nil fields, valid sig and so on)
func (inUTx *InterceptedUnsignedTransaction) CheckValidity() error {
err := inUTx.integrity()
if err != nil {
return err
}
return nil
}
func (inUTx *InterceptedUnsignedTransaction) processFields(uTxBuffWithSig []byte) error {
inUTx.hash = inUTx.hasher.Compute(string(uTxBuffWithSig))
inUTx.rcvShard = inUTx.coordinator.ComputeId(inUTx.uTx.RcvAddr)
inUTx.sndShard = inUTx.coordinator.ComputeId(inUTx.uTx.SndAddr)
isForCurrentShardRecv := inUTx.rcvShard == inUTx.coordinator.SelfId()
isForCurrentShardSender := inUTx.sndShard == inUTx.coordinator.SelfId()
inUTx.isForCurrentShard = isForCurrentShardRecv || isForCurrentShardSender
return nil
}
// integrity checks for not nil fields and negative value
func (inUTx *InterceptedUnsignedTransaction) integrity() error {
if len(inUTx.uTx.RcvAddr) == 0 {
return process.ErrNilRcvAddr
}
if len(inUTx.uTx.SndAddr) == 0 {
return process.ErrNilSndAddr
}
if inUTx.uTx.Value == nil {
return process.ErrNilValue
}
if inUTx.uTx.Value.Sign() < 0 {
return process.ErrNegativeValue
}
if len(inUTx.uTx.PrevTxHash) == 0 {
return process.ErrNilTxHash
}
return nil
}
// Nonce returns the transaction nonce
func (inUTx *InterceptedUnsignedTransaction) Nonce() uint64 {
return inUTx.uTx.Nonce
}
// SenderAddress returns the transaction sender address
func (inUTx *InterceptedUnsignedTransaction) SenderAddress() []byte {
return inUTx.uTx.SndAddr
}
// ReceiverShardId returns the receiver shard
func (inUTx *InterceptedUnsignedTransaction) ReceiverShardId() uint32 {
return inUTx.rcvShard
}
// SenderShardId returns the sender shard
func (inUTx *InterceptedUnsignedTransaction) SenderShardId() uint32 {
return inUTx.sndShard
}
// IsForCurrentShard returns true if this transaction is meant to be processed by the node from this shard
func (inUTx *InterceptedUnsignedTransaction) IsForCurrentShard() bool {
return inUTx.isForCurrentShard
}
// Transaction returns the transaction pointer that actually holds the data
func (inUTx *InterceptedUnsignedTransaction) Transaction() data.TransactionHandler {
return inUTx.uTx
}
// Fee represents the unsigned transaction fee. It is always 0
func (inUTx *InterceptedUnsignedTransaction) Fee() *big.Int {
return big.NewInt(0)
}
// Hash gets the hash of this transaction
func (inUTx *InterceptedUnsignedTransaction) Hash() []byte {
return inUTx.hash
}
// Type returns the type of this intercepted data
func (inUTx *InterceptedUnsignedTransaction) Type() string {
return "intercepted unsigned tx"
}
// String returns the unsigned transaction's most important fields as string
func (inUTx *InterceptedUnsignedTransaction) String() string {
return fmt.Sprintf("sender=%s, nonce=%d, value=%s, recv=%s",
logger.DisplayByteSlice(inUTx.uTx.SndAddr),
inUTx.uTx.Nonce,
inUTx.uTx.Value.String(),
logger.DisplayByteSlice(inUTx.uTx.RcvAddr),
)
}
// Identifiers returns the identifiers used in requests
func (inUTx *InterceptedUnsignedTransaction) Identifiers() [][]byte {
return [][]byte{inUTx.hash}
}
// IsInterfaceNil returns true if there is no value under the interface
func (inUTx *InterceptedUnsignedTransaction) IsInterfaceNil() bool {
return inUTx == nil
}