Merge pull request #515 from antiochp/output_identifiers

grin output_identifier refactoring
This commit is contained in:
Antioch Peverell 2020-09-03 13:09:16 +01:00 committed by GitHub
commit b040ae4c5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 24 deletions

16
Cargo.lock generated
View file

@ -1171,7 +1171,7 @@ checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
[[package]] [[package]]
name = "grin_api" name = "grin_api"
version = "4.1.0-alpha.1" version = "4.1.0-alpha.1"
source = "git+https://github.com/mimblewimble/grin#caa6b8c747788e32953950a2dfd0a7c1e5d29cd2" source = "git+https://github.com/mimblewimble/grin#133089e9855686854c87d6fbe4d67df775918bee"
dependencies = [ dependencies = [
"bytes", "bytes",
"easy-jsonrpc-mw", "easy-jsonrpc-mw",
@ -1204,7 +1204,7 @@ dependencies = [
[[package]] [[package]]
name = "grin_chain" name = "grin_chain"
version = "4.1.0-alpha.1" version = "4.1.0-alpha.1"
source = "git+https://github.com/mimblewimble/grin#caa6b8c747788e32953950a2dfd0a7c1e5d29cd2" source = "git+https://github.com/mimblewimble/grin#133089e9855686854c87d6fbe4d67df775918bee"
dependencies = [ dependencies = [
"bit-vec", "bit-vec",
"bitflags 1.2.1", "bitflags 1.2.1",
@ -1228,7 +1228,7 @@ dependencies = [
[[package]] [[package]]
name = "grin_core" name = "grin_core"
version = "4.1.0-alpha.1" version = "4.1.0-alpha.1"
source = "git+https://github.com/mimblewimble/grin#caa6b8c747788e32953950a2dfd0a7c1e5d29cd2" source = "git+https://github.com/mimblewimble/grin#133089e9855686854c87d6fbe4d67df775918bee"
dependencies = [ dependencies = [
"blake2-rfc", "blake2-rfc",
"byteorder", "byteorder",
@ -1254,7 +1254,7 @@ dependencies = [
[[package]] [[package]]
name = "grin_keychain" name = "grin_keychain"
version = "4.1.0-alpha.1" version = "4.1.0-alpha.1"
source = "git+https://github.com/mimblewimble/grin#caa6b8c747788e32953950a2dfd0a7c1e5d29cd2" source = "git+https://github.com/mimblewimble/grin#133089e9855686854c87d6fbe4d67df775918bee"
dependencies = [ dependencies = [
"blake2-rfc", "blake2-rfc",
"byteorder", "byteorder",
@ -1276,7 +1276,7 @@ dependencies = [
[[package]] [[package]]
name = "grin_p2p" name = "grin_p2p"
version = "4.1.0-alpha.1" version = "4.1.0-alpha.1"
source = "git+https://github.com/mimblewimble/grin#caa6b8c747788e32953950a2dfd0a7c1e5d29cd2" source = "git+https://github.com/mimblewimble/grin#133089e9855686854c87d6fbe4d67df775918bee"
dependencies = [ dependencies = [
"bitflags 1.2.1", "bitflags 1.2.1",
"chrono", "chrono",
@ -1297,7 +1297,7 @@ dependencies = [
[[package]] [[package]]
name = "grin_pool" name = "grin_pool"
version = "4.1.0-alpha.1" version = "4.1.0-alpha.1"
source = "git+https://github.com/mimblewimble/grin#caa6b8c747788e32953950a2dfd0a7c1e5d29cd2" source = "git+https://github.com/mimblewimble/grin#133089e9855686854c87d6fbe4d67df775918bee"
dependencies = [ dependencies = [
"blake2-rfc", "blake2-rfc",
"chrono", "chrono",
@ -1331,7 +1331,7 @@ dependencies = [
[[package]] [[package]]
name = "grin_store" name = "grin_store"
version = "4.1.0-alpha.1" version = "4.1.0-alpha.1"
source = "git+https://github.com/mimblewimble/grin#caa6b8c747788e32953950a2dfd0a7c1e5d29cd2" source = "git+https://github.com/mimblewimble/grin#133089e9855686854c87d6fbe4d67df775918bee"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"croaring-mw", "croaring-mw",
@ -1351,7 +1351,7 @@ dependencies = [
[[package]] [[package]]
name = "grin_util" name = "grin_util"
version = "4.1.0-alpha.1" version = "4.1.0-alpha.1"
source = "git+https://github.com/mimblewimble/grin#caa6b8c747788e32953950a2dfd0a7c1e5d29cd2" source = "git+https://github.com/mimblewimble/grin#133089e9855686854c87d6fbe4d67df775918bee"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"base64 0.12.3", "base64 0.12.3",

View file

@ -370,3 +370,55 @@ impl NodeClient for HTTPNodeClient {
Ok((res.last_retrieved_index, res.highest_index)) Ok((res.last_retrieved_index, res.highest_index))
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use crate::core::core::KernelFeatures;
use crate::core::libtx::build;
use crate::core::libtx::ProofBuilder;
use crate::keychain::{ExtKeychain, Keychain};
fn tx1i1o() -> Transaction {
let keychain = ExtKeychain::from_random_seed(false).unwrap();
let builder = ProofBuilder::new(&keychain);
let key_id1 = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
let key_id2 = ExtKeychain::derive_key_id(1, 2, 0, 0, 0);
let tx = build::transaction(
KernelFeatures::Plain { fee: 2 },
&[build::input(5, key_id1), build::output(3, key_id2)],
&keychain,
&builder,
)
.unwrap();
tx
}
// Wallet will "push" a transaction to node, serializing the transaction as json.
// We are testing the json structure is what we expect here.
#[test]
fn test_transaction_json_ser_deser() {
let tx1 = tx1i1o();
let value = serde_json::to_value(&tx1).unwrap();
assert!(value["offset"].is_string());
assert_eq!(value["body"]["inputs"][0]["features"], "Plain");
assert!(value["body"]["inputs"][0]["commit"].is_string());
assert_eq!(value["body"]["outputs"][0]["features"], "Plain");
assert!(value["body"]["outputs"][0]["commit"].is_string());
assert!(value["body"]["outputs"][0]["proof"].is_string());
// Note: Tx kernel "features" serialize in a slightly unexpected way.
assert_eq!(value["body"]["kernels"][0]["features"]["Plain"]["fee"], 2);
assert!(value["body"]["kernels"][0]["excess"].is_string());
assert!(value["body"]["kernels"][0]["excess_sig"].is_string());
let tx2: Transaction = serde_json::from_value(value).unwrap();
assert_eq!(tx1, tx2);
let str = serde_json::to_string(&tx1).unwrap();
println!("{}", str);
let tx2: Transaction = serde_json::from_str(&str).unwrap();
assert_eq!(tx1, tx2);
}
}

View file

@ -40,10 +40,11 @@ fn get_output_local(chain: &chain::Chain, commit: pedersen::Commitment) -> Optio
if chain.get_unspent(commit).unwrap().is_some() { if chain.get_unspent(commit).unwrap().is_some() {
let block_height = chain.get_header_for_output(commit).unwrap().height; let block_height = chain.get_header_for_output(commit).unwrap().height;
let output_pos = chain.get_output_pos(&commit).unwrap_or(0); let output_pos = chain.get_output_pos(&commit).unwrap_or(0);
return Some(api::Output::new(&commit, block_height, output_pos)); Some(api::Output::new(&commit, block_height, output_pos))
} } else {
None None
} }
}
/// Get a kernel from the chain locally /// Get a kernel from the chain locally
fn get_kernel_local( fn get_kernel_local(

View file

@ -616,7 +616,7 @@ mod test {
.unwrap(); .unwrap();
let inputs: Vec<Input> = tx2.inputs().into(); let inputs: Vec<Input> = tx2.inputs().into();
assert_eq!(tx1.outputs()[0].features, inputs[0].features); assert_eq!(tx1.outputs()[0].features(), inputs[0].features);
assert_eq!(tx1.outputs()[0].commitment(), inputs[0].commitment()); assert_eq!(tx1.outputs()[0].commitment(), inputs[0].commitment());
} }

View file

@ -839,8 +839,8 @@ impl From<&Slate> for Option<Vec<CommitsV4>> {
} }
for o in outs.iter() { for o in outs.iter() {
ret_vec.push(CommitsV4 { ret_vec.push(CommitsV4 {
f: o.features.into(), f: o.features().into(),
c: o.commit, c: o.commitment(),
p: Some(o.proof), p: Some(o.proof),
}); });
} }
@ -1020,14 +1020,8 @@ pub fn tx_from_slate_v4(slate: &SlateV4) -> Option<Transaction> {
let mut tx = Transaction::empty().with_kernel(kernel); let mut tx = Transaction::empty().with_kernel(kernel);
for c in coms.iter() { for c in coms.iter() {
match &c.p { match c.p {
Some(p) => { Some(p) => tx = tx.with_output(Output::new(c.f.into(), c.c, p)),
tx = tx.with_output(Output {
features: c.f.into(),
commit: c.c,
proof: p.clone(),
})
}
None => { None => {
tx = tx.with_input(Input { tx = tx.with_input(Input {
features: c.f.into(), features: c.f.into(),
@ -1036,7 +1030,7 @@ pub fn tx_from_slate_v4(slate: &SlateV4) -> Option<Transaction> {
} }
} }
} }
tx.offset = slate.off.clone(); tx = tx.with_offset(slate.off.clone());
Some(tx) Some(tx)
} }

View file

@ -316,8 +316,8 @@ impl From<&Output> for CbOutputV4 {
fn from(output: &Output) -> CbOutputV4 { fn from(output: &Output) -> CbOutputV4 {
CbOutputV4 { CbOutputV4 {
features: CbOutputFeatures::Coinbase, features: CbOutputFeatures::Coinbase,
commit: output.commit, commit: output.commitment(),
proof: output.proof, proof: output.proof(),
} }
} }
} }