Decoding CBOR values

In the version v0.20 of Cadence the storage format was updated.

How to decode data from a storableNode ?

var bufReader io.Reader = bufio.NewReader(r)
storableNode,_ := flattener.ReadStorableNode(bufReader)
payload, err := encoding.DecodePayload(storableNode.EncPayload)

address := common.BytesToAddress(payload.Key.KeyParts[0].Value)
fmt.Println(address)   // decoded ok

decoder := interpreter.CBORDecMode.NewByteStreamDecoder(payload.Value)
storable,_ := interpreter.DecodeStorable(decoder, atree.StorageIDUndefined)

/// further decode storable into `Value`

tagging @turbolent

@turbolent I’m trying to decode the data from the latest checkpoint file.
I saw there’s a utility file here: https://github.com/onflow/cadence/blob/v0.20.2/runtime/cmd/decode-state-values/main.go
but it looks like it expects a different file format.

In the new storage format, individual payloads don’t store the full data anymore. Instead, large values like arrays, are split into multiple payloads (registers in the ledger). Do you really want to decode the low-level data in each payload/register as low-level CBOR structures? Or do you rather want to decode the high-level Cadence values?

The decode-state-values tool accepts a state dump in JSONL format, and is only used for testing/debugging purposes. I think you can generate it from a checkpoint file using https://github.com/onflow/flow-go/blob/master/cmd/util/cmd/export-json-execution-state/cmd.go.

If you want to read in the execution state and operate on the payloads, you can look at the state reporting/migration tool at https://github.com/onflow/flow-go/blob/master/cmd/util/cmd/execution-state-extract/execution_state_extract.go. For example, you can find some of the reporters here: https://github.com/onflow/flow-go/tree/master/cmd/util/ledger/reporters.

1 Like