github.com/koko1123/flow-go-1@v0.29.6/state/cluster/badger/state_root.go (about)

     1  package badger
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/koko1123/flow-go-1/model/cluster"
     7  	"github.com/koko1123/flow-go-1/model/flow"
     8  )
     9  
    10  // StateRoot is the root information required to bootstrap the cluster state
    11  type StateRoot struct {
    12  	block *cluster.Block
    13  }
    14  
    15  func NewStateRoot(genesis *cluster.Block) (*StateRoot, error) {
    16  	err := validateClusterGenesis(genesis)
    17  	if err != nil {
    18  		return nil, fmt.Errorf("inconsistent state root: %w", err)
    19  	}
    20  	return &StateRoot{
    21  		block: genesis,
    22  	}, nil
    23  }
    24  
    25  func validateClusterGenesis(genesis *cluster.Block) error {
    26  	// check height of genesis block
    27  	if genesis.Header.Height != 0 {
    28  		return fmt.Errorf("height of genesis cluster block should be 0 (got %d)", genesis.Header.Height)
    29  	}
    30  	// check header parent ID
    31  	if genesis.Header.ParentID != flow.ZeroID {
    32  		return fmt.Errorf("genesis parent ID must be zero hash (got %x)", genesis.Header.ParentID)
    33  	}
    34  
    35  	// check payload integrity
    36  	if genesis.Header.PayloadHash != genesis.Payload.Hash() {
    37  		return fmt.Errorf("computed payload hash does not match header")
    38  	}
    39  
    40  	// check payload
    41  	collSize := len(genesis.Payload.Collection.Transactions)
    42  	if collSize != 0 {
    43  		return fmt.Errorf("genesis collection should contain no transactions (got %d)", collSize)
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func (s StateRoot) ClusterID() flow.ChainID {
    50  	return s.block.Header.ChainID
    51  }
    52  
    53  func (s StateRoot) Block() *cluster.Block {
    54  	return s.block
    55  }