Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change(log): Log a cute message for blocks that were mined by Zebra (off by default) #6098

Merged
merged 12 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions zebra-chain/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
block::Height,
parameters::{Network, NetworkUpgrade},
transaction::{LockTime, Transaction},
transparent::{self, EXTRA_ZEBRA_COINBASE_DATA},
transparent,
};

impl Transaction {
Expand All @@ -15,6 +15,7 @@ impl Transaction {
network: Network,
height: Height,
outputs: impl IntoIterator<Item = (Amount<NonNegative>, transparent::Script)>,
extra_coinbase_data: Vec<u8>,
) -> Transaction {
// # Consensus
//
Expand All @@ -36,15 +37,15 @@ impl Transaction {
//
// > A coinbase transaction script MUST have length in {2 .. 100} bytes.
//
// Zebra does not add any extra coinbase data.
// Zebra adds extra coinbase data if configured to do so.
//
// Since we're not using a lock time, any sequence number is valid here.
// See `Transaction::lock_time()` for the relevant consensus rules.
//
// <https://zips.z.cash/protocol/protocol.pdf#txnconsensus>
let inputs = vec![transparent::Input::new_coinbase(
height,
Some(EXTRA_ZEBRA_COINBASE_DATA.as_bytes().to_vec()),
Some(extra_coinbase_data),
None,
)];

Expand Down Expand Up @@ -112,20 +113,23 @@ impl Transaction {
height: Height,
outputs: impl IntoIterator<Item = (Amount<NonNegative>, transparent::Script)>,
like_zcashd: bool,
extra_coinbase_data: Vec<u8>,
) -> Transaction {
// `zcashd` includes an extra byte after the coinbase height in the coinbase data,
// and a sequence number of u32::MAX.
let mut extra_data = None;
let mut sequence = None;

// `zcashd` includes an extra byte after the coinbase height in the coinbase data,
// and a sequence number of u32::MAX.
if like_zcashd {
// TODO: add a debug_like_zcashd config and use 0x00 for like_zcashd
// add an arbitrary extra coinbase data config?
//extra_data = Some(vec![0x00]);
extra_data = Some(EXTRA_ZEBRA_COINBASE_DATA.as_bytes().to_vec());
extra_data = Some(vec![0x00]);
sequence = Some(u32::MAX);
}

// Override like_zcashd if extra_coinbase_data was supplied
if !extra_coinbase_data.is_empty() {
extra_data = Some(extra_coinbase_data);
}

// # Consensus
//
// See the other consensus rules above in new_v5_coinbase().
Expand Down
5 changes: 4 additions & 1 deletion zebra-rpc/src/methods/get_block_template_rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,11 @@ where
&self,
parameters: Option<get_block_template::JsonParameters>,
) -> BoxFuture<Result<get_block_template::Response>> {
// Clone Config
// Clone Configs
let network = self.network;
let miner_address = self.miner_address;
let debug_like_zcashd = self.debug_like_zcashd;
let extra_coinbase_data = self.extra_coinbase_data.clone();

// Clone Services
let mempool = self.mempool.clone();
Expand Down Expand Up @@ -660,6 +661,7 @@ where
miner_address,
mempool_txs,
debug_like_zcashd,
extra_coinbase_data.clone(),
)
.await;

Expand All @@ -681,6 +683,7 @@ where
mempool_txs,
submit_old,
debug_like_zcashd,
extra_coinbase_data,
);

Ok(response.into())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,18 @@ pub fn generate_coinbase_and_roots(
mempool_txs: &[VerifiedUnminedTx],
history_tree: Arc<zebra_chain::history_tree::HistoryTree>,
like_zcashd: bool,
extra_coinbase_data: Vec<u8>,
) -> (TransactionTemplate<NegativeOrZero>, DefaultRoots) {
// Generate the coinbase transaction
let miner_fee = calculate_miner_fee(mempool_txs);
let coinbase_txn =
generate_coinbase_transaction(network, height, miner_address, miner_fee, like_zcashd);
let coinbase_txn = generate_coinbase_transaction(
network,
height,
miner_address,
miner_fee,
like_zcashd,
extra_coinbase_data,
);

// Calculate block default roots
//
Expand All @@ -301,13 +308,15 @@ pub fn generate_coinbase_transaction(
miner_address: transparent::Address,
miner_fee: Amount<NonNegative>,
like_zcashd: bool,
extra_coinbase_data: Vec<u8>,
) -> UnminedTx {
let outputs = standard_coinbase_outputs(network, height, miner_address, miner_fee, like_zcashd);

if like_zcashd {
Transaction::new_v4_coinbase(network, height, outputs, like_zcashd).into()
Transaction::new_v4_coinbase(network, height, outputs, like_zcashd, extra_coinbase_data)
.into()
} else {
Transaction::new_v5_coinbase(network, height, outputs).into()
Transaction::new_v5_coinbase(network, height, outputs, extra_coinbase_data).into()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ impl GetBlockTemplate {
///
/// If `like_zcashd` is true, try to match the coinbase transactions generated by `zcashd`
/// in the `getblocktemplate` RPC.
#[allow(clippy::too_many_arguments)]
pub fn new(
network: Network,
miner_address: transparent::Address,
Expand All @@ -190,6 +191,7 @@ impl GetBlockTemplate {
mempool_txs: Vec<VerifiedUnminedTx>,
submit_old: Option<bool>,
like_zcashd: bool,
extra_coinbase_data: Vec<u8>,
) -> Self {
// Calculate the next block height.
let next_block_height =
Expand Down Expand Up @@ -229,6 +231,7 @@ impl GetBlockTemplate {
&mempool_txs,
chain_tip_and_local_time.history_tree.clone(),
like_zcashd,
extra_coinbase_data,
);

// Convert difficulty
Expand Down
21 changes: 17 additions & 4 deletions zebra-rpc/src/methods/get_block_template_rpcs/zip317.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ pub async fn select_mempool_transactions(
miner_address: transparent::Address,
mempool_txs: Vec<VerifiedUnminedTx>,
like_zcashd: bool,
extra_coinbase_data: Vec<u8>,
) -> Vec<VerifiedUnminedTx> {
// Use a fake coinbase transaction to break the dependency between transaction
// selection, the miner fee, and the fee payment in the coinbase transaction.
let fake_coinbase_tx =
fake_coinbase_transaction(network, next_block_height, miner_address, like_zcashd);
let fake_coinbase_tx = fake_coinbase_transaction(
network,
next_block_height,
miner_address,
like_zcashd,
extra_coinbase_data,
);

// Setup the transaction lists.
let (mut conventional_fee_txs, mut low_fee_txs): (Vec<_>, Vec<_>) = mempool_txs
Expand Down Expand Up @@ -117,6 +123,7 @@ pub fn fake_coinbase_transaction(
height: Height,
miner_address: transparent::Address,
like_zcashd: bool,
extra_coinbase_data: Vec<u8>,
) -> TransactionTemplate<NegativeOrZero> {
// Block heights are encoded as variable-length (script) and `u32` (lock time, expiry height).
// They can also change the `u32` consensus branch id.
Expand All @@ -129,8 +136,14 @@ pub fn fake_coinbase_transaction(
// https://developer.bitcoin.org/reference/transactions.html#txout-a-transaction-output
let miner_fee = 1.try_into().expect("amount is valid and non-negative");

let coinbase_tx =
generate_coinbase_transaction(network, height, miner_address, miner_fee, like_zcashd);
let coinbase_tx = generate_coinbase_transaction(
network,
height,
miner_address,
miner_fee,
like_zcashd,
extra_coinbase_data,
);

TransactionTemplate::from_coinbase(&coinbase_tx, miner_fee)
}
Expand Down