2018-05-09 12:15:58 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
pub mod common;
|
|
|
|
use crate::common::{new_block, tx1i2o, tx2i1o, txspend1i1o};
|
2019-02-01 13:44:04 +03:00
|
|
|
use crate::core::consensus::BLOCK_OUTPUT_WEIGHT;
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::core::core::block::Error;
|
|
|
|
use crate::core::core::hash::Hashed;
|
|
|
|
use crate::core::core::id::ShortIdentifiable;
|
2019-01-14 21:15:42 +03:00
|
|
|
use crate::core::core::transaction::{self, Transaction};
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::core::core::verifier_cache::{LruVerifierCache, VerifierCache};
|
|
|
|
use crate::core::core::Committed;
|
2019-05-08 23:10:42 +03:00
|
|
|
use crate::core::core::{
|
|
|
|
Block, BlockHeader, CompactBlock, HeaderVersion, KernelFeatures, OutputFeatures,
|
|
|
|
};
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::core::libtx::build::{self, input, output, with_fee};
|
|
|
|
use crate::core::{global, ser};
|
|
|
|
use crate::keychain::{BlindingFactor, ExtKeychain, Keychain};
|
|
|
|
use crate::util::secp;
|
|
|
|
use crate::util::RwLock;
|
|
|
|
use chrono::Duration;
|
|
|
|
use grin_core as core;
|
|
|
|
use grin_keychain as keychain;
|
|
|
|
use grin_util as util;
|
2018-10-20 03:13:07 +03:00
|
|
|
use std::sync::Arc;
|
2018-08-30 17:44:34 +03:00
|
|
|
use std::time::Instant;
|
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
fn verifier_cache() -> Arc<RwLock<dyn VerifierCache>> {
|
2018-08-30 17:44:34 +03:00
|
|
|
Arc::new(RwLock::new(LruVerifierCache::new()))
|
|
|
|
}
|
|
|
|
|
2018-05-09 12:15:58 +03:00
|
|
|
// Too slow for now #[test]
|
|
|
|
// TODO: make this fast enough or add similar but faster test?
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn too_large_block() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2019-02-01 13:44:04 +03:00
|
|
|
let max_out = global::max_block_weight() / BLOCK_OUTPUT_WEIGHT;
|
2018-05-09 12:15:58 +03:00
|
|
|
|
|
|
|
let mut pks = vec![];
|
|
|
|
for n in 0..(max_out + 1) {
|
2018-10-10 12:11:01 +03:00
|
|
|
pks.push(ExtKeychain::derive_key_id(1, n as u32, 0, 0, 0));
|
2018-05-09 12:15:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut parts = vec![];
|
|
|
|
for _ in 0..max_out {
|
|
|
|
parts.push(output(5, pks.pop().unwrap()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let now = Instant::now();
|
|
|
|
parts.append(&mut vec![input(500000, pks.pop().unwrap()), with_fee(2)]);
|
2018-05-30 23:57:13 +03:00
|
|
|
let tx = build::transaction(parts, &keychain).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
println!("Build tx: {}", now.elapsed().as_secs());
|
|
|
|
|
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-30 23:57:13 +03:00
|
|
|
let b = new_block(vec![&tx], &keychain, &prev, &key_id);
|
2018-12-08 02:59:40 +03:00
|
|
|
assert!(b
|
|
|
|
.validate(&BlindingFactor::zero(), verifier_cache())
|
|
|
|
.is_err());
|
2018-05-09 12:15:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
// block with no inputs/outputs/kernels
|
|
|
|
// no fees, no reward, no coinbase
|
|
|
|
fn very_empty_block() {
|
2018-08-16 00:14:48 +03:00
|
|
|
let b = Block::with_header(BlockHeader::default());
|
2018-05-09 12:15:58 +03:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
b.verify_coinbase(),
|
|
|
|
Err(Error::Secp(secp::Error::IncorrectCommitSum))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
// builds a block with a tx spending another and check that cut_through occurred
|
|
|
|
fn block_with_cut_through() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-10-10 12:11:01 +03:00
|
|
|
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 key_id3 = ExtKeychain::derive_key_id(1, 3, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
|
|
|
|
let mut btx1 = tx2i1o();
|
|
|
|
let mut btx2 = build::transaction(
|
|
|
|
vec![input(7, key_id1), output(5, key_id2.clone()), with_fee(2)],
|
|
|
|
&keychain,
|
2018-12-08 02:59:40 +03:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
|
|
|
|
// spending tx2 - reuse key_id2
|
|
|
|
|
|
|
|
let mut btx3 = txspend1i1o(5, &keychain, key_id2.clone(), key_id3);
|
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let b = new_block(
|
|
|
|
vec![&mut btx1, &mut btx2, &mut btx3],
|
|
|
|
&keychain,
|
|
|
|
&prev,
|
|
|
|
&key_id,
|
|
|
|
);
|
|
|
|
|
|
|
|
// block should have been automatically compacted (including reward
|
|
|
|
// output) and should still be valid
|
2018-10-13 04:15:29 +03:00
|
|
|
b.validate(&BlindingFactor::zero(), verifier_cache())
|
2018-08-30 17:44:34 +03:00
|
|
|
.unwrap();
|
2018-08-16 00:14:48 +03:00
|
|
|
assert_eq!(b.inputs().len(), 3);
|
|
|
|
assert_eq!(b.outputs().len(), 3);
|
2018-05-09 12:15:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_block_with_coinbase_is_valid() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let b = new_block(vec![], &keychain, &prev, &key_id);
|
|
|
|
|
2018-08-16 00:14:48 +03:00
|
|
|
assert_eq!(b.inputs().len(), 0);
|
|
|
|
assert_eq!(b.outputs().len(), 1);
|
|
|
|
assert_eq!(b.kernels().len(), 1);
|
2018-05-09 12:15:58 +03:00
|
|
|
|
2018-08-16 00:14:48 +03:00
|
|
|
let coinbase_outputs = b
|
|
|
|
.outputs()
|
2018-05-09 12:15:58 +03:00
|
|
|
.iter()
|
2018-12-18 21:26:34 +03:00
|
|
|
.filter(|out| out.is_coinbase())
|
2018-05-09 12:15:58 +03:00
|
|
|
.map(|o| o.clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(coinbase_outputs.len(), 1);
|
|
|
|
|
2018-08-16 00:14:48 +03:00
|
|
|
let coinbase_kernels = b
|
|
|
|
.kernels()
|
2018-05-09 12:15:58 +03:00
|
|
|
.iter()
|
2018-12-18 21:26:34 +03:00
|
|
|
.filter(|out| out.is_coinbase())
|
2018-05-09 12:15:58 +03:00
|
|
|
.map(|o| o.clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(coinbase_kernels.len(), 1);
|
|
|
|
|
|
|
|
// the block should be valid here (single coinbase output with corresponding
|
|
|
|
// txn kernel)
|
2018-12-08 02:59:40 +03:00
|
|
|
assert!(b
|
|
|
|
.validate(&BlindingFactor::zero(), verifier_cache())
|
|
|
|
.is_ok());
|
2018-05-09 12:15:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-12-18 21:26:34 +03:00
|
|
|
// test that flipping the COINBASE flag on the output features
|
2018-05-09 12:15:58 +03:00
|
|
|
// invalidates the block and specifically it causes verify_coinbase to fail
|
|
|
|
// additionally verifying the merkle_inputs_outputs also fails
|
|
|
|
fn remove_coinbase_output_flag() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let mut b = new_block(vec![], &keychain, &prev, &key_id);
|
|
|
|
|
2018-12-18 21:26:34 +03:00
|
|
|
assert!(b.outputs()[0].is_coinbase());
|
2019-01-08 19:07:38 +03:00
|
|
|
b.outputs_mut()[0].features = OutputFeatures::Plain;
|
2018-05-09 12:15:58 +03:00
|
|
|
|
|
|
|
assert_eq!(b.verify_coinbase(), Err(Error::CoinbaseSumMismatch));
|
2018-12-08 02:59:40 +03:00
|
|
|
assert!(b
|
|
|
|
.verify_kernel_sums(b.header.overage(), b.header.total_kernel_offset())
|
|
|
|
.is_ok());
|
2018-05-09 12:15:58 +03:00
|
|
|
assert_eq!(
|
2018-10-13 04:15:29 +03:00
|
|
|
b.validate(&BlindingFactor::zero(), verifier_cache()),
|
2018-05-09 12:15:58 +03:00
|
|
|
Err(Error::CoinbaseSumMismatch)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-12-18 21:26:34 +03:00
|
|
|
// test that flipping the COINBASE flag on the kernel features
|
2018-05-09 12:15:58 +03:00
|
|
|
// invalidates the block and specifically it causes verify_coinbase to fail
|
|
|
|
fn remove_coinbase_kernel_flag() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let mut b = new_block(vec![], &keychain, &prev, &key_id);
|
|
|
|
|
2018-12-18 21:26:34 +03:00
|
|
|
assert!(b.kernels()[0].is_coinbase());
|
2019-01-08 19:07:38 +03:00
|
|
|
b.kernels_mut()[0].features = KernelFeatures::Plain;
|
2018-05-09 12:15:58 +03:00
|
|
|
|
2018-12-12 12:19:36 +03:00
|
|
|
// Flipping the coinbase flag results in kernels not summing correctly.
|
2018-05-09 12:15:58 +03:00
|
|
|
assert_eq!(
|
|
|
|
b.verify_coinbase(),
|
|
|
|
Err(Error::Secp(secp::Error::IncorrectCommitSum))
|
|
|
|
);
|
|
|
|
|
2018-12-12 12:19:36 +03:00
|
|
|
// Also results in the block no longer validating correctly
|
|
|
|
// because the message being signed on each tx kernel includes the kernel features.
|
2018-05-09 12:15:58 +03:00
|
|
|
assert_eq!(
|
2018-10-13 04:15:29 +03:00
|
|
|
b.validate(&BlindingFactor::zero(), verifier_cache()),
|
2018-12-12 12:19:36 +03:00
|
|
|
Err(Error::Transaction(transaction::Error::IncorrectSignature))
|
2018-05-09 12:15:58 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-08 23:10:42 +03:00
|
|
|
#[test]
|
|
|
|
fn serialize_deserialize_header_version() {
|
|
|
|
let mut vec1 = Vec::new();
|
|
|
|
ser::serialize(&mut vec1, &1_u16).expect("serialization failed");
|
|
|
|
|
|
|
|
let mut vec2 = Vec::new();
|
|
|
|
ser::serialize(&mut vec2, &HeaderVersion::default()).expect("serialization failed");
|
|
|
|
|
|
|
|
// Check that a header_version serializes to a
|
|
|
|
// single u16 value with no extraneous bytes wrapping it.
|
|
|
|
assert_eq!(vec1, vec2);
|
|
|
|
|
|
|
|
// Check we can successfully deserialize a header_version.
|
|
|
|
let version: HeaderVersion = ser::deserialize(&mut &vec2[..]).unwrap();
|
|
|
|
assert_eq!(version.0, 1)
|
|
|
|
}
|
|
|
|
|
2018-08-22 17:40:24 +03:00
|
|
|
#[test]
|
|
|
|
fn serialize_deserialize_block_header() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-08-22 17:40:24 +03:00
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-08-22 17:40:24 +03:00
|
|
|
let b = new_block(vec![], &keychain, &prev, &key_id);
|
|
|
|
let header1 = b.header;
|
|
|
|
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
ser::serialize(&mut vec, &header1).expect("serialization failed");
|
|
|
|
let header2: BlockHeader = ser::deserialize(&mut &vec[..]).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(header1.hash(), header2.hash());
|
|
|
|
assert_eq!(header1, header2);
|
|
|
|
}
|
|
|
|
|
2018-05-09 12:15:58 +03:00
|
|
|
#[test]
|
|
|
|
fn serialize_deserialize_block() {
|
2018-08-22 17:40:24 +03:00
|
|
|
let tx1 = tx1i2o();
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-08-22 17:40:24 +03:00
|
|
|
let b = new_block(vec![&tx1], &keychain, &prev, &key_id);
|
2018-05-09 12:15:58 +03:00
|
|
|
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
ser::serialize(&mut vec, &b).expect("serialization failed");
|
|
|
|
let b2: Block = ser::deserialize(&mut &vec[..]).unwrap();
|
|
|
|
|
2018-08-22 17:40:24 +03:00
|
|
|
assert_eq!(b.hash(), b2.hash());
|
2018-05-09 12:15:58 +03:00
|
|
|
assert_eq!(b.header, b2.header);
|
2018-08-16 00:14:48 +03:00
|
|
|
assert_eq!(b.inputs(), b2.inputs());
|
|
|
|
assert_eq!(b.outputs(), b2.outputs());
|
|
|
|
assert_eq!(b.kernels(), b2.kernels());
|
2018-05-09 12:15:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_block_serialized_size() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let b = new_block(vec![], &keychain, &prev, &key_id);
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
ser::serialize(&mut vec, &b).expect("serialization failed");
|
2018-11-24 23:33:17 +03:00
|
|
|
let target_len = 1_265;
|
2018-06-29 20:41:28 +03:00
|
|
|
assert_eq!(vec.len(), target_len);
|
2018-05-09 12:15:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_single_tx_serialized_size() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
let tx1 = tx1i2o();
|
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let b = new_block(vec![&tx1], &keychain, &prev, &key_id);
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
ser::serialize(&mut vec, &b).expect("serialization failed");
|
2018-11-24 23:33:17 +03:00
|
|
|
let target_len = 2_847;
|
2018-05-09 12:15:58 +03:00
|
|
|
assert_eq!(vec.len(), target_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_compact_block_serialized_size() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let b = new_block(vec![], &keychain, &prev, &key_id);
|
2018-08-22 22:19:37 +03:00
|
|
|
let cb: CompactBlock = b.into();
|
2018-05-09 12:15:58 +03:00
|
|
|
let mut vec = Vec::new();
|
2018-08-22 22:19:37 +03:00
|
|
|
ser::serialize(&mut vec, &cb).expect("serialization failed");
|
2018-11-24 23:33:17 +03:00
|
|
|
let target_len = 1_273;
|
2018-06-29 20:41:28 +03:00
|
|
|
assert_eq!(vec.len(), target_len);
|
2018-05-09 12:15:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compact_block_single_tx_serialized_size() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
let tx1 = tx1i2o();
|
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let b = new_block(vec![&tx1], &keychain, &prev, &key_id);
|
2018-08-22 22:19:37 +03:00
|
|
|
let cb: CompactBlock = b.into();
|
2018-05-09 12:15:58 +03:00
|
|
|
let mut vec = Vec::new();
|
2018-08-22 22:19:37 +03:00
|
|
|
ser::serialize(&mut vec, &cb).expect("serialization failed");
|
2018-11-24 23:33:17 +03:00
|
|
|
let target_len = 1_279;
|
2018-06-29 20:41:28 +03:00
|
|
|
assert_eq!(vec.len(), target_len);
|
2018-05-09 12:15:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_10_tx_serialized_size() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
global::set_mining_mode(global::ChainTypes::Mainnet);
|
|
|
|
|
|
|
|
let mut txs = vec![];
|
|
|
|
for _ in 0..10 {
|
|
|
|
let tx = tx1i2o();
|
|
|
|
txs.push(tx);
|
|
|
|
}
|
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let b = new_block(txs.iter().collect(), &keychain, &prev, &key_id);
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
ser::serialize(&mut vec, &b).expect("serialization failed");
|
2018-11-24 23:33:17 +03:00
|
|
|
let target_len = 17_085;
|
2018-05-09 12:15:58 +03:00
|
|
|
assert_eq!(vec.len(), target_len,);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compact_block_10_tx_serialized_size() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
|
|
|
|
let mut txs = vec![];
|
|
|
|
for _ in 0..10 {
|
|
|
|
let tx = tx1i2o();
|
|
|
|
txs.push(tx);
|
|
|
|
}
|
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let b = new_block(txs.iter().collect(), &keychain, &prev, &key_id);
|
2018-08-22 22:19:37 +03:00
|
|
|
let cb: CompactBlock = b.into();
|
2018-05-09 12:15:58 +03:00
|
|
|
let mut vec = Vec::new();
|
2018-08-22 22:19:37 +03:00
|
|
|
ser::serialize(&mut vec, &cb).expect("serialization failed");
|
2018-11-24 23:33:17 +03:00
|
|
|
let target_len = 1_333;
|
2018-05-09 12:15:58 +03:00
|
|
|
assert_eq!(vec.len(), target_len,);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compact_block_hash_with_nonce() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
let tx = tx1i2o();
|
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let b = new_block(vec![&tx], &keychain, &prev, &key_id);
|
2018-08-22 22:19:37 +03:00
|
|
|
let cb1: CompactBlock = b.clone().into();
|
|
|
|
let cb2: CompactBlock = b.clone().into();
|
2018-05-09 12:15:58 +03:00
|
|
|
|
|
|
|
// random nonce will not affect the hash of the compact block itself
|
|
|
|
// hash is based on header POW only
|
|
|
|
assert!(cb1.nonce != cb2.nonce);
|
|
|
|
assert_eq!(b.hash(), cb1.hash());
|
|
|
|
assert_eq!(cb1.hash(), cb2.hash());
|
|
|
|
|
2018-08-22 22:19:37 +03:00
|
|
|
assert!(cb1.kern_ids()[0] != cb2.kern_ids()[0]);
|
2018-05-09 12:15:58 +03:00
|
|
|
|
|
|
|
// check we can identify the specified kernel from the short_id
|
|
|
|
// correctly in both of the compact_blocks
|
|
|
|
assert_eq!(
|
2018-08-22 22:19:37 +03:00
|
|
|
cb1.kern_ids()[0],
|
2018-08-16 00:14:48 +03:00
|
|
|
tx.kernels()[0].short_id(&cb1.hash(), cb1.nonce)
|
2018-05-09 12:15:58 +03:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2018-08-22 22:19:37 +03:00
|
|
|
cb2.kern_ids()[0],
|
2018-08-16 00:14:48 +03:00
|
|
|
tx.kernels()[0].short_id(&cb2.hash(), cb2.nonce)
|
2018-05-09 12:15:58 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn convert_block_to_compact_block() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
let tx1 = tx1i2o();
|
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let b = new_block(vec![&tx1], &keychain, &prev, &key_id);
|
2018-08-22 22:19:37 +03:00
|
|
|
let cb: CompactBlock = b.clone().into();
|
2018-05-09 12:15:58 +03:00
|
|
|
|
2018-08-22 22:19:37 +03:00
|
|
|
assert_eq!(cb.out_full().len(), 1);
|
|
|
|
assert_eq!(cb.kern_full().len(), 1);
|
|
|
|
assert_eq!(cb.kern_ids().len(), 1);
|
2018-05-09 12:15:58 +03:00
|
|
|
|
|
|
|
assert_eq!(
|
2018-08-22 22:19:37 +03:00
|
|
|
cb.kern_ids()[0],
|
2018-08-16 00:14:48 +03:00
|
|
|
b.kernels()
|
2018-05-09 12:15:58 +03:00
|
|
|
.iter()
|
2018-12-18 21:26:34 +03:00
|
|
|
.find(|x| !x.is_coinbase())
|
2018-05-09 12:15:58 +03:00
|
|
|
.unwrap()
|
|
|
|
.short_id(&cb.hash(), cb.nonce)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hydrate_empty_compact_block() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-05-09 12:15:58 +03:00
|
|
|
let b = new_block(vec![], &keychain, &prev, &key_id);
|
2018-08-22 22:19:37 +03:00
|
|
|
let cb: CompactBlock = b.clone().into();
|
2018-08-16 02:20:33 +03:00
|
|
|
let hb = Block::hydrate_from(cb, vec![]).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
assert_eq!(hb.header, b.header);
|
2018-08-16 00:14:48 +03:00
|
|
|
assert_eq!(hb.outputs(), b.outputs());
|
|
|
|
assert_eq!(hb.kernels(), b.kernels());
|
2018-05-09 12:15:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_deserialize_compact_block() {
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain = ExtKeychain::from_random_seed(false).unwrap();
|
2018-08-22 22:19:37 +03:00
|
|
|
let tx1 = tx1i2o();
|
|
|
|
let prev = BlockHeader::default();
|
2018-10-10 12:11:01 +03:00
|
|
|
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
2018-08-22 22:19:37 +03:00
|
|
|
let b = new_block(vec![&tx1], &keychain, &prev, &key_id);
|
|
|
|
|
|
|
|
let mut cb1: CompactBlock = b.into();
|
2018-05-09 12:15:58 +03:00
|
|
|
|
|
|
|
let mut vec = Vec::new();
|
2018-08-22 22:19:37 +03:00
|
|
|
ser::serialize(&mut vec, &cb1).expect("serialization failed");
|
2018-05-09 12:15:58 +03:00
|
|
|
|
2018-08-22 22:19:37 +03:00
|
|
|
// After header serialization, timestamp will lose 'nanos' info, that's the designed behavior.
|
|
|
|
// To suppress 'nanos' difference caused assertion fail, we force b.header also lose 'nanos'.
|
|
|
|
let origin_ts = cb1.header.timestamp;
|
|
|
|
cb1.header.timestamp =
|
|
|
|
origin_ts - Duration::nanoseconds(origin_ts.timestamp_subsec_nanos() as i64);
|
|
|
|
|
|
|
|
let cb2: CompactBlock = ser::deserialize(&mut &vec[..]).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(cb1.header, cb2.header);
|
|
|
|
assert_eq!(cb1.kern_ids(), cb2.kern_ids());
|
2018-05-09 12:15:58 +03:00
|
|
|
}
|
2019-01-14 21:15:42 +03:00
|
|
|
|
|
|
|
// Duplicate a range proof from a valid output into another of the same amount
|
|
|
|
#[test]
|
|
|
|
fn same_amount_outputs_copy_range_proof() {
|
|
|
|
let keychain = keychain::ExtKeychain::from_random_seed(false).unwrap();
|
|
|
|
let key_id1 = keychain::ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
|
|
|
let key_id2 = keychain::ExtKeychain::derive_key_id(1, 2, 0, 0, 0);
|
|
|
|
let key_id3 = keychain::ExtKeychain::derive_key_id(1, 3, 0, 0, 0);
|
|
|
|
|
|
|
|
let tx = build::transaction(
|
|
|
|
vec![
|
|
|
|
input(7, key_id1),
|
|
|
|
output(3, key_id2),
|
|
|
|
output(3, key_id3),
|
|
|
|
with_fee(1),
|
|
|
|
],
|
|
|
|
&keychain,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// now we reconstruct the transaction, swapping the rangeproofs so they
|
|
|
|
// have the wrong privkey
|
|
|
|
let ins = tx.inputs();
|
|
|
|
let mut outs = tx.outputs().clone();
|
|
|
|
let kernels = tx.kernels();
|
|
|
|
outs[0].proof = outs[1].proof;
|
|
|
|
|
|
|
|
let key_id = keychain::ExtKeychain::derive_key_id(1, 4, 0, 0, 0);
|
|
|
|
let prev = BlockHeader::default();
|
|
|
|
let b = new_block(
|
|
|
|
vec![&mut Transaction::new(
|
|
|
|
ins.clone(),
|
|
|
|
outs.clone(),
|
|
|
|
kernels.clone(),
|
|
|
|
)],
|
|
|
|
&keychain,
|
|
|
|
&prev,
|
|
|
|
&key_id,
|
|
|
|
);
|
|
|
|
|
|
|
|
// block should have been automatically compacted (including reward
|
|
|
|
// output) and should still be valid
|
|
|
|
match b.validate(&BlindingFactor::zero(), verifier_cache()) {
|
|
|
|
Err(Error::Transaction(transaction::Error::Secp(secp::Error::InvalidRangeProof))) => {}
|
|
|
|
_ => panic!("Bad range proof should be invalid"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap a range proof with the right private key but wrong amount
|
|
|
|
#[test]
|
|
|
|
fn wrong_amount_range_proof() {
|
|
|
|
let keychain = keychain::ExtKeychain::from_random_seed(false).unwrap();
|
|
|
|
let key_id1 = keychain::ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
|
|
|
let key_id2 = keychain::ExtKeychain::derive_key_id(1, 2, 0, 0, 0);
|
|
|
|
let key_id3 = keychain::ExtKeychain::derive_key_id(1, 3, 0, 0, 0);
|
|
|
|
|
|
|
|
let tx1 = build::transaction(
|
|
|
|
vec![
|
|
|
|
input(7, key_id1.clone()),
|
|
|
|
output(3, key_id2.clone()),
|
|
|
|
output(3, key_id3.clone()),
|
|
|
|
with_fee(1),
|
|
|
|
],
|
|
|
|
&keychain,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let tx2 = build::transaction(
|
|
|
|
vec![
|
|
|
|
input(7, key_id1),
|
|
|
|
output(2, key_id2),
|
|
|
|
output(4, key_id3),
|
|
|
|
with_fee(1),
|
|
|
|
],
|
|
|
|
&keychain,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// we take the range proofs from tx2 into tx1 and rebuild the transaction
|
|
|
|
let ins = tx1.inputs();
|
|
|
|
let mut outs = tx1.outputs().clone();
|
|
|
|
let kernels = tx1.kernels();
|
|
|
|
outs[0].proof = tx2.outputs()[0].proof;
|
|
|
|
outs[1].proof = tx2.outputs()[1].proof;
|
|
|
|
|
|
|
|
let key_id = keychain::ExtKeychain::derive_key_id(1, 4, 0, 0, 0);
|
|
|
|
let prev = BlockHeader::default();
|
|
|
|
let b = new_block(
|
|
|
|
vec![&mut Transaction::new(
|
|
|
|
ins.clone(),
|
|
|
|
outs.clone(),
|
|
|
|
kernels.clone(),
|
|
|
|
)],
|
|
|
|
&keychain,
|
|
|
|
&prev,
|
|
|
|
&key_id,
|
|
|
|
);
|
|
|
|
|
|
|
|
// block should have been automatically compacted (including reward
|
|
|
|
// output) and should still be valid
|
|
|
|
match b.validate(&BlindingFactor::zero(), verifier_cache()) {
|
|
|
|
Err(Error::Transaction(transaction::Error::Secp(secp::Error::InvalidRangeProof))) => {}
|
|
|
|
_ => panic!("Bad range proof should be invalid"),
|
|
|
|
}
|
|
|
|
}
|