-
Notifications
You must be signed in to change notification settings - Fork 199
/
interceptedRewardTransaction.go
170 lines (142 loc) · 4.82 KB
/
interceptedRewardTransaction.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
package rewardTransaction
import (
"fmt"
"math/big"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go-core/data/rewardTx"
"github.com/ElrondNetwork/elrond-go-core/hashing"
"github.com/ElrondNetwork/elrond-go-core/marshal"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
)
var _ process.InterceptedData = (*InterceptedRewardTransaction)(nil)
// InterceptedRewardTransaction holds and manages a transaction based struct with extended functionality
type InterceptedRewardTransaction struct {
rTx *rewardTx.RewardTx
marshalizer marshal.Marshalizer
hasher hashing.Hasher
pubkeyConv core.PubkeyConverter
coordinator sharding.Coordinator
hash []byte
rcvShard uint32
sndShard uint32
isForCurrentShard bool
}
// NewInterceptedRewardTransaction returns a new instance of InterceptedRewardTransaction
func NewInterceptedRewardTransaction(
rewardTxBuff []byte,
marshalizer marshal.Marshalizer,
hasher hashing.Hasher,
pubkeyConv core.PubkeyConverter,
coordinator sharding.Coordinator,
) (*InterceptedRewardTransaction, error) {
if rewardTxBuff == 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
}
rTx := &rewardTx.RewardTx{}
err := marshalizer.Unmarshal(rTx, rewardTxBuff)
if err != nil {
return nil, err
}
inRewardTx := &InterceptedRewardTransaction{
rTx: rTx,
marshalizer: marshalizer,
hasher: hasher,
pubkeyConv: pubkeyConv,
coordinator: coordinator,
}
err = inRewardTx.processFields(rewardTxBuff)
if err != nil {
return nil, err
}
return inRewardTx, nil
}
func (inRTx *InterceptedRewardTransaction) processFields(rewardTxBuff []byte) error {
inRTx.hash = inRTx.hasher.Compute(string(rewardTxBuff))
inRTx.rcvShard = inRTx.coordinator.ComputeId(inRTx.rTx.RcvAddr)
inRTx.sndShard = core.MetachainShardId
if inRTx.coordinator.SelfId() == core.MetachainShardId {
inRTx.isForCurrentShard = false
return nil
}
isForCurrentShardRecv := inRTx.rcvShard == inRTx.coordinator.SelfId()
isForCurrentShardSender := inRTx.sndShard == inRTx.coordinator.SelfId()
inRTx.isForCurrentShard = isForCurrentShardRecv || isForCurrentShardSender
return nil
}
// Nonce returns the transaction nonce
func (inRTx *InterceptedRewardTransaction) Nonce() uint64 {
return inRTx.rTx.GetNonce()
}
// Fee represents the reward transaction fee. It is always 0
func (inRTx *InterceptedRewardTransaction) Fee() *big.Int {
return big.NewInt(0)
}
// SenderAddress returns the transaction sender address
func (inRTx *InterceptedRewardTransaction) SenderAddress() []byte {
return nil
}
// ReceiverShardId returns the receiver shard
func (inRTx *InterceptedRewardTransaction) ReceiverShardId() uint32 {
return inRTx.rcvShard
}
// SenderShardId returns the sender shard
func (inRTx *InterceptedRewardTransaction) SenderShardId() uint32 {
return inRTx.sndShard
}
// Transaction returns the reward transaction pointer that actually holds the data
func (inRTx *InterceptedRewardTransaction) Transaction() data.TransactionHandler {
return inRTx.rTx
}
// Hash gets the hash of this transaction
func (inRTx *InterceptedRewardTransaction) Hash() []byte {
return inRTx.hash
}
// CheckValidity checks if the received transaction is valid (not nil fields, valid sig and so on)
func (inRTx *InterceptedRewardTransaction) CheckValidity() error {
err := inRTx.rTx.CheckIntegrity()
if err != nil {
return err
}
return nil
}
// IsForCurrentShard returns true if this transaction is meant to be processed by the node from this shard
func (inRTx *InterceptedRewardTransaction) IsForCurrentShard() bool {
return inRTx.isForCurrentShard
}
// Type returns the type of this intercepted data
func (inRTx *InterceptedRewardTransaction) Type() string {
return "intercepted reward tx"
}
// String returns the reward's most important fields as string
func (inRTx *InterceptedRewardTransaction) String() string {
return fmt.Sprintf("epoch=%d, round=%d, address=%s, value=%s",
inRTx.rTx.Epoch,
inRTx.rTx.Round,
logger.DisplayByteSlice(inRTx.rTx.RcvAddr),
inRTx.rTx.Value.String(),
)
}
// Identifiers returns the identifiers used in requests
func (inRTx *InterceptedRewardTransaction) Identifiers() [][]byte {
return [][]byte{inRTx.hash}
}
// IsInterfaceNil returns true if there is no value under the interface
func (inRTx *InterceptedRewardTransaction) IsInterfaceNil() bool {
return inRTx == nil
}