Giter VIP home page Giter VIP logo

Comments (1)

shekohex avatar shekohex commented on August 14, 2024

This Like a two part operation here.

Runtime API:
So for you to do this, you need to declare it for the runtime API first like here:

sp_api::decl_runtime_apis! {
pub trait MerkleTreeApi<E: ElementTrait> {
/// Get the leaf of tree id at a given index.
fn get_leaf(tree_id: u32, index: u32) -> Option<E>;
}
}

And then you will need to implement that Runtime API in the */runtime/lib.rs like so:

impl pallet_mt_rpc_runtime_api::MerkleTreeApi<Block, Element> for Runtime {
fn get_leaf(tree_id: u32, index: u32) -> Option<Element> {
let v = MerkleTreeBn254::leaves(tree_id, index);
if v == Element::default() {
None
} else {
Some(v)
}
}
}

RPC:
And here for the RPC

/// Merkle RPC methods.
#[rpc(client, server)]
pub trait MerkleTreeRpcApi<BlockHash, Element> {
/// Get The MerkleTree leaves.
///
/// This method calls into a runtime with `Merkle` pallet included and
/// attempts to get the merkletree leaves.
/// Optionally, a block hash at which the runtime should be queried can be
/// specified.
///
/// Returns the (full) a Vec<[u8; 32]> of the leaves.
#[method(name = "mt_getLeaves")]
fn get_leaves(
&self,
tree_id: u32,
from: usize,
to: usize,
at: Option<BlockHash>,
) -> RpcResult<Vec<Element>>;
}

Then you just implement it inside the RPC

This an example implementation of the get_leaves

fn get_leaves(
&self,
tree_id: u32,
from: usize,
to: usize,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<Element>> {
self.deny_unsafe.check_if_safe()?;
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);
if to - from >= 512 {
return Err(error::Error::TooManyLeavesRequested.into())
}
let leaves = (from..to)
.into_iter()
.flat_map(|i| api.get_leaf(at, tree_id, i as u32)) // Result<Option<Element>>
.flatten() // Element
.collect();
Ok(leaves)

And that's it actually.

from protocol-substrate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.