Skip to content

Commit

Permalink
add set structures
Browse files Browse the repository at this point in the history
  • Loading branch information
lisicky committed Dec 5, 2023
1 parent 58dcd11 commit bdf07d9
Show file tree
Hide file tree
Showing 40 changed files with 689 additions and 470 deletions.
6 changes: 3 additions & 3 deletions rust/src/builders/certificates_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ impl CertificatesBuilder {
Ok(())
}

pub(crate) fn get_required_signers(&self) -> RequiredSignersSet {
let mut set = RequiredSignersSet::new();
pub(crate) fn get_required_signers(&self) -> Ed25519KeyHashesSet {
let mut set = Ed25519KeyHashesSet::new();
for (cert, script_wit) in &self.certs {
let cert_req_signers = witness_keys_for_cert(&cert);
set.extend(cert_req_signers);
set.0.extend(cert_req_signers);
if let Some(ScriptWitnessType::NativeScriptWitness(script_source)) = script_wit {
set.extend(script_source.required_signers());
}
Expand Down
4 changes: 2 additions & 2 deletions rust/src/builders/script_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ impl NativeScriptSourceEnum {
}
}

pub fn required_signers(&self) -> RequiredSignersSet {
pub fn required_signers(&self) -> Ed25519KeyHashesSet {
match self {
NativeScriptSourceEnum::NativeScript(script) => RequiredSignersSet::from(script),
NativeScriptSourceEnum::NativeScript(script) => Ed25519KeyHashesSet::from(script),
NativeScriptSourceEnum::RefInput(_, _, required_signers) => required_signers.into(),
}
}
Expand Down
13 changes: 7 additions & 6 deletions rust/src/builders/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::*;
use crate::fees;
use crate::utils;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use crate::map_names::TxBodyNames::RequiredSigners;

pub(crate) fn fake_private_key() -> Bip32PrivateKey {
Bip32PrivateKey::from_bytes(&[
Expand Down Expand Up @@ -39,11 +40,11 @@ pub(crate) fn fake_raw_key_public() -> PublicKey {
}

fn count_needed_vkeys(tx_builder: &TransactionBuilder) -> usize {
let mut input_hashes: RequiredSignersSet = RequiredSignersSet::from(&tx_builder.inputs);
input_hashes.extend(RequiredSignersSet::from(&tx_builder.collateral));
input_hashes.extend(RequiredSignersSet::from(&tx_builder.required_signers));
let mut input_hashes: Ed25519KeyHashesSet = Ed25519KeyHashesSet::from(&tx_builder.inputs);
input_hashes.extend(Ed25519KeyHashesSet::from(&tx_builder.collateral));
input_hashes.extend(tx_builder.required_signers.clone());
if let Some(mint_builder) = &tx_builder.mint {
input_hashes.extend(RequiredSignersSet::from(&mint_builder.get_native_scripts()));
input_hashes.extend(Ed25519KeyHashesSet::from(&mint_builder.get_native_scripts()));
}
if let Some(withdrawals_builder) = &tx_builder.withdrawals {
input_hashes.extend(withdrawals_builder.get_required_signers());
Expand Down Expand Up @@ -326,7 +327,7 @@ pub struct TransactionBuilder {
pub(crate) validity_start_interval: Option<SlotBigNum>,
pub(crate) mint: Option<MintBuilder>,
pub(crate) script_data_hash: Option<ScriptDataHash>,
pub(crate) required_signers: Ed25519KeyHashes,
pub(crate) required_signers: Ed25519KeyHashesSet,
pub(crate) collateral_return: Option<TransactionOutput>,
pub(crate) total_collateral: Option<Coin>,
pub(crate) reference_inputs: HashSet<TransactionInput>,
Expand Down Expand Up @@ -1290,7 +1291,7 @@ impl TransactionBuilder {
validity_start_interval: None,
mint: None,
script_data_hash: None,
required_signers: Ed25519KeyHashes::new(),
required_signers: Ed25519KeyHashesSet::new(),
collateral_return: None,
total_collateral: None,
reference_inputs: HashSet::new(),
Expand Down
16 changes: 8 additions & 8 deletions rust/src/builders/tx_inputs_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ impl InputsWithScriptWitness {

// We need to know how many of each type of witness will be in the transaction so we can calculate the tx fee
#[derive(Clone, Debug)]
pub struct RequiredWitnessSet {
vkeys: RequiredSignersSet,
pub struct InputsRequiredWitness {
vkeys: Ed25519KeyHashesSet,
scripts: LinkedHashMap<ScriptHash, LinkedHashMap<TransactionInput, Option<ScriptWitnessType>>>,
bootstraps: BTreeSet<Vec<u8>>,
}
Expand All @@ -75,7 +75,7 @@ pub struct RequiredWitnessSet {
#[derive(Clone, Debug)]
pub struct TxInputsBuilder {
inputs: BTreeMap<TransactionInput, (TxBuilderInput, Option<ScriptHash>)>,
required_witnesses: RequiredWitnessSet,
required_witnesses: InputsRequiredWitness,
}

pub(crate) fn get_bootstraps(inputs: &TxInputsBuilder) -> BTreeSet<Vec<u8>> {
Expand All @@ -87,8 +87,8 @@ impl TxInputsBuilder {
pub fn new() -> Self {
Self {
inputs: BTreeMap::new(),
required_witnesses: RequiredWitnessSet {
vkeys: BTreeSet::new(),
required_witnesses: InputsRequiredWitness {
vkeys: Ed25519KeyHashesSet::new(),
scripts: LinkedHashMap::new(),
bootstraps: BTreeSet::new(),
},
Expand All @@ -113,7 +113,7 @@ impl TxInputsBuilder {
amount: amount.clone(),
};
self.push_input((inp, None));
self.required_witnesses.vkeys.insert(hash.clone());
self.required_witnesses.vkeys.add_move(hash.clone());
}

#[deprecated(
Expand Down Expand Up @@ -466,7 +466,7 @@ impl TxInputsBuilder {
}

pub fn add_required_signer(&mut self, key: &Ed25519KeyHash) {
self.required_witnesses.vkeys.insert(key.clone());
self.required_witnesses.vkeys.add_move(key.clone());
}

pub fn add_required_signers(&mut self, keys: &RequiredSigners) {
Expand Down Expand Up @@ -526,7 +526,7 @@ impl TxInputsBuilder {
}
}

impl From<&TxInputsBuilder> for RequiredSignersSet {
impl From<&TxInputsBuilder> for Ed25519KeyHashesSet {
fn from(inputs: &TxInputsBuilder) -> Self {
let mut set = inputs.required_witnesses.vkeys.clone();
inputs
Expand Down
6 changes: 3 additions & 3 deletions rust/src/builders/voting_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ impl VotingBuilder {
Ok(())
}

pub(crate) fn get_required_signers(&self) -> RequiredSignersSet {
let mut set = RequiredSignersSet::new();
pub(crate) fn get_required_signers(&self) -> Ed25519KeyHashesSet {
let mut set = Ed25519KeyHashesSet::new();
for (voter, voter_votes) in &self.votes {
let req_signature = voter.to_key_hash();
if let Some(req_signature) = req_signature {
set.insert(req_signature);
set.add_move(req_signature);
}

if let Some(ScriptWitnessType::NativeScriptWitness(script_source)) =
Expand Down
6 changes: 3 additions & 3 deletions rust/src/builders/withdrawals_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ impl WithdrawalsBuilder {
Ok(())
}

pub(crate) fn get_required_signers(&self) -> RequiredSignersSet {
let mut set = RequiredSignersSet::new();
pub(crate) fn get_required_signers(&self) -> Ed25519KeyHashesSet {
let mut set = Ed25519KeyHashesSet::new();
for (address, (_, script_wit)) in &self.withdrawals {
let req_signature = address.payment_cred().to_keyhash();
if let Some(req_signature) = req_signature {
set.insert(req_signature);
set.add_move(req_signature);
}

if let Some(ScriptWitnessType::NativeScriptWitness(script_source)) = script_wit {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/fakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
Ed25519Signature, NetworkInfo, PolicyID, TransactionHash, TransactionIndex, TransactionInput,
TransactionOutput, Value, Vkey, AssetName,
};
use crate::address::RewardAddress;
use crate::RewardAddress;

pub(crate) fn fake_bytes_32(x: u8) -> Vec<u8> {
vec![
Expand Down
3 changes: 1 addition & 2 deletions rust/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ pub fn min_script_fee(tx: &Transaction, ex_unit_prices: &ExUnitPrices) -> Result
mod tests {
use super::*;
use crate::TransactionOutputBuilder;
use address::*;
use crypto::*;

// based off tx test vectors (https://gist.github.com/KtorZ/5a2089df0915f21aca368d12545ab230)
Expand Down Expand Up @@ -331,7 +330,7 @@ mod tests {

let mut certs = Certificates::new();

let mut pool_owners = Ed25519KeyHashes::new();
let mut pool_owners = Ed25519KeyHashesSet::new();
pool_owners.add(
&PublicKey::from_bytes(
&hex::decode("54d1a9c5ad69586ceeb839c438400c376c0bd34825fb4c17cc2f58c54e1437f3")
Expand Down
118 changes: 14 additions & 104 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ use cbor_event::{
se::{Serialize, Serializer},
};

mod address;
pub use address::*;
mod builders;
pub use builders::*;
pub mod chain_core;
Expand Down Expand Up @@ -291,62 +289,6 @@ impl DataCost {
}
}

pub type RequiredSigners = Ed25519KeyHashes;
pub type RequiredSignersSet = BTreeSet<Ed25519KeyHash>;

impl NoneOrEmpty for RequiredSigners {
fn is_none_or_empty(&self) -> bool {
self.0.is_empty()
}
}

impl From<&Ed25519KeyHashes> for RequiredSignersSet {
fn from(keys: &Ed25519KeyHashes) -> Self {
keys.0.iter().fold(BTreeSet::new(), |mut set, k| {
set.insert(k.clone());
set
})
}
}

#[wasm_bindgen]
#[derive(
Clone,
Debug,
Eq,
Ord,
PartialEq,
PartialOrd,
Hash,
serde::Serialize,
serde::Deserialize,
JsonSchema,
)]
pub struct TransactionInput {
transaction_id: TransactionHash,
index: TransactionIndex,
}

impl_to_from!(TransactionInput);

#[wasm_bindgen]
impl TransactionInput {
pub fn transaction_id(&self) -> TransactionHash {
self.transaction_id.clone()
}

pub fn index(&self) -> TransactionIndex {
self.index.clone()
}

pub fn new(transaction_id: &TransactionHash, index: TransactionIndex) -> Self {
Self {
transaction_id: transaction_id.clone(),
index: index,
}
}
}

#[wasm_bindgen]
#[derive(Debug, Clone, Eq, Ord, PartialOrd, serde::Serialize, serde::Deserialize, JsonSchema)]
pub struct TransactionOutput {
Expand Down Expand Up @@ -948,33 +890,6 @@ impl PoolMetadata {
}
}

#[wasm_bindgen]
#[derive(
Clone, Debug, Eq, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize, JsonSchema,
)]
pub struct Credentials(Vec<Credential>);

impl_to_from!(Credentials);

#[wasm_bindgen]
impl Credentials {
pub fn new() -> Self {
Self(Vec::new())
}

pub fn len(&self) -> usize {
self.0.len()
}

pub fn get(&self, index: usize) -> Credential {
self.0[index].clone()
}

pub fn add(&mut self, elem: &Credential) {
self.0.push(elem.clone());
}
}

#[wasm_bindgen]
#[derive(
Clone, Debug, Eq, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize, JsonSchema,
Expand Down Expand Up @@ -1553,16 +1468,11 @@ impl NativeScript {
}
}

/// Returns an array of unique Ed25519KeyHashes
/// Returns a set of Ed25519KeyHashes
/// contained within this script recursively on any depth level.
/// The order of the keys in the result is not determined in any way.
pub fn get_required_signers(&self) -> Ed25519KeyHashes {
Ed25519KeyHashes(
RequiredSignersSet::from(self)
.iter()
.map(|k| k.clone())
.collect(),
)
pub fn get_required_signers(&self) -> Ed25519KeyHashesSet {
Ed25519KeyHashesSet::from(self)
}
}

Expand Down Expand Up @@ -2749,27 +2659,27 @@ impl NetworkId {
}
}

impl From<&NativeScript> for RequiredSignersSet {
impl From<&NativeScript> for Ed25519KeyHashesSet {
fn from(script: &NativeScript) -> Self {
match &script.0 {
NativeScriptEnum::ScriptPubkey(spk) => {
let mut set = BTreeSet::new();
set.insert(spk.addr_keyhash());
let mut set = Ed25519KeyHashesSet::new();
set.add_move(spk.addr_keyhash());
set
}
NativeScriptEnum::ScriptAll(all) => RequiredSignersSet::from(&all.native_scripts),
NativeScriptEnum::ScriptAny(any) => RequiredSignersSet::from(&any.native_scripts),
NativeScriptEnum::ScriptNOfK(ofk) => RequiredSignersSet::from(&ofk.native_scripts),
_ => BTreeSet::new(),
NativeScriptEnum::ScriptAll(all) => Ed25519KeyHashesSet::from(&all.native_scripts),
NativeScriptEnum::ScriptAny(any) => Ed25519KeyHashesSet::from(&any.native_scripts),
NativeScriptEnum::ScriptNOfK(ofk) => Ed25519KeyHashesSet::from(&ofk.native_scripts),
_ => Ed25519KeyHashesSet::new(),
}
}
}

impl From<&NativeScripts> for RequiredSignersSet {
impl From<&NativeScripts> for Ed25519KeyHashesSet {
fn from(scripts: &NativeScripts) -> Self {
scripts.0.iter().fold(BTreeSet::new(), |mut set, s| {
RequiredSignersSet::from(s).iter().for_each(|pk| {
set.insert(pk.clone());
scripts.0.iter().fold(Ed25519KeyHashesSet::new(), |mut set, s| {
Ed25519KeyHashesSet::from(s).0.iter().for_each(|pk| {
set.add_move(pk.clone());
});
set
})
Expand Down
Loading

0 comments on commit bdf07d9

Please sign in to comment.