Skip to content

Commit

Permalink
feat: panic on inconsistent block submission
Browse files Browse the repository at this point in the history
  • Loading branch information
raventid committed Jul 10, 2024
1 parent 312bb77 commit 35cd141
Showing 1 changed file with 25 additions and 40 deletions.
65 changes: 25 additions & 40 deletions contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,30 +226,24 @@ impl Contract {
///
/// # Panics
/// - Many cases
///
/// # Errors
/// - No previous block recorded, so we cannot validate chain
#[handle_result]
#[pause(except(roles(Role::UnrestrictedSubmitBlocks)))]
pub fn submit_block_header(&mut self, block_header: Header) -> Result<(), String> {
pub fn submit_block_header(&mut self, block_header: Header) {
// Chainwork is validated inside block_header structure (other consistency checks too)
let prev_blockhash = block_header.prev_blockhash.to_string();

let prev_block_header = if let Some(header) = self.headers_pool.get(&prev_blockhash) {
header.clone()
} else {
// We do not have a previous block in the headers_pool, there is a high probability
//it means we are starting to receive a new fork,
// so what we do now is we are returning the error code
// to ask the relay to deploy the previous block.
//
// Offchain relay now, should submit blocks one by one in decreasing height order
// 80 -> 79 -> 78 -> ...
// And do it until we can accept the block.
// It means we found an initial fork position.
// We are starting to gather new fork from this initial position.
return Err(String::from("1"));
};
// We do not have a previous block in the headers_pool, there is a high probability
//it means we are starting to receive a new fork,
// so what we do now is we are returning the error code
// to ask the relay to deploy the previous block.
//
// Offchain relay now, should submit blocks one by one in decreasing height order
// 80 -> 79 -> 78 -> ...
// And do it until we can accept the block.
// It means we found an initial fork position.
// We are starting to gather new fork from this initial position.
let prev_block_header = self.headers_pool
.get(&prev_blockhash)
.unwrap_or_else(|| env::panic_str("PrevBlockNotFound"));

if self.enable_check {
block_header
Expand Down Expand Up @@ -296,8 +290,6 @@ impl Contract {
self.reorg_chain(&current_blockhash);
}
}

Ok(())
}

/// The most expensive operation which reorganizes the chain, based on fork weight
Expand Down Expand Up @@ -607,7 +599,7 @@ mod tests {
let header = fork_block_header_example();
let mut contract = Contract::new(genesis_block_header(), 0, true, 3);

contract.submit_block_header(header).unwrap();
contract.submit_block_header(header);

let received_header = contract.get_last_block_header();

Expand All @@ -630,7 +622,7 @@ mod tests {

let mut contract = Contract::new(genesis_block_header(), 0, false, 3);

contract.submit_block_header(header).unwrap();
contract.submit_block_header(header);

let received_header = contract.get_last_block_header();

Expand All @@ -653,11 +645,10 @@ mod tests {

let mut contract = Contract::new(genesis_block_header(), 0, false, 3);

contract.submit_block_header(header).unwrap();
contract.submit_block_header(header);

contract
.submit_block_header(fork_block_header_example())
.unwrap();
.submit_block_header(fork_block_header_example());

let received_header = contract.get_last_block_header();

Expand All @@ -680,8 +671,7 @@ mod tests {
let mut contract = Contract::new(genesis_block_header(), 0, false, 3);

contract
.submit_block_header(block_header_example())
.unwrap();
.submit_block_header(block_header_example());

assert_eq!(
contract.get_blockhash_by_height(0).unwrap(),
Expand All @@ -698,8 +688,7 @@ mod tests {
let mut contract = Contract::new(genesis_block_header(), 0, false, 3);

contract
.submit_block_header(block_header_example())
.unwrap();
.submit_block_header(block_header_example());

assert_eq!(
contract
Expand All @@ -720,15 +709,12 @@ mod tests {
let mut contract = Contract::new(genesis_block_header(), 0, false, 3);

contract
.submit_block_header(block_header_example())
.unwrap();
.submit_block_header(block_header_example());

contract
.submit_block_header(fork_block_header_example())
.unwrap();
.submit_block_header(fork_block_header_example());
contract
.submit_block_header(fork_block_header_example_2())
.unwrap();
.submit_block_header(fork_block_header_example_2());

let received_header = contract.get_last_block_header();

Expand All @@ -746,11 +732,10 @@ mod tests {
}

#[test]
#[should_panic]
fn test_getting_an_error_if_submitting_unattached_block() {
let mut contract = Contract::new(genesis_block_header(), 0, false, 3);

let result = contract.submit_block_header(fork_block_header_example_2());
assert!(result.is_err());
assert!(result.is_err_and(|value| value == *"1"));
contract.submit_block_header(fork_block_header_example_2());
}
}

0 comments on commit 35cd141

Please sign in to comment.