-
Notifications
You must be signed in to change notification settings - Fork 178
/
state_root.go
55 lines (45 loc) · 1.35 KB
/
state_root.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
package badger
import (
"fmt"
"github.com/onflow/flow-go/model/cluster"
"github.com/onflow/flow-go/model/flow"
)
// StateRoot is the root information required to bootstrap the cluster state
type StateRoot struct {
block *cluster.Block
}
func NewStateRoot(genesis *cluster.Block) (*StateRoot, error) {
err := validateClusterGenesis(genesis)
if err != nil {
return nil, fmt.Errorf("inconsistent state root: %w", err)
}
return &StateRoot{
block: genesis,
}, nil
}
func validateClusterGenesis(genesis *cluster.Block) error {
// check height of genesis block
if genesis.Header.Height != 0 {
return fmt.Errorf("height of genesis cluster block should be 0 (got %d)", genesis.Header.Height)
}
// check header parent ID
if genesis.Header.ParentID != flow.ZeroID {
return fmt.Errorf("genesis parent ID must be zero hash (got %x)", genesis.Header.ParentID)
}
// check payload integrity
if genesis.Header.PayloadHash != genesis.Payload.Hash() {
return fmt.Errorf("computed payload hash does not match header")
}
// check payload
collSize := len(genesis.Payload.Collection.Transactions)
if collSize != 0 {
return fmt.Errorf("genesis collection should contain no transactions (got %d)", collSize)
}
return nil
}
func (s StateRoot) ClusterID() flow.ChainID {
return s.block.Header.ChainID
}
func (s StateRoot) Block() *cluster.Block {
return s.block
}