-
Notifications
You must be signed in to change notification settings - Fork 199
/
interceptedBlockHeader.go
200 lines (169 loc) · 5.57 KB
/
interceptedBlockHeader.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
package interceptedBlocks
import (
"fmt"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/data/block"
"github.com/ElrondNetwork/elrond-go/data/typeConverters"
"github.com/ElrondNetwork/elrond-go/hashing"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
)
// InterceptedHeader represents the wrapper over HeaderWrapper struct.
// It implements Newer and Hashed interfaces
type InterceptedHeader struct {
hdr *block.Header
sigVerifier process.InterceptedHeaderSigVerifier
hasher hashing.Hasher
shardCoordinator sharding.Coordinator
hash []byte
isForCurrentShard bool
chainID []byte
validityAttester process.ValidityAttester
epochStartTrigger process.EpochStartTriggerHandler
nonceConverter typeConverters.Uint64ByteSliceConverter
}
// NewInterceptedHeader creates a new instance of InterceptedHeader struct
func NewInterceptedHeader(arg *ArgInterceptedBlockHeader) (*InterceptedHeader, error) {
err := checkBlockHeaderArgument(arg)
if err != nil {
return nil, err
}
hdr, err := createShardHdr(arg.Marshalizer, arg.HdrBuff)
if err != nil {
return nil, err
}
inHdr := &InterceptedHeader{
hdr: hdr,
hasher: arg.Hasher,
sigVerifier: arg.HeaderSigVerifier,
shardCoordinator: arg.ShardCoordinator,
chainID: arg.ChainID,
validityAttester: arg.ValidityAttester,
epochStartTrigger: arg.EpochStartTrigger,
nonceConverter: arg.NonceConverter,
}
inHdr.processFields(arg.HdrBuff)
return inHdr, nil
}
func createShardHdr(marshalizer marshal.Marshalizer, hdrBuff []byte) (*block.Header, error) {
hdr := &block.Header{}
err := marshalizer.Unmarshal(hdr, hdrBuff)
if err != nil {
return nil, err
}
return hdr, nil
}
func (inHdr *InterceptedHeader) processFields(txBuff []byte) {
inHdr.hash = inHdr.hasher.Compute(string(txBuff))
isHeaderForCurrentShard := inHdr.shardCoordinator.SelfId() == inHdr.HeaderHandler().GetShardID()
isMetachainShardCoordinator := inHdr.shardCoordinator.SelfId() == core.MetachainShardId
inHdr.isForCurrentShard = isHeaderForCurrentShard || isMetachainShardCoordinator
}
// CheckValidity checks if the received header is valid (not nil fields, valid sig and so on)
func (inHdr *InterceptedHeader) CheckValidity() error {
err := inHdr.hdr.CheckChainID(inHdr.chainID)
if err != nil {
return err
}
err = inHdr.integrity()
if err != nil {
return err
}
err = inHdr.sigVerifier.VerifyRandSeedAndLeaderSignature(inHdr.hdr)
if err != nil {
return err
}
return inHdr.sigVerifier.VerifySignature(inHdr.hdr)
}
func (inHdr *InterceptedHeader) isEpochCorrect() bool {
if inHdr.shardCoordinator.SelfId() != core.MetachainShardId {
return true
}
if inHdr.epochStartTrigger.EpochStartRound() >= inHdr.epochStartTrigger.EpochFinalityAttestingRound() {
return true
}
if inHdr.hdr.GetEpoch() >= inHdr.epochStartTrigger.Epoch() {
return true
}
if inHdr.hdr.GetRound() <= inHdr.epochStartTrigger.EpochStartRound() {
return true
}
if inHdr.hdr.GetRound() <= inHdr.epochStartTrigger.EpochFinalityAttestingRound()+process.EpochChangeGracePeriod {
return true
}
return false
}
// integrity checks the integrity of the header block wrapper
func (inHdr *InterceptedHeader) integrity() error {
if !inHdr.isEpochCorrect() {
return fmt.Errorf("%w : shard header with old epoch and bad round: "+
"shardHeaderHash=%s, "+
"shardId=%v, "+
"metaEpoch=%v, "+
"shardEpoch=%v, "+
"shardRound=%v, "+
"metaFinalityAttestingRound=%v ",
process.ErrEpochDoesNotMatch,
logger.DisplayByteSlice(inHdr.hash),
inHdr.hdr.ShardID,
inHdr.epochStartTrigger.Epoch(),
inHdr.hdr.Epoch,
inHdr.hdr.Round,
inHdr.epochStartTrigger.EpochFinalityAttestingRound())
}
err := checkHeaderHandler(inHdr.HeaderHandler())
if err != nil {
return err
}
err = inHdr.validityAttester.CheckBlockAgainstFinal(inHdr.HeaderHandler())
if err != nil {
return err
}
err = inHdr.validityAttester.CheckBlockAgainstRounder(inHdr.HeaderHandler())
if err != nil {
return err
}
err = checkMiniblocks(inHdr.hdr.MiniBlockHeaders, inHdr.shardCoordinator)
if err != nil {
return err
}
return nil
}
// Hash gets the hash of this header
func (inHdr *InterceptedHeader) Hash() []byte {
return inHdr.hash
}
// HeaderHandler returns the HeaderHandler pointer that holds the data
func (inHdr *InterceptedHeader) HeaderHandler() data.HeaderHandler {
return inHdr.hdr
}
// IsForCurrentShard returns true if this header is meant to be processed by the node from this shard
func (inHdr *InterceptedHeader) IsForCurrentShard() bool {
return inHdr.isForCurrentShard
}
// Type returns the type of this intercepted data
func (inHdr *InterceptedHeader) Type() string {
return "intercepted header"
}
// String returns the header's most important fields as string
func (inHdr *InterceptedHeader) String() string {
return fmt.Sprintf("shardId=%d, metaEpoch=%d, shardEpoch=%d, round=%d, nonce=%d",
inHdr.hdr.ShardID,
inHdr.epochStartTrigger.Epoch(),
inHdr.hdr.Epoch,
inHdr.hdr.Round,
inHdr.hdr.Nonce,
)
}
// Identifiers returns the identifiers used in requests
func (inHdr *InterceptedHeader) Identifiers() [][]byte {
nonceBytes := inHdr.nonceConverter.ToByteSlice(inHdr.hdr.Nonce)
return [][]byte{inHdr.hash, nonceBytes}
}
// IsInterfaceNil returns true if there is no value under the interface
func (inHdr *InterceptedHeader) IsInterfaceNil() bool {
return inHdr == nil
}