github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/cmd/bootstrap/utils/file.go (about)

     1  package utils
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"path/filepath"
     7  
     8  	"github.com/onflow/flow-go/engine/common/rpc/convert"
     9  	model "github.com/onflow/flow-go/model/bootstrap"
    10  	"github.com/onflow/flow-go/state/protocol/inmem"
    11  	io "github.com/onflow/flow-go/utils/io"
    12  )
    13  
    14  func ReadRootProtocolSnapshot(bootDir string) (*inmem.Snapshot, error) {
    15  
    16  	// default path root snapshot is written to
    17  	snapshotPath := filepath.Join(bootDir, model.PathRootProtocolStateSnapshot)
    18  
    19  	// read snapshot file bytes
    20  	bz, err := io.ReadFile(snapshotPath)
    21  	if err != nil {
    22  		return nil, fmt.Errorf("could not read snapshot: %w", err)
    23  	}
    24  
    25  	// convert bytes to snapshot struct
    26  	snapshot, err := convert.BytesToInmemSnapshot(bz)
    27  	if err != nil {
    28  		return nil, fmt.Errorf("could not convert bytes to snapshot: %w", err)
    29  	}
    30  
    31  	return snapshot, nil
    32  }
    33  
    34  func ReadData[T any](path string) (*T, error) {
    35  	bytes, err := io.ReadFile(path)
    36  	if err != nil {
    37  		return nil, fmt.Errorf("could not read data from file: %w", err)
    38  	}
    39  
    40  	var encodable T
    41  	err = json.Unmarshal(bytes, &encodable)
    42  	if err != nil {
    43  		return nil, fmt.Errorf("could not unmarshal root block: %w", err)
    44  	}
    45  	return &encodable, nil
    46  }