Implement some more ToJson's

This commit is contained in:
Andrew Poelstra 2014-07-29 10:19:21 -07:00
parent a34f2642f1
commit 8cd8947cf3
4 changed files with 11 additions and 7 deletions

View File

@ -415,9 +415,9 @@ impl Blockchain {
}
}
/// Looks up a block in the chain and returns a reference to it
pub fn get_block<'a>(&'a self, hash: Sha256dHash) -> Option<&'a Block> {
self.tree.lookup(&hash.as_uint256(), 256).as_ref().map(|node| &node.block)
/// Looks up a block in the chain and returns the BlockchainNode containing it
pub fn get_block<'a>(&'a self, hash: Sha256dHash) -> Option<&'a BlockchainNode> {
self.tree.lookup(&hash.as_uint256(), 256).map(|node| &**node)
}
/// Locates a block in the chain and overwrites its txdata

View File

@ -26,7 +26,7 @@
use std::char::from_digit;
use std::io::IoResult;
use serialize::{Encoder, Encodable};
use serialize::json;
use network::serialize::Serializable;
use blockdata::opcodes;
@ -122,16 +122,16 @@ impl Script {
}
// User-facing serialization
impl<S:Encoder<E>, E> Encodable<S, E> for Script {
impl json::ToJson for Script {
// TODO: put this in a struct alongside an opcode decode
fn encode(&self, s: &mut S) -> Result<(), E> {
fn to_json(&self) -> json::Json {
let &Script(ref raw) = self;
let mut ret = String::new();
for dat in raw.iter() {
ret.push_char(from_digit((dat / 0x10) as uint, 16).unwrap());
ret.push_char(from_digit((dat & 0x0f) as uint, 16).unwrap());
}
s.emit_str(ret.as_slice())
json::String(ret)
}
}

View File

@ -71,8 +71,11 @@ pub struct Transaction {
}
impl_serializable!(TxIn, prev_hash, prev_index, script_sig, sequence)
impl_json!(TxIn, prev_hash, prev_index, script_sig, sequence)
impl_serializable!(TxOut, value, script_pubkey)
impl_json!(TxOut, value, script_pubkey)
impl_serializable!(Transaction, version, input, output, lock_time)
impl_json!(Transaction, version, input, output, lock_time)
#[test]
fn test_txin() {

View File

@ -50,6 +50,7 @@ macro_rules! impl_json(
use std::collections::TreeMap;
use serialize::json::{ToJson, Object};
let mut ret = TreeMap::new();
ret.insert("hash".to_string(), self.bitcoin_hash().to_json());
$( ret.insert(stringify!($field).to_string(), self.$field.to_json()); )+
Object(ret)
}