Skip to content

Commit 411bc73

Browse files
authored
fix(primitives): enrich error types (#56)
* fix(primitives): enrich error types
1 parent 32305dd commit 411bc73

File tree

3 files changed

+97
-57
lines changed

3 files changed

+97
-57
lines changed

pvq-primitives/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ pub type PvqResponse = Vec<u8>;
1010

1111
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, TypeInfo)]
1212
pub enum PvqError {
13+
FailedToDecode,
14+
InvalidPvqProgramFormat,
15+
QueryExceedsWeightLimit,
1316
Custom(String),
1417
}
1518

pvq-test-runner/src/lib.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use parity_scale_codec::Encode;
2+
use pvq_extension::{extensions_impl, ExtensionsExecutor, InvokeSource};
3+
4+
#[derive(Encode)]
5+
#[allow(non_camel_case_types)]
6+
#[allow(dead_code)]
7+
pub enum ExtensionFungiblesFunctions {
8+
#[codec(index = 0)]
9+
total_supply { asset: u32 },
10+
#[codec(index = 1)]
11+
balance { asset: u32, who: [u8; 32] },
12+
}
13+
14+
#[extensions_impl]
15+
pub mod extensions {
16+
#[extensions_impl::impl_struct]
17+
pub struct ExtensionsImpl;
18+
19+
#[extensions_impl::extension]
20+
impl pvq_extension_core::extension::ExtensionCore for ExtensionsImpl {
21+
type ExtensionId = u64;
22+
fn has_extension(id: Self::ExtensionId) -> bool {
23+
matches!(id, 0 | 1)
24+
}
25+
}
26+
27+
#[extensions_impl::extension]
28+
impl pvq_extension_fungibles::extension::ExtensionFungibles for ExtensionsImpl {
29+
type AssetId = u32;
30+
type AccountId = [u8; 32];
31+
type Balance = u64;
32+
fn total_supply(_asset: Self::AssetId) -> Self::Balance {
33+
100
34+
}
35+
fn balance(_asset: Self::AssetId, _who: Self::AccountId) -> Self::Balance {
36+
100
37+
}
38+
}
39+
}
40+
41+
pub struct TestRunner {
42+
executor: ExtensionsExecutor<extensions::Extensions, ()>,
43+
}
44+
45+
impl TestRunner {
46+
pub fn new() -> Self {
47+
Self {
48+
executor: ExtensionsExecutor::new(InvokeSource::RuntimeAPI),
49+
}
50+
}
51+
52+
pub fn prepare_input_data(program_path: &str) -> Vec<u8> {
53+
let mut input_data = Vec::new();
54+
55+
if program_path.contains("sum-balance") {
56+
input_data.extend_from_slice(&0u32.encode());
57+
input_data.extend_from_slice(&vec![[0u8; 32], [1u8; 32]].encode());
58+
} else if program_path.contains("total-supply") {
59+
input_data.extend_from_slice(&0u32.encode());
60+
} else if program_path.contains("transparent-call") {
61+
input_data.extend_from_slice(&4071833530116166512u64.encode());
62+
input_data.extend_from_slice(
63+
&ExtensionFungiblesFunctions::balance {
64+
asset: 0,
65+
who: [1u8; 32],
66+
}
67+
.encode(),
68+
);
69+
}
70+
71+
input_data
72+
}
73+
74+
pub fn execute_program(
75+
&mut self,
76+
program_blob: &[u8],
77+
input_data: &[u8],
78+
) -> Result<Vec<u8>, pvq_primitives::PvqError> {
79+
self.executor.execute_method(program_blob, input_data, 0)
80+
}
81+
}
82+
83+
impl Default for TestRunner {
84+
fn default() -> Self {
85+
Self::new()
86+
}
87+
}

pvq-test-runner/src/main.rs

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use clap::Parser;
2-
use parity_scale_codec::Encode;
3-
use pvq_extension::{extensions_impl, ExtensionsExecutor, InvokeSource};
42
use tracing_subscriber::prelude::*;
53

4+
use pvq_test_runner::TestRunner;
5+
66
#[derive(Parser, Debug)]
77
#[command(version, about)]
88
struct Cli {
@@ -26,63 +26,13 @@ fn main() {
2626
let cli = Cli::parse();
2727

2828
let blob = std::fs::read(&cli.program).expect("Failed to read program");
29-
let mut executor = ExtensionsExecutor::<extensions::Extensions, ()>::new(InvokeSource::RuntimeAPI);
30-
let mut input_data = Vec::new();
3129
let program_str = cli.program.to_string_lossy();
32-
if program_str.contains("sum-balance") {
33-
input_data.extend_from_slice(&0u32.encode());
34-
input_data.extend_from_slice(&vec![[0u8; 32], [1u8; 32]].encode());
35-
} else if program_str.contains("total-supply") {
36-
input_data.extend_from_slice(&0u32.encode());
37-
} else if program_str.contains("transparent-call") {
38-
input_data.extend_from_slice(&4071833530116166512u64.encode());
39-
input_data.extend_from_slice(
40-
&ExtensionFungiblesFunctions::balance {
41-
asset: 0,
42-
who: [1u8; 32],
43-
}
44-
.encode(),
45-
);
46-
}
47-
tracing::info!("Input data: {:?}", input_data);
48-
let res = executor.execute_method(&blob, &input_data, 0).unwrap();
49-
50-
tracing::info!("Result: {:?}", res);
51-
}
5230

53-
#[derive(Encode)]
54-
#[allow(non_camel_case_types)]
55-
#[allow(dead_code)]
56-
enum ExtensionFungiblesFunctions {
57-
#[codec(index = 0)]
58-
total_supply { asset: u32 },
59-
#[codec(index = 1)]
60-
balance { asset: u32, who: [u8; 32] },
61-
}
62-
63-
#[extensions_impl]
64-
pub mod extensions {
65-
#[extensions_impl::impl_struct]
66-
pub struct ExtensionsImpl;
31+
let input_data = TestRunner::prepare_input_data(&program_str);
32+
tracing::info!("Input data: {:?}", input_data);
6733

68-
#[extensions_impl::extension]
69-
impl pvq_extension_core::extension::ExtensionCore for ExtensionsImpl {
70-
type ExtensionId = u64;
71-
fn has_extension(id: Self::ExtensionId) -> bool {
72-
matches!(id, 0 | 1)
73-
}
74-
}
34+
let mut runner = TestRunner::new();
35+
let res = runner.execute_program(&blob, &input_data).unwrap();
7536

76-
#[extensions_impl::extension]
77-
impl pvq_extension_fungibles::extension::ExtensionFungibles for ExtensionsImpl {
78-
type AssetId = u32;
79-
type AccountId = [u8; 32];
80-
type Balance = u64;
81-
fn total_supply(_asset: Self::AssetId) -> Self::Balance {
82-
100
83-
}
84-
fn balance(_asset: Self::AssetId, _who: Self::AccountId) -> Self::Balance {
85-
100
86-
}
87-
}
37+
tracing::info!("Result: {:?}", res);
8838
}

0 commit comments

Comments
 (0)