Skip to content

Commit c48adc9

Browse files
committed
refactor TransactionBuilder to centralize as much logic as possible
1 parent 49c8c6d commit c48adc9

11 files changed

Lines changed: 592 additions & 1109 deletions

File tree

dash/src/blockdata/transaction/special_transaction/asset_lock.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ pub struct AssetLockPayload {
4646
}
4747

4848
impl AssetLockPayload {
49+
/// Latest spec version of the AssetLock payload.
50+
pub const CURRENT_VERSION: u8 = 1;
51+
52+
/// Create a new AssetLock payload at [`Self::CURRENT_VERSION`].
53+
pub fn new(credit_outputs: Vec<TxOut>) -> Self {
54+
Self {
55+
version: Self::CURRENT_VERSION,
56+
credit_outputs,
57+
}
58+
}
59+
4960
/// The size of the payload in bytes.
5061
pub fn size(&self) -> usize {
5162
let size = 1 + VarInt(self.credit_outputs.len() as u64).len();

dash/src/blockdata/transaction/special_transaction/coinbase.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ pub struct CoinbasePayload {
4747
}
4848

4949
impl CoinbasePayload {
50+
/// Latest spec version of the Coinbase payload.
51+
pub const CURRENT_VERSION: u16 = 3;
52+
53+
/// Create a new Coinbase payload at [`Self::CURRENT_VERSION`].
54+
pub fn new(
55+
height: u32,
56+
merkle_root_masternode_list: MerkleRootMasternodeList,
57+
merkle_root_quorums: MerkleRootQuorums,
58+
best_cl_height: Option<u32>,
59+
best_cl_signature: Option<BLSSignature>,
60+
asset_locked_amount: Option<u64>,
61+
) -> Self {
62+
Self {
63+
version: Self::CURRENT_VERSION,
64+
height,
65+
merkle_root_masternode_list,
66+
merkle_root_quorums,
67+
best_cl_height,
68+
best_cl_signature,
69+
asset_locked_amount,
70+
}
71+
}
72+
5073
/// The size of the payload in bytes.
5174
/// version(2) + height(4) + merkle_root_masternode_list(32) + merkle_root_quorums(32)
5275
/// in addition to the above, if version >= 3: asset_locked_amount(8) + best_cl_height(compact_size) +

dash/src/blockdata/transaction/special_transaction/provider_registration.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,46 @@ pub struct ProviderRegistrationPayload {
114114
}
115115

116116
impl ProviderRegistrationPayload {
117+
/// Latest spec version of the ProRegTx payload.
118+
pub const CURRENT_VERSION: u16 = 2;
119+
120+
/// Create a new ProRegTx payload at [`Self::CURRENT_VERSION`].
121+
#[allow(clippy::too_many_arguments)]
122+
pub fn new(
123+
masternode_type: ProviderMasternodeType,
124+
masternode_mode: u16,
125+
collateral_outpoint: OutPoint,
126+
service_address: SocketAddr,
127+
owner_key_hash: PubkeyHash,
128+
operator_public_key: BLSPublicKey,
129+
voting_key_hash: PubkeyHash,
130+
operator_reward: u16,
131+
script_payout: ScriptBuf,
132+
inputs_hash: InputsHash,
133+
signature: Vec<u8>,
134+
platform_node_id: Option<PubkeyHash>,
135+
platform_p2p_port: Option<u16>,
136+
platform_http_port: Option<u16>,
137+
) -> Self {
138+
Self {
139+
version: Self::CURRENT_VERSION,
140+
masternode_type,
141+
masternode_mode,
142+
collateral_outpoint,
143+
service_address,
144+
owner_key_hash,
145+
operator_public_key,
146+
voting_key_hash,
147+
operator_reward,
148+
script_payout,
149+
inputs_hash,
150+
signature,
151+
platform_node_id,
152+
platform_p2p_port,
153+
platform_http_port,
154+
}
155+
}
156+
117157
/// A convenience method to get the address from payout script
118158
pub fn payout_address(&self, network: Network) -> Result<Address, encode::Error> {
119159
match Address::from_script(&self.script_payout, network) {

dash/src/blockdata/transaction/special_transaction/provider_update_registrar.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ pub struct ProviderUpdateRegistrarPayload {
5858
}
5959

6060
impl ProviderUpdateRegistrarPayload {
61+
/// Latest spec version of the ProUpRegTx payload.
62+
pub const CURRENT_VERSION: u16 = 2;
63+
64+
/// Create a new ProUpRegTx payload at [`Self::CURRENT_VERSION`].
65+
pub fn new(
66+
pro_tx_hash: Txid,
67+
provider_mode: u16,
68+
operator_public_key: BLSPublicKey,
69+
voting_key_hash: PubkeyHash,
70+
script_payout: ScriptBuf,
71+
inputs_hash: InputsHash,
72+
payload_sig: Vec<u8>,
73+
) -> Self {
74+
Self {
75+
version: Self::CURRENT_VERSION,
76+
pro_tx_hash,
77+
provider_mode,
78+
operator_public_key,
79+
voting_key_hash,
80+
script_payout,
81+
inputs_hash,
82+
payload_sig,
83+
}
84+
}
85+
6186
/// The size of the payload in bytes.
6287
pub fn size(&self) -> usize {
6388
let mut size = 2 + 32 + 2 + 48 + 20 + 32; // 136

dash/src/blockdata/transaction/special_transaction/provider_update_revocation.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ pub struct ProviderUpdateRevocationPayload {
6161
}
6262

6363
impl ProviderUpdateRevocationPayload {
64+
/// Latest spec version of the ProUpRevTx payload.
65+
pub const CURRENT_VERSION: u16 = 2;
66+
67+
/// Create a new ProUpRevTx payload at [`Self::CURRENT_VERSION`].
68+
pub fn new(
69+
pro_tx_hash: Txid,
70+
reason: u16,
71+
inputs_hash: InputsHash,
72+
payload_sig: BLSSignature,
73+
) -> Self {
74+
Self {
75+
version: Self::CURRENT_VERSION,
76+
pro_tx_hash,
77+
reason,
78+
inputs_hash,
79+
payload_sig,
80+
}
81+
}
82+
6483
/// The size of the payload in bytes.
6584
pub fn size(&self) -> usize {
6685
2 + 32 + 2 + 32 + 96

dash/src/blockdata/transaction/special_transaction/provider_update_service.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,38 @@ pub struct ProviderUpdateServicePayload {
7676
}
7777

7878
impl ProviderUpdateServicePayload {
79+
/// Latest spec version of the ProUpServTx payload (BasicBLS).
80+
pub const CURRENT_VERSION: u16 = 2;
81+
82+
/// Create a new ProUpServTx payload at [`Self::CURRENT_VERSION`].
83+
#[allow(clippy::too_many_arguments)]
84+
pub fn new(
85+
mn_type: Option<u16>,
86+
pro_tx_hash: Txid,
87+
ip_address: u128,
88+
port: u16,
89+
script_payout: ScriptBuf,
90+
inputs_hash: InputsHash,
91+
platform_node_id: Option<[u8; 20]>,
92+
platform_p2p_port: Option<u16>,
93+
platform_http_port: Option<u16>,
94+
payload_sig: BLSSignature,
95+
) -> Self {
96+
Self {
97+
version: Self::CURRENT_VERSION,
98+
mn_type,
99+
pro_tx_hash,
100+
ip_address,
101+
port,
102+
script_payout,
103+
inputs_hash,
104+
platform_node_id,
105+
platform_p2p_port,
106+
platform_http_port,
107+
payload_sig,
108+
}
109+
}
110+
79111
/// The size of the payload in bytes.
80112
pub fn size(&self) -> usize {
81113
let mut size = 2 + 32 + 16 + 2 + 32 + 96; // 180

key-wallet-ffi/src/transaction.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,14 @@ pub unsafe extern "C" fn wallet_build_and_sign_transaction(
132132
let mut manager = manager_ref.manager.write().await;
133133

134134
let (transaction, fee) = unwrap_or_return!(
135-
manager.build_and_sign_transaction(
136-
&wallet_id,
137-
account_index,
138-
outputs,
139-
FeeRate::new(fee_per_kb)
140-
),
135+
manager
136+
.build_and_sign_transaction(
137+
&wallet_id,
138+
account_index,
139+
outputs,
140+
FeeRate::new(fee_per_kb)
141+
)
142+
.await,
141143
error
142144
);
143145
*fee_out = fee;
@@ -837,7 +839,7 @@ pub unsafe extern "C" fn wallet_build_and_sign_asset_lock_transaction(
837839
account_index,
838840
fundings,
839841
fee_per_kb,
840-
), error);
842+
).await, error);
841843

842844
// Write outputs
843845
*fee_out = result.fee;

key-wallet-manager/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ impl WalletManager<ManagedWalletInfo> {
602602
managed_info.next_change_address(wallet, account_index, account_type_pref, mark_as_used)
603603
}
604604

605-
pub fn build_and_sign_transaction(
605+
pub async fn build_and_sign_transaction(
606606
&mut self,
607607
wallet_id: &WalletId,
608608
account_index: u32,
@@ -616,6 +616,7 @@ impl WalletManager<ManagedWalletInfo> {
616616

617617
managed_wallet
618618
.build_and_sign_transaction(wallet, account_index, outputs, fee_rate)
619+
.await
619620
.map_err(|e| WalletError::TransactionBuild(e.to_string()))
620621
}
621622
}

0 commit comments

Comments
 (0)