Solana: Error using concurrent merkle tree: Invalid root recomputed from proof when use updateMetadata
Error to use the concomitant Merkle wood on Solana: Invalid root recalculation from evidence when you refresh metadata
As a creator of CNFT -K, the upgrading of metadata on Solana Blockchain can be a simple process. However, in the case of a re-counting of the simultaneous Merkle tree, many creators left disappointment. In this article we are immersed in the question and give guidance to solve it.
The error
The error occurs when they try to update metadata to CNFT on the Solana blockchain. The “UPDATETATATA method” is called at the same time as another operation, so re -formulation of an invalid root from the evidence. This can lead to inconsistent condition of the network and potentially problems with CNFT functionality.
Here are the reference materials used:
- [Solana API Documentation] (
- [CNFT Documentation of Solana] (
The code example
Suppose we have a CNFT contract for demonstration purposes with the following code:
`Solana
Import {program} from the “Solana program”;
Import {updatemetata} from “../lib/update-metadata”;
Async Function Main () {
// initialize metadata
CONST METAADATATA = Waiting for Gettetata ();
Try {
// update metadata
Waiting for your updatemetata (metadat);
} catch (error) {
IF (Error Instance of Error && Error.message.includes (“Invalid root”)) {
// Manage the error by recalculating the root mower from a certificate
CONST PROOF = Waiting for GetProofforupdate ();
Const root = waiting for Computemerkletree (evidence);
Try {
// update metadata with the new root
Waiting for your updatemetata (metadata, root);
} catch (error) {
// Manage errors when updating metadata
Console.Error (error);
}
} Else {
throw error;
}
}
}
ASYNC Function Gettetata () {
// return some samples metadata data
return [
“
“Some other metadata information”,
];
}
// Helper functions to produce evidence and calculate the Merkle tree
function getproofforupdate () {
// generating random evidence to update metadata
Back the return of the new promise (soluble => setmeout (() => resole (new Uint8array (32))), 100);
}
Async Function Computemerkletree (Certificate) {
// Calculate the Merkle tree from evidence
Back to the return of the new promise (dissolved => setmeout (() => resole (new Uint8array (256)), 200));
}
main();
`
The solution
To solve the problem, you only need to recalculate the root when updating the metadata. This is achieved by introducing a new variable that tracks the upgrade of the upgrade:
`Solana
Import {program} from the “Solana program”;
Import {updatemetata} from “../lib/update-metadata”;
CNTF class {
Private upgrades = false;
Private proofindex = 0;
Async getmetadata () {
if (this.updateinProgress) null returns; // The update is in progress, do not continue
this.proofindex ++;
CONST METAADATATA = Wait for GetMetadatafromproof ();
Returning metadata;
}
Async updatememetata (metadata) {
if (this.updateinProgress) throw a new error (“Invalid root recalculation from evidence”);
this.updateinProgress = TRUE;
Try {
// update metadata using metadata
Waiting for your updatemetata (metadat);
} catch (error) {
IF (Error Instance of Error && Error.message.includes (“Invalid root”)) {
// Manage the error by recalculating the root mower from a certificate
Const Preutindex = this.proofindex;
Const PreyData = Waiting for GetProoffrom index (proofindex);
Try {
this.updateinProgress = fake; // Restoring the flag of update
this.