Skip to content

Commit 33361ce

Browse files
committed
Merge #151: Add 'submitblock' rpc call
86278c0 Add 'submitblock' RPC call. (Elias Rohrer) Pull request description: I added two methods to submit raw and hex-encoded blocks via RPC calls. Submitting blocks works, as tested in regtest mode. However, in case of success it currently yields the `Result`: ``` Err(JsonRpc(Json(Error("invalid type: null, expected a string", line: 0, column: 0)))) ``` I think this happens because `submitblock` returns `null` in case of success (see [BIP022](https://github.com/bitcoin/bips/blob/master/bip-0022.mediawiki)). Any suggestions how I should handle that? ACKs for top commit: shesek: ACK 86278c0. Would be great to have this in :) Tree-SHA512: 7b8632fef5e829a8a9923adddeaf80e51c236dcd2963f147f7ca620aff00c0c1e84bdd2ffdf74263ece224c2e330774da5d070a674cf4c7e4235de5f25c6dd8a
2 parents 661b58d + 86278c0 commit 33361ce

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

client/src/client.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,27 @@ pub trait RpcApi: Sized {
11621162
self.call("uptime", &[])
11631163
}
11641164

1165+
/// Submit a block
1166+
fn submit_block(&self, block: &bitcoin::Block) -> Result<()> {
1167+
let block_hex: String = bitcoin::consensus::encode::serialize_hex(block);
1168+
self.submit_block_hex(&block_hex)
1169+
}
1170+
1171+
/// Submit a raw block
1172+
fn submit_block_bytes(&self, block_bytes: &[u8]) -> Result<()> {
1173+
let block_hex: String = block_bytes.to_hex();
1174+
self.submit_block_hex(&block_hex)
1175+
}
1176+
1177+
/// Submit a block as a hex string
1178+
fn submit_block_hex(&self, block_hex: &str) -> Result<()> {
1179+
match self.call("submitblock", &[into_json(&block_hex)?]) {
1180+
Ok(serde_json::Value::Null) => Ok(()),
1181+
Ok(res) => Err(Error::ReturnedError(res.to_string())),
1182+
Err(err) => Err(err.into()),
1183+
}
1184+
}
1185+
11651186
fn scan_tx_out_set_blocking(
11661187
&self,
11671188
descriptors: &[json::ScanTxOutRequest],

client/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub enum Error {
2929
InvalidCookieFile,
3030
/// The JSON result had an unexpected structure.
3131
UnexpectedStructure,
32+
/// The daemon returned an error string.
33+
ReturnedError(String),
3234
}
3335

3436
impl From<jsonrpc::error::Error> for Error {
@@ -85,6 +87,7 @@ impl fmt::Display for Error {
8587
Error::InvalidAmount(ref e) => write!(f, "invalid amount: {}", e),
8688
Error::InvalidCookieFile => write!(f, "invalid cookie file"),
8789
Error::UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"),
90+
Error::ReturnedError(ref s) => write!(f, "the daemon returned an error string: {}", s),
8891
}
8992
}
9093
}

0 commit comments

Comments
 (0)