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

2. change(rpc): Add some transaction fields to the getblocktemplate RPC #5496

Merged
merged 26 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4b9d9c2
Add documentation for the getblocktemplate RPC
teor2345 Oct 28, 2022
ac7f03b
Add a new mempool::Request::Transactions
teor2345 Oct 28, 2022
d0cc321
Add conversions from Vec<UnminedTx> to merkle::Root and AuthDataRoot
teor2345 Oct 28, 2022
daf5c18
Fill in the merkle root and auth data root fields
teor2345 Oct 28, 2022
377df8d
Delete the Coinbase type, it's the same as Transaction
teor2345 Oct 28, 2022
ae38114
Fill in some other existing types
teor2345 Oct 28, 2022
0b65145
Add Hex serialization support to some zebra-chain types
teor2345 Oct 28, 2022
13eff5c
Add TransactionTemplate fields and fill some in
teor2345 Oct 28, 2022
cdc0fb8
Fix test hangs by spawning async tasks
teor2345 Oct 28, 2022
b16d019
Add temporary workaround for no transactions in the block
teor2345 Oct 28, 2022
3d63ab8
Encode hashes and roots as hex
teor2345 Oct 28, 2022
747798f
Update RPC snapshots
teor2345 Oct 28, 2022
498af2d
Add a missing Request::Transactions handler
teor2345 Oct 28, 2022
5b41ff2
Fix doc warnings
teor2345 Oct 28, 2022
09ae3d4
Fix fee serialization
teor2345 Oct 28, 2022
72c984e
Update snapshots for serialization changes
teor2345 Oct 30, 2022
4f5ce34
Add a missing Cargo.lock change
teor2345 Oct 30, 2022
e462ac4
Change depends type
teor2345 Oct 31, 2022
5f3d491
Merge branch 'main' into getblocktemplate-transactions
mergify[bot] Nov 1, 2022
44fbe33
Remove duplicate feature entry
teor2345 Nov 1, 2022
6d902f7
Document the new RPC feature
teor2345 Nov 1, 2022
d1f57bc
Fix a comment typo
teor2345 Nov 2, 2022
1c4672c
Update default roots docs
teor2345 Nov 2, 2022
2edd69a
Fix comment typo
teor2345 Nov 2, 2022
c55de09
Merge branch 'main' into getblocktemplate-transactions
mergify[bot] Nov 2, 2022
9929d84
Fix a comment typo
teor2345 Nov 2, 2022
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
24 changes: 22 additions & 2 deletions zebra-chain/src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

use std::{
cmp::Ordering,
convert::{TryFrom, TryInto},
hash::{Hash, Hasher},
marker::PhantomData,
ops::RangeInclusive,
Expand All @@ -28,7 +27,8 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
/// A runtime validated type for representing amounts of zatoshis
#[derive(Clone, Copy, Serialize, Deserialize)]
#[serde(try_from = "i64")]
#[serde(bound = "C: Constraint")]
#[serde(into = "i64")]
#[serde(bound = "C: Constraint + Clone")]
pub struct Amount<C = NegativeAllowed>(
/// The inner amount value.
i64,
Expand Down Expand Up @@ -498,6 +498,26 @@ impl Constraint for NonNegative {
}
}

/// Marker type for `Amount` that requires negative or zero values.
///
/// Used for coinbase transactions in `getblocktemplate` RPCs.
///
/// ```
/// # use zebra_chain::amount::{Constraint, MAX_MONEY, NegativeOrZero};
/// assert_eq!(
/// NegativeOrZero::valid_range(),
/// -MAX_MONEY..=0,
/// );
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct NegativeOrZero;

impl Constraint for NegativeOrZero {
fn valid_range() -> RangeInclusive<i64> {
-MAX_MONEY..=0
}
}

/// Number of zatoshis in 1 ZEC
pub const COIN: i64 = 100_000_000;

Expand Down
111 changes: 111 additions & 0 deletions zebra-chain/src/block/commitment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The Commitment enum, used for the corresponding block header field.

use hex::{FromHex, ToHex};
use thiserror::Error;

use crate::{
Expand Down Expand Up @@ -159,6 +160,62 @@ impl From<ChainHistoryMmrRootHash> for [u8; 32] {
}
}

impl ChainHistoryMmrRootHash {
/// Return the hash bytes in big-endian byte-order suitable for printing out byte by byte.
///
/// Zebra displays transaction and block hashes in big-endian byte-order,
/// following the u256 convention set by Bitcoin and zcashd.
pub fn bytes_in_display_order(&self) -> [u8; 32] {
let mut reversed_bytes = self.0;
reversed_bytes.reverse();
reversed_bytes
}

/// Convert bytes in big-endian byte-order into a `ChainHistoryMmrRootHash`.
///
/// Zebra displays transaction and block hashes in big-endian byte-order,
/// following the u256 convention set by Bitcoin and zcashd.
pub fn from_bytes_in_display_order(
bytes_in_display_order: &[u8; 32],
) -> ChainHistoryMmrRootHash {
let mut internal_byte_order = *bytes_in_display_order;
internal_byte_order.reverse();

ChainHistoryMmrRootHash(internal_byte_order)
}
}

impl ToHex for &ChainHistoryMmrRootHash {
fn encode_hex<T: FromIterator<char>>(&self) -> T {
self.bytes_in_display_order().encode_hex()
}

fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
self.bytes_in_display_order().encode_hex_upper()
}
}

impl ToHex for ChainHistoryMmrRootHash {
fn encode_hex<T: FromIterator<char>>(&self) -> T {
(&self).encode_hex()
}

fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
(&self).encode_hex_upper()
}
}

impl FromHex for ChainHistoryMmrRootHash {
type Error = <[u8; 32] as FromHex>::Error;

fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
let mut hash = <[u8; 32]>::from_hex(hex)?;
hash.reverse();

Ok(hash.into())
}
}

/// A block commitment to chain history and transaction auth.
/// - the chain history tree for all ancestors in the current network upgrade,
/// and
Expand Down Expand Up @@ -212,6 +269,60 @@ impl ChainHistoryBlockTxAuthCommitmentHash {
.expect("32 byte array");
Self(hash_block_commitments)
}

/// Return the hash bytes in big-endian byte-order suitable for printing out byte by byte.
///
/// Zebra displays transaction and block hashes in big-endian byte-order,
/// following the u256 convention set by Bitcoin and zcashd.
pub fn bytes_in_display_order(&self) -> [u8; 32] {
let mut reversed_bytes = self.0;
reversed_bytes.reverse();
reversed_bytes
}

/// Convert bytes in big-endian byte-order into a `ChainHistoryBlockTxAuthCommitmentHash`.
///
/// Zebra displays transaction and block hashes in big-endian byte-order,
/// following the u256 convention set by Bitcoin and zcashd.
pub fn from_bytes_in_display_order(
bytes_in_display_order: &[u8; 32],
) -> ChainHistoryBlockTxAuthCommitmentHash {
let mut internal_byte_order = *bytes_in_display_order;
internal_byte_order.reverse();

ChainHistoryBlockTxAuthCommitmentHash(internal_byte_order)
}
}

impl ToHex for &ChainHistoryBlockTxAuthCommitmentHash {
fn encode_hex<T: FromIterator<char>>(&self) -> T {
self.bytes_in_display_order().encode_hex()
}

fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
self.bytes_in_display_order().encode_hex_upper()
}
}

impl ToHex for ChainHistoryBlockTxAuthCommitmentHash {
fn encode_hex<T: FromIterator<char>>(&self) -> T {
(&self).encode_hex()
}

fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
(&self).encode_hex_upper()
}
}

impl FromHex for ChainHistoryBlockTxAuthCommitmentHash {
type Error = <[u8; 32] as FromHex>::Error;

fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
let mut hash = <[u8; 32]>::from_hex(hex)?;
hash.reverse();

Ok(hash.into())
}
}

/// Errors that can occur when checking RootHash consensus rules.
Expand Down
Loading