From 7164f7aade580f518c0f6ee68f6347f83e28c856 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Wed, 18 Dec 2024 10:41:52 +0100 Subject: [PATCH] fix PrefabParams with using assignment --- src/mound.rs | 2 ++ src/popls/bp.rs | 57 +++++++++++++++++++++++++++++------------------- src/stockpile.rs | 4 ++-- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/mound.rs b/src/mound.rs index 7fc417ee..0ced4978 100644 --- a/src/mound.rs +++ b/src/mound.rs @@ -40,9 +40,11 @@ pub trait Excavate, P: Pile, const CAPS: u32> { fn contracts(&mut self) -> impl Iterator)>; } +/// Mound is a collection of smart contracts which have homogenous capabilities. pub struct Mound, P: Pile, X: Excavate, const CAPS: u32> { schemata: BTreeMap, contracts: BTreeMap>, + /// Persistence does loading of a stockpiles and their storage when a new contract is added. persistence: X, } diff --git a/src/popls/bp.rs b/src/popls/bp.rs index 832f38ba..09a17523 100644 --- a/src/popls/bp.rs +++ b/src/popls/bp.rs @@ -46,6 +46,7 @@ use strict_types::StrictVal; use crate::stockpile::EitherSeal; use crate::{Assignment, CreateParams, Excavate, Mound, Pile}; +/// Trait abstracting specific implementation of a bitcoin wallet. pub trait WalletProvider { fn noise_seed(&self) -> Bytes32; fn utxos(&self) -> impl Iterator; @@ -56,7 +57,7 @@ pub trait TapretProvider: WalletProvider {} pub const BP_BLANK_METHOD: &str = "_"; // TODO: Support failback seals -#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)] +#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display)] #[cfg_attr( feature = "serde", derive(Serialize, Deserialize), @@ -67,7 +68,7 @@ pub enum BuilderSeal { Oneself(Vout), #[display("{0}")] - Extern(Outpoint), + Extern(AuthToken), } impl EitherSeal { @@ -118,19 +119,27 @@ impl CreateParams { } } +#[derive(Clone, Debug)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))] +pub struct UsedState { + pub addr: CellAddr, + pub outpoint: Outpoint, + pub val: StrictVal, +} + /// Parameters used by BP-based wallet for constructing operations. /// /// Differs from [`CallParams`] in the fact that it uses [`BuilderSeal`]s instead of /// [`hypersonic::AuthTokens`] for output definitions. #[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))] -pub struct ConstructParams { +pub struct PrefabParams { pub contract_id: ContractId, pub method: MethodName, pub reading: Vec, - pub using: Vec<(CellAddr, Outpoint, StrictVal)>, + pub using: Vec, pub global: Vec>, - pub owned: Vec<(BuilderSeal, NamedState)>, + pub owned: Vec>>, } /// Prefabricated operation, which includes information on the contract id and closed seals @@ -234,12 +243,12 @@ impl< } /// Creates a single operation basing on the provided construction parameters. - pub fn prefab(&mut self, params: ConstructParams) -> Prefab { + pub fn prefab(&mut self, params: PrefabParams) -> Prefab { // convert ConstructParams into CallParams let (closes, using) = params .using .into_iter() - .map(|(auth, outpoint, val)| (outpoint, (auth, val))) + .map(|used| (used.outpoint, (used.addr, used.val))) .unzip(); let closes = SmallOrdSet::try_from(closes).expect("too many inputs"); let mut defines = SmallOrdSet::new(); @@ -250,8 +259,8 @@ impl< .owned .into_iter() .enumerate() - .map(|(nonce, (seal, val))| { - let seal = match seal { + .map(|(nonce, assignment)| { + let auth = match assignment.state.seal { BuilderSeal::Oneself(vout) => { defines.push(vout).expect("too many seals"); // NB: We use opret type here, but this doesn't matter since we create seal @@ -261,18 +270,17 @@ impl< noise_engine.clone(), nonce as u64, ) + .auth_token() } - BuilderSeal::Extern(outpoint) => { - TxoSeal::no_fallback(outpoint, noise_engine.clone(), nonce as u64) - } + BuilderSeal::Extern(auth) => auth, }; let state = DataCell { - data: val.state, - auth: seal.auth_token(), + data: assignment.state.data, + auth, lock: None, }; NamedState { - name: val.name, + name: assignment.name, state, } }) @@ -345,7 +353,12 @@ impl< == auth }) .map(|(_, outpoint)| { - ((addr, outpoint, StrictVal::Unit), (name.clone(), val)) + let prevout = UsedState { + addr, + outpoint, + val: StrictVal::Unit, + }; + (prevout, (name.clone(), val)) }) }) .unzip(); @@ -361,16 +374,16 @@ impl< let mut owned = Vec::new(); for (name, calc) in calcs { - for state in calc.diff().expect("non-computable state") { + for data in calc.diff().expect("non-computable state") { let state = NamedState { name: name.clone(), - state, + state: Assignment { seal, data }, }; - owned.push((seal, state)); + owned.push(state); } } - let params = ConstructParams { + let params = PrefabParams { contract_id, method: MethodName::from(BP_BLANK_METHOD), global: none!(), @@ -654,7 +667,7 @@ pub mod file { } } - pub fn prefab(&mut self, params: ConstructParams) -> Prefab { + pub fn prefab(&mut self, params: PrefabParams) -> Prefab { match self { #[cfg(feature = "bitcoin")] Self::BcOpret(barrow) => barrow.prefab(params), @@ -667,7 +680,7 @@ pub mod file { } } - pub fn bundle(&mut self, items: impl IntoIterator) -> PrefabBundle { + pub fn bundle(&mut self, items: impl IntoIterator) -> PrefabBundle { let iter = items.into_iter().map(|params| self.prefab(params)); let items = SmallOrdSet::try_from_iter(iter).expect("too large script"); PrefabBundle(items) diff --git a/src/stockpile.rs b/src/stockpile.rs index 6cbfee7d..4ec409ca 100644 --- a/src/stockpile.rs +++ b/src/stockpile.rs @@ -89,7 +89,7 @@ impl EitherSeal { ) )] pub struct Assignment { - pub seal: EitherSeal, + pub seal: Seal, pub data: StrictVal, } @@ -113,7 +113,7 @@ pub struct CreateParams { pub name: TypeName, pub timestamp: Option>, pub global: Vec>, - pub owned: Vec>>, + pub owned: Vec>>>, } #[derive(Getters)]