Skip to content

Commit

Permalink
# This is a combination of 3 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

Upgraded substrate

# This is the commit message #2:

Bumped ByteDeposit and changed remove_contract to allow anyone remove not signed contract

# This is the commit message #3:

fixed fmt
  • Loading branch information
Michal Lustyk authored and Michal Lustyk committed Feb 20, 2024
1 parent 8e69950 commit 96a85f1
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 21 deletions.
47 changes: 47 additions & 0 deletions frame/contracts-registry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[package]
name = "pallet-contracts-registry"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
frame-support = { version = "4.0.0-dev", default-features = false, branch = "polkadot-v0.9.43", git = "https://github.com/paritytech/substrate" }
frame-system = { version = "4.0.0-dev", default-features = false, branch = "polkadot-v0.9.43", git = "https://github.com/paritytech/substrate" }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
scale-info = { version = "2.0.1", default-features = false, features = ["derive"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, branch = "polkadot-v0.9.43", git = "https://github.com/paritytech/substrate" }
sp-runtime = { version = "7.0.0", default-features = false, branch = "polkadot-v0.9.43", git = "https://github.com/paritytech/substrate" }
sp-core = { version = "7.0.0", default-features = false, branch = "polkadot-v0.9.43", git = "https://github.com/paritytech/substrate" }
node-primitives = { version = "2.0.0", branch = "polkadot-v0.9.43", git = "https://github.com/paritytech/substrate", default-features = false }
pallet-balances = { branch = "polkadot-v0.9.43", git = "https://github.com/paritytech/substrate", default-features = false }
sp-std = { version = "5.0.0", default-features = false, branch = "polkadot-v0.9.43", git = "https://github.com/paritytech/substrate" }
sp-io = { version = "7.0.0", default-features = false, branch = "polkadot-v0.9.43", git = "https://github.com/paritytech/substrate" }

[dev-dependencies]

[features]
default = ["std"]
std = [
"frame-support/std",
"frame-system/std",
"codec/std",
"scale-info/std",
"frame-benchmarking/std",
"sp-io/std",
"sp-std/std",
"sp-core/std",
"sp-runtime/std",
"pallet-balances/std",
"node-primitives/std"
]

try-runtime = [
"frame-support/try-runtime",
"pallet-balances/try-runtime"
]

runtime-benchmarks= [
"frame-benchmarking/runtime-benchmarks",
"pallet-balances/runtime-benchmarks"
]
3 changes: 1 addition & 2 deletions substrate/frame/contracts-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ sp-std = { default-features = false, tag = "polkadot-v1.1.0", git = "https://git
sp-io = { default-features = false, tag = "polkadot-v1.1.0", git = "https://github.com/paritytech/polkadot-sdk" }
node-primitives = { default-features = false, tag = "polkadot-v1.1.0", git = "https://github.com/paritytech/polkadot-sdk" }
pallet-balances = { default-features = false, tag = "polkadot-v1.1.0", git = "https://github.com/paritytech/polkadot-sdk" }

codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
scale-info = { version = "2.0.1", default-features = false, features = ["derive"] }

Expand Down Expand Up @@ -43,6 +42,6 @@ try-runtime = [
]

runtime-benchmarks= [
"frame-benchmarking/runtime-benchmarks",
"frame-benchmarking/runtime-benchmarks",
"pallet-balances/runtime-benchmarks"
]
11 changes: 6 additions & 5 deletions substrate/frame/contracts-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,7 @@ pub mod pallet {
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::remove_contract())]
pub fn remove_contract(origin: OriginFor<T>, contract_id: ContractIndex) -> DispatchResult {
let who = T::SubmitOrigin::ensure_origin(origin)?;
let contract =
Contracts::<T, I>::get(contract_id).ok_or(Error::<T, I>::ContractNotFound)?;
ensure!(contract.creator == who, Error::<T, I>::NotCreator);
T::Currency::unreserve_named(T::ReserveIdentifier::get(), &who, contract.deposit);
T::SubmitOrigin::ensure_origin(origin)?;

let judges_signatures_len = JudgesSignatures::<T, I>::get(contract_id)
.ok_or(Error::<T, I>::ContractNotFound)?
Expand All @@ -333,6 +329,11 @@ pub mod pallet {
Error::<T, I>::ContractInUse
);

let contract =
Contracts::<T, I>::get(contract_id).ok_or(Error::<T, I>::ContractNotFound)?;

T::Currency::unreserve_named(T::ReserveIdentifier::get(), &contract.creator, contract.deposit);

JudgesSignatures::<T, I>::remove(contract_id);
PartiesSignatures::<T, I>::remove(contract_id);
Contracts::<T, I>::remove(contract_id);
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/contracts-registry/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl pallet_contracts_registry::Config for Test {
type WeightInfo = ();
type Currency = Balances;
type BaseDeposit = ConstU64<1>;
type ByteDeposit = ConstU64<1>;
type ByteDeposit = ConstU64<2>;
type ReserveIdentifier = ReserveIdentifier;
}

Expand Down
26 changes: 13 additions & 13 deletions substrate/frame/contracts-registry/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn anyone_can_create_contract() {
vec![].try_into().unwrap(),
vec![].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 98u64);
assert_eq!(Balances::free_balance(2), 97u64);
});
}

Expand Down Expand Up @@ -94,7 +94,7 @@ fn judge_can_sign_existing_contract() {
vec![3, 4, 5].try_into().unwrap(),
vec![].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 95u64);
assert_eq!(Balances::free_balance(2), 91u64);

assert_ok!(ContractsRegistry::add_judge(origin, acc));

Expand All @@ -114,7 +114,7 @@ fn only_judge_can_use_judge_sign_contract() {
vec![3, 4, 5].try_into().unwrap(),
vec![].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 95u64);
assert_eq!(Balances::free_balance(2), 91u64);

assert_noop!(
ContractsRegistry::judge_sign_contract(origin.into(), 0),
Expand All @@ -134,7 +134,7 @@ fn judge_already_signed_contract() {
vec![3, 4, 5].try_into().unwrap(),
vec![].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 95u64);
assert_eq!(Balances::free_balance(2), 91u64);

let acc = account("a", 1, SEED);
assert_ok!(ContractsRegistry::add_judge(origin, acc));
Expand All @@ -161,7 +161,7 @@ fn party_can_sign_contract() {
vec![3, 4, 5].try_into().unwrap(),
vec![acc].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 95u64);
assert_eq!(Balances::free_balance(2), 91u64);

let origin = RawOrigin::Signed(acc);
assert_ok!(ContractsRegistry::party_sign_contract(origin.into(), 0));
Expand Down Expand Up @@ -192,7 +192,7 @@ fn party_can_not_sign_not_a_party() {
vec![3, 4, 5].try_into().unwrap(),
vec![].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 95u64);
assert_eq!(Balances::free_balance(2), 91u64);

let origin = RawOrigin::Signed(acc);
assert_noop!(
Expand All @@ -214,7 +214,7 @@ fn party_already_signed_contract() {
vec![3, 4, 5].try_into().unwrap(),
vec![acc].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 95u64);
assert_eq!(Balances::free_balance(2), 91u64);

let origin = RawOrigin::Signed(acc);
assert_ok!(ContractsRegistry::party_sign_contract(origin.clone().into(), 0));
Expand Down Expand Up @@ -268,7 +268,7 @@ fn judge_sign_contract_deposits_event() {
vec![3, 4, 5].try_into().unwrap(),
vec![].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 95u64);
assert_eq!(Balances::free_balance(2), 91u64);

assert_ok!(ContractsRegistry::add_judge(origin, signer));

Expand Down Expand Up @@ -309,7 +309,7 @@ fn party_sign_contract_deposits_event() {
vec![3, 4, 5].try_into().unwrap(),
vec![signer].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 95u64);
assert_eq!(Balances::free_balance(2), 91u64);

let origin = RawOrigin::Signed(signer);
assert_ok!(ContractsRegistry::party_sign_contract(origin.into(), 0));
Expand Down Expand Up @@ -344,7 +344,7 @@ fn creator_can_remove_contract() {
vec![].try_into().unwrap(),
vec![].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 98u64);
assert_eq!(Balances::free_balance(2), 97u64);
assert!(Contracts::<Test>::get(0).is_some());
assert_ok!(ContractsRegistry::remove_contract(origin.into(), 0));
assert!(Contracts::<Test>::get(0).is_none());
Expand All @@ -363,7 +363,7 @@ fn remove_contract_deposits_event() {
vec![].try_into().unwrap(),
vec![].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 98u64);
assert_eq!(Balances::free_balance(2), 97u64);
assert!(Contracts::<Test>::get(0).is_some());
assert_ok!(ContractsRegistry::remove_contract(origin.into(), 0));
assert!(Contracts::<Test>::get(0).is_none());
Expand All @@ -383,7 +383,7 @@ fn create_contract_successfully_reserve_deposit() {
vec![1, 2, 3].try_into().unwrap(),
vec![].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 95u64);
assert_eq!(Balances::free_balance(2), 91u64);
});
}

Expand All @@ -398,7 +398,7 @@ fn remove_contract_successfully_unreserve_deposit() {
vec![1, 2, 3].try_into().unwrap(),
vec![].try_into().unwrap()
));
assert_eq!(Balances::free_balance(2), 95u64);
assert_eq!(Balances::free_balance(2), 91u64);
assert_ok!(ContractsRegistry::remove_contract(origin.clone(), 0));
assert_eq!(Balances::free_balance(2), 100u64);
});
Expand Down

0 comments on commit 96a85f1

Please sign in to comment.