Unverified Commit 321d655d authored by Roman Zeyde's avatar Roman Zeyde
Browse files

Support 'blockchain.scripthash.listunspent' Electrum RPC

parent 7ca32964
......@@ -16,16 +16,19 @@ use util::{FullHash, HashPrefix, HeaderEntry};
use errors::*;
struct FundingOutput {
txn_id: Sha256dHash,
height: u32,
output_index: usize,
value: u64,
pub struct FundingOutput {
pub txn_id: Sha256dHash,
pub height: u32,
pub output_index: usize,
pub value: u64,
}
type OutPoint = (Sha256dHash, usize); // (txid, output_index)
struct SpendingInput {
txn_id: Sha256dHash,
height: u32,
funding_output: OutPoint,
value: u64,
}
......@@ -71,6 +74,23 @@ impl Status {
txns
}
pub fn unspent(&self) -> Vec<&FundingOutput> {
let mut outputs_map = HashMap::<OutPoint, &FundingOutput>::new();
for f in self.funding() {
outputs_map.insert((f.txn_id, f.output_index), f);
}
for s in self.spending() {
if let None = outputs_map.remove(&s.funding_output) {
warn!("failed to remove {:?}", s.funding_output);
}
}
outputs_map
.into_iter()
.map(|item| item.1) // a reference to unspent output
.collect::<Vec<&FundingOutput>>()
// TODO: sort the outputs by height
}
pub fn hash(&self) -> Option<FullHash> {
let txns = self.history();
if txns.is_empty() {
......@@ -210,6 +230,7 @@ impl Query {
spending_inputs.push(SpendingInput {
txn_id: t.txn.txid(),
height: t.height,
funding_output: (funding.txn_id, funding.output_index),
value: funding.value,
})
}
......
......@@ -164,6 +164,25 @@ impl Connection {
)))
}
fn blockchain_scripthash_listunspent(&self, params: &[Value]) -> Result<Value> {
let script_hash = hash_from_value(params.get(0)).chain_err(|| "bad script_hash")?;
let status = self.query.status(&script_hash[..])?;
Ok(json!(Value::Array(
status
.unspent()
.into_iter()
.map(|out| {
json!({
"height": out.height,
"tx_pos": out.output_index,
"tx_hash": out.txn_id.be_hex_string(),
"value": out.value,
})
})
.collect()
)))
}
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")?;
......@@ -215,6 +234,7 @@ impl Connection {
"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.scripthash.listunspent" => self.blockchain_scripthash_listunspent(&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),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment