I'm trying to store the block in a DB and then serve it over an API. But when I retrieve the stored (JSON)data from the DB and send over an API I can't reconstruct the L2Block-object from the data. I know there are to/fromString and to/fromBuffer, but I want to make the data stored in DB queryable.
When trying to use fromFields with the JSON-data it works, but the object is not fully reconstructed. Also, the necessary types are not exported so that I can reconstruct the object.
import { L2Block } from "@aztec/aztec.js";
// ... receiving `block`
const jsonString = JSON.stringify(block, null, 2);
const recreatedObj = L2Block.fromFields(JSON.parse(jsonString) as L2Block);
logger.info(`Fields: ${JSON.stringify(recreatedObj, null, 2)}`);
// below line produces an error
logger.info(`Fields: ${recreatedObj.hash()}`);
// ...
...produces this...
TypeError: this.header.hash is not a function
at L2Block.hash (/usr/main/node_modules/@aztec/circuit-types/src/l2_block.ts:139:24)
at Object.store (/usr/main/services/explorer-api/src/database/models/block.ts:36:30)
at onBlock (/usr/main/services/explorer-api/src/event-handler/on-block.ts:6:17)
...
When trying to reconstruct the obj required for fromFields I get stuck at not being able to import AppendOnlyTreeSnapshot for re-creation. It seems to be plenty of more classes that's not able to import.
I'm trying to store the block in a DB and then serve it over an API. But when I retrieve the stored (JSON)data from the DB and send over an API I can't reconstruct the L2Block-object from the data. I know there are
to/fromString
andto/fromBuffer
, but I want to make the data stored in DB queryable.When trying to use
fromFields
with the JSON-data it works, but the object is not fully reconstructed. Also, the necessary types are not exported so that I can reconstruct the object.I've reproduced it here. But the gist of it is this....
...produces this...