Error using a simultaneous Merkle tree on Solana: Invalid root of proof when updating metadata
As a creator of CNFTS on the Solana blockchain, the update of metadata can be a simple process. However, if you come across an error in which a Merkle tree is newly computed, many creators have frustrated. In this article we will deal with the problem and give instructions for the solution.
The error
When trying, the error occurs to update metadata for a CNFT on the Solana blockchain. The “UpdateMatata” method is also called up with another operation, which means that an invalid root is recalculated from a detection. This can lead to inconsistent state in the network and possibly cause problems with the functionality of the CNFT.
Here are the reference materials you use:
- [Solana api documentation] (
- [CNFT -Documentation to Solana] (
The code example
Let’s assume demonstration purposes, we have a CNFT contract with the following code:
`Solana
{Program} import from "Solana program";
import {updateematata} from "../lib/update- metadata";
Asynchrical function main () {
// Initialize metadata
Const Metadata = WaTte Getmatata ();
attempt {
// update metadata
Wait update metadata (metadata);
} Catch (error) {
if (error instance of error && error.message.includes ("invalid root")) {
// treat the error by reinstalling the tribe from a proof
Const Proof = WaTte GetProofforupdate ();
Const root = waiting computerkletree (evidence);
attempt {
// update metadata with the new root
Wait update metadata (Metadata, root);
} Catch (error) {
// Treat all errors that occur during the metadata update
console.error (error);
}
} differently {
Throw errors;
}
}
}
Asynchrical function GetMetadata () {
// return some example -metastatat data
return [
"
"Some other metadata information",
];
}
// helper functions to generate evidence and to calculate the Merkle tree
Function GetProofforupdate () {
// generate random proof of the update of metadata
Return New Promise (Resolve => Settimeout (() => Resolve (New Uint8array (32))), 100);
}
Async function computemekletre (proof) {
// calculate the Merkle tree from the evidence
Return New Promise (Resolve => Settimeout (() => Resolve (New Uint8array (256)), 200));
}
primarily();
The solution
In order to fix this problem, we only have to recalculate the regular procedure when updating metadata. This can be achieved by introducing a new variable that follows whether an update is underway:
“ Solana
{Program} import from “Solana program”;
Import {updateematata} from “../lib/update- metadata”;
Class CNTF {
private update progression = false;
private proofindex = 0;
Async GetMetadata () {
if (this.updateinprogress) Return zero; // update is underway, do not go away
This.Proodeindex ++;
Const Metadata = WaTte Getmetadatasafromproof ();
Return metadata;
}
Async updateemata (metadata) {
if (this.upateinprogress) throw new errors (“invalid root of evidence”);
This.Updateinprogress = True;
attempt {
// update metadata with the metadata
Wait update metadata (metadata);
} Catch (error) {
if (error instance of error && error.message.includes (“invalid root”))) {
// treat the error by reinstalling the tribe from a proof
Const proofindex = this. Proodeindex;
Const Proofdata = WaTte GetProoffrom index (proofindex);
attempt {
This.updatrogress = false; // reset update flag
The.
Leave a Reply