From 18167be1e55582075a8e27f7dc1de7c959cb74a8 Mon Sep 17 00:00:00 2001 From: jwinterm Date: Sun, 7 Jun 2026 19:50:52 -0400 Subject: [PATCH] Contracts: tidy review TODOs, document the owner API, simplify get_net_change Comment/doc cleanups from review; the only behavior-adjacent change is the get_net_change signature. - get_net_change takes the slate id instead of the whole slate. - replace the owner-API contract methods' `/// TODO` placeholders with real docs. - narrow or remove now-resolved TODOs (slate proof fns, context, utils stored_tx, a doc-comment that should be //, wallet_args parse notes); reword the rest as concrete FUTURE notes. - prune the contract actions README of items already fixed. Addresses PR #754 review (comment hygiene). --- api/src/owner.rs | 12 ++++++------ libwallet/src/contract/actions/README.md | 5 ----- libwallet/src/contract/actions/sign.rs | 2 +- libwallet/src/contract/context.rs | 7 +++---- libwallet/src/contract/proofs.rs | 3 ++- libwallet/src/contract/slate.rs | 6 ++---- libwallet/src/contract/utils.rs | 13 ++++++------- src/cmd/wallet_args.rs | 14 +++----------- 8 files changed, 23 insertions(+), 39 deletions(-) diff --git a/api/src/owner.rs b/api/src/owner.rs index d72b2ca..d7abe22 100644 --- a/api/src/owner.rs +++ b/api/src/owner.rs @@ -773,7 +773,7 @@ where owner::issue_invoice_tx(&mut **w, keychain_mask, args, self.doctest_mode) } - /// TODO + /// Initiate a new contract. Also performs the initial setup on the slate. pub fn contract_new( &self, keychain_mask: Option<&SecretKey>, @@ -781,11 +781,10 @@ where ) -> Result { let mut w_lock = self.wallet_inst.lock(); let w = w_lock.lc_provider()?.wallet_inst()?; - // TODO: self.doctest_mode ? owner::contract_new(&mut **w, keychain_mask, &args) } - /// TODO + /// Sign a contract, running setup first if it has not been done yet. pub fn contract_sign( &self, keychain_mask: Option<&SecretKey>, @@ -797,7 +796,7 @@ where owner::contract_sign(&mut **w, keychain_mask, &args, &slate) } - /// TODO + /// Return the participant index in the slate that matches this wallet's context. pub fn get_slate_index_matching_my_context( &self, keychain_mask: Option<&SecretKey>, @@ -808,7 +807,7 @@ where owner::get_slate_index_matching_my_context(&mut **w, keychain_mask, &slate) } - /// TODO + /// Revoke a contract by double-spending one of its locked inputs. pub fn contract_revoke( &self, keychain_mask: Option<&SecretKey>, @@ -2408,7 +2407,8 @@ where ) } - /// TODO: Temporary, likely should merge with above + /// Retrieve the invoice payment proof for a stored transaction. + /// FUTURE: likely merge with retrieve_payment_proof above. pub fn retrieve_payment_proof_invoice( &self, keychain_mask: Option<&SecretKey>, diff --git a/libwallet/src/contract/actions/README.md b/libwallet/src/contract/actions/README.md index a490669..1b192cd 100644 --- a/libwallet/src/contract/actions/README.md +++ b/libwallet/src/contract/actions/README.md @@ -33,20 +33,15 @@ Separating side effects until the 'save_step' part would make these functions mu #### TODOs - for payjoins, instead of doing Some("any"), find an actual input and put the actual commitment in --use-inputs (only do that if none is added). Think about how to do that in a way that would be nice also for the API. Maybe the API should just take "any" which gets transformed into one of the random inputs. Maybe the "any" is ok. It seems to work fine, might be better if we selected the input though. - - make sure to forget the Context when we sign or at least forget the secret keys for it to avoid signing with the same nonce twice - make_outputs api should receive nanogrins rather than grin. We have to make the conversion before calling the API - sometimes the slatepack outputs with a \n for some reason which makes pasting it register \n as the end of paste and crashes? - remove casting decimals, we should accept value in nanogrins (including --make-outputs option), never as 0.1 Grin through the interface. Casting should be left to the gui wallet logic - - Check casts to/from i64/u64 etc. consider using saturating methods. Make sure conversions are safe. - separate side-effects out from the main computations - is keys::next_available_key(..) safe from race-conditions? (do we lock?) - - function add_output_to_ctx has a comment around next_available_key - - add support for different accounts not just main (check parent_id, parent_key_id, etc. usage) - ensure counterparty can't make you overpay fees through num_participants param - make sure the stored transaction is saved correctly at each step (TxLogEntry has stored_tx field, check other fields as well) - make sure the transaction log contains all the necessary data (check TODO comments on tx log entry functions) - graceful error handling - - setup.rs# TODO: verify that the parent_key_id is consistent - replace mutable objects with immutable when possible - make sure we lock the wallet when needed (check wallet_lock!() macro that is used in api/owner.rs) - add support for more than 2 parties (includes a new 'setup' API endpoint and command) diff --git a/libwallet/src/contract/actions/sign.rs b/libwallet/src/contract/actions/sign.rs index 544da7f..11b7396 100644 --- a/libwallet/src/contract/actions/sign.rs +++ b/libwallet/src/contract/actions/sign.rs @@ -68,7 +68,7 @@ where // Ensure net_change has been provided let expected_net_change = - contract::utils::get_net_change(w, keychain_mask, &sl, setup_args.net_change)?; + contract::utils::get_net_change(w, keychain_mask, &sl.id, setup_args.net_change)?; // Define the values that must be provided in the setup phase at the sign step let mut setup_args = setup_args.clone(); diff --git a/libwallet/src/contract/context.rs b/libwallet/src/contract/context.rs index 1f967ff..49a0be6 100644 --- a/libwallet/src/contract/context.rs +++ b/libwallet/src/contract/context.rs @@ -70,7 +70,6 @@ fn create<'a, T: ?Sized, C, K>( keychain_mask: Option<&SecretKey>, slate: &mut Slate, current_height: u64, - // TODO: compare with &InitTxArgs to see if any information is missing setup_args: &ContractSetupArgsAPI, parent_key_id: &Identifier, use_test_rng: bool, @@ -120,14 +119,14 @@ where // Create a Context for this slate let keychain = w.keychain(keychain_mask)?; - // TODO: it seems 'is_initiator: true' is only used in test_rng. Do we care about this? + // is_initiator only affects the deterministic test RNG; contracts always pass true. let mut context = Context::new(keychain.secp(), &parent_key_id, use_test_rng, true); // Context.fee will hold _our_ fee contribution and not the total slate fee context.fee = my_fee.as_opt(); // Context.amount is not used in contracts, but we set it anyway. context.amount = slate.amount; - // TODO: looking at what uses Context.late_lock_args, it seems only the args in SelectionArgs are used except - // for args.ttl_blocks. Is this needed? Can we refactor this? + // Store the full setup args on the context; later steps (add_outputs) read the + // SelectionArgs and ttl from here. context.setup_args = Some(setup_args.clone()); debug!( "Setting Context.net_change as: {}", diff --git a/libwallet/src/contract/proofs.rs b/libwallet/src/contract/proofs.rs index 9ec9247..73e0371 100644 --- a/libwallet/src/contract/proofs.rs +++ b/libwallet/src/contract/proofs.rs @@ -373,7 +373,8 @@ where C: NodeClient + 'a, K: Keychain + 'a, { - // TODO: Just generating invoice (type 1) for now + // Invoice (type 1) is the only contract proof type today; other types are future + // work tied to the early-payment-proof RFC. let (invoice_proof, promise_signature, receiver_address) = generate_invoice_signature(wallet, keychain_mask, slate, context, proof_args)?; let timestamp = NaiveDateTime::from_timestamp_opt(Utc::now().timestamp(), 0).unwrap(); diff --git a/libwallet/src/contract/slate.rs b/libwallet/src/contract/slate.rs index 63561e7..9e42772 100644 --- a/libwallet/src/contract/slate.rs +++ b/libwallet/src/contract/slate.rs @@ -40,8 +40,7 @@ where C: NodeClient + 'a, K: Keychain + 'a, { - // TODO: Implement. Consider adding this function to the Slate itself so they can easily be versioned - // e.g. slate.add_payment_proof_data() + // FUTURE: move proof handling onto Slate itself so it can be versioned (slate.add_payment_proof_data()). debug!("contract::slate::add_payment_proof => called"); // If we're a recipient, generate proof unless explicity told not to if let Some(ref c) = net_change { @@ -59,8 +58,7 @@ pub fn verify_payment_proof( net_change: i64, recipient_address: &DalekPublicKey, ) -> Result<(), Error> { - // TODO: Implement. Consider adding this function to the Slate itself so they can easily be versioned - // e.g. slate.verify_payment_proof_sig() + // FUTURE: move proof verification onto Slate itself so it can be versioned (slate.verify_payment_proof_sig()). debug!("contract::slate::verify_payment_proof => called"); if net_change > 0 && slate.payment_proof.is_some() { let invoice_proof = InvoiceProof::from_slate(&slate, 1, None)?; diff --git a/libwallet/src/contract/utils.rs b/libwallet/src/contract/utils.rs index 9a80d5c..e6a8f8e 100644 --- a/libwallet/src/contract/utils.rs +++ b/libwallet/src/contract/utils.rs @@ -44,7 +44,8 @@ pub fn create_tx_log_entry( } }; let mut t = TxLogEntry::new(parent_key_id.clone(), log_type, log_id); - // TODO: TxLogEntry has stored_tx field. Check what this needs to be set to and check other fields as well + // stored_tx on the entry is left unset; contracts persist the tx separately via + // store_tx in save_step. t.tx_slate_id = Some(slate.id); if net_change > 0 { @@ -107,9 +108,8 @@ where sender_address: sender_address.to_ed25519()?, sender_address_path, sender_signature: None, - /// TODO: Will fill these as separate steps for now, check whether this - /// can be merged in a general case (which means knowing which nonces here belong to - /// the recipient) + // Filled as separate steps for now; could be merged into a general case + // once we know which nonces here belong to the recipient. proof_type: Some(1u8), receiver_public_nonce: Some(slate.participant_data[recipient_index].public_nonce), receiver_public_excess: Some( @@ -130,8 +130,7 @@ where pub fn get_net_change<'a, T: ?Sized, C, K>( w: &mut T, keychain_mask: Option<&SecretKey>, - // TODO: make this receive only slate.id instead of passing the whole slate - slate: &Slate, + slate_id: &Uuid, setup_args_net_change: Option, ) -> Result where @@ -140,7 +139,7 @@ where K: Keychain + 'a, { let mut expected_net_change: Option = setup_args_net_change; - match w.get_private_context(keychain_mask, slate.id.as_bytes()) { + match w.get_private_context(keychain_mask, slate_id.as_bytes()) { Ok(ctx) => { debug!("contract::sign => context found"); // We have a context so we must have agreed on a certain net_change value in Context.net_change. diff --git a/src/cmd/wallet_args.rs b/src/cmd/wallet_args.rs index dc80c95..a20680f 100644 --- a/src/cmd/wallet_args.rs +++ b/src/cmd/wallet_args.rs @@ -972,7 +972,6 @@ pub fn parse_verify_proof_args(args: &ArgMatches) -> Result Some(parse_grin_amount(g, "receive")?), @@ -1001,7 +998,6 @@ pub fn parse_contract_new_args( "You must specify either --receive or --send.", ))); }; - // TODO: verify this is correct e.g. which values are passed here by default etc. let src_acct_name = Some(String::from(account)); let add_outputs = args.is_present("add-outputs"); let as_json = args.is_present("as-json"); @@ -1053,18 +1049,15 @@ pub fn parse_contract_new_args( add_outputs: add_outputs, use_inputs: use_inputs, make_outputs: make_outputs, - // TODO: Future features below + // Not yet wired to CLI flags: fee_rate: None, outfile: None, }) } -// TODO: parse args pub fn parse_contract_setup_args( args: &ArgMatches, ) -> Result { - // TODO: Make sure the values are in some expected bounds. - // TODO: How to deal with decimals and precision? we probably want users to express the value in Grin. let counterparty_addr = match args.value_of("encrypt-for") { Some(v) => Some(String::from(v)), None => None, @@ -1114,8 +1107,7 @@ pub fn parse_contract_setup_args( } None => None, }; - // TODO: should we catch if the person calls "--receive=5" when it should be "--send=5"? - // Perhaps we could detect this from the slate state e.g. S1 -> receive, I1 -> send? + // FUTURE: detect a reversed --receive/--send from the slate state (S1 -> receive, I1 -> send). Ok(command::ContractSetupArgs { counterparty_addr: counterparty_addr, @@ -1125,7 +1117,7 @@ pub fn parse_contract_setup_args( add_outputs: false, use_inputs: use_inputs, make_outputs: make_outputs, - // TODO: Future features below + // Not yet wired to CLI flags: fee_rate: None, outfile: None, })