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

feat(core)!: define OutputFlags for side-chain contracts #4088

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl TryFrom<grpc::OutputFeatures> for OutputFeatures {
} else {
Some(PublicKey::from_bytes(features.parent_public_key.as_bytes()).map_err(|err| format!("{:?}", err))?)
};
let flags = u8::try_from(features.flags).map_err(|_| "Invalid output flags: overflowed u8")?;
let flags = u16::try_from(features.flags).map_err(|_| "Invalid output flags: overflowed u8")?;

Ok(OutputFeatures::new(
OutputFeaturesVersion::try_from(
Expand Down
3 changes: 1 addition & 2 deletions base_layer/core/src/blocks/genesis_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub fn get_dibbler_genesis_block() -> ChainBlock {
// hardcode the Merkle roots once they've been computed above
block.header.kernel_mr = from_hex("5b91bebd33e18798e03e9c5d831d161ee9c3d12560f50b987e1a8c3ec53146df").unwrap();
block.header.witness_mr = from_hex("11227f6ce9ff34349d7dcab606b633f55234d5c8a73696a68c6e9ddc7cd3bc40").unwrap();
block.header.output_mr = from_hex("8904e47f6a390417d83d531ee12bcaa9dfbb85f64ed83d4665c2ed26092b3599").unwrap();
block.header.output_mr = from_hex("5e69274e72f8590e1cf91c189e24368527414aed966de62135d9273a6c14c3ef").unwrap();

let accumulated_data = BlockHeaderAccumulatedData {
hash: block.hash(),
Expand Down Expand Up @@ -319,7 +319,6 @@ mod test {
};

#[test]
#[allow(clippy::similar_names)]
fn dibbler_genesis_sanity_check() {
let block = get_dibbler_genesis_block();
assert_eq!(block.block().body.outputs().len(), 4001);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,7 @@ impl UnconfirmedPool {
let mut max_fee_per_gram = MicroTari::zero();
let mut last_count = 0;
for (i, key) in self.tx_by_priority.values().rev().enumerate().skip(offset) {
let tx = self
.tx_by_key
.get(key)
.ok_or_else(|| UnconfirmedPoolError::StorageOutofSync)?;
let tx = self.tx_by_key.get(key).ok_or(UnconfirmedPoolError::StorageOutofSync)?;
let weight = tx.weight;

if total_weight + weight > target_block_weight {
Expand Down Expand Up @@ -1088,7 +1085,7 @@ mod test {
let tx3 = Arc::new(tx3);
let tx4 = Arc::new(tx4);
unconfirmed_pool
.insert_many(vec![tx1.clone(), tx2.clone(), tx3.clone(), tx4.clone()], &tx_weight)
.insert_many(vec![tx1, tx2, tx3, tx4], &tx_weight)
.unwrap();

let stats = unconfirmed_pool.get_fee_per_gram_stats(1, 19500).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/proto/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl TryFrom<proto::types::OutputFeatures> for OutputFeatures {
Some(PublicKey::from_bytes(features.parent_public_key.as_bytes()).map_err(|err| format!("{:?}", err))?)
};

let flags = u8::try_from(features.flags).map_err(|_| "Invalid output flags: overflowed u8")?;
let flags = u16::try_from(features.flags).map_err(|_| "Invalid output flags: overflowed u8")?;

Ok(OutputFeatures::new(
OutputFeaturesVersion::try_from(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,24 @@ use crate::consensus::{ConsensusDecoding, ConsensusEncoding, ConsensusEncodingSi

bitflags! {
#[derive(Deserialize, Serialize)]
pub struct OutputFlags: u8 {
/// Output is a coinbase output, must not be spent until maturity
const COINBASE_OUTPUT = 0b0000_0001;
pub struct OutputFlags: u16 {
/// Output is a coinbase output, must not be spent until maturity.
const COINBASE_OUTPUT = 0x0001;
/// Output defines a side-chain contract.
const CONTRACT_DEFINITION = 0x0100;
/// Output defines the constitution for a side-chain contract.
const CONTRACT_CONSTITUTION = 0x0200;
/// Output signals validator node acceptance to run a contract.
const CONTRACT_ACCEPT = 0x0400;
/// Output is a contract checkpoint.
const CONTRACT_CHECKPOINT = 0x0800;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need an additional flag for the initial checkpoint where all the contract acceptance UTXOs have to be spent into it - a slight nuance on the rules to be enforced.

Copy link
Contributor

@mrnaveira mrnaveira May 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that going to be just the first checkpoint? Or does it need to be a custom output for that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right and we might need an abandoned flag so the asset owner can spend an abandoned checkpoint into that state to activate the emergency keys

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left off the INITIAL_CHECKPOINT flag for now because even if we have it, we'd have to check that it is an INITIAL checkpoint (we can't trust the flag), in doing so we have done the work to detect it.

/// Output that deregisters an existing contract. This MUST be combined with
/// CONTRACT_DEFINITION or CONTRACT_CONSTITUTION.
const CONTRACT_DEREGISTER = 0x1000;
/// Output is an abandoned contract checkpoint.
const CONTRACT_ABANDONED = 0x2000;

// TODO: Remove these deprecated flags
const NON_FUNGIBLE = 0b0000_1000;
const ASSET_REGISTRATION = 0b0000_0010 | Self::NON_FUNGIBLE.bits;
const MINT_NON_FUNGIBLE = 0b0000_0100 | Self::NON_FUNGIBLE.bits;
Expand All @@ -51,13 +66,13 @@ impl ConsensusEncoding for OutputFlags {

impl ConsensusEncodingSized for OutputFlags {
fn consensus_encode_exact_size(&self) -> usize {
1
2
}
}

impl ConsensusDecoding for OutputFlags {
fn consensus_decode<R: Read>(reader: &mut R) -> Result<Self, io::Error> {
let mut buf = [0u8; 1];
let mut buf = [0u8; 2];
reader.read_exact(&mut buf)?;
// SAFETY: we have 3 options here:
// 1. error if unsupported flags are used, meaning that every new flag will be a hard fork
Expand All @@ -66,6 +81,6 @@ impl ConsensusDecoding for OutputFlags {
// Once those flags are defined at some point in the future, depending on the functionality of the flag,
// a consensus rule may be needed that ignores flags prior to a given block height.
// Option 3 is used here
Ok(unsafe { OutputFlags::from_bits_unchecked(u8::from_le_bytes(buf)) })
Ok(unsafe { OutputFlags::from_bits_unchecked(u16::from_le_bytes(buf)) })
}
}
26 changes: 13 additions & 13 deletions base_layer/core/src/transactions/transaction_components/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 +476,15 @@ mod output_features {

let mut buf = Vec::new();
let written = features.consensus_encode(&mut buf).unwrap();
assert_eq!(buf.len(), 9);
assert_eq!(written, 9);
assert_eq!(buf.len(), 10);
assert_eq!(written, 10);

let mut features = OutputFeatures::default();
features.version = OutputFeaturesVersion::V1;
let mut buf = Vec::new();
let written = features.consensus_encode(&mut buf).unwrap();
assert_eq!(buf.len(), 11);
assert_eq!(written, 11);
assert_eq!(buf.len(), 12);
assert_eq!(written, 12);
}

#[test]
Expand All @@ -497,10 +497,10 @@ mod output_features {
let known_size_u8_min = features_u8_min.consensus_encode_exact_size();
assert_eq!(known_size_u8_max, known_size_u8_min);
let mut buf = Vec::with_capacity(known_size_u8_max);
assert_eq!(known_size_u8_max, 18);
assert_eq!(known_size_u8_max, 19);
let written = features_u8_max.consensus_encode(&mut buf).unwrap();
assert_eq!(buf.len(), 18);
assert_eq!(written, 18);
assert_eq!(buf.len(), 19);
assert_eq!(written, 19);
let decoded_features = OutputFeatures::consensus_decode(&mut &buf[..]).unwrap();
// Recovery byte is not encoded for OutputFeaturesVersion::V0; the default is returned when decoded
assert_ne!(features_u8_max, decoded_features);
Expand All @@ -512,22 +512,22 @@ mod output_features {
let known_size_u8_max = features_u8_max.consensus_encode_exact_size();
let known_size_u8_min = features_u8_min.consensus_encode_exact_size();
assert_eq!(known_size_u8_max, known_size_u8_min);
assert_eq!(known_size_u8_max, 21);
let mut buf = Vec::with_capacity(known_size_u8_max);
assert_eq!(known_size_u8_max, 20);
let written = features_u8_max.consensus_encode(&mut buf).unwrap();
assert_eq!(buf.len(), 20);
assert_eq!(written, 20);
assert_eq!(buf.len(), 21);
assert_eq!(written, 21);
let decoded_features = OutputFeatures::consensus_decode(&mut &buf[..]).unwrap();
assert_eq!(features_u8_max, decoded_features);

let mut features = OutputFeatures::create_coinbase(u64::MAX, rand::thread_rng().gen::<u8>());
features.version = OutputFeaturesVersion::V1;
let known_size = features.consensus_encode_exact_size();
let mut buf = Vec::with_capacity(known_size);
assert_eq!(known_size, 20);
assert_eq!(known_size, 21);
let written = features.consensus_encode(&mut buf).unwrap();
assert_eq!(buf.len(), 20);
assert_eq!(written, 20);
assert_eq!(buf.len(), 21);
assert_eq!(written, 21);
let decoded_features = OutputFeatures::consensus_decode(&mut &buf[..]).unwrap();
assert_eq!(features, decoded_features);
}
Expand Down
2 changes: 2 additions & 0 deletions base_layer/tari_mining_helper_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ mod tests {
}

#[test]
#[ignore = "test requires new value for the NONCE"]
fn check_difficulty() {
unsafe {
let mut error = -1;
Expand Down Expand Up @@ -402,6 +403,7 @@ mod tests {
}

#[test]
#[ignore = "test requires new value for the NONCE"]
fn check_share() {
unsafe {
let mut error = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,15 @@ impl TryFrom<OutputSql> for DbUnblindedOutput {
reason: format!("Could not convert json into OutputFeatures:{}", s),
})?;

features.flags = OutputFlags::from_bits(u8::try_from(o.flags).unwrap()).ok_or(
OutputManagerStorageError::ConversionError {
reason: "Flags could not be converted from bits".to_string(),
},
)?;
let flags = o
.flags
.try_into()
.map_err(|_| OutputManagerStorageError::ConversionError {
reason: format!("Unable to convert flag bits with value {} to OutputFlags", o.flags),
})?;
features.flags = OutputFlags::from_bits(flags).ok_or(OutputManagerStorageError::ConversionError {
reason: "Flags could not be converted from bits".to_string(),
})?;
features.maturity = o.maturity as u64;
features.metadata = o.metadata.unwrap_or_default();
features.unique_id = o.features_unique_id.clone();
Expand Down
2 changes: 1 addition & 1 deletion base_layer/wallet/src/transaction_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ where
let mut client = connectivity
.obtain_base_node_wallet_rpc_client()
.await
.ok_or_else(|| TransactionServiceError::Shutdown)?;
.ok_or(TransactionServiceError::Shutdown)?;

let resp = client
.get_mempool_fee_per_gram_stats(base_node_proto::GetMempoolFeePerGramStatsRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5618,7 +5618,7 @@ fn test_get_fee_per_gram_per_block_basic() {
let factories = CryptoFactories::default();
let mut runtime = Runtime::new().unwrap();
let (connection, _temp_dir) = make_wallet_database_connection(None);
let mut alice_ts_interface = setup_transaction_service_no_comms(&mut runtime, factories.clone(), connection, None);
let mut alice_ts_interface = setup_transaction_service_no_comms(&mut runtime, factories, connection, None);
let stats = vec![base_node_proto::MempoolFeePerGramStat {
order: 0,
min_fee_per_gram: 1,
Expand Down
3 changes: 1 addition & 2 deletions integration_tests/features/WalletQuery.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@wallet-query @wallet
Feature: Wallet Querying


Scenario: As a wallet I want to query the status of utxos in blocks
Given I have a seed node WalletSeedA
When I mine a block on WalletSeedA with coinbase CB1
Expand All @@ -19,7 +18,7 @@ Feature: Wallet Querying
When I create a transaction TX1 spending CB1 to UTX1
When I submit transaction TX1 to SeedA
Then TX1 is in the mempool
When I mine 1 blocks on SeedA
When I mine 2 blocks on SeedA
Then the UTXO UTX1 has been mined according to SeedA


Expand Down
2 changes: 1 addition & 1 deletion integration_tests/helpers/transactionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class TransactionBuilder {
// version
Buffer.from([OUTPUT_FEATURES_VERSION]),
Buffer.from([parseInt(features.maturity || 0)]),
Buffer.from([features.flags]),
toLittleEndian(features.flags, 16),
OUTPUT_FEATURES_VERSION === 0x00
? Buffer.from([])
: Buffer.from([features.recovery_byte]),
Expand Down
35 changes: 29 additions & 6 deletions integration_tests/helpers/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,38 @@ function toLittleEndianInner(n) {
}

function toLittleEndian(n, numBits) {
const s = toLittleEndianInner(n);

for (let i = s.length; i < numBits / 8; i++) {
s.push("00");
if (numBits % 8 !== 0) {
throw new Error("toLittleEndian: numBits not a multiple of 8");
}

const arr = Buffer.from(s.join(""), "hex");
switch (numBits) {
case 8: {
let buf = Buffer.alloc(numBits / 8);
buf.writeUint8(n);
return buf;
}
case 16: {
let buf = Buffer.alloc(numBits / 8);
buf.writeUint16LE(n);
return buf;
}
case 32: {
let buf = Buffer.alloc(numBits / 8);
buf.writeUInt32LE(n);
return buf;
}
default: {
const s = toLittleEndianInner(n);

return arr;
for (let i = s.length; i < numBits / 8; i++) {
s.push("00");
}

const arr = Buffer.from(s.join(""), "hex");

return arr;
}
}
}

function littleEndianHexStringToBigEndianHexString(string) {
Expand Down