1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 01:43:29 +01:00

Support transaction broadcast RPC

This commit is contained in:
Roman Zeyde 2018-05-17 15:52:38 +03:00
parent 31abb3e538
commit 4a8f7b5c0b
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
3 changed files with 29 additions and 2 deletions

View File

@ -2,7 +2,7 @@ use base64;
use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::network::serialize::BitcoinHash;
use bitcoin::network::serialize::deserialize;
use bitcoin::network::serialize::{deserialize, serialize};
use bitcoin::util::hash::Sha256dHash;
use hex;
use serde_json::{from_str, Value};
@ -233,6 +233,15 @@ impl Daemon {
Ok(MempoolEntry::new(fee, vsize))
}
pub fn broadcast(&self, tx: &Transaction) -> Result<Sha256dHash> {
let tx = hex::encode(serialize(tx).unwrap());
let txid = self.request("sendrawtransaction", json!([tx]))?;
Ok(
Sha256dHash::from_hex(txid.as_str().chain_err(|| "non-string txid")?)
.chain_err(|| "failed to parse txid")?,
)
}
pub fn get_all_headers(&self) -> Result<HeaderMap> {
let info: Value = self.request("getblockchaininfo", json!([]))?;
let max_height = info.get("blocks")

View File

@ -87,6 +87,10 @@ impl<'a> Query<'a> {
}
}
pub fn daemon(&self) -> &Daemon {
self.daemon
}
fn load_txns(&self, prefixes: Vec<HashPrefix>) -> Vec<TxnHeight> {
let mut txns = Vec::new();
for txid_prefix in prefixes {

View File

@ -1,5 +1,6 @@
use bitcoin::blockdata::block::BlockHeader;
use bitcoin::network::serialize::serialize;
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::network::serialize::{deserialize, serialize};
use bitcoin::util::hash::Sha256dHash;
use crossbeam;
use crypto::digest::Digest;
@ -165,6 +166,18 @@ impl<'a> Handler<'a> {
)))
}
fn blockchain_transaction_broadcast(&self, params: &[Value]) -> Result<Value> {
let tx = params.get(0).chain_err(|| "missing tx")?;
let tx = tx.as_str().chain_err(|| "non-string tx")?;
let tx = hex::decode(&tx).chain_err(|| "non-hex tx")?;
let tx: Transaction = deserialize(&tx).chain_err(|| "failed to parse tx")?;
let txid = self.query
.daemon()
.broadcast(&tx)
.chain_err(|| "broadcast failed")?;
Ok(json!(txid.be_hex_string()))
}
fn blockchain_transaction_get(&self, params: &[Value]) -> Result<Value> {
// TODO: handle 'verbose' param
let tx_hash = hash_from_value(params.get(0)).chain_err(|| "bad tx_hash")?;
@ -204,6 +217,7 @@ impl<'a> Handler<'a> {
"blockchain.scripthash.subscribe" => self.blockchain_scripthash_subscribe(&params),
"blockchain.scripthash.get_balance" => self.blockchain_scripthash_get_balance(&params),
"blockchain.scripthash.get_history" => self.blockchain_scripthash_get_history(&params),
"blockchain.transaction.broadcast" => self.blockchain_transaction_broadcast(&params),
"blockchain.transaction.get" => self.blockchain_transaction_get(&params),
"blockchain.transaction.get_merkle" => self.blockchain_transaction_get_merkle(&params),
&_ => bail!("unknown method {} {:?}", method, params),