Skip to content

Commit

Permalink
Merge branch 'dev' into utxo-counts
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Stacks authored Sep 2, 2024
2 parents ca1e550 + 7cdabb4 commit 8be0e84
Show file tree
Hide file tree
Showing 32 changed files with 584 additions and 424 deletions.
25 changes: 24 additions & 1 deletion consensus/client/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,34 @@ export interface IHeader {
blueScore: bigint;
pruningPoint: HexString;
}
/**
* Interface defining the structure of a raw block header.
*
* This interface is explicitly used by GetBlockTemplate and SubmitBlock RPCs
* and unlike `IHeader`, does not include a hash.
*
* @category Consensus
*/
export interface IRawHeader {
version: number;
parentsByLevel: Array<Array<HexString>>;
hashMerkleRoot: HexString;
acceptedIdMerkleRoot: HexString;
utxoCommitment: HexString;
timestamp: bigint;
bits: number;
nonce: bigint;
daaScore: bigint;
blueWork: bigint | HexString;
blueScore: bigint;
pruningPoint: HexString;
}
"#;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "IHeader | Header")]
#[wasm_bindgen(typescript_type = "Header | IHeader | IRawHeader")]
pub type HeaderT;
}

Expand Down
10 changes: 8 additions & 2 deletions consensus/client/src/serializable/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ pub struct SerializableTransaction {
pub outputs: Vec<SerializableTransactionOutput>,
pub lock_time: u64,
pub gas: u64,
#[serde(default)]
pub mass: u64,
pub subnetwork_id: SubnetworkId,
#[serde(with = "hex::serde")]
pub payload: Vec<u8>,
Expand Down Expand Up @@ -265,6 +267,7 @@ impl SerializableTransaction {
lock_time: transaction.lock_time,
subnetwork_id: transaction.subnetwork_id.clone(),
gas: transaction.gas,
mass: transaction.mass(),
payload: transaction.payload.clone(),
id: transaction.id(),
})
Expand All @@ -284,6 +287,7 @@ impl SerializableTransaction {
subnetwork_id: inner.subnetwork_id.clone(),
gas: inner.gas,
payload: inner.payload.clone(),
mass: inner.mass,
id: inner.id,
})
}
Expand Down Expand Up @@ -311,6 +315,7 @@ impl SerializableTransaction {
lock_time: transaction.lock_time,
subnetwork_id: transaction.subnetwork_id.clone(),
gas: transaction.gas,
mass: transaction.mass(),
payload: transaction.payload.clone(),
})
}
Expand All @@ -336,7 +341,8 @@ impl TryFrom<SerializableTransaction> for cctx::SignableTransaction {
serializable.subnetwork_id,
serializable.gas,
serializable.payload,
);
)
.with_mass(serializable.mass);

Ok(Self::with_entries(tx, entries))
}
Expand All @@ -349,6 +355,6 @@ impl TryFrom<SerializableTransaction> for Transaction {
let inputs: Vec<TransactionInput> = tx.inputs.iter().map(TryInto::try_into).collect::<Result<Vec<_>>>()?;
let outputs: Vec<TransactionOutput> = tx.outputs.iter().map(TryInto::try_into).collect::<Result<Vec<_>>>()?;

Transaction::new(Some(id), tx.version, inputs, outputs, tx.lock_time, tx.subnetwork_id, tx.gas, tx.payload)
Transaction::new(Some(id), tx.version, inputs, outputs, tx.lock_time, tx.subnetwork_id, tx.gas, tx.payload, tx.mass)
}
}
20 changes: 18 additions & 2 deletions consensus/client/src/serializable/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ pub struct SerializableTransaction {
pub subnetwork_id: SubnetworkId,
pub lock_time: String,
pub gas: String,
#[serde(default)]
pub mass: String,
#[serde(with = "hex::serde")]
pub payload: Vec<u8>,
}
Expand Down Expand Up @@ -260,6 +262,7 @@ impl SerializableTransaction {
lock_time: transaction.lock_time.to_string(),
subnetwork_id: transaction.subnetwork_id.clone(),
gas: transaction.gas.to_string(),
mass: transaction.mass().to_string(),
payload: transaction.payload.clone(),
})
}
Expand All @@ -277,6 +280,7 @@ impl SerializableTransaction {
lock_time: inner.lock_time.to_string(),
subnetwork_id: inner.subnetwork_id.clone(),
gas: inner.gas.to_string(),
mass: inner.mass.to_string(),
payload: inner.payload.clone(),
id: inner.id,
})
Expand Down Expand Up @@ -305,6 +309,7 @@ impl SerializableTransaction {
lock_time: transaction.lock_time.to_string(),
subnetwork_id: transaction.subnetwork_id.clone(),
gas: transaction.gas.to_string(),
mass: transaction.mass().to_string(),
payload: transaction.payload.clone(),
})
}
Expand All @@ -330,7 +335,8 @@ impl TryFrom<SerializableTransaction> for cctx::SignableTransaction {
signable.subnetwork_id,
signable.gas.parse()?,
signable.payload,
);
)
.with_mass(signable.mass.parse().unwrap_or_default());

Ok(Self::with_entries(tx, entries))
}
Expand All @@ -343,6 +349,16 @@ impl TryFrom<SerializableTransaction> for crate::Transaction {
let inputs: Vec<TransactionInput> = tx.inputs.iter().map(TryInto::try_into).collect::<Result<Vec<_>>>()?;
let outputs: Vec<TransactionOutput> = tx.outputs.iter().map(TryInto::try_into).collect::<Result<Vec<_>>>()?;

Transaction::new(Some(id), tx.version, inputs, outputs, tx.lock_time.parse()?, tx.subnetwork_id, tx.gas.parse()?, tx.payload)
Transaction::new(
Some(id),
tx.version,
inputs,
outputs,
tx.lock_time.parse()?,
tx.subnetwork_id,
tx.gas.parse()?,
tx.payload,
tx.mass.parse().unwrap_or_default(),
)
}
}
30 changes: 25 additions & 5 deletions consensus/client/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface ITransaction {
subnetworkId: HexString;
gas: bigint;
payload: HexString;
/** The mass of the transaction (the mass is undefined or zero unless explicitly set or obtained from the node) */
mass?: bigint;
/** Optional verbose data provided by RPC */
verboseData?: ITransactionVerboseData;
Expand All @@ -43,7 +45,7 @@ export interface ITransaction {
export interface ITransactionVerboseData {
transactionId : HexString;
hash : HexString;
mass : bigint;
computeMass : bigint;
blockHash : HexString;
blockTime : bigint;
}
Expand All @@ -65,6 +67,7 @@ pub struct TransactionInner {
pub subnetwork_id: SubnetworkId,
pub gas: u64,
pub payload: Vec<u8>,
pub mass: u64,

// A field that is used to cache the transaction ID.
// Always use the corresponding self.id() instead of accessing this field directly
Expand Down Expand Up @@ -92,6 +95,7 @@ impl Transaction {
subnetwork_id: SubnetworkId,
gas: u64,
payload: Vec<u8>,
mass: u64,
) -> Result<Self> {
let finalize = id.is_none();
let tx = Self {
Expand All @@ -104,6 +108,7 @@ impl Transaction {
subnetwork_id,
gas,
payload,
mass,
})),
};
if finalize {
Expand Down Expand Up @@ -254,6 +259,16 @@ impl Transaction {
pub fn set_payload_from_js_value(&mut self, js_value: JsValue) {
self.inner.lock().unwrap().payload = js_value.try_as_vec_u8().unwrap_or_else(|err| panic!("payload value error: {err}"));
}

#[wasm_bindgen(getter = mass)]
pub fn get_mass(&self) -> u64 {
self.inner().mass
}

#[wasm_bindgen(setter = mass)]
pub fn set_mass(&self, v: u64) {
self.inner().mass = v;
}
}

impl TryCastFromJs for Transaction {
Expand All @@ -265,15 +280,15 @@ impl TryCastFromJs for Transaction {
Self::resolve_cast(value, || {
if let Some(object) = Object::try_from(value.as_ref()) {
if let Some(tx) = object.try_get_value("tx")? {
// TODO - optimize to use ref anchor
Transaction::try_captured_cast_from(tx)
// Ok(Cast::value(Transaction::try_owned_from(tx)?))
} else {
let id = object.try_cast_into::<TransactionId>("id")?;
let version = object.get_u16("version")?;
let lock_time = object.get_u64("lockTime")?;
let gas = object.get_u64("gas")?;
let payload = object.get_vec_u8("payload")?;
// mass field is optional
let mass = object.get_u64("mass").unwrap_or_default();
let subnetwork_id = object.get_vec_u8("subnetworkId")?;
if subnetwork_id.len() != subnets::SUBNETWORK_ID_SIZE {
return Err(Error::Custom("subnetworkId must be 20 bytes long".into()));
Expand All @@ -292,7 +307,7 @@ impl TryCastFromJs for Transaction {
.iter()
.map(TryCastFromJs::try_owned_from)
.collect::<std::result::Result<Vec<TransactionOutput>, Error>>()?;
Transaction::new(id, version, inputs, outputs, lock_time, subnetwork_id, gas, payload).map(Into::into)
Transaction::new(id, version, inputs, outputs, lock_time, subnetwork_id, gas, payload, mass).map(Into::into)
}
} else {
Err("Transaction must be an object".into())
Expand All @@ -305,6 +320,7 @@ impl TryCastFromJs for Transaction {
impl From<cctx::Transaction> for Transaction {
fn from(tx: cctx::Transaction) -> Self {
let id = tx.id();
let mass = tx.mass();
let inputs: Vec<TransactionInput> = tx.inputs.into_iter().map(|input| input.into()).collect::<Vec<TransactionInput>>();
let outputs: Vec<TransactionOutput> = tx.outputs.into_iter().map(|output| output.into()).collect::<Vec<TransactionOutput>>();
Self::new_with_inner(TransactionInner {
Expand All @@ -314,6 +330,7 @@ impl From<cctx::Transaction> for Transaction {
lock_time: tx.lock_time,
gas: tx.gas,
payload: tx.payload,
mass,
subnetwork_id: tx.subnetwork_id,
id,
})
Expand All @@ -336,6 +353,7 @@ impl From<&Transaction> for cctx::Transaction {
inner.gas,
inner.payload.clone(),
)
.with_mass(inner.mass)
}
}

Expand Down Expand Up @@ -366,6 +384,7 @@ impl Transaction {
lock_time: tx.lock_time,
gas: tx.gas,
payload: tx.payload.clone(),
mass: tx.mass(),
subnetwork_id: tx.subnetwork_id.clone(),
})
}
Expand All @@ -392,7 +411,8 @@ impl Transaction {
inner.subnetwork_id.clone(),
inner.gas,
inner.payload.clone(),
);
)
.with_mass(inner.mass);

Ok((tx, utxos))
}
Expand Down
Loading

0 comments on commit 8be0e84

Please sign in to comment.