From 03ac26eef0057de2aadb6a9090ef14515a0ed319 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Fri, 2 Feb 2024 15:16:34 +0100 Subject: [PATCH] Temp changes to the proof generation logic --- src/sibling.ts | 4 ++-- src/utils.ts | 20 ++++++++++--------- .../.env.develop.test | 6 +++--- .../develop.test.ts | 9 +++++---- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/sibling.ts b/src/sibling.ts index 8b67d31..41df24b 100644 --- a/src/sibling.ts +++ b/src/sibling.ts @@ -108,9 +108,8 @@ export async function generateDipAuthorizedTxForSibling({ providerBlockHeight, }) - // Proof of commitment must be generated with the state root at the block before the last one finalized. const dipRootProofBlockHash = await providerApi.rpc.chain.getBlockHash( - providerStateRootProofProviderBlockHeight.subn(1), + providerStateRootProofProviderBlockHeight, ) const { @@ -122,6 +121,7 @@ export async function generateDipAuthorizedTxForSibling({ version: proofVersion, }) + // TODO: Getting an invalid DID merkle proof now, need to be investigated further. const { proof: dipIdentityProof } = await generateDipIdentityProof({ didUri, providerApi, diff --git a/src/utils.ts b/src/utils.ts index a40dadd..3ec3e91 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -66,28 +66,30 @@ export async function generateProviderStateRootProof({ // Optional providerBlockHeight, }: ProviderStateRootProofOpts): Promise { - const [providerBlockNumber, providerBlockHash] = await (async () => { + const providerBlockNumber = await (async () => { if (providerBlockHeight !== undefined) { - const blockHash = - await providerApi.rpc.chain.getBlockHash(providerBlockHeight) - return [providerBlockHeight, blockHash] + return providerBlockHeight } - const providerLastFinalizedBlockHash = - await providerApi.rpc.chain.getFinalizedHead() + const providerLastFinalizedBlockHash = await providerApi.rpc.chain.getFinalizedHead() const providerLastFinalizedBlockHeight = await providerApi.rpc.chain .getHeader(providerLastFinalizedBlockHash) .then((h) => h.number.toBn()) - return [providerLastFinalizedBlockHeight, providerLastFinalizedBlockHash] + return providerLastFinalizedBlockHeight })() - const providerApiAtBlock = await providerApi.at(providerBlockHash) + console.log(`Provider block height: ${providerBlockNumber}`) + // State for this block is finalized by the relaychain at the end of the next block, hence we take the next block and retrieve the relaychain validation data. + const nextProviderBlockNumber = providerBlockNumber.addn(1) + const nextProviderBlockHash = await providerApi.rpc.chain.getBlockHash(nextProviderBlockNumber) + const providerApiAtBlock = await providerApi.at(nextProviderBlockHash) const providerChainId = await providerApiAtBlock.query.parachainInfo.parachainId() const relayParentBlockNumber = await providerApiAtBlock.query.parachainSystem.lastRelayChainBlockNumber() - // This refers to the previously finalized block, we need the current one. + console.log(`Relay parent block number for the provider finalized block: ${relayParentBlockNumber}`) const relayParentBlockHash = await relayApi.rpc.chain.getBlockHash( relayParentBlockNumber, ) + console.log(`Relay parent block hash for the provider finalized block: ${relayParentBlockNumber}`) const proof = await relayApi.rpc.state.getReadProof( [relayApi.query.paras.heads.key(providerChainId)], diff --git a/tests/peregrine-dip-consumer-template/.env.develop.test b/tests/peregrine-dip-consumer-template/.env.develop.test index c44b682..b224947 100644 --- a/tests/peregrine-dip-consumer-template/.env.develop.test +++ b/tests/peregrine-dip-consumer-template/.env.develop.test @@ -1,6 +1,6 @@ RELAY_IMAGE=parity/polkadot:v1.0.0 -RELAY_ALICE_RPC=10001 +RELAY_ALICE_RPC=50001 PROVIDER_IMAGE=kiltprotocol/kilt-node:latest-develop -PROVIDER_ALICE_RPC=10011 +PROVIDER_ALICE_RPC=50010 CONSUMER_IMAGE=kiltprotocol/dip-consumer-node-template:latest-develop -CONSUMER_ALICE_RPC=10021 +CONSUMER_ALICE_RPC=50060 diff --git a/tests/peregrine-dip-consumer-template/develop.test.ts b/tests/peregrine-dip-consumer-template/develop.test.ts index 1b93afd..ce6d406 100644 --- a/tests/peregrine-dip-consumer-template/develop.test.ts +++ b/tests/peregrine-dip-consumer-template/develop.test.ts @@ -193,6 +193,9 @@ describe("V0", () => { withCrossModuleSystemImport( "..", async (DipSdk) => { + // TODO: The provider block considered is the one where the state is changed, and so far so good. + // The problem stems from the fact that such state, even when it's finalized, will be included into a successive block by the relaychain, not the previous one. + // Hence, given a block number for the provider, the relay block to consider is the one in the validation data + 1. it("Successful posts on the consumer's PostIt pallet using by default the latest provider finalized block", async () => { const { consumerApi } = testConfig const postText = "Hello, world!" @@ -219,8 +222,7 @@ describe("V0", () => { const postKey = blake2AsHex( consumerApi .createType( - `(${ - config.blockNumberRuntimeType as string + `(${config.blockNumberRuntimeType as string }, ${web3NameRuntimeType}, Bytes)`, [blockNumber, web3Name, postText], ) @@ -262,8 +264,7 @@ describe("V0", () => { const postKey = blake2AsHex( consumerApi .createType( - `(${ - config.blockNumberRuntimeType as string + `(${config.blockNumberRuntimeType as string }, ${web3NameRuntimeType}, Bytes)`, [blockNumber, web3Name, postText], )