Utxo support, based on Substrate's workshop.
This is only the pallet; no node and runtime implementation.
To run the test cases, just run command cargo test
.
How to run the benchmark in mintlayer-node:
-
Insert this pallet-utxo crate in pallets directory.
-
At runtime's Cargo.toml:
2.1. add to local dependencies:pallet-utxo = { default-features = false, path = "../pallets/utxo" }
2.2. add to runtime-benchmarks features:
'pallet-utxo/runtime-benchmarks'
2.3. add to std features:
'pallet-utxo/std'
-
At runtime's lib.rs:
3.1. Import the following:pub use pallet_utxo; use sp_runtime::transaction_validity::{TransactionValidityError, InvalidTransaction}; use sp_core::H256; use frame_support::traits::IsSubType;
3.2. Add the utxo config:
impl pallet_utxo::Config for Runtime { type Event = Event; type Call = Call; type WeightInfo = pallet_utxo::weights::WeightInfo<Runtime>; fn authorities() -> Vec<H256> { Aura::authorities() .iter() .map(|x| { let r: &sp_core::sr25519::Public = x.as_ref(); r.0.into() }) .collect() } }
3.3. Add into
construct_runtime!
this line:Utxo: pallet_utxo::{Pallet, Call, Config<T>, Storage, Event<T>},
3.4. inside
fn validate_transaction()
, add this code before theExecutive::validate_transaction(source, tx)
line:if let Some(pallet_utxo::Call::spend(ref tx)) = IsSubType::<pallet_utxo::Call::<Runtime>>::is_sub_type(&tx.function) { match pallet_utxo::validate_transaction::<Runtime>(&tx) { Ok(valid_tx) => { return Ok(valid_tx); } Err(_) => { return Err(TransactionValidityError::Invalid(InvalidTransaction::Custom(1))); } } }
3.5. In the function
fn dispatch_benchmark()
, add another line:add_benchmark!(params, batches, pallet_utxo, Utxo);
-
In node's chain_spec.rs:
4.1. Import the ff:use node_template_runtime::{UtxoConfig, pallet_utxo}; use sp_core:H256;
4.2. add one more param on function
testnet_genesis()
:endowed_utxos: Vec<sr25519::Public>
4.3. inside function
testnet_genesis()
, create the genesis utxo:let genesis:Vec<pallet_utxo::TransactionOutput> = endowed_utxos.iter().map(|x| { let pub_key = H256::from_slice(x.as_slice()); let tx_output = pallet_utxo::TransactionOutput::new( 100 as pallet_utxo::Value, pub_key ); let blake_hash = BlakeTwo256::hash_of(&tx_output); tx_output }).collect();
4.4. Still inside
testnet_genesis()
function, add to theGenesisConfig
:pallet_utxo: UtxoConfig { genesis_utxos: genesis, _marker: Default::default() }
4.5. Inside both
fn development_config()
andfn local_testnet_config()
, add the missing param oftestnet_genesis()
for the endowed_utxos:vec![ get_from_seed::<sr25519::Public>("Alice"), get_account_id_from_seed::<sr25519::Public>("Bob") ]
-
On the terminal, move to the node directory and run
cargo b --release --features runtime-benchmarks
-
Go back to the workspace directory
$> cd ..
and run:RUST_LOG=runtime=debug target/release/node-template benchmark --chain dev --execution=wasm --wasm-execution=compiled --pallet pallet_utxo --extrinsic runtime_spend --steps 20 --repeat 10 --output . --raw