Skip to content

Commit d56de71

Browse files
authored
change: ETCM-9687 document smart-contracts/offchain (#796)
1 parent 7fa319b commit d56de71

File tree

24 files changed

+279
-145
lines changed

24 files changed

+279
-145
lines changed

toolkit/partner-chains-cli/src/prepare_configuration/init_governance.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use partner_chains_cardano_offchain::{
1313
use sidechain_domain::{MainchainKeyHash, McTxHash, UtxoId};
1414

1515
pub(crate) fn run_init_governance<C: IOContext>(
16-
retries: FixedDelayRetries,
16+
await_tx: FixedDelayRetries,
1717
genesis_utxo: UtxoId,
1818
payment_key: &CardanoPaymentSigningKey,
1919
ogmios_config: &ServiceConfig,
@@ -32,7 +32,7 @@ pub(crate) fn run_init_governance<C: IOContext>(
3232
let runtime = tokio::runtime::Runtime::new().map_err(|e| anyhow::anyhow!(e))?;
3333
let tx_id = runtime
3434
.block_on(offchain.init_governance(
35-
retries,
35+
await_tx,
3636
&multisig_parameters,
3737
&payment_key,
3838
genesis_utxo,

toolkit/partner-chains-cli/src/setup_main_chain_state/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fn print_on_chain_d_parameter<C: IOContext>(
169169
}
170170

171171
fn set_candidates_on_main_chain<C: IOContext>(
172-
retries: FixedDelayRetries,
172+
await_tx: FixedDelayRetries,
173173
context: &C,
174174
offchain: &C::Offchain,
175175
candidates: SortedPermissionedCandidates,
@@ -184,7 +184,7 @@ fn set_candidates_on_main_chain<C: IOContext>(
184184
let tokio_runtime = tokio::runtime::Runtime::new().map_err(|e| anyhow::anyhow!(e))?;
185185
let result = tokio_runtime
186186
.block_on(offchain.upsert_permissioned_candidates(
187-
retries,
187+
await_tx,
188188
genesis_utxo,
189189
&candidates.0,
190190
&pkey,
@@ -210,7 +210,7 @@ fn set_candidates_on_main_chain<C: IOContext>(
210210
}
211211

212212
fn set_d_parameter_on_main_chain<C: IOContext>(
213-
retries: FixedDelayRetries,
213+
await_tx: FixedDelayRetries,
214214
context: &C,
215215
offchain: &C::Offchain,
216216
default_d_parameter: DParameter,
@@ -237,7 +237,7 @@ fn set_d_parameter_on_main_chain<C: IOContext>(
237237
sidechain_domain::DParameter { num_permissioned_candidates, num_registered_candidates };
238238
let tokio_runtime = tokio::runtime::Runtime::new().map_err(|e| anyhow::anyhow!(e))?;
239239
let result = tokio_runtime.block_on(offchain.upsert_d_param(
240-
retries,
240+
await_tx,
241241
genesis_utxo,
242242
&d_parameter,
243243
&payment_signing_key,

toolkit/smart-contracts/commands/src/assemble_tx.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::transaction_submitted_json;
2-
use partner_chains_cardano_offchain::assemble_tx::assemble_tx;
2+
use partner_chains_cardano_offchain::assemble_and_submit_tx::assemble_and_submit_tx;
33
use partner_chains_cardano_offchain::csl::{transaction_from_bytes, vkey_witness_from_bytes};
44
use sidechain_domain::{TransactionCbor, VKeyWitnessCbor};
55

@@ -29,8 +29,13 @@ impl AssembleAndSubmitCmd {
2929
.map(|w| vkey_witness_from_bytes(w.0.clone().into_iter().skip(2).collect()))
3030
.collect::<Result<Vec<_>, _>>()?;
3131

32-
let tx_hash =
33-
assemble_tx(transaction, witnesses, &client, &self.common_arguments.retries()).await?;
32+
let tx_hash = assemble_and_submit_tx(
33+
transaction,
34+
witnesses,
35+
&client,
36+
&self.common_arguments.retries(),
37+
)
38+
.await?;
3439
Ok(transaction_submitted_json(tx_hash))
3540
}
3641
}

toolkit/smart-contracts/offchain/src/assemble_tx.rs renamed to toolkit/smart-contracts/offchain/src/assemble_and_submit_tx.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::await_tx::{AwaitTx, FixedDelayRetries};
1+
use crate::await_tx::AwaitTx;
22
use anyhow::anyhow;
33
use cardano_serialization_lib::{Transaction, Vkeywitness, Vkeywitnesses};
44
use ogmios_client::query_ledger_state::QueryUtxoByUtxoId;
@@ -7,28 +7,8 @@ use ogmios_client::{
77
};
88
use sidechain_domain::{McTxHash, UtxoId};
99

10-
pub trait AssembleTx {
11-
#[allow(async_fn_in_trait)]
12-
async fn assemble_tx(
13-
&self,
14-
retries: &FixedDelayRetries,
15-
transaction: Transaction,
16-
witnesses: Vec<Vkeywitness>,
17-
) -> anyhow::Result<McTxHash>;
18-
}
19-
20-
impl<C: QueryLedgerState + QueryNetwork + Transactions + QueryUtxoByUtxoId> AssembleTx for C {
21-
async fn assemble_tx(
22-
&self,
23-
retries: &FixedDelayRetries,
24-
transaction: Transaction,
25-
witnesses: Vec<Vkeywitness>,
26-
) -> anyhow::Result<McTxHash> {
27-
assemble_tx(transaction, witnesses, self, retries).await
28-
}
29-
}
30-
31-
pub async fn assemble_tx<
10+
/// Adds `witnesses` to `transaction` and submits it with `ogmios_client`.
11+
pub async fn assemble_and_submit_tx<
3212
C: QueryLedgerState + QueryNetwork + Transactions + QueryUtxoByUtxoId,
3313
A: AwaitTx,
3414
>(

toolkit/smart-contracts/offchain/src/await_tx.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,46 @@ use sidechain_domain::UtxoId;
44
use std::time::Duration;
55
use tokio_retry::{Retry, strategy::FixedInterval};
66

7+
/// Trait for different strategies of waiting for a Cardano transaction to complete.
78
pub trait AwaitTx {
89
#[allow(async_fn_in_trait)]
9-
async fn await_tx_output<Q: QueryUtxoByUtxoId>(
10+
/// This is used for waiting until the output of a submitted transaction can be observed.
11+
/// TODO: make this take a Transaction ID instead of a UtxoId
12+
async fn await_tx_output<C: QueryUtxoByUtxoId>(
1013
&self,
11-
query: &Q,
14+
client: &C,
1215
utxo_id: UtxoId,
1316
) -> anyhow::Result<()>;
1417
}
1518

19+
/// Transaction awaiting strategy that uses fixed number of retries and a fixed delay.
1620
pub struct FixedDelayRetries {
1721
delay: Duration,
1822
retries: usize,
1923
}
2024

2125
impl FixedDelayRetries {
26+
/// Constructs [FixedDelayRetries] with `delay` [Duration] and `retries` number of maximum retries.
2227
pub fn new(delay: Duration, retries: usize) -> Self {
2328
Self { delay, retries }
2429
}
2530

31+
/// Constructs [FixedDelayRetries] that keeps retrying every 5 seconds for 5 minutes.
2632
pub fn five_minutes() -> Self {
2733
Self { delay: Duration::from_secs(5), retries: 59 }
2834
}
2935
}
3036

3137
impl AwaitTx for FixedDelayRetries {
32-
async fn await_tx_output<Q: QueryUtxoByUtxoId>(
38+
async fn await_tx_output<C: QueryUtxoByUtxoId>(
3339
&self,
34-
query: &Q,
40+
client: &C,
3541
utxo_id: UtxoId,
3642
) -> anyhow::Result<()> {
3743
let strategy = FixedInterval::new(self.delay).take(self.retries);
3844
let _ = Retry::spawn(strategy, || async {
3945
log::info!("Probing for transaction output '{}'", utxo_id);
40-
let utxo = query.query_utxo_by_id(utxo_id).await.map_err(|_| ())?;
46+
let utxo = client.query_utxo_by_id(utxo_id).await.map_err(|_| ())?;
4147
utxo.ok_or(())
4248
})
4349
.await

toolkit/smart-contracts/offchain/src/cardano_keys.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,37 @@ use anyhow::anyhow;
22
use cardano_serialization_lib::{PrivateKey, PublicKey};
33
use sidechain_domain::MainchainKeyHash;
44

5-
/// Signing (payment) key abstraction layer. Hides internal crytpo library details.
5+
/// Signing (payment) wrapper layer. Hides internal crypto library details.
66
/// It is either:
7-
/// * 32 bytes regural private key
7+
/// * 32 bytes regular private key
88
/// * 64 bytes extended private key
99
pub struct CardanoPaymentSigningKey(pub(crate) PrivateKey);
1010

1111
impl CardanoPaymentSigningKey {
12+
/// Constructs [CardanoPaymentSigningKey] from 128 byte extended payment signing key.
13+
/// The 128 bytes of the key are: 64 byte prefix, 32 byte verification key, 32 byte chain code
1214
pub fn from_extended_128_bytes(bytes: [u8; 128]) -> anyhow::Result<Self> {
13-
// All 128 bytes are: 64 bytes of prefix, 32 bytes of verification key, 32 bytes of chain code
1415
let prefix: [u8; 64] = bytes[0..64].try_into().unwrap();
1516
Ok(Self(
1617
PrivateKey::from_extended_bytes(&prefix)
1718
.map_err(|e| anyhow!("Couldn't parse 128 bytes into a BIP32 Private Key: {e}"))?,
1819
))
1920
}
2021

22+
/// Constructs [CardanoPaymentSigningKey] from 32 byte payment signing key.
2123
pub fn from_normal_bytes(bytes: [u8; 32]) -> Result<Self, anyhow::Error> {
2224
Ok(Self(
2325
PrivateKey::from_normal_bytes(&bytes)
2426
.map_err(|e| anyhow!("Couldn't parse 32 bytes into a Private Key: {e}"))?,
2527
))
2628
}
2729

30+
/// Signs `message` with the [CardanoPaymentSigningKey].
2831
pub fn sign(&self, message: &[u8]) -> Result<Vec<u8>, anyhow::Error> {
2932
Ok(self.0.sign(message).to_bytes())
3033
}
3134

35+
/// Hashes [CardanoPaymentSigningKey] to domain type [MainchainKeyHash].
3236
pub fn to_pub_key_hash(&self) -> MainchainKeyHash {
3337
MainchainKeyHash(
3438
self.0
@@ -41,10 +45,12 @@ impl CardanoPaymentSigningKey {
4145
)
4246
}
4347

48+
/// Converts [CardanoPaymentSigningKey] to CSL [PublicKey].
4449
pub fn to_csl_pub_key(&self) -> PublicKey {
4550
self.0.to_public()
4651
}
4752

53+
/// Returns raw bytes of [CardanoPaymentSigningKey].
4854
pub fn to_bytes(&self) -> Vec<u8> {
4955
self.0.as_bytes()
5056
}
@@ -81,12 +87,18 @@ impl Clone for CardanoPaymentSigningKey {
8187

8288
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
8389
#[serde(rename_all = "camelCase")]
90+
/// Type representing Cardano key file.
91+
///
92+
/// Note: Field names are used for serialization, do not rename.
8493
pub struct CardanoKeyFileContent {
94+
/// Type of the Cardano key.
8595
pub r#type: String,
96+
/// CBOR hex of the key.
8697
pub cbor_hex: String,
8798
}
8899

89100
impl CardanoKeyFileContent {
101+
/// Parses file to [CardanoKeyFileContent].
90102
pub fn parse_file(path: &str) -> anyhow::Result<Self> {
91103
let file_content = std::fs::read_to_string(path)
92104
.map_err(|e| anyhow!("Could not read Cardano key file at: {path}. Cause: {e}"))?;

0 commit comments

Comments
 (0)