Skip to content

Commit

Permalink
Cleanups after Verifier trait overhaul (#200)
Browse files Browse the repository at this point in the history
* Properly encode strongly typed redeemers

* Persist block number in storage
  • Loading branch information
JoshOrndorff authored Apr 5, 2024
1 parent 16906d1 commit 2a241a4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion tuxedo-core/aggregator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub fn tuxedo_verifier(_: TokenStream, body: TokenStream) -> TokenStream {
/// It is a combined redeemer type for the redeemers of each individual verifier.
///
/// This type is accessible downstream as `<OuterVerifier as Verifier>::Redeemer`
#[derive(Debug, Decode)]
#[derive(Debug, Encode, Decode)]
#vis enum #redeemer_type {
#(
#variants(<#inner_types as tuxedo_core::Verifier>::Redeemer),
Expand Down
20 changes: 13 additions & 7 deletions tuxedo-core/src/executive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
types::{DispatchResult, OutputRef, RedemptionStrategy, Transaction, UtxoError},
utxo_set::TransparentUtxoSet,
verifier::Verifier,
EXTRINSIC_KEY, HEADER_KEY, LOG_TARGET,
EXTRINSIC_KEY, HEADER_KEY, HEIGHT_KEY, LOG_TARGET,
};
use log::debug;
use parity_scale_codec::{Decode, Encode};
Expand Down Expand Up @@ -239,10 +239,9 @@ where
where
B::Header: HeaderT,
{
*sp_io::storage::get(HEADER_KEY)
.and_then(|d| B::Header::decode(&mut &*d).ok())
.expect("A header is always stored at the beginning of the block")
.number()
sp_io::storage::get(HEIGHT_KEY)
.and_then(|d| <<B as BlockT>::Header as HeaderT>::Number::decode(&mut &*d).ok())
.expect("A header is stored at the beginning of block one and never cleared.")
}

// These next three methods are for the block authoring workflow.
Expand All @@ -257,6 +256,10 @@ where
// Store the transient partial header for updating at the end of the block.
// This will be removed from storage before the end of the block.
sp_io::storage::set(HEADER_KEY, &header.encode());

// Also store the height persistently so it is available when
// performing pool validations and other off-chain runtime calls.
sp_io::storage::set(HEIGHT_KEY, &header.number().encode());
}

pub fn apply_extrinsic(extrinsic: <B as BlockT>::Extrinsic) -> ApplyExtrinsicResult {
Expand Down Expand Up @@ -436,7 +439,7 @@ where
"Entering `inherent_extrinsics`."
);

// Extract the complete parent block from the inheret data
// Extract the complete parent block from the inherent data
let parent: B = data
.get_data(&PARENT_INHERENT_IDENTIFIER)
.expect("Parent block inherent data should be able to decode.")
Expand Down Expand Up @@ -651,6 +654,9 @@ mod tests {
});
ext.insert(HEADER_KEY.to_vec(), pre_header.encode());

// Write a block height.
ext.insert(HEIGHT_KEY.to_vec(), pre_header.number.encode());

// Write the noted extrinsics
ext.insert(EXTRINSIC_KEY.to_vec(), self.noted_extrinsics.encode());

Expand Down Expand Up @@ -679,7 +685,7 @@ mod tests {
.execute_with(|| {
let input = Input {
output_ref,
redeemer: Default::default(),
redeemer: RedemptionStrategy::Redemption(Vec::new()),
};

let tx = TestTransactionBuilder::default()
Expand Down
6 changes: 5 additions & 1 deletion tuxedo-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const LOG_TARGET: &str = "tuxedo-core";

/// A transient storage key that will hold the partial header while a block is being built.
/// This key is cleared before the end of the block.
const HEADER_KEY: &[u8] = b"header"; // 686561646572
const HEADER_KEY: &[u8] = b"header";

/// A storage key that will store the block height during and after execution.
/// This allows the block number to be available in the runtime even during off-chain api calls.
const HEIGHT_KEY: &[u8] = b"height";

/// A transient storage key that will hold the list of extrinsics that have been applied so far.
/// This key is cleared before the end of the block.
Expand Down
2 changes: 1 addition & 1 deletion tuxedo-core/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use simple_signature::{Sr25519Signature, P2PKH};
/// * An redeemer supplied by the user attempting to spend the input.
pub trait Verifier: Debug + Encode + Decode + Clone {
/// The type that will be supplied to satisfy the verifier and redeem the UTXO.
type Redeemer: Decode;
type Redeemer: Debug + Encode + Decode;

/// Main function in the trait. Does the checks to make sure an output can be spent.
fn verify(&self, simplified_tx: &[u8], block_height: u32, redeemer: &Self::Redeemer) -> bool;
Expand Down
10 changes: 4 additions & 6 deletions wallet/src/keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use parity_scale_codec::Encode;
use sc_keystore::LocalKeystore;
use sp_core::{
crypto::Pair as PairT,
sr25519::{Pair, Public},
sr25519::{Pair, Public, Signature},
H256,
};
use sp_keystore::Keystore;
Expand Down Expand Up @@ -39,12 +39,10 @@ pub fn sign_with(
keystore: &LocalKeystore,
public: &Public,
message: &[u8],
) -> anyhow::Result<Vec<u8>> {
let sig = keystore
) -> anyhow::Result<Signature> {
keystore
.sr25519_sign(KEY_TYPE, public, message)?
.ok_or(anyhow!("Key doesn't exist in keystore"))?;

Ok(sig.encode())
.ok_or(anyhow!("Key doesn't exist in keystore"))
}

/// Insert the private key associated with the given seed into the keystore for later use.
Expand Down
15 changes: 11 additions & 4 deletions wallet/src/money.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use jsonrpsee::{core::client::ClientT, http_client::HttpClient, rpc_params};
use parity_scale_codec::Encode;
use runtime::{
money::{Coin, MoneyConstraintChecker},
OuterConstraintChecker, OuterVerifier,
OuterConstraintChecker, OuterVerifier, OuterVerifierRedeemer,
};
use sc_keystore::LocalKeystore;
use sled::Db;
Expand Down Expand Up @@ -168,16 +168,23 @@ pub async fn spend_coins_helper<Checker: ConstraintChecker + From<OuterConstrain
let redeemer = match utxo.verifier {
OuterVerifier::Sr25519Signature(Sr25519Signature { owner_pubkey }) => {
let public = Public::from_h256(owner_pubkey);
crate::keystore::sign_with(keystore, &public, &stripped_encoded_transaction)?
let signature =
crate::keystore::sign_with(keystore, &public, &stripped_encoded_transaction)?;
OuterVerifierRedeemer::Sr25519Signature(signature)
}
OuterVerifier::UpForGrabs(_) => Vec::new(),
OuterVerifier::UpForGrabs(_) => OuterVerifierRedeemer::UpForGrabs(()),
OuterVerifier::ThresholdMultiSignature(_) => todo!(),
};

// insert the proof
input.redeemer = RedemptionStrategy::Redemption(redeemer);
let encoded_redeemer = redeemer.encode();
log::debug!("encoded redeemer is: {:?}", encoded_redeemer);

input.redeemer = RedemptionStrategy::Redemption(encoded_redeemer);
}

log::debug!("signed transactions is: {:#?}", transaction);

// Send the transaction
let genesis_spend_hex = hex::encode(transaction.encode());
let params = rpc_params![genesis_spend_hex];
Expand Down

0 comments on commit 2a241a4

Please sign in to comment.