Skip to content

Commit

Permalink
refactor: Drop block consensus program TransactionIsValid
Browse files Browse the repository at this point in the history
Use `transaction::SingleProof` instead.
  • Loading branch information
aszepieniec committed Oct 29, 2024
1 parent e6d157f commit 2571131
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 243 deletions.
10 changes: 10 additions & 0 deletions src/models/blockchain/block/block_appendix.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Deref;

use arbitrary::Arbitrary;
use get_size::GetSize;
use serde::Deserialize;
Expand Down Expand Up @@ -36,3 +38,11 @@ impl BlockAppendix {
.collect()
}
}

impl Deref for BlockAppendix {
type Target = Vec<Claim>;

fn deref(&self) -> &Self::Target {
&self.claims
}
}
11 changes: 8 additions & 3 deletions src/models/blockchain/block/block_kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tasm_lib::twenty_first::math::bfield_codec::BFieldCodec;
use super::block_appendix::BlockAppendix;
use super::block_body::BlockBody;
use super::block_header::BlockHeader;
use super::validity::transaction_is_valid::TransactionIsValid;
use crate::models::blockchain::transaction::validity::single_proof::SingleProof;
use crate::models::proof_abstractions::mast_hash::HasDiscriminant;
use crate::models::proof_abstractions::mast_hash::MastHash;
use crate::models::proof_abstractions::tasm::program::ConsensusProgram;
Expand All @@ -26,8 +26,13 @@ pub struct BlockKernel {
impl BlockKernel {
pub(crate) fn new(header: BlockHeader, body: BlockBody) -> Self {
// todo: populate appendix properly
let transaction_is_valid_claim = Claim::new(TransactionIsValid.hash())
.with_input(body.mast_hash().reversed().values().to_vec());
let transaction_is_valid_claim = Claim::new(SingleProof.hash()).with_input(
body.transaction_kernel
.mast_hash()
.reversed()
.values()
.to_vec(),
);
let appendix = BlockAppendix::new(vec![transaction_is_valid_claim]);
Self {
header,
Expand Down
10 changes: 10 additions & 0 deletions src/models/blockchain/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use super::type_scripts::time_lock::TimeLock;
use crate::config_models::network::Network;
use crate::models::blockchain::block::difficulty_control::difficulty_control;
use crate::models::blockchain::shared::Hash;
use crate::models::blockchain::transaction::validity::single_proof::SingleProof;
use crate::models::proof_abstractions::mast_hash::MastHash;
use crate::models::proof_abstractions::timestamp::Timestamp;
use crate::models::state::wallet::address::ReceivingAddress;
Expand Down Expand Up @@ -630,6 +631,15 @@ impl Block {
return false;
}

// 1.a)i: Verify that transaction validity is an appendix claim proven
// by the block proof.
let transaction_is_valid =
SingleProof::claim(block_copy.body().transaction_kernel.mast_hash());
if !block_copy.appendix().contains(&transaction_is_valid) {
warn!("Block proof does not establish transaction validity.");
return false;
}

// 1.b) Check block size
if self.size() > MAX_BLOCK_SIZE {
warn!(
Expand Down
1 change: 0 additions & 1 deletion src/models/blockchain/block/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub mod correct_mmr_update;
pub mod correct_mutator_set_update;
pub mod mmr_membership;
pub mod predecessor_is_valid;
pub mod transaction_is_valid;

/// The validity of a block, in the principal case, decomposes into these subclaims.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, GetSize, BFieldCodec)]
Expand Down
27 changes: 17 additions & 10 deletions src/models/blockchain/block/validity/appendix_witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use tokio::sync::TryLockError;

use super::block_primitive_witness::BlockPrimitiveWitness;
use super::block_program::BlockProgram;
use super::transaction_is_valid::TransactionIsValid;
use super::transaction_is_valid::TransactionIsValidWitness;
use crate::models::blockchain::block::block_body::BlockBody;
use crate::models::blockchain::transaction::validity::single_proof::SingleProof;
use crate::models::blockchain::transaction::TransactionProof;
use crate::models::proof_abstractions::mast_hash::MastHash;
use crate::models::proof_abstractions::tasm::program::ConsensusProgram;
use crate::models::proof_abstractions::tasm::program::TritonProverSync;
Expand Down Expand Up @@ -64,16 +64,23 @@ impl AppendixWitness {
// TODO: Rename when used
pub(crate) async fn _produce(
block_primitive_witness: BlockPrimitiveWitness,
sync_device: &TritonProverSync,
_sync_device: &TritonProverSync,
) -> Result<AppendixWitness, TryLockError> {
let block_mast_hash = block_primitive_witness.body().mast_hash();
let txk_mast_hash = block_primitive_witness
.body()
.transaction_kernel
.mast_hash();

let tx_is_valid_claim = TransactionIsValid::_claim(block_mast_hash);
let tx_is_valid_witness = TransactionIsValidWitness::from(block_primitive_witness.clone());
let tx_is_valid_nondeterminism = tx_is_valid_witness.nondeterminism();
let tx_is_valid_proof = TransactionIsValid
.prove(&tx_is_valid_claim, tx_is_valid_nondeterminism, sync_device)
.await?;
let tx_is_valid_claim = SingleProof::claim(txk_mast_hash);
let tx_is_valid_proof = match &block_primitive_witness.transaction.proof {
TransactionProof::SingleProof(proof) => proof.clone(),
_ => {
panic!(
"can only produce appendix witness from single-proof transaction; got {:?}",
block_primitive_witness.transaction.proof
);
}
};

// todo: add other claims and proofs

Expand Down
8 changes: 2 additions & 6 deletions src/models/blockchain/block/validity/block_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,14 @@ impl BlockProgram {

impl ConsensusProgram for BlockProgram {
fn source(&self) {
let block_body_digest: Digest = tasmlib::tasmlib_io_read_stdin___digest();
let _block_body_digest: Digest = tasmlib::tasmlib_io_read_stdin___digest();
let start_address: BFieldElement = FIRST_NON_DETERMINISTICALLY_INITIALIZED_MEMORY_ADDRESS;
let block_witness: AppendixWitness = tasmlib::decode_from_memory(start_address);
let claims: Vec<Claim> = block_witness.claims;
let proofs: Vec<Proof> = block_witness.proofs;

let mut i = 0;
while i < claims.len() {
assert_eq!(
claims[i].input,
block_body_digest.reversed().values().to_vec()
);
tasmlib::tasmlib_io_write_to_stdout___digest(Tip5::hash(&claims[i]));
verify_stark(Stark::default(), &claims[i], &proofs[i]);

Expand Down Expand Up @@ -207,7 +203,7 @@ pub(crate) mod test {
assert_eq!(rust_output, tasm_output);

let expected_output = appendix_witness
.claims
.claims()
.iter()
.flat_map(|appendix_claim| Tip5::hash(appendix_claim).values().to_vec())
.collect_vec();
Expand Down
223 changes: 0 additions & 223 deletions src/models/blockchain/block/validity/transaction_is_valid.rs

This file was deleted.

0 comments on commit 2571131

Please sign in to comment.