rpc.rs 14 KB
Newer Older
1 2
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::network::serialize::{deserialize, serialize};
3
use bitcoin::util::hash::Sha256dHash;
4
use error_chain::ChainedError;
5
use hex;
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
6
use serde_json::{from_str, Number, Value};
Roman Zeyde's avatar
Roman Zeyde committed
7
use std::collections::HashMap;
8
use std::io::{BufRead, BufReader, Write};
9
use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream};
10
use std::sync::mpsc::{sync_channel, Receiver, RecvTimeoutError, SyncSender};
Roman Zeyde's avatar
Roman Zeyde committed
11
use std::sync::Arc;
12
use std::thread;
13
use std::time::Duration;
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
14

15
use query::Query;
Roman Zeyde's avatar
Roman Zeyde committed
16
use util::HeaderEntry;
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
17

Roman Zeyde's avatar
Roman Zeyde committed
18
use errors::*;
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
19

Roman Zeyde's avatar
Roman Zeyde committed
20
// TODO: Sha256dHash should be a generic hash-container (since script hash is single SHA256)
Roman Zeyde's avatar
Roman Zeyde committed
21 22
fn hash_from_value(val: Option<&Value>) -> Result<Sha256dHash> {
    let script_hash = val.chain_err(|| "missing hash")?;
Roman Zeyde's avatar
Roman Zeyde committed
23 24 25 26 27
    let script_hash = script_hash.as_str().chain_err(|| "non-string hash")?;
    let script_hash = Sha256dHash::from_hex(script_hash).chain_err(|| "non-hex hash")?;
    Ok(script_hash)
}

28 29
fn jsonify_header(entry: &HeaderEntry) -> Value {
    let header = entry.header();
Roman Zeyde's avatar
Roman Zeyde committed
30
    json!({
31
        "block_height": entry.height(),
Roman Zeyde's avatar
Roman Zeyde committed
32 33 34 35 36 37 38 39 40
        "version": header.version,
        "prev_block_hash": header.prev_blockhash.be_hex_string(),
        "merkle_root": header.merkle_root.be_hex_string(),
        "timestamp": header.time,
        "bits": header.bits,
        "nonce": header.nonce
    })
}

41 42
struct Connection {
    query: Arc<Query>,
Roman Zeyde's avatar
Roman Zeyde committed
43
    last_header_entry: Option<HeaderEntry>,
44
    status_hashes: HashMap<Sha256dHash, Value>, // ScriptHash -> StatusHash
45 46
    stream: TcpStream,
    addr: SocketAddr,
47 48
}

49 50
impl Connection {
    pub fn new(query: Arc<Query>, stream: TcpStream, addr: SocketAddr) -> Connection {
51
        Connection {
52
            query: query,
Roman Zeyde's avatar
Roman Zeyde committed
53
            last_header_entry: None, // disable header subscription for now
54
            status_hashes: HashMap::new(),
55 56
            stream,
            addr,
57 58 59 60
        }
    }

    fn blockchain_headers_subscribe(&mut self) -> Result<Value> {
61
        let entry = self.query.get_best_header()?;
Roman Zeyde's avatar
Roman Zeyde committed
62
        self.last_header_entry = Some(entry.clone());
63
        Ok(jsonify_header(&entry))
64
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
65

66
    fn server_version(&self) -> Result<Value> {
67
        Ok(json!(["RustElectrum 0.1.0", "1.2"]))
68
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
69

70
    fn server_banner(&self) -> Result<Value> {
71
        Ok(json!("Welcome to RustElectrum Server!\n"))
72
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
73

74
    fn server_donation_address(&self) -> Result<Value> {
75
        Ok(Value::Null)
76
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
77

78 79 80
    fn server_peers_subscribe(&self) -> Result<Value> {
        Ok(json!([]))
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
81

82
    fn mempool_get_fee_histogram(&self) -> Result<Value> {
83
        Ok(json!(self.query.get_fee_histogram()))
84
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
85

86 87 88 89 90
    fn blockchain_block_get_chunk(&self, params: &[Value]) -> Result<Value> {
        const CHUNK_SIZE: usize = 2016;
        let index = params.get(0).chain_err(|| "missing index")?;
        let index = index.as_u64().chain_err(|| "non-number index")? as usize;
        let heights: Vec<usize> = (0..CHUNK_SIZE).map(|h| index * CHUNK_SIZE + h).collect();
Roman Zeyde's avatar
Roman Zeyde committed
91 92 93
        let headers: Vec<String> = self.query
            .get_headers(&heights)
            .into_iter()
94
            .map(|entry| hex::encode(&serialize(entry.header()).unwrap()))
Roman Zeyde's avatar
Roman Zeyde committed
95 96
            .collect();
        Ok(json!(headers.join("")))
97 98
    }

Roman Zeyde's avatar
Roman Zeyde committed
99 100 101
    fn blockchain_block_get_header(&self, params: &[Value]) -> Result<Value> {
        let height = params.get(0).chain_err(|| "missing height")?;
        let height = height.as_u64().chain_err(|| "non-number height")? as usize;
102 103 104 105 106 107
        let mut entries = self.query.get_headers(&[height]);
        let entry = entries
            .pop()
            .chain_err(|| format!("missing header #{}", height))?;
        assert_eq!(entries.len(), 0);
        Ok(json!(jsonify_header(&entry)))
Roman Zeyde's avatar
Roman Zeyde committed
108 109
    }

110 111 112 113 114
    fn blockchain_estimatefee(&self, params: &[Value]) -> Result<Value> {
        let blocks = params.get(0).chain_err(|| "missing blocks")?;
        let blocks = blocks.as_u64().chain_err(|| "non-number blocks")? as usize;
        let fee_rate = self.query.estimate_fee(blocks); // in BTC/kB
        Ok(json!(fee_rate))
115
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
116

Roman Zeyde's avatar
Roman Zeyde committed
117
    fn blockchain_relayfee(&self) -> Result<Value> {
118
        Ok(json!(0.0)) // allow sending transactions with any fee.
Roman Zeyde's avatar
Roman Zeyde committed
119 120
    }

121
    fn blockchain_scripthash_subscribe(&mut self, params: &[Value]) -> Result<Value> {
Roman Zeyde's avatar
Roman Zeyde committed
122
        let script_hash = hash_from_value(params.get(0)).chain_err(|| "bad script_hash")?;
123
        let status = self.query.status(&script_hash[..])?;
124
        let result = status.hash().map_or(Value::Null, |h| json!(hex::encode(h)));
125 126
        self.status_hashes.insert(script_hash, result.clone());
        Ok(result)
127
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
128

129
    fn blockchain_scripthash_get_balance(&self, params: &[Value]) -> Result<Value> {
Roman Zeyde's avatar
Roman Zeyde committed
130
        let script_hash = hash_from_value(params.get(0)).chain_err(|| "bad script_hash")?;
131
        let status = self.query.status(&script_hash[..])?;
132 133 134
        Ok(
            json!({ "confirmed": status.confirmed_balance(), "unconfirmed": status.mempool_balance() }),
        )
135
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
136

Roman Zeyde's avatar
Roman Zeyde committed
137
    fn blockchain_scripthash_get_history(&self, params: &[Value]) -> Result<Value> {
Roman Zeyde's avatar
Roman Zeyde committed
138
        let script_hash = hash_from_value(params.get(0)).chain_err(|| "bad script_hash")?;
139
        let status = self.query.status(&script_hash[..])?;
Roman Zeyde's avatar
Roman Zeyde committed
140
        Ok(json!(Value::Array(
141 142
            status
                .history()
Roman Zeyde's avatar
Roman Zeyde committed
143 144 145 146
                .into_iter()
                .map(|item| json!({"height": item.0, "tx_hash": item.1.be_hex_string()}))
                .collect()
        )))
147
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
148

149 150 151 152 153
    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")?;
154
        let txid = self.query.broadcast(&tx)?;
155 156 157
        Ok(json!(txid.be_hex_string()))
    }

158
    fn blockchain_transaction_get(&self, params: &[Value]) -> Result<Value> {
Roman Zeyde's avatar
Roman Zeyde committed
159
        // TODO: handle 'verbose' param
Roman Zeyde's avatar
Roman Zeyde committed
160
        let tx_hash = hash_from_value(params.get(0)).chain_err(|| "bad tx_hash")?;
161
        let tx = self.query.load_txn(&tx_hash, /*blockhash=*/ None)?;
162
        Ok(json!(hex::encode(&serialize(&tx).unwrap())))
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
163 164
    }

Roman Zeyde's avatar
Roman Zeyde committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    fn blockchain_transaction_get_merkle(&self, params: &[Value]) -> Result<Value> {
        let tx_hash = hash_from_value(params.get(0)).chain_err(|| "bad tx_hash")?;
        let height = params.get(1).chain_err(|| "missing height")?;
        let height = height.as_u64().chain_err(|| "non-number height")? as usize;
        let (merkle, pos) = self.query
            .get_merkle_proof(&tx_hash, height)
            .chain_err(|| "cannot create merkle proof")?;
        let merkle: Vec<String> = merkle
            .into_iter()
            .map(|txid| txid.be_hex_string())
            .collect();
        Ok(json!({
                "block_height": height,
                "merkle": merkle,
                "pos": pos}))
180
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
181

182
    fn handle_command(&mut self, method: &str, params: &[Value], id: &Number) -> Result<Value> {
183 184 185 186 187 188 189
        let result = match method {
            "blockchain.headers.subscribe" => self.blockchain_headers_subscribe(),
            "server.version" => self.server_version(),
            "server.banner" => self.server_banner(),
            "server.donation_address" => self.server_donation_address(),
            "server.peers.subscribe" => self.server_peers_subscribe(),
            "mempool.get_fee_histogram" => self.mempool_get_fee_histogram(),
190
            "blockchain.block.get_chunk" => self.blockchain_block_get_chunk(&params),
Roman Zeyde's avatar
Roman Zeyde committed
191
            "blockchain.block.get_header" => self.blockchain_block_get_header(&params),
192
            "blockchain.estimatefee" => self.blockchain_estimatefee(&params),
Roman Zeyde's avatar
Roman Zeyde committed
193
            "blockchain.relayfee" => self.blockchain_relayfee(),
194 195 196
            "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),
197
            "blockchain.transaction.broadcast" => self.blockchain_transaction_broadcast(&params),
198 199 200
            "blockchain.transaction.get" => self.blockchain_transaction_get(&params),
            "blockchain.transaction.get_merkle" => self.blockchain_transaction_get_merkle(&params),
            &_ => bail!("unknown method {} {:?}", method, params),
201
        };
202
        // TODO: return application errors should be sent to the client
203 204 205
        Ok(match result {
            Ok(result) => json!({"jsonrpc": "2.0", "id": id, "result": result}),
            Err(e) => {
Roman Zeyde's avatar
Roman Zeyde committed
206 207 208 209 210 211 212
                warn!(
                    "rpc #{} {} {:?} failed: {}",
                    id,
                    method,
                    params,
                    e.display_chain()
                );
213 214 215
                json!({"jsonrpc": "2.0", "id": id, "error": format!("{}", e)})
            }
        })
216 217
    }

Roman Zeyde's avatar
Roman Zeyde committed
218
    fn update_subscriptions(&mut self) -> Result<Vec<Value>> {
219
        let mut result = vec![];
Roman Zeyde's avatar
Roman Zeyde committed
220
        if let Some(ref mut last_entry) = self.last_header_entry {
221
            let entry = self.query.get_best_header()?;
Roman Zeyde's avatar
Roman Zeyde committed
222 223
            if *last_entry != entry {
                *last_entry = entry;
224
                let header = jsonify_header(last_entry);
Roman Zeyde's avatar
Roman Zeyde committed
225 226 227 228 229
                result.push(json!({
                    "jsonrpc": "2.0",
                    "method": "blockchain.headers.subscribe",
                    "params": [header]}));
            }
230 231
        }
        for (script_hash, status_hash) in self.status_hashes.iter_mut() {
232
            let status = self.query.status(&script_hash[..])?;
233
            let new_status_hash = status.hash().map_or(Value::Null, |h| json!(hex::encode(h)));
234 235 236 237 238 239 240 241 242 243 244 245
            if new_status_hash == *status_hash {
                continue;
            }
            result.push(json!({
                "jsonrpc": "2.0",
                "method": "blockchain.scripthash.subscribe",
                "params": [script_hash.be_hex_string(), new_status_hash]}));
            *status_hash = new_status_hash;
        }
        Ok(result)
    }

246 247 248 249 250 251 252 253 254
    fn send_values(&mut self, values: &[Value]) -> Result<()> {
        for value in values {
            debug!("[{}] <- {}", self.addr, value);
            let line = value.to_string() + "\n";
            self.stream
                .write_all(line.as_bytes())
                .chain_err(|| format!("failed to send {}", value))?;
        }
        Ok(())
255 256 257
    }

    fn handle_replies(&mut self, chan: &Channel) -> Result<()> {
258
        let poll_duration = Duration::from_secs(5);
Roman Zeyde's avatar
Roman Zeyde committed
259 260
        let rx = chan.receiver();
        loop {
261 262 263 264 265
            let msg = match rx.recv_timeout(poll_duration) {
                Ok(msg) => msg,
                Err(RecvTimeoutError::Timeout) => Message::PeriodicUpdate,
                Err(RecvTimeoutError::Disconnected) => bail!("channel closed"),
            };
Roman Zeyde's avatar
Roman Zeyde committed
266 267 268
            match msg {
                Message::Request(line) => {
                    let cmd: Value = from_str(&line).chain_err(|| "invalid JSON format")?;
269
                    debug!("[{}] -> {}", self.addr, cmd);
Roman Zeyde's avatar
Roman Zeyde committed
270 271 272 273 274 275 276 277
                    let reply = match (cmd.get("method"), cmd.get("params"), cmd.get("id")) {
                        (
                            Some(&Value::String(ref method)),
                            Some(&Value::Array(ref params)),
                            Some(&Value::Number(ref id)),
                        ) => self.handle_command(method, params, id)?,
                        _ => bail!("invalid command: {}", cmd),
                    };
278
                    self.send_values(&[reply])?
279
                }
280
                Message::PeriodicUpdate => {
281 282 283
                    let values = self.update_subscriptions()
                        .chain_err(|| "failed to update subscriptions")?;
                    self.send_values(&values)?
Roman Zeyde's avatar
Roman Zeyde committed
284 285 286 287
                }
                Message::Done => {
                    debug!("done");
                    break;
288
                }
289
            }
Roman Zeyde's avatar
Roman Zeyde committed
290 291 292 293
        }
        Ok(())
    }

294
    fn handle_requests(mut reader: BufReader<TcpStream>, tx: SyncSender<Message>) {
Roman Zeyde's avatar
Roman Zeyde committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308
        loop {
            let mut line = String::new();
            reader
                .read_line(&mut line)  // TODO: use .lines() iterator
                .expect("failed to read a request");
            if line.is_empty() {
                tx.send(Message::Done).expect("channel closed");
                break;
            } else {
                tx.send(Message::Request(line)).expect("channel closed");
            }
        }
    }

309
    pub fn run(mut self) {
310
        let reader = BufReader::new(self.stream.try_clone().expect("failed to clone TcpStream"));
311 312 313 314 315 316 317 318 319 320
        let chan = Channel::new();
        let tx = chan.sender();
        let child = thread::spawn(|| Connection::handle_requests(reader, tx));
        if let Err(e) = self.handle_replies(&chan) {
            error!(
                "[{}] connection handling failed: {}",
                self.addr,
                e.display_chain().to_string()
            );
        }
321
        info!("[{}] shutting down connection", self.addr);
322 323 324 325
        let _ = self.stream.shutdown(Shutdown::Both);
        if child.join().is_err() {
            error!("[{}] receiver panicked", self.addr);
        }
326 327 328 329 330
    }
}

pub enum Message {
    Request(String),
331
    PeriodicUpdate,
332 333 334 335 336 337 338 339 340 341
    Done,
}

pub struct Channel {
    tx: SyncSender<Message>,
    rx: Receiver<Message>,
}

impl Channel {
    pub fn new() -> Channel {
342
        let (tx, rx) = sync_channel(10);
343 344 345 346 347 348 349 350 351
        Channel { tx, rx }
    }

    pub fn sender(&self) -> SyncSender<Message> {
        self.tx.clone()
    }

    pub fn receiver(&self) -> &Receiver<Message> {
        &self.rx
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
352 353 354
    }
}

355 356
pub fn start(addr: &SocketAddr, query: Arc<Query>) -> thread::JoinHandle<()> {
    let listener = TcpListener::bind(addr).expect(&format!("bind({}) failed", addr));
357
    info!("RPC server running on {}", addr);
358 359
    thread::spawn(move || loop {
        let (stream, addr) = listener.accept().expect("accept failed");
360 361 362 363 364 365
        let query = query.clone();
        thread::spawn(move || {
            info!("[{}] connected peer", addr);
            Connection::new(query, stream, addr).run();
            info!("[{}] disconnected peer", addr);
        });
366
    })
Roman Zeyde's avatar
Roman Zeyde committed
367
}