github.com/koko1123/flow-go-1@v0.29.6/cmd/bootstrap/utils/file.go (about)

     1  package utils
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"path/filepath"
     7  
     8  	"github.com/koko1123/flow-go-1/engine/common/rpc/convert"
     9  	model "github.com/koko1123/flow-go-1/model/bootstrap"
    10  	"github.com/koko1123/flow-go-1/model/flow"
    11  	"github.com/koko1123/flow-go-1/state/protocol/inmem"
    12  	io "github.com/koko1123/flow-go-1/utils/io"
    13  )
    14  
    15  func ReadRootProtocolSnapshot(bootDir string) (*inmem.Snapshot, error) {
    16  
    17  	// default path root snapshot is written to
    18  	snapshotPath := filepath.Join(bootDir, model.PathRootProtocolStateSnapshot)
    19  
    20  	// read snapshot file bytes
    21  	bz, err := io.ReadFile(snapshotPath)
    22  	if err != nil {
    23  		return nil, fmt.Errorf("could not read snapshot: %w", err)
    24  	}
    25  
    26  	// convert bytes to snapshot struct
    27  	snapshot, err := convert.BytesToInmemSnapshot(bz)
    28  	if err != nil {
    29  		return nil, fmt.Errorf("could not convert bytes to snapshot: %w", err)
    30  	}
    31  
    32  	return snapshot, nil
    33  }
    34  
    35  func ReadRootBlock(rootBlockDataPath string) (*flow.Block, error) {
    36  	bytes, err := io.ReadFile(rootBlockDataPath)
    37  	if err != nil {
    38  		return nil, fmt.Errorf("could not read root block: %w", err)
    39  	}
    40  
    41  	var encodable flow.Block
    42  	err = json.Unmarshal(bytes, &encodable)
    43  	if err != nil {
    44  		return nil, fmt.Errorf("could not unmarshal root block: %w", err)
    45  	}
    46  	return &encodable, nil
    47  }
    48  
    49  func ReadDKGData(dkgDataPath string) (*inmem.EncodableFullDKG, error) {
    50  	bytes, err := io.ReadFile(dkgDataPath)
    51  	if err != nil {
    52  		return nil, fmt.Errorf("could not read dkg data: %w", err)
    53  	}
    54  
    55  	var encodable inmem.EncodableFullDKG
    56  	err = json.Unmarshal(bytes, &encodable)
    57  	if err != nil {
    58  		return nil, fmt.Errorf("could not unmarshal dkg data: %w", err)
    59  	}
    60  	return &encodable, nil
    61  }