Skip to content

Commit

Permalink
Merge pull request #11 from Yamaguchi/feature/block_with_aggregated_p…
Browse files Browse the repository at this point in the history
…ublic_key

Block should include an aggregated public key
  • Loading branch information
rantan authored Dec 16, 2019
2 parents 7b985e1 + 19b41a3 commit 0cfd1c1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ base64 = "0.10.1"
redis = "0.10.0"
clap = "2.33.0"
toml = "0.5"
multi-party-schnorr = { git = "https://github.com/Yamaguchi/multi-party-schnorr", branch = "master" }
multi-party-schnorr = { git = "https://github.com/Yamaguchi/multi-party-schnorr", rev = "cd92eecc736b7a48bd3a24284f3c3155c7f18179" }
curv = { git = "https://github.com/KZen-networks/curv" , tag = "v0.2.0", features = ["ec_secp256k1"]}
42 changes: 39 additions & 3 deletions src/blockdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@ impl BlockHash {
pub struct Block(Vec<u8>);

impl Block {
const PROOF_POSISION: usize = 105;

pub fn new(data: Vec<u8>) -> Block {
Block(data)
}
/// Length of block header without proof is 104 bytes.

/// Length of block header without proof is 105 bytes.
/// Version: 4
/// hasPrevBlock: 32
/// hashMerkleRoot: 32
/// hashImMerkleRoot: 32
/// time: 4
/// length of aggPubkey: 1
pub fn get_header_without_proof(&self) -> &[u8] {
&self.0[..104]
&self.0[..Self::PROOF_POSISION]
}

pub fn hex(&self) -> String {
Expand All @@ -56,8 +65,35 @@ impl Block {
&self.0
}
pub fn add_proof(&self, proof: Vec<u8>) -> Block {
let (header, txs) = self.payload().split_at(104);
let (header, txs) = self.payload().split_at(Self::PROOF_POSISION);
let new_payload = [header, &proof[..], &txs[1..]].concat();
Block(new_payload)
}
}

#[test]
fn test_get_header_without_proof() {
let hex_block = "010000000000000000000000000000000000000000000000000000000000000000000000c1457ff3e5c527e69858108edf0ff1f49eea9c58d8d37300a164b3b4f8c8c7cef1a2e72770d547feae29f2dd40123a97c580d44fd4493de072416d53331997617b96f05d00403a4c09253c7b583e5260074380c9b99b895f938e37799d326ded984fb707e91fa4df2e0524a4ccf5fe224945b4fb94784b411a760eb730d95402d3383dd7ffdc01010000000100000000000000000000000000000000000000000000000000000000000000000000000022210366262690cbdf648132ce0c088962c6361112582364ede120f3780ab73438fc4bffffffff0100f2052a010000002776a9226d70757956774d32596a454d755a4b72687463526b614a787062715447417346484688ac00000000";
let raw_block = hex::decode(hex_block).unwrap();
let block = Block(raw_block);

let hex_expect = "010000000000000000000000000000000000000000000000000000000000000000000000c1457ff3e5c527e69858108edf0ff1f49eea9c58d8d37300a164b3b4f8c8c7cef1a2e72770d547feae29f2dd40123a97c580d44fd4493de072416d53331997617b96f05d00";
let raw_expect = hex::decode(hex_expect).unwrap();

assert_eq!(block.get_header_without_proof(), &raw_expect[..]);
}

#[test]
fn test_add_proof() {
let hex_block = "010000000000000000000000000000000000000000000000000000000000000000000000c1457ff3e5c527e69858108edf0ff1f49eea9c58d8d37300a164b3b4f8c8c7cef1a2e72770d547feae29f2dd40123a97c580d44fd4493de072416d53331997617b96f05d000001010000000100000000000000000000000000000000000000000000000000000000000000000000000022210366262690cbdf648132ce0c088962c6361112582364ede120f3780ab73438fc4bffffffff0100f2052a010000002776a9226d70757956774d32596a454d755a4b72687463526b614a787062715447417346484688ac00000000";
let raw_block = hex::decode(hex_block).unwrap();
let block = Block(raw_block);

let sig_hex = "403a4c09253c7b583e5260074380c9b99b895f938e37799d326ded984fb707e91fa4df2e0524a4ccf5fe224945b4fb94784b411a760eb730d95402d3383dd7ffdc";

let hex_expect = "010000000000000000000000000000000000000000000000000000000000000000000000c1457ff3e5c527e69858108edf0ff1f49eea9c58d8d37300a164b3b4f8c8c7cef1a2e72770d547feae29f2dd40123a97c580d44fd4493de072416d53331997617b96f05d00403a4c09253c7b583e5260074380c9b99b895f938e37799d326ded984fb707e91fa4df2e0524a4ccf5fe224945b4fb94784b411a760eb730d95402d3383dd7ffdc01010000000100000000000000000000000000000000000000000000000000000000000000000000000022210366262690cbdf648132ce0c088962c6361112582364ede120f3780ab73438fc4bffffffff0100f2052a010000002776a9226d70757956774d32596a454d755a4b72687463526b614a787062715447417346484688ac00000000";
let raw_expect = hex::decode(hex_expect).unwrap();
let expect = Block(raw_expect);

assert_eq!(block.add_proof(hex::decode(sig_hex).unwrap()), expect);
}

0 comments on commit 0cfd1c1

Please sign in to comment.