Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add submitpackage RPC #371

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,22 @@ pub trait RpcApi: Sized {
self.call("testmempoolaccept", &[hexes.into()])
}

/// Submit a package of raw transactions to the node. The package will be
/// validated according to consensus and mempool policy rules. If all
/// transactions pass, they will be accepted to mempool.
fn submit_package<R: RawTx>(
&self,
rawtxs: &[R],
maxfeerate: Option<f64>,
maxburnamount: Option<f64>,
) -> Result<json::SubmitPackageResult> {
let hexes: Vec<serde_json::Value> =
rawtxs.to_vec().into_iter().map(|r| r.raw_hex().into()).collect();
let mut args = [hexes.into(), opt_into_json(maxfeerate)?, opt_into_json(maxburnamount)?];
let defaults = [null(), null()];
self.call("submitpackage", handle_defaults(&mut args, &defaults))
}

fn stop(&self) -> Result<String> {
self.call("stop", &[])
}
Expand Down
90 changes: 90 additions & 0 deletions integration_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ fn main() {
test_decode_raw_transaction(&cl);
test_fund_raw_transaction(&cl);
test_test_mempool_accept(&cl);
test_submit_package(&cl);
test_wallet_create_funded_psbt(&cl);
test_wallet_process_psbt(&cl);
test_join_psbt(&cl);
Expand Down Expand Up @@ -770,6 +771,95 @@ fn test_test_mempool_accept(cl: &Client) {
assert!(res[0].allowed, "not allowed: {:?}", res[0].reject_reason);
}

fn test_submit_package(cl: &Client) {
let sk = PrivateKey {
network: Network::Regtest.into(),
inner: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()),
compressed: true,
};
let pk = CompressedPublicKey::from_private_key(&SECP, &sk).unwrap();
let addr = Address::p2wpkh(&pk, Network::Regtest);

let options = json::ListUnspentQueryOptions {
minimum_amount: Some(btc(2)),
..Default::default()
};
let unspent = cl.list_unspent(Some(6), None, None, None, Some(options)).unwrap();
let unspent = unspent.into_iter().nth(0).unwrap();

let tx = Transaction {
version: transaction::Version::ONE,
lock_time: LockTime::ZERO,
input: vec![TxIn {
previous_output: OutPoint {
txid: unspent.txid,
vout: unspent.vout,
},
sequence: Sequence::MAX,
script_sig: ScriptBuf::new(),
witness: Witness::new(),
}],
output: vec![TxOut {
value: (unspent.amount - *FEE),
script_pubkey: addr.script_pubkey(),
}],
};

let input = json::SignRawTransactionInput {
txid: unspent.txid,
vout: unspent.vout,
script_pub_key: unspent.script_pub_key,
redeem_script: None,
amount: Some(unspent.amount),
};
let res1 = cl.sign_raw_transaction_with_wallet(&tx, Some(&[input]), None).unwrap();
assert!(res1.complete);
let tx1 = res1.transaction().unwrap();
let txid1 = tx1.compute_txid();

let tx = Transaction {
version: transaction::Version::ONE,
lock_time: LockTime::ZERO,
input: vec![TxIn {
previous_output: OutPoint {
txid: txid1,
vout: 0,
},
script_sig: ScriptBuf::new(),
sequence: Sequence::MAX,
witness: Witness::new(),
}],
output: vec![TxOut {
value: (unspent.amount - *FEE - *FEE),
script_pubkey: RANDOM_ADDRESS.script_pubkey(),
}],
};

let input = json::SignRawTransactionInput {
txid: txid1,
vout: 0,
script_pub_key: tx1.output[0].script_pubkey.clone(),
redeem_script: None,
amount: Some(tx1.output[0].value),
};

let res2 = cl
.sign_raw_transaction_with_key(&tx, &[sk], Some(&[input]), Some(sighash::EcdsaSighashType::All.into()))
.unwrap();
assert!(res2.complete);

let tx2 = res2.transaction().unwrap();

let signed_transactions = vec![tx1, tx2];

let signed_refs: Vec<&Transaction> = signed_transactions.iter().map(|s| s).collect();
let signed_refs = signed_refs.as_slice();

let res = cl.submit_package(signed_refs, None, None).unwrap();

assert!(res.package_msg == "success");
}

fn test_wallet_create_funded_psbt(cl: &Client) {
let addr = cl.get_new_address(None, None).unwrap();
let options = json::ListUnspentQueryOptions {
Expand Down
50 changes: 50 additions & 0 deletions json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,56 @@ pub struct TestMempoolAcceptResultFees {
// unlike GetMempoolEntryResultFees, this only has the `base` fee
}

#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct Fees {
/// Transaction fee.
#[serde(with = "bitcoin::amount::serde::as_btc")]
pub base: Amount,

/// If the transaction was not already in the mempool, the effective feerate
/// in BTC per KvB. For example, the package feerate and/or feerate with
/// modified fees from prioritisetransaction.
#[serde(default, rename = "effective-feerate", with = "bitcoin::amount::serde::as_btc::opt")]
pub effective_feerate: Option<Amount>,

/// If effective-feerate is provided, the wtxids of the transactions whose
/// fees and vsizes are included in effective-feerate.
#[serde(rename = "effective-includes")]
pub effective_includes: Option<Vec<bitcoin::Wtxid>>,
}

#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct TxResult {
pub txid: bitcoin::Txid,

/// The wtxid of a different transaction with the same txid but different
/// witness found in the mempool. This means the submitted transaction was
/// ignored.
#[serde(rename = "other-wtxid")]
pub other_wtxid: Option<bitcoin::Wtxid>,

/// Virtual transaction size as defined in BIP 141.
pub vsize: Option<u64>,

pub fees: Option<Fees>,

pub error: Option<String>,
}

#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct SubmitPackageResult {
/// The transaction package result message. "success" indicates all transactions were accepted into or are already in the mempool
pub package_msg: String,

/// Transaction results keyed by wtxid.
#[serde(rename = "tx-results")]
pub tx_results: HashMap<bitcoin::Wtxid, TxResult>,

/// List of txids of replaced transactions.
#[serde(rename = "replaced-transactions")]
pub replaced_transactions: Vec<bitcoin::Txid>,
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Bip9SoftforkStatus {
Expand Down