-
Notifications
You must be signed in to change notification settings - Fork 990
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use mmr sizes in header to validate header weight (#3395)
* use mmr sizes in header to validate block weight given header only * add global weight validaton to headers when reading them untrusted off the network * fixup tests, one pending test * add test for deserializing untrusted block header exceeeding weight limits * validate header mmr sizes, must grow for each block * remove redundant height check * fix tests after rebase block specific TooHeavy error
- Loading branch information
Showing
12 changed files
with
239 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2020 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. | ||
|
||
use grin_chain as chain; | ||
use grin_core as core; | ||
use grin_keychain as keychain; | ||
|
||
mod chain_test_helper; | ||
|
||
use self::chain_test_helper::{clean_output_dir, mine_chain}; | ||
use crate::chain::{Chain, ErrorKind, Options}; | ||
use crate::core::{ | ||
consensus, | ||
core::{block, Block}, | ||
global, | ||
libtx::{reward, ProofBuilder}, | ||
pow, | ||
}; | ||
use crate::keychain::{ExtKeychain, ExtKeychainPath, Keychain}; | ||
use chrono::Duration; | ||
|
||
fn build_block(chain: &Chain) -> Block { | ||
let keychain = ExtKeychain::from_random_seed(false).unwrap(); | ||
let pk = ExtKeychainPath::new(1, 1, 0, 0, 0).to_identifier(); | ||
|
||
let prev = chain.head_header().unwrap(); | ||
let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter().unwrap()); | ||
let reward = reward::output(&keychain, &ProofBuilder::new(&keychain), &pk, 0, false).unwrap(); | ||
let mut block = Block::new(&prev, &[], next_header_info.clone().difficulty, reward).unwrap(); | ||
|
||
block.header.timestamp = prev.timestamp + Duration::seconds(60); | ||
block.header.pow.secondary_scaling = next_header_info.secondary_scaling; | ||
|
||
chain.set_txhashset_roots(&mut block).unwrap(); | ||
|
||
let edge_bits = global::min_edge_bits(); | ||
block.header.pow.proof.edge_bits = edge_bits; | ||
pow::pow_size( | ||
&mut block.header, | ||
next_header_info.difficulty, | ||
global::proofsize(), | ||
edge_bits, | ||
) | ||
.unwrap(); | ||
|
||
block | ||
} | ||
|
||
#[test] | ||
fn test_header_weight_validation() { | ||
let chain_dir = ".grin.header_weight"; | ||
clean_output_dir(chain_dir); | ||
let chain = mine_chain(chain_dir, 5); | ||
assert_eq!(chain.head().unwrap().height, 4); | ||
|
||
let block = build_block(&chain); | ||
let mut header = block.header; | ||
|
||
// Artificially set the output_mmr_size too large for a valid block. | ||
// Note: We will validate this even if just processing the header. | ||
header.output_mmr_size = 1_000; | ||
|
||
let res = chain | ||
.process_block_header(&header, Options::NONE) | ||
.map_err(|e| e.kind()); | ||
|
||
// Weight validation is done via transaction body and results in a slightly counter-intuitive tx error. | ||
assert_eq!(res, Err(ErrorKind::Block(block::Error::TooHeavy))); | ||
|
||
clean_output_dir(chain_dir); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.