github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/common/rpc/convert/snapshots.go (about) 1 package convert 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/onflow/flow-go/state/protocol" 8 "github.com/onflow/flow-go/state/protocol/inmem" 9 ) 10 11 // SnapshotToBytes converts a `protocol.Snapshot` to bytes, encoded as JSON 12 func SnapshotToBytes(snapshot protocol.Snapshot) ([]byte, error) { 13 serializable, err := inmem.FromSnapshot(snapshot) 14 if err != nil { 15 return nil, err 16 } 17 18 data, err := json.Marshal(serializable.Encodable()) 19 if err != nil { 20 return nil, err 21 } 22 23 return data, nil 24 } 25 26 // BytesToInmemSnapshot converts an array of bytes to `inmem.Snapshot` 27 func BytesToInmemSnapshot(bytes []byte) (*inmem.Snapshot, error) { 28 var encodable inmem.EncodableSnapshot 29 err := json.Unmarshal(bytes, &encodable) 30 if err != nil { 31 return nil, fmt.Errorf("could not unmarshal decoded snapshot: %w", err) 32 } 33 34 return inmem.SnapshotFromEncodable(encodable), nil 35 }