Files
pezkuwi-subxt/substrate/frame/statement/src/tests.rs
T
Arkadiy Paronyan bfafbf7bac Statement store (#13701)
* WIP Statement store

* Sync with networking changes in master

* WIP statement pallet

* Statement validation

* pallet tests

* Validation queue

* Store maintenance

* Basic statement refactoring + tests + docs

* Store metrics

* Store tests

* Store maintenance test

* cargo fmt

* Build fix

* OCW Api

* Offchain worker

* Enable host functions

* fmt

* Minor tweaks

* Fixed a warning

* Removed tracing

* Manual expiration

* Reworked constraint management

* Updated pallet constraint calculation

* Added small test

* Added remove function to the APIs

* Copy-paste spec into readme

* Comments

* Made the store optional

* Removed network protocol controller

* fmt

* Clippy fixes

* fmt

* fmt

* More clippy fixes

* More clippy fixes

* More clippy fixes

* Update client/statement-store/README.md

Co-authored-by: cheme <emericchevalier.pro@gmail.com>

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Removed sstore from node-template

* Sort out data path

* Added offline check

* Removed dispatch_statement

* Renamed into_generic

* Fixed commit placement

* Use HashSet for tracking peers/statements

* fmt

* Use ExtendedHostFunctions

* Fixed benches

* Tweaks

* Apply suggestions from code review

Co-authored-by: cheme <emericchevalier.pro@gmail.com>

* Fixed priority mixup

* Rename

* newtypes for priorities

* Added MAX_TOPICS

* Fixed key filtering logic

* Remove empty entrie

* Removed prefix from signing

* More documentation

* fmt

* Moved store setup from sc-service to node

* Handle maintenance task in sc-statement-store

* Use statement iterator

* Renamed runtime API mod

* fmt

* Remove dump_encoded

* fmt

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Fixed build after applying review suggestions

* License exceptions

* fmt

* Store options

* Moved pallet consts to config trait

* Removed global priority

* Validate fields when decoding

* Limit validation channel size

* Made a comment into module doc

* Removed submit_encoded

---------

Co-authored-by: cheme <emericchevalier.pro@gmail.com>
Co-authored-by: Bastian Köcher <git@kchr.de>
2023-05-04 10:24:32 +00:00

160 lines
5.3 KiB
Rust

// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! # Statement runtime support tests.
#![cfg(test)]
use super::*;
use crate::mock::*;
use sp_core::Pair;
use sp_runtime::AccountId32;
use sp_statement_store::{
runtime_api::{InvalidStatement, StatementSource, ValidStatement},
Proof, Statement,
};
#[test]
fn sign_and_validate_no_balance() {
new_test_ext().execute_with(|| {
let pair = sp_core::sr25519::Pair::from_string("//Bob", None).unwrap();
let mut statement = Statement::new();
statement.sign_sr25519_private(&pair);
let result = Pallet::<Test>::validate_statement(StatementSource::Chain, statement);
assert_eq!(
Ok(ValidStatement { max_count: MIN_ALLOWED_STATEMENTS, max_size: MIN_ALLOWED_BYTES }),
result
);
let pair = sp_core::ed25519::Pair::from_string("//Bob", None).unwrap();
let mut statement = Statement::new();
statement.sign_ed25519_private(&pair);
let result = Pallet::<Test>::validate_statement(StatementSource::Chain, statement);
assert_eq!(
Ok(ValidStatement { max_count: MIN_ALLOWED_STATEMENTS, max_size: MIN_ALLOWED_BYTES }),
result
);
let pair = sp_core::ecdsa::Pair::from_string("//Bob", None).unwrap();
let mut statement = Statement::new();
statement.sign_ecdsa_private(&pair);
let result = Pallet::<Test>::validate_statement(StatementSource::Chain, statement);
assert_eq!(
Ok(ValidStatement { max_count: MIN_ALLOWED_STATEMENTS, max_size: MIN_ALLOWED_BYTES }),
result
);
});
}
#[test]
fn validate_with_balance() {
new_test_ext().execute_with(|| {
let pair = sp_core::sr25519::Pair::from_string("//Alice", None).unwrap();
let mut statement = Statement::new();
statement.sign_sr25519_private(&pair);
let result = Pallet::<Test>::validate_statement(StatementSource::Chain, statement);
assert_eq!(Ok(ValidStatement { max_count: 6, max_size: 3000 }), result);
let pair = sp_core::sr25519::Pair::from_string("//Charlie", None).unwrap();
let mut statement = Statement::new();
statement.sign_sr25519_private(&pair);
let result = Pallet::<Test>::validate_statement(StatementSource::Chain, statement);
assert_eq!(
Ok(ValidStatement { max_count: MAX_ALLOWED_STATEMENTS, max_size: MAX_ALLOWED_BYTES }),
result
);
});
}
#[test]
fn validate_no_proof_fails() {
new_test_ext().execute_with(|| {
let statement = Statement::new();
let result = Pallet::<Test>::validate_statement(StatementSource::Chain, statement);
assert_eq!(Err(InvalidStatement::NoProof), result);
});
}
#[test]
fn validate_bad_signature_fails() {
new_test_ext().execute_with(|| {
let statement = Statement::new_with_proof(Proof::Sr25519 {
signature: [0u8; 64],
signer: Default::default(),
});
let result = Pallet::<Test>::validate_statement(StatementSource::Chain, statement);
assert_eq!(Err(InvalidStatement::BadProof), result);
});
}
#[test]
fn validate_event() {
new_test_ext().execute_with(|| {
let parent_hash = sp_core::H256::random();
System::reset_events();
System::initialize(&1, &parent_hash, &Default::default());
let mut statement = Statement::new();
let pair = sp_core::sr25519::Pair::from_string("//Alice", None).unwrap();
let account: AccountId32 = pair.public().into();
Pallet::<Test>::submit_statement(account.clone(), statement.clone());
statement.set_proof(Proof::OnChain {
who: account.clone().into(),
event_index: 0,
block_hash: parent_hash.into(),
});
let result = Pallet::<Test>::validate_statement(StatementSource::Chain, statement.clone());
assert_eq!(Ok(ValidStatement { max_count: 6, max_size: 3000 }), result);
// Use wrong event index
statement.set_proof(Proof::OnChain {
who: account.clone().into(),
event_index: 1,
block_hash: parent_hash.into(),
});
let result = Pallet::<Test>::validate_statement(StatementSource::Chain, statement.clone());
assert_eq!(Err(InvalidStatement::BadProof), result);
// Use wrong block hash
statement.set_proof(Proof::OnChain {
who: account.clone().into(),
event_index: 0,
block_hash: sp_core::H256::random().into(),
});
let result = Pallet::<Test>::validate_statement(StatementSource::Chain, statement.clone());
assert_eq!(Err(InvalidStatement::BadProof), result);
});
}
#[test]
fn validate_no_event_fails() {
new_test_ext().execute_with(|| {
let parent_hash = sp_core::H256::random();
System::reset_events();
System::initialize(&1, &parent_hash, &Default::default());
let mut statement = Statement::new();
let pair = sp_core::sr25519::Pair::from_string("//Alice", None).unwrap();
let account: AccountId32 = pair.public().into();
statement.set_proof(Proof::OnChain {
who: account.into(),
event_index: 0,
block_hash: parent_hash.into(),
});
let result = Pallet::<Test>::validate_statement(StatementSource::Chain, statement);
assert_eq!(Err(InvalidStatement::BadProof), result);
});
}