-
Notifications
You must be signed in to change notification settings - Fork 199
/
interceptedNode.go
147 lines (121 loc) · 3.51 KB
/
interceptedNode.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
package trie
import (
"fmt"
"math/big"
"sync"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"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"
)
var _ process.TxValidatorHandler = (*InterceptedTrieNode)(nil)
var _ process.InterceptedData = (*InterceptedTrieNode)(nil)
// InterceptedTrieNode implements intercepted data interface and is used when trie nodes are intercepted
type InterceptedTrieNode struct {
node node
serializedNode []byte
hash []byte
mutex sync.RWMutex
}
// NewInterceptedTrieNode creates a new instance of InterceptedTrieNode
func NewInterceptedTrieNode(
buff []byte,
marshalizer marshal.Marshalizer,
hasher hashing.Hasher,
) (*InterceptedTrieNode, error) {
if len(buff) == 0 {
return nil, ErrValueTooShort
}
if check.IfNil(marshalizer) {
return nil, ErrNilMarshalizer
}
if check.IfNil(hasher) {
return nil, ErrNilHasher
}
n, err := decodeNode(buff, marshalizer, hasher)
if err != nil {
return nil, err
}
n.setDirty(true)
err = n.setHash()
if err != nil {
return nil, err
}
return &InterceptedTrieNode{
node: n,
serializedNode: buff,
hash: n.getHash(),
}, nil
}
// CheckValidity checks if the intercepted data is valid
func (inTn *InterceptedTrieNode) CheckValidity() error {
if inTn.node.isValid() {
return nil
}
return ErrInvalidNode
}
// IsForCurrentShard checks if the intercepted data is for the current shard
func (inTn *InterceptedTrieNode) IsForCurrentShard() bool {
return true
}
// Hash returns the hash of the intercepted node
func (inTn *InterceptedTrieNode) Hash() []byte {
return inTn.hash
}
// IsInterfaceNil returns true if there is no value under the interface
func (inTn *InterceptedTrieNode) IsInterfaceNil() bool {
return inTn == nil
}
// GetSerialized returns the intercepted encoded node
func (inTn *InterceptedTrieNode) GetSerialized() []byte {
inTn.mutex.RLock()
defer inTn.mutex.RUnlock()
return inTn.serializedNode
}
// SetSerialized sets the given bytes as the SerializedNode
func (inTn *InterceptedTrieNode) SetSerialized(serializedNode []byte) {
inTn.mutex.Lock()
defer inTn.mutex.Unlock()
inTn.serializedNode = serializedNode
}
// Type returns the type of this intercepted data
func (inTn *InterceptedTrieNode) Type() string {
return "intercepted trie node"
}
// String returns the trie node's most important fields as string
func (inTn *InterceptedTrieNode) String() string {
return fmt.Sprintf("hash=%s",
logger.DisplayByteSlice(inTn.hash),
)
}
// SenderShardId returns 0
func (inTn *InterceptedTrieNode) SenderShardId() uint32 {
return 0
}
// ReceiverShardId returns 0
func (inTn *InterceptedTrieNode) ReceiverShardId() uint32 {
return 0
}
// Nonce return 0
func (inTn *InterceptedTrieNode) Nonce() uint64 {
return 0
}
// SenderAddress returns nil
func (inTn *InterceptedTrieNode) SenderAddress() []byte {
return nil
}
// Fee returns big.NewInt(0)
func (inTn *InterceptedTrieNode) Fee() *big.Int {
return big.NewInt(0)
}
// SizeInBytes returns the size in bytes held by this instance plus the inner node's instance size
func (inTn *InterceptedTrieNode) SizeInBytes() int {
inTn.mutex.RLock()
defer inTn.mutex.RUnlock()
return len(inTn.hash) + len(inTn.serializedNode) + inTn.node.sizeInBytes()
}
// Identifiers returns the identifiers used in requests
func (inTn *InterceptedTrieNode) Identifiers() [][]byte {
return [][]byte{inTn.hash}
}