-
Notifications
You must be signed in to change notification settings - Fork 199
/
interceptedBlockHeader.go
151 lines (124 loc) · 4.2 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
package interceptedBlocks
import (
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/data/block"
"github.com/ElrondNetwork/elrond-go/hashing"
"github.com/ElrondNetwork/elrond-go/marshal"
"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 *headerSigVerifier
hasher hashing.Hasher
shardCoordinator sharding.Coordinator
hash []byte
isForCurrentShard bool
}
// 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
}
sigVerifier := &headerSigVerifier{
marshalizer: arg.Marshalizer,
hasher: arg.Hasher,
nodesCoordinator: arg.NodesCoordinator,
multiSigVerifier: arg.MultiSigVerifier,
singleSigVerifier: arg.SingleSigVerifier,
keyGen: arg.KeyGen,
}
inHdr := &InterceptedHeader{
hdr: hdr,
hasher: arg.Hasher,
sigVerifier: sigVerifier,
shardCoordinator: arg.ShardCoordinator,
}
//wire-up the "virtual" function
inHdr.sigVerifier.copyHeaderWithoutSig = inHdr.copyHeaderWithoutSig
inHdr.sigVerifier.copyHeaderWithoutLeaderSig = inHdr.copyHeaderWithoutLeaderSig
inHdr.processFields(arg.HdrBuff)
return inHdr, nil
}
func createShardHdr(marshalizer marshal.Marshalizer, hdrBuff []byte) (*block.Header, error) {
hdr := &block.Header{
MiniBlockHeaders: make([]block.MiniBlockHeader, 0),
MetaBlockHashes: make([][]byte, 0),
}
err := marshalizer.Unmarshal(hdr, hdrBuff)
if err != nil {
return nil, err
}
return hdr, nil
}
func (inHdr *InterceptedHeader) copyHeaderWithoutSig(header data.HeaderHandler) data.HeaderHandler {
//it is virtually impossible here to have a wrong type assertion case
hdr := header.(*block.Header)
headerCopy := *hdr
headerCopy.Signature = nil
headerCopy.PubKeysBitmap = nil
headerCopy.LeaderSignature = nil
return &headerCopy
}
func (inHdr *InterceptedHeader) copyHeaderWithoutLeaderSig(header data.HeaderHandler) data.HeaderHandler {
//it is virtually impossible here to have a wrong type assertion case
hdr := header.(*block.Header)
headerCopy := *hdr
headerCopy.LeaderSignature = nil
return &headerCopy
}
func (inHdr *InterceptedHeader) processFields(txBuff []byte) {
inHdr.hash = inHdr.hasher.Compute(string(txBuff))
isHeaderForCurrentShard := inHdr.shardCoordinator.SelfId() == inHdr.HeaderHandler().GetShardID()
isMetachainShardCoordinator := inHdr.shardCoordinator.SelfId() == sharding.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.integrity()
if err != nil {
return err
}
err = inHdr.sigVerifier.verifyRandSeedAndLeaderSignature(inHdr.hdr)
if err != nil {
return err
}
return inHdr.sigVerifier.verifySig(inHdr.hdr)
}
// integrity checks the integrity of the header block wrapper
func (inHdr *InterceptedHeader) integrity() error {
err := checkHeaderHandler(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
}
// IsInterfaceNil returns true if there is no value under the interface
func (inHdr *InterceptedHeader) IsInterfaceNil() bool {
if inHdr == nil {
return true
}
return false
}