-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from Concordium/add-deserialization-of-contrac…
…t-state Add deserialization of contract state
- Loading branch information
Showing
13 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule concordium-contracts-common
added at
48d595
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as wasm from '../pkg/node_sdk_helpers'; | ||
import { Buffer } from 'buffer/'; | ||
|
||
/** | ||
* Given a contract's raw state, its name and its schema, return the state as a JSON object. | ||
* The return type is any, and the actual type should be determined by using the schema. | ||
*/ | ||
export function deserializeContractState( | ||
contractName: string, | ||
schema: Buffer, | ||
state: Buffer | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
): any { | ||
const serializedState = wasm.deserializeState( | ||
contractName, | ||
state.toString('hex'), | ||
schema.toString('hex') | ||
); | ||
try { | ||
return JSON.parse(serializedState); | ||
} catch (e) { | ||
throw new Error( | ||
'unable to deserialize state, due to: ' + serializedState | ||
); // In this case serializedState is the error message from the rust module | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { getNodeClient } from './testHelpers'; | ||
import { deserializeContractState } from '../src/deserialization'; | ||
import { Buffer } from 'buffer/'; | ||
import * as fs from 'fs'; | ||
|
||
const client = getNodeClient(); | ||
|
||
test('Deserialize state with schema from file (two-step-transfer)', async () => { | ||
const blockHash = | ||
'fad0981b0424c6e1af746a39667628861481ac225f90decd233980311c2e19cb'; | ||
const contractAddress = { index: BigInt(1646), subindex: BigInt(0) }; | ||
|
||
const instanceInfo = await client.getInstanceInfo( | ||
blockHash, | ||
contractAddress | ||
); | ||
if (!instanceInfo) { | ||
throw new Error( | ||
'The instance info should exist for the provided block hash.' | ||
); | ||
} | ||
const schema = Buffer.from( | ||
fs.readFileSync('./test/resources/two-step-transfer-schema.bin') | ||
); | ||
const state = deserializeContractState( | ||
'two-step-transfer', | ||
schema, | ||
instanceInfo.model | ||
); | ||
expect(state.init_params.transfer_agreement_threshold).toBe(2); | ||
expect(state.init_params.account_holders.length).toBe(2); | ||
expect(state.init_params.account_holders[0]).toBe( | ||
'3Y1RLgi5pW3x96xZ7CiDiKsTL9huU92qn6mfxpebwmtkeku8ry' | ||
); | ||
expect(state.init_params.account_holders[1]).toBe( | ||
'4EdBeGmpnQZWxaiig7FGEhWwmJurYmYsPWXo6owMDxA7ZtJMMH' | ||
); | ||
expect(state.init_params.transfer_request_ttl).toBe('0d 0h 0m 0s 0ms'); | ||
expect(state.requests.length).toBe(0); | ||
}); |
Binary file not shown.