forked from multiversx/mx-chain-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaderValidator.go
73 lines (62 loc) · 2.18 KB
/
headerValidator.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
package fallback
import (
"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/marshal"
"github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/dataRetriever"
"github.com/ElrondNetwork/elrond-go/process"
)
var log = logger.GetOrCreate("fallback")
type fallbackHeaderValidator struct {
headersPool dataRetriever.HeadersPool
marshalizer marshal.Marshalizer
storageService dataRetriever.StorageService
}
// NewFallbackHeaderValidator creates a new fallbackHeaderValidator object
func NewFallbackHeaderValidator(
headersPool dataRetriever.HeadersPool,
marshalizer marshal.Marshalizer,
storageService dataRetriever.StorageService,
) (*fallbackHeaderValidator, error) {
if check.IfNil(headersPool) {
return nil, process.ErrNilHeadersDataPool
}
if check.IfNil(marshalizer) {
return nil, process.ErrNilMarshalizer
}
if check.IfNil(storageService) {
return nil, process.ErrNilStorage
}
hv := &fallbackHeaderValidator{
headersPool: headersPool,
marshalizer: marshalizer,
storageService: storageService,
}
return hv, nil
}
// ShouldApplyFallbackValidation returns if for the given header could be applied fallback validation or not
func (fhv *fallbackHeaderValidator) ShouldApplyFallbackValidation(headerHandler data.HeaderHandler) bool {
if check.IfNil(headerHandler) {
return false
}
if headerHandler.GetShardID() != core.MetachainShardId {
return false
}
if !headerHandler.IsStartOfEpochBlock() {
return false
}
previousHeader, err := process.GetMetaHeader(headerHandler.GetPrevHash(), fhv.headersPool, fhv.marshalizer, fhv.storageService)
if err != nil {
log.Debug("ShouldApplyFallbackValidation", "GetMetaHeader", err.Error())
return false
}
isRoundTooOld := int64(headerHandler.GetRound())-int64(previousHeader.GetRound()) >= common.MaxRoundsWithoutCommittedStartInEpochBlock
return isRoundTooOld
}
// IsInterfaceNil returns true if there is no value under the interface
func (fhv *fallbackHeaderValidator) IsInterfaceNil() bool {
return fhv == nil
}