rpc.rs 13 KB
Newer Older
Roman Zeyde's avatar
Roman Zeyde committed
1
use bitcoin::blockdata::block::BlockHeader;
2 3
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::network::serialize::{deserialize, serialize};
4
use bitcoin::util::hash::Sha256dHash;
5
use crossbeam;
6
use hex;
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
7
use serde_json::{from_str, Number, Value};
Roman Zeyde's avatar
Roman Zeyde committed
8
use std::collections::HashMap;
9
use std::io::{BufRead, BufReader, Write};
10
use std::net::{SocketAddr, TcpListener, TcpStream};
11
use std::sync::Arc;
12 13
use std::sync::mpsc::{sync_channel, Receiver, RecvTimeoutError, SyncSender};
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 18 19

error_chain!{}

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
fn jsonify_header(header: &BlockHeader, height: usize) -> Value {
Roman Zeyde's avatar
Roman Zeyde committed
29 30 31 32 33 34 35 36 37 38 39
    json!({
        "block_height": height,
        "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
    })
}

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

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

    fn blockchain_headers_subscribe(&mut self) -> Result<Value> {
Roman Zeyde's avatar
Roman Zeyde committed
60 61 62
        let entry = self.query
            .get_best_header()
            .chain_err(|| "no headers found")?;
Roman Zeyde's avatar
Roman Zeyde committed
63
        self.last_header_entry = Some(entry.clone());
64
        Ok(jsonify_header(entry.header(), entry.height()))
65
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
66

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

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

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

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

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

87 88 89 90 91
    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
92 93 94 95 96 97
        let headers: Vec<String> = self.query
            .get_headers(&heights)
            .into_iter()
            .map(|x| hex::encode(&serialize(&x).unwrap()))
            .collect();
        Ok(json!(headers.join("")))
98 99
    }

Roman Zeyde's avatar
Roman Zeyde committed
100 101 102 103
    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;
        let headers = self.query.get_headers(&vec![height]);
104
        Ok(json!(jsonify_header(&headers[0], height)))
Roman Zeyde's avatar
Roman Zeyde committed
105 106
    }

107 108 109 110 111
    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))
112
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
113

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

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

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

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

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

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

Roman Zeyde's avatar
Roman Zeyde committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    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}))
177
    }
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
178

179
    fn handle_command(&mut self, method: &str, params: &[Value], id: &Number) -> Result<Value> {
180 181 182 183 184 185 186
        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(),
187
            "blockchain.block.get_chunk" => self.blockchain_block_get_chunk(&params),
Roman Zeyde's avatar
Roman Zeyde committed
188
            "blockchain.block.get_header" => self.blockchain_block_get_header(&params),
189
            "blockchain.estimatefee" => self.blockchain_estimatefee(&params),
Roman Zeyde's avatar
Roman Zeyde committed
190
            "blockchain.relayfee" => self.blockchain_relayfee(),
191 192 193
            "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),
194
            "blockchain.transaction.broadcast" => self.blockchain_transaction_broadcast(&params),
195 196 197 198
            "blockchain.transaction.get" => self.blockchain_transaction_get(&params),
            "blockchain.transaction.get_merkle" => self.blockchain_transaction_get_merkle(&params),
            &_ => bail!("unknown method {} {:?}", method, params),
        }?;
199
        // TODO: return application errors should be sent to the client
Roman Zeyde's avatar
Roman Zeyde committed
200
        Ok(json!({"jsonrpc": "2.0", "id": id, "result": result}))
201 202
    }

Roman Zeyde's avatar
Roman Zeyde committed
203
    fn update_subscriptions(&mut self) -> Result<Vec<Value>> {
204
        let mut result = vec![];
Roman Zeyde's avatar
Roman Zeyde committed
205
        if let Some(ref mut last_entry) = self.last_header_entry {
206 207 208
            let entry = self.query
                .get_best_header()
                .chain_err(|| "no headers found")?;
Roman Zeyde's avatar
Roman Zeyde committed
209 210 211 212 213 214 215 216
            if *last_entry != entry {
                *last_entry = entry;
                let header = jsonify_header(last_entry.header(), last_entry.height());
                result.push(json!({
                    "jsonrpc": "2.0",
                    "method": "blockchain.headers.subscribe",
                    "params": [header]}));
            }
217 218 219
        }
        for (script_hash, status_hash) in self.status_hashes.iter_mut() {
            let status = self.query.status(&script_hash[..]);
220
            let new_status_hash = status.hash().map_or(Value::Null, |h| json!(hex::encode(h)));
221 222 223 224 225 226 227 228 229 230 231 232
            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)
    }

233 234 235 236 237 238 239 240 241
    fn send_value(&mut self, v: Value) -> Result<()> {
        debug!("[{}] <- {}", self.addr, v);
        let line = v.to_string() + "\n";
        self.stream
            .write_all(line.as_bytes())
            .chain_err(|| format!("failed to send {}", v))
    }

    fn handle_replies(&mut self, chan: &Channel) -> Result<()> {
242
        let poll_duration = Duration::from_secs(5);
Roman Zeyde's avatar
Roman Zeyde committed
243 244
        let rx = chan.receiver();
        loop {
245 246 247 248 249
            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
250 251 252
            match msg {
                Message::Request(line) => {
                    let cmd: Value = from_str(&line).chain_err(|| "invalid JSON format")?;
253
                    debug!("[{}] -> {}", self.addr, cmd);
Roman Zeyde's avatar
Roman Zeyde committed
254 255 256 257 258 259 260 261
                    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),
                    };
262
                    self.send_value(reply)?
263
                }
264
                Message::PeriodicUpdate => {
Roman Zeyde's avatar
Roman Zeyde committed
265
                    for update in self.update_subscriptions()
266
                        .chain_err(|| "failed to update subscriptions")?
Roman Zeyde's avatar
Roman Zeyde committed
267
                    {
268
                        self.send_value(update)?
269
                    }
Roman Zeyde's avatar
Roman Zeyde committed
270 271 272 273
                }
                Message::Done => {
                    debug!("done");
                    break;
274
                }
275
            }
Roman Zeyde's avatar
Roman Zeyde committed
276 277 278 279
        }
        Ok(())
    }

280
    fn handle_requests(mut reader: BufReader<TcpStream>, tx: SyncSender<Message>) {
Roman Zeyde's avatar
Roman Zeyde committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294
        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");
            }
        }
    }

295
    pub fn run(mut self) {
296
        let reader = BufReader::new(self.stream.try_clone().expect("failed to clone TcpStream"));
Roman Zeyde's avatar
Roman Zeyde committed
297
        crossbeam::scope(|scope| {
298
            let chan = Channel::new();
Roman Zeyde's avatar
Roman Zeyde committed
299
            let tx = chan.sender();
300
            scope.spawn(|| Connection::handle_requests(reader, tx));
301
            self.handle_replies(&chan).unwrap();
Roman Zeyde's avatar
Roman Zeyde committed
302
        });
303 304 305 306 307
    }
}

pub enum Message {
    Request(String),
308
    PeriodicUpdate,
309 310 311 312 313 314 315 316 317 318
    Done,
}

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

impl Channel {
    pub fn new() -> Channel {
319
        let (tx, rx) = sync_channel(10);
320 321 322 323 324 325 326 327 328
        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
329 330 331
    }
}

332
pub fn serve(addr: &SocketAddr, query: Arc<Query>) {
333 334
    let listener = TcpListener::bind(addr).unwrap();
    info!("RPC server running on {}", addr);
Roman Zeyde's avatar
WiP  
Roman Zeyde committed
335 336 337
    loop {
        let (stream, addr) = listener.accept().unwrap();
        info!("[{}] connected peer", addr);
338
        Connection::new(query.clone(), stream, addr).run();
Roman Zeyde's avatar
Roman Zeyde committed
339 340 341
        info!("[{}] disconnected peer", addr);
    }
}