-
Notifications
You must be signed in to change notification settings - Fork 202
/
headerIntegrityVerifier.go
77 lines (63 loc) · 2.15 KB
/
headerIntegrityVerifier.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
package headerCheck
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go/cmd/node/factory"
"github.com/ElrondNetwork/elrond-go/process"
)
type headerIntegrityVerifier struct {
referenceChainID []byte
headerVersionHandler factory.HeaderVersionHandler
}
// NewHeaderIntegrityVerifier returns a new instance of a structure capable of verifying the integrity of a provided header
func NewHeaderIntegrityVerifier(
referenceChainID []byte,
headerVersionHandler factory.HeaderVersionHandler,
) (*headerIntegrityVerifier, error) {
if len(referenceChainID) == 0 {
return nil, ErrInvalidReferenceChainID
}
if check.IfNil(headerVersionHandler) {
return nil, fmt.Errorf("%w, in NewHeaderVersioningHandler", ErrNilHeaderVersionHandler)
}
hdrIntVer := &headerIntegrityVerifier{
referenceChainID: referenceChainID,
headerVersionHandler: headerVersionHandler,
}
return hdrIntVer, nil
}
// GetVersion returns the version by providing the epoch
func (hdrIntVer *headerIntegrityVerifier) GetVersion(epoch uint32) string {
return hdrIntVer.headerVersionHandler.GetVersion(epoch)
}
// Verify will check the header's fields such as the chain ID or the software version
func (hdrIntVer *headerIntegrityVerifier) Verify(hdr data.HeaderHandler) error {
if len(hdr.GetReserved()) > 0 {
return process.ErrReservedFieldInvalid
}
err := hdrIntVer.headerVersionHandler.Verify(hdr)
if err != nil {
return err
}
return hdrIntVer.checkChainID(hdr)
}
// checkChainID returns nil if the header's chain ID matches the one provided
// otherwise, it will error
func (hdrIntVer *headerIntegrityVerifier) checkChainID(hdr data.HeaderHandler) error {
if !bytes.Equal(hdrIntVer.referenceChainID, hdr.GetChainID()) {
return fmt.Errorf(
"%w, expected: %s, got %s",
ErrInvalidChainID,
hex.EncodeToString(hdrIntVer.referenceChainID),
hex.EncodeToString(hdr.GetChainID()),
)
}
return nil
}
// IsInterfaceNil returns true if the value under the interface is nil
func (hdrIntVer *headerIntegrityVerifier) IsInterfaceNil() bool {
return hdrIntVer == nil
}