Skip to content

Commit

Permalink
chore: Address PR comments and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbencin committed Mar 26, 2024
1 parent 80a3565 commit 78b05a6
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 93 deletions.
2 changes: 1 addition & 1 deletion libsigner/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ mod test {
use blockstack_lib::util_lib::strings::StacksString;
use rand::Rng;
use rand_core::OsRng;
use stacks_common::consts::CHAIN_ID_TESTNET;
use stacks_common::consts::{CHAIN_ID_TESTNET, SIGNER_SLOTS_PER_USER};
use stacks_common::types::chainstate::StacksPrivateKey;
use wsts::common::Signature;

Expand Down
5 changes: 4 additions & 1 deletion stackslib/src/chainstate/nakamoto/staging_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ impl<'a> NakamotoStagingBlocksConnRef<'a> {
let Some(block_bytes) = data else {
return Ok(None);
};
let block = NakamotoBlock::consensus_deserialize(&mut block_bytes.as_slice())?;
let block = NakamotoBlock::consensus_deserialize_with_epoch(
&mut block_bytes.as_slice(),
StacksEpochId::latest(),
)?;
if &block.header.consensus_hash != consensus_hash {
error!(
"Staging DB corruption: expected {}, got {}",
Expand Down
7 changes: 5 additions & 2 deletions stackslib/src/chainstate/nakamoto/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use rand::{thread_rng, RngCore};
use rusqlite::{Connection, ToSql};
use stacks_common::address::AddressHashMode;
use stacks_common::bitvec::BitVec;
use stacks_common::codec::StacksMessageCodec;
use stacks_common::codec::{DeserializeWithEpoch, StacksMessageCodec};
use stacks_common::consts::{
CHAIN_ID_MAINNET, CHAIN_ID_TESTNET, FIRST_BURNCHAIN_CONSENSUS_HASH, FIRST_STACKS_BLOCK_HASH,
};
Expand Down Expand Up @@ -101,7 +101,10 @@ impl<'a> NakamotoStagingBlocksConnRef<'a> {
let block_data: Vec<Vec<u8>> = query_rows(self, qry, args)?;
let mut blocks = Vec::with_capacity(block_data.len());
for data in block_data.into_iter() {
let block = NakamotoBlock::consensus_deserialize(&mut data.as_slice())?;
let block = NakamotoBlock::consensus_deserialize_with_epoch(
&mut data.as_slice(),
StacksEpochId::latest(),
)?;
blocks.push(block);
}
Ok(blocks)
Expand Down
19 changes: 6 additions & 13 deletions stackslib/src/chainstate/stacks/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ impl OrderIndependentMultisigSpendingCondition {
cond_code: &TransactionAuthFlags,
) -> Result<Txid, net_error> {
let mut pubkeys = vec![];
let cur_sighash = initial_sighash.clone();
let mut num_sigs: u16 = 0;
let mut have_uncompressed = false;
for field in self.fields.iter() {
Expand All @@ -466,7 +465,7 @@ impl OrderIndependentMultisigSpendingCondition {
}

let (pubkey, _next_sighash) = TransactionSpendingCondition::next_verification(
&cur_sighash,
&initial_sighash,
cond_code,
self.tx_fee,
self.nonce,
Expand Down Expand Up @@ -515,7 +514,7 @@ impl OrderIndependentMultisigSpendingCondition {
)));
}

Ok(cur_sighash)
Ok(initial_sighash.clone())
}
}

Expand Down Expand Up @@ -1209,26 +1208,20 @@ impl TransactionAuth {
privks: &[StacksPrivateKey],
num_sigs: u16,
) -> Option<TransactionAuth> {
let mut pubks = vec![];
for privk in privks.iter() {
pubks.push(StacksPublicKey::from_private(privk));
}
let pubks = privks.iter().map(StacksPublicKey::from_private).collect();

TransactionSpendingCondition::new_multisig_order_independent_p2sh(num_sigs, pubks)
.map(|auth| TransactionAuth::Standard(auth))
.map(TransactionAuth::Standard)
}

pub fn from_order_independent_p2wsh(
privks: &[StacksPrivateKey],
num_sigs: u16,
) -> Option<TransactionAuth> {
let mut pubks = vec![];
for privk in privks.iter() {
pubks.push(StacksPublicKey::from_private(privk));
}
let pubks = privks.iter().map(StacksPublicKey::from_private).collect();

TransactionSpendingCondition::new_multisig_order_independent_p2wsh(num_sigs, pubks)
.map(|auth| TransactionAuth::Standard(auth))
.map(TransactionAuth::Standard)
}

pub fn from_p2wpkh(privk: &StacksPrivateKey) -> Option<TransactionAuth> {
Expand Down
44 changes: 20 additions & 24 deletions stackslib/src/chainstate/stacks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ use crate::chainstate::stacks::{Error, StacksBlockHeader, StacksMicroblockHeader
use crate::core::*;
use crate::net::Error as net_error;

/// Quietable error
macro_rules! qerror {
($cond: expr, $($arg:tt)*) => ({
if ($cond) {
error!($($arg)*);
}
});
}

impl StacksMessageCodec for StacksBlockHeader {
fn consensus_serialize<W: Write>(&self, fd: &mut W) -> Result<(), codec_error> {
write_next(fd, &self.version)?;
Expand Down Expand Up @@ -583,40 +592,33 @@ impl StacksBlock {
if let TransactionPayload::Coinbase(_, ref recipient_opt, ref proof_opt) = &tx.payload {
if proof_opt.is_some() && epoch_id < StacksEpochId::Epoch30 {
// not supported
if !quiet {
error!("Coinbase with VRF proof not supported before Stacks 3.0"; "txid" => %tx.txid());
}
qerror!(quiet, "Coinbase with VRF proof not supported before Stacks 3.0"; "txid" => %tx.txid());
return false;
}
if proof_opt.is_none() && epoch_id >= StacksEpochId::Epoch30 {
// not supported
if !quiet {
error!("Coinbase with VRF proof is required in Stacks 3.0 and later"; "txid" => %tx.txid());
}
qerror!(quiet, "Coinbase with VRF proof is required in Stacks 3.0 and later"; "txid" => %tx.txid());
return false;
}
if recipient_opt.is_some() && epoch_id < StacksEpochId::Epoch21 {
// not supported
if !quiet {
error!("Coinbase pay-to-alt-recipient not supported before Stacks 2.1"; "txid" => %tx.txid());
}
qerror!(quiet, "Coinbase pay-to-alt-recipient not supported before Stacks 2.1"; "txid" => %tx.txid());
return false;
}
}
if let TransactionPayload::SmartContract(_, ref version_opt) = &tx.payload {
if version_opt.is_some() && epoch_id < StacksEpochId::Epoch21 {
// not supported
if !quiet {
error!("Versioned smart contracts not supported before Stacks 2.1");
}
qerror!(
quiet,
"Versioned smart contracts not supported before Stacks 2.1"
);
return false;
}
}
if let TransactionPayload::TenureChange(..) = &tx.payload {
if epoch_id < StacksEpochId::Epoch30 {
if !quiet {
error!("TenureChange transaction not supported before Stacks 3.0"; "txid" => %tx.txid());
}
qerror!(quiet, "TenureChange transaction not supported before Stacks 3.0"; "txid" => %tx.txid());
return false;
}
}
Expand All @@ -625,9 +627,7 @@ impl StacksBlock {
match origin {
TransactionSpendingCondition::OrderIndependentMultisig(..) => {
if epoch_id < StacksEpochId::Epoch30 {
if !quiet {
error!("Order independent multisig transactions not supported before Stacks 3.0");
}
qerror!(quiet, "Order independent multisig transactions not supported before Stacks 3.0");
return false;
}
}
Expand All @@ -636,9 +636,7 @@ impl StacksBlock {
match sponsor {
TransactionSpendingCondition::OrderIndependentMultisig(..) => {
if epoch_id < StacksEpochId::Epoch30 {
if !quiet {
error!("Order independent multisig transactions not supported before Stacks 3.0");
}
qerror!(quiet, "Order independent multisig transactions not supported before Stacks 3.0");
return false;
}
}
Expand All @@ -648,9 +646,7 @@ impl StacksBlock {
TransactionAuth::Standard(ref origin) => match origin {
TransactionSpendingCondition::OrderIndependentMultisig(..) => {
if epoch_id < StacksEpochId::Epoch30 {
if !quiet {
error!("Order independent multisig transactions not supported before Stacks 3.0");
}
qerror!(quiet, "Order independent multisig transactions not supported before Stacks 3.0");
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion stackslib/src/chainstate/stacks/db/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6719,7 +6719,7 @@ impl StacksChainState {
let epoch = clarity_connection.get_epoch().clone();

let is_transaction_valid_in_epoch =
StacksBlock::validate_transactions_static_epoch(&vec![tx.clone()], epoch, true);
StacksBlock::validate_transactions_static_epoch(&[tx.clone()], epoch, true);

if !is_transaction_valid_in_epoch {
return Err(MemPoolRejection::Other(
Expand Down
63 changes: 23 additions & 40 deletions stackslib/src/chainstate/stacks/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ impl StacksTransaction {
payload,
};

if !StacksBlock::validate_transactions_static_epoch(&vec![tx.clone()], epoch_id, false) {
if !StacksBlock::validate_transactions_static_epoch(&[tx.clone()], epoch_id, false) {
warn!("Invalid tx: target epoch is not activated");
return Err(codec_error::DeserializeError(
"Failed to parse transaction: target epoch is not activated".to_string(),
Expand Down Expand Up @@ -973,7 +973,8 @@ impl StacksTransaction {
Ok(next_sighash)
}

pub fn sign_no_append_origin(
#[cfg(any(test, feature = "testing"))]
fn sign_no_append_origin(
&self,
cur_sighash: &Txid,
privk: &StacksPrivateKey,
Expand All @@ -994,7 +995,8 @@ impl StacksTransaction {
Ok(next_sig)
}

pub fn append_origin_signature(
#[cfg(any(test, feature = "testing"))]
fn append_origin_signature(
&mut self,
signature: MessageSignature,
key_encoding: TransactionPublicKeyEncoding,
Expand All @@ -1016,7 +1018,8 @@ impl StacksTransaction {
Ok(())
}

pub fn sign_no_append_sponsor(
#[cfg(any(test, feature = "testing"))]
fn sign_no_append_sponsor(
&mut self,
cur_sighash: &Txid,
privk: &StacksPrivateKey,
Expand Down Expand Up @@ -1496,30 +1499,20 @@ mod test {
TransactionSpendingCondition::Multisig(ref data) => {
let mut j = 0;
for f in 0..data.fields.len() {
match data.fields[f] {
TransactionAuthField::Signature(_, _) => {
j = f;
break;
}
_ => {
continue;
}
}
if matches!(data.fields[f], TransactionAuthField::Signature(..)) {
j = f;
break;
};
}
j
}
TransactionSpendingCondition::OrderIndependentMultisig(ref data) => {
let mut j = 0;
for f in 0..data.fields.len() {
match data.fields[f] {
TransactionAuthField::Signature(_, _) => {
j = f;
break;
}
_ => {
continue;
}
}
if matches!(data.fields[f], TransactionAuthField::Signature(..)) {
j = f;
break;
};
}
j
}
Expand All @@ -1532,30 +1525,20 @@ mod test {
TransactionSpendingCondition::Multisig(ref data) => {
let mut j = 0;
for f in 0..data.fields.len() {
match data.fields[f] {
TransactionAuthField::PublicKey(_) => {
j = f;
break;
}
_ => {
continue;
}
}
if matches!(data.fields[f], TransactionAuthField::PublicKey(_)) {
j = f;
break;
};
}
j
}
TransactionSpendingCondition::OrderIndependentMultisig(ref data) => {
let mut j = 0;
for f in 0..data.fields.len() {
match data.fields[f] {
TransactionAuthField::PublicKey(_) => {
j = f;
break;
}
_ => {
continue;
}
}
if matches!(data.fields[f], TransactionAuthField::PublicKey(_)) {
j = f;
break;
};
}
j
}
Expand Down
6 changes: 5 additions & 1 deletion stackslib/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,6 @@ simulating a miner.
let events_file = &argv[3];
let mine_tip_height: u64 = argv[4].parse().expect("Could not parse mine_tip_height");
let mine_max_txns: u64 = argv[5].parse().expect("Could not parse mine-num-txns");
let epoch_id = parse_input_epoch(6);

let sort_db = SortitionDB::open(&sort_db_path, false, PoxConstants::mainnet_default())
.unwrap_or_else(|_| panic!("Failed to open {sort_db_path}"));
Expand All @@ -1368,6 +1367,11 @@ simulating a miner.
let estimator = Box::new(UnitEstimator);
let metric = Box::new(UnitMetric);

let epoch_id = SortitionDB::get_stacks_epoch(&sort_db.conn(), chain_tip.block_height)
.expect("Error getting Epoch")
.expect("No Epoch found")
.epoch_id;

let mut mempool_db = MemPoolDB::open(true, chain_id, &chain_state_path, estimator, metric)
.expect("Failed to open mempool db");

Expand Down
8 changes: 6 additions & 2 deletions stackslib/src/net/api/getblock_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ use std::{fs, io};
use regex::{Captures, Regex};
use rusqlite::Connection;
use serde::de::Error as de_Error;
use stacks_common::codec::{StacksMessageCodec, MAX_MESSAGE_LEN};
use stacks_common::codec::{DeserializeWithEpoch, StacksMessageCodec, MAX_MESSAGE_LEN};
use stacks_common::types::chainstate::{ConsensusHash, StacksBlockId};
use stacks_common::types::net::PeerHost;
use stacks_common::types::StacksEpochId;
use stacks_common::util::hash::to_hex;
use {serde, serde_json};

Expand Down Expand Up @@ -317,7 +318,10 @@ impl StacksHttpResponse {

// contents will be raw bytes
let block_bytes: Vec<u8> = contents.try_into()?;
let block = NakamotoBlock::consensus_deserialize(&mut &block_bytes[..])?;
let block = NakamotoBlock::consensus_deserialize_with_epoch(
&mut &block_bytes[..],
StacksEpochId::latest(),
)?;

Ok(block)
}
Expand Down
6 changes: 4 additions & 2 deletions stackslib/src/net/api/gettenure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ use std::{fs, io};

use regex::{Captures, Regex};
use serde::de::Error as de_Error;
use stacks_common::codec::{StacksMessageCodec, MAX_MESSAGE_LEN};
use stacks_common::codec::{DeserializeWithEpoch, StacksMessageCodec, MAX_MESSAGE_LEN};
use stacks_common::types::chainstate::{ConsensusHash, StacksBlockId};
use stacks_common::types::net::PeerHost;
use stacks_common::types::StacksEpochId;
use stacks_common::util::hash::to_hex;
use {serde, serde_json};

Expand Down Expand Up @@ -355,7 +356,8 @@ impl StacksHttpResponse {

let mut blocks = vec![];
while ptr.len() > 0 {
let block = NakamotoBlock::consensus_deserialize(ptr)?;
let block =
NakamotoBlock::consensus_deserialize_with_epoch(ptr, StacksEpochId::latest())?;
blocks.push(block);
}

Expand Down
Loading

0 comments on commit 78b05a6

Please sign in to comment.