-
Notifications
You must be signed in to change notification settings - Fork 199
/
metachain.go
100 lines (81 loc) · 2.54 KB
/
metachain.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
package blockchain
import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-go/common"
)
var _ data.ChainHandler = (*metaChain)(nil)
// metaChain holds the block information for the beacon chain
//
// The MetaChain also holds pointers to the Genesis block and the current block.
type metaChain struct {
*baseBlockChain
currentBlockRootHash []byte
}
// NewMetaChain will initialize a new metachain instance
func NewMetaChain(appStatusHandler core.AppStatusHandler) (*metaChain, error) {
if check.IfNil(appStatusHandler) {
return nil, ErrNilAppStatusHandler
}
return &metaChain{
baseBlockChain: &baseBlockChain{
appStatusHandler: appStatusHandler,
finalBlockInfo: &blockInfo{},
},
}, nil
}
// SetGenesisHeader returns the genesis block header pointer
func (mc *metaChain) SetGenesisHeader(header data.HeaderHandler) error {
if check.IfNil(header) {
mc.mut.Lock()
mc.genesisHeader = nil
mc.mut.Unlock()
return nil
}
genBlock, ok := header.(*block.MetaBlock)
if !ok {
return ErrWrongTypeInSet
}
mc.mut.Lock()
mc.genesisHeader = genBlock.ShallowClone()
mc.mut.Unlock()
return nil
}
// SetCurrentBlockHeaderAndRootHash sets current block header pointer and the root hash
func (mc *metaChain) SetCurrentBlockHeaderAndRootHash(header data.HeaderHandler, rootHash []byte) error {
if check.IfNil(header) {
mc.mut.Lock()
mc.currentBlockHeader = nil
mc.currentBlockRootHash = nil
mc.mut.Unlock()
return nil
}
currHead, ok := header.(*block.MetaBlock)
if !ok {
return ErrWrongTypeInSet
}
mc.appStatusHandler.SetUInt64Value(common.MetricNonce, currHead.Nonce)
mc.appStatusHandler.SetUInt64Value(common.MetricSynchronizedRound, currHead.Round)
mc.mut.Lock()
mc.currentBlockHeader = currHead.ShallowClone()
mc.currentBlockRootHash = make([]byte, len(rootHash))
copy(mc.currentBlockRootHash, rootHash)
mc.mut.Unlock()
return nil
}
// GetCurrentBlockRootHash returns the current committed block root hash. The returned byte slice is a new copy
// of the contained root hash.
func (mc *metaChain) GetCurrentBlockRootHash() []byte {
mc.mut.RLock()
rootHash := mc.currentBlockRootHash
mc.mut.RUnlock()
cloned := make([]byte, len(rootHash))
copy(cloned, rootHash)
return cloned
}
// IsInterfaceNil returns true if there is no value under the interface
func (mc *metaChain) IsInterfaceNil() bool {
return mc == nil || mc.baseBlockChain == nil
}