Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pallets: Add Anchors V2 pallet #1878

Merged
merged 12 commits into from
Jun 24, 2024
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"libs/types",
"libs/utils",
"pallets/anchors",
"pallets/anchors-v2",
"pallets/bridge",
"pallets/block-rewards",
"pallets/collator-allowlist",
Expand Down Expand Up @@ -219,6 +220,7 @@ substrate-build-script-utils = { git = "https://github.com/paritytech/polkadot-s
axelar-gateway-precompile = { path = "pallets/liquidity-pools-gateway/axelar-gateway-precompile", default-features = false }
liquidity-pools-gateway-routers = { path = "pallets/liquidity-pools-gateway/routers", default-features = false }
pallet-anchors = { path = "pallets/anchors", default-features = false }
pallet-anchors-v2 = { path = "pallets/anchors-v2", default-features = false }
pallet-block-rewards = { path = "pallets/block-rewards", default-features = false }
pallet-bridge = { path = "pallets/bridge", default-features = false }
pallet-collator-allowlist = { path = "pallets/collator-allowlist", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Centrifuge is the infrastructure that facilitates the decentralized financing of
On top of the [Substrate FRAME](https://docs.substrate.io/reference/frame-pallets/) framework, Centrifuge Chain is composed of custom pallets which can be found inside the `pallets` folder. The following list gives a brief overview, and links to the corresponding documentation:

- [**anchors**](https://github.com/centrifuge/centrifuge-chain/tree/main/pallets/anchors) ([docs](https://reference.centrifuge.io/pallet_anchors/index.html)): Storing hashes of documents on-chain. The documents are stored in the Private Off-chain Data (POD) node network.
-
- [**anchors-v2**](https://github.com/centrifuge/centrifuge-chain/tree/main/pallets/anchors-v2) ([docs](https://reference.centrifuge.io/pallet_anchors_v2/index.html)): Second version of the pallet used to store document hashes on-chain.

- [**block-rewards**](https://github.com/centrifuge/centrifuge-chain/tree/main/pallets/block-rewards) ([docs](https://reference.centrifuge.io/pallet_block_rewards/index.html)): Provides means of configuring and distributing block rewards to collators as well as the annual treasury inflation.

Expand Down
52 changes: 52 additions & 0 deletions pallets/anchors-v2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[package]
name = "pallet-anchors-v2"
description = "Anchors V2 pallet for runtime"
version = "1.0.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
documentation.workspace = true

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }

frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

[dev-dependencies]
pallet-balances = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"scale-info/std",
"frame-support/std",
"frame-system/std",
"sp-runtime/std",
"sp-std/std",
"frame-benchmarking/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
]
91 changes: 91 additions & 0 deletions pallets/anchors-v2/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2021 Centrifuge Foundation (centrifuge.io).
// This file is part of Centrifuge chain project.

// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).

// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use frame_benchmarking::{account, impl_benchmark_test_suite, v2::*};
use frame_support::traits::Currency;
use frame_system::RawOrigin;
use parity_scale_codec::EncodeLike;
use sp_core::H256;

use super::*;

#[benchmarks(
where
T: Config<Balance = u128, Hash = H256>,
Copy link
Contributor

@lemunozm lemunozm Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not bad, and probably this pallet is only for us, so it will not be an issue. But to explain the why:

These bounds force the user of this pallet to always choose Balance = u128 and Hash = H256 in their runtime if they want to perform the benchmarks. Ideally, we do not want to restrict the usage of the bounds to certain concrete types. We want to allow any type that can be configured by the pallet.

The solution for that is to add extra bounds to those associated types as:

T: Config,
T::Balance: Into<u128>, // Or maybe T::Balance: Default is just enough
T::Hash: Default, // And use later T::Hash::default() instead of H256::from_low_u64_be(1)

Not blocker at all, just for reference

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can leave it as it is for now, these benchmarks are still tests at the end of the day and I think it's fine to have some hardcoded types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not blocker 👍🏻

T::AccountId: EncodeLike<<T as frame_system::Config>::AccountId>,
)]
mod benchmarks {
use super::*;

#[benchmark]
fn set_anchor() -> Result<(), BenchmarkError> {
let caller: T::AccountId = account("acc_0", 0, 0);

let document_id = 123;
let document_version = 456;
let hash = H256::from_low_u64_be(1);

let _ = T::Currency::deposit_creating(
&caller.clone().into(),
T::Currency::minimum_balance() + T::DefaultAnchorDeposit::get(),
);

#[extrinsic_call]
set_anchor(
RawOrigin::Signed(caller),
document_id,
document_version,
hash,
);

Ok(())
}

#[benchmark]
fn remove_anchor() -> Result<(), BenchmarkError> {
let caller: T::AccountId = account("acc_0", 0, 0);

let document_id = 123;
let document_version = 456;
let hash = H256::from_low_u64_be(1);
let deposit = AnchorDeposit::<T>::get();

let anchor = Anchor::<T> {
account_id: caller.clone(),
document_id,
document_version,
hash,
deposit,
};

Anchors::<T>::insert((document_id, document_version), anchor);
PersonalAnchors::<T>::insert(caller.clone(), (document_id, document_version), ());

#[extrinsic_call]
remove_anchor(RawOrigin::Signed(caller), document_id, document_version);

Ok(())
}

#[benchmark]
fn set_deposit_value() -> Result<(), BenchmarkError> {
let deposit = 2 * T::DefaultAnchorDeposit::get();

#[extrinsic_call]
set_deposit_value(RawOrigin::Root, deposit);

Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime);
}
Loading