Skip to content

Commit

Permalink
feat(gwos): impl v2 finality mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
keroro520 committed Nov 16, 2022
1 parent cb282b0 commit d4e6dc1
Show file tree
Hide file tree
Showing 24 changed files with 570 additions and 197 deletions.
35 changes: 35 additions & 0 deletions crates/tests/src/script_tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::{fs, io::Read, path::PathBuf};
const SCRIPT_DIR: &str = "../../gwos/build/debug";
const CHALLENGE_LOCK_PATH: &str = "challenge-lock";
const WITHDRAWAL_LOCK_PATH: &str = "withdrawal-lock";
const CUSTODIAN_LOCK_PATH: &str = "custodian-lock";
const STAKE_LOCK_PATH: &str = "stake-lock";
const STATE_VALIDATOR: &str = "state-validator";
const SECP256K1_DATA_PATH: &str = "../../gwos/c/deps/ckb-production-scripts/build/secp256k1_data";
const ANYONE_CAN_PAY_LOCK_PATH: &str =
Expand Down Expand Up @@ -51,6 +53,39 @@ lazy_static! {
hasher.finalize(&mut buf);
buf
};
pub static ref CUSTODIAN_LOCK_PROGRAM: Bytes = {
let mut buf = Vec::new();
let mut path = PathBuf::new();
path.push(&SCRIPT_DIR);
path.push(&CUSTODIAN_LOCK_PATH);
let mut f = fs::File::open(&path).expect("load custodian lock program");
f.read_to_end(&mut buf)
.expect("read custodian lock program");
Bytes::from(buf.to_vec())
};
pub static ref CUSTODIAN_LOCK_CODE_HASH: [u8; 32] = {
let mut buf = [0u8; 32];
let mut hasher = new_blake2b();
hasher.update(&CUSTODIAN_LOCK_PROGRAM);
hasher.finalize(&mut buf);
buf
};
pub static ref STAKE_LOCK_PROGRAM: Bytes = {
let mut buf = Vec::new();
let mut path = PathBuf::new();
path.push(&SCRIPT_DIR);
path.push(&STAKE_LOCK_PATH);
let mut f = fs::File::open(&path).expect("load stake lock program");
f.read_to_end(&mut buf).expect("read stake lock program");
Bytes::from(buf.to_vec())
};
pub static ref STAKE_LOCK_CODE_HASH: [u8; 32] = {
let mut buf = [0u8; 32];
let mut hasher = new_blake2b();
hasher.update(&STAKE_LOCK_PROGRAM);
hasher.finalize(&mut buf);
buf
};
pub static ref ETH_ACCOUNT_LOCK_PROGRAM: Bytes = {
let mut buf = Vec::new();
let mut path = PathBuf::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::script_tests::utils::init_env_log;
use crate::script_tests::utils::layer1::build_simple_tx_with_out_point;
use crate::script_tests::utils::layer1::random_out_point;
use crate::script_tests::utils::rollup::{
build_always_success_cell, build_rollup_locked_cell, build_type_id_script,
calculate_state_validator_type_id, CellContext, CellContextParam,
build_always_success_cell, build_rollup_locked_cell, calculate_type_id,
named_always_success_script, CellContext, CellContextParam,
};
use crate::testing_tool::chain::into_deposit_info_cell;
use crate::testing_tool::chain::setup_chain_with_account_lock_manage;
Expand Down Expand Up @@ -66,7 +66,7 @@ pub(crate) fn build_merkle_proof(leaves: &[H256], indices: &[u32]) -> CKBMerkleP
async fn test_burn_challenge_capacity() {
init_env_log();
let input_out_point = random_out_point();
let type_id = calculate_state_validator_type_id(input_out_point.clone());
let type_id = calculate_type_id(input_out_point.clone());
let rollup_type_script = {
Script::new_builder()
.code_hash(Pack::pack(&*STATE_VALIDATOR_CODE_HASH))
Expand All @@ -80,9 +80,9 @@ async fn test_burn_challenge_capacity() {
.code_hash(CKBPack::pack(&[0u8; 32]))
.build();
let reward_burn_lock_hash: [u8; 32] = reward_burn_lock.calc_script_hash().unpack().into();
let stake_lock_type = build_type_id_script(b"stake_lock_type_id");
let challenge_lock_type = build_type_id_script(b"challenge_lock_type_id");
let eoa_lock_type = build_type_id_script(b"eoa_lock_type_id");
let stake_lock_type = named_always_success_script(b"stake_lock_type_id");
let challenge_lock_type = named_always_success_script(b"challenge_lock_type_id");
let eoa_lock_type = named_always_success_script(b"eoa_lock_type_id");
let challenge_script_type_hash: [u8; 32] =
challenge_lock_type.calc_script_hash().unpack().into();
let eoa_lock_type_hash: [u8; 32] = eoa_lock_type.calc_script_hash().unpack().into();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::script_tests::utils::init_env_log;
use crate::script_tests::utils::layer1::build_simple_tx_with_out_point;
use crate::script_tests::utils::layer1::random_out_point;
use crate::script_tests::utils::rollup::{
build_always_success_cell, build_rollup_locked_cell, build_type_id_script,
calculate_state_validator_type_id, CellContext, CellContextParam,
build_always_success_cell, build_rollup_locked_cell, calculate_type_id,
named_always_success_script, CellContext, CellContextParam,
};
use crate::testing_tool::chain::setup_chain_with_config;
use crate::testing_tool::chain::{apply_block_result, construct_block};
Expand Down Expand Up @@ -47,7 +47,7 @@ use gw_types::{
async fn test_cancel_tx_execute() {
init_env_log();
let input_out_point = random_out_point();
let type_id = calculate_state_validator_type_id(input_out_point.clone());
let type_id = calculate_type_id(input_out_point.clone());
let rollup_type_script = {
Script::new_builder()
.code_hash(Pack::pack(&*STATE_VALIDATOR_CODE_HASH))
Expand All @@ -56,10 +56,10 @@ async fn test_cancel_tx_execute() {
.build()
};
// rollup lock & config
let stake_lock_type = build_type_id_script(b"stake_lock_type_id");
let challenge_lock_type = build_type_id_script(b"challenge_lock_type_id");
let eoa_lock_type = build_type_id_script(b"eoa_lock_type_id");
let l2_sudt_type = build_type_id_script(b"l2_sudt_type_id");
let stake_lock_type = named_always_success_script(b"stake_lock_type_id");
let challenge_lock_type = named_always_success_script(b"challenge_lock_type_id");
let eoa_lock_type = named_always_success_script(b"eoa_lock_type_id");
let l2_sudt_type = named_always_success_script(b"l2_sudt_type_id");
let challenge_script_type_hash: [u8; 32] =
challenge_lock_type.calc_script_hash().unpack().into();
let l2_sudt_type_hash: [u8; 32] = l2_sudt_type.calc_script_hash().unpack().into();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::script_tests::utils::init_env_log;
use crate::script_tests::utils::layer1::build_simple_tx_with_out_point;
use crate::script_tests::utils::layer1::random_out_point;
use crate::script_tests::utils::rollup::{
build_always_success_cell, build_rollup_locked_cell, build_type_id_script,
calculate_state_validator_type_id, CellContext, CellContextParam,
build_always_success_cell, build_rollup_locked_cell, calculate_type_id,
named_always_success_script, CellContext, CellContextParam,
};
use crate::testing_tool::chain::into_deposit_info_cell;
use crate::testing_tool::chain::setup_chain_with_account_lock_manage;
Expand Down Expand Up @@ -55,7 +55,7 @@ use gw_types::{
async fn test_cancel_tx_signature() {
init_env_log();
let input_out_point = random_out_point();
let type_id = calculate_state_validator_type_id(input_out_point.clone());
let type_id = calculate_type_id(input_out_point.clone());
let rollup_type_script = {
Script::new_builder()
.code_hash(Pack::pack(&*STATE_VALIDATOR_CODE_HASH))
Expand All @@ -64,10 +64,10 @@ async fn test_cancel_tx_signature() {
.build()
};
// rollup lock & config
let stake_lock_type = build_type_id_script(b"stake_lock_type_id");
let challenge_lock_type = build_type_id_script(b"challenge_lock_type_id");
let eoa_lock_type = build_type_id_script(b"eoa_lock_type_id");
let l2_sudt_type = build_type_id_script(b"l2_sudt_type_id");
let stake_lock_type = named_always_success_script(b"stake_lock_type_id");
let challenge_lock_type = named_always_success_script(b"challenge_lock_type_id");
let eoa_lock_type = named_always_success_script(b"eoa_lock_type_id");
let l2_sudt_type = named_always_success_script(b"l2_sudt_type_id");
let challenge_script_type_hash: [u8; 32] =
challenge_lock_type.calc_script_hash().unpack().into();
let eoa_lock_type_hash: [u8; 32] = eoa_lock_type.calc_script_hash().unpack().into();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::script_tests::utils::init_env_log;
use crate::script_tests::utils::layer1::build_simple_tx_with_out_point;
use crate::script_tests::utils::layer1::random_out_point;
use crate::script_tests::utils::rollup::{
build_always_success_cell, build_rollup_locked_cell, build_type_id_script,
calculate_state_validator_type_id, CellContext, CellContextParam,
build_always_success_cell, build_rollup_locked_cell, calculate_type_id,
named_always_success_script, CellContext, CellContextParam,
};
use crate::testing_tool::chain::into_deposit_info_cell;
use crate::testing_tool::chain::{
Expand Down Expand Up @@ -52,7 +52,7 @@ use gw_types::{
async fn test_cancel_withdrawal() {
init_env_log();
let input_out_point = random_out_point();
let type_id = calculate_state_validator_type_id(input_out_point.clone());
let type_id = calculate_type_id(input_out_point.clone());
let rollup_type_script = {
Script::new_builder()
.code_hash(Pack::pack(&*STATE_VALIDATOR_CODE_HASH))
Expand All @@ -61,9 +61,9 @@ async fn test_cancel_withdrawal() {
.build()
};
// rollup lock & config
let stake_lock_type = build_type_id_script(b"stake_lock_type_id");
let challenge_lock_type = build_type_id_script(b"challenge_lock_type_id");
let eoa_lock_type = build_type_id_script(b"eoa_lock_type_id");
let stake_lock_type = named_always_success_script(b"stake_lock_type_id");
let challenge_lock_type = named_always_success_script(b"challenge_lock_type_id");
let eoa_lock_type = named_always_success_script(b"eoa_lock_type_id");
let challenge_script_type_hash: [u8; 32] =
challenge_lock_type.calc_script_hash().unpack().into();
let eoa_lock_type_hash: [u8; 32] = eoa_lock_type.calc_script_hash().unpack().into();
Expand Down
16 changes: 8 additions & 8 deletions crates/tests/src/script_tests/state_validator/enter_challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::script_tests::utils::init_env_log;
use crate::script_tests::utils::layer1::build_simple_tx_with_out_point;
use crate::script_tests::utils::layer1::random_out_point;
use crate::script_tests::utils::rollup::{
build_always_success_cell, build_rollup_locked_cell, build_type_id_script,
calculate_state_validator_type_id, CellContext, CellContextParam,
build_always_success_cell, build_rollup_locked_cell, calculate_type_id,
named_always_success_script, CellContext, CellContextParam,
};
use crate::testing_tool::chain::{apply_block_result, construct_block};
use crate::testing_tool::chain::{
Expand Down Expand Up @@ -45,7 +45,7 @@ const INVALID_CHALLENGE_TARGET_ERROR: i8 = 32;
async fn test_enter_challenge() {
init_env_log();
let input_out_point = random_out_point();
let type_id = calculate_state_validator_type_id(input_out_point.clone());
let type_id = calculate_type_id(input_out_point.clone());
let rollup_type_script = {
Script::new_builder()
.code_hash(Pack::pack(&*STATE_VALIDATOR_CODE_HASH))
Expand All @@ -54,8 +54,8 @@ async fn test_enter_challenge() {
.build()
};
// rollup lock & config
let stake_lock_type = build_type_id_script(b"stake_lock_type_id");
let challenge_lock_type = build_type_id_script(b"challenge_lock_type_id");
let stake_lock_type = named_always_success_script(b"stake_lock_type_id");
let challenge_lock_type = named_always_success_script(b"challenge_lock_type_id");
let challenge_script_type_hash: [u8; 32] =
challenge_lock_type.calc_script_hash().unpack().into();
let finality_blocks = 10;
Expand Down Expand Up @@ -279,7 +279,7 @@ async fn test_enter_challenge() {
async fn test_enter_challenge_finalized_block() {
init_env_log();
let input_out_point = random_out_point();
let type_id = calculate_state_validator_type_id(input_out_point.clone());
let type_id = calculate_type_id(input_out_point.clone());
let rollup_type_script = {
Script::new_builder()
.code_hash(Pack::pack(&*STATE_VALIDATOR_CODE_HASH))
Expand All @@ -288,8 +288,8 @@ async fn test_enter_challenge_finalized_block() {
.build()
};
// rollup lock & config
let stake_lock_type = build_type_id_script(b"stake_lock_type_id");
let challenge_lock_type = build_type_id_script(b"challenge_lock_type_id");
let stake_lock_type = named_always_success_script(b"stake_lock_type_id");
let challenge_lock_type = named_always_success_script(b"challenge_lock_type_id");
let challenge_script_type_hash: [u8; 32] =
challenge_lock_type.calc_script_hash().unpack().into();
let finality_blocks = 1;
Expand Down
10 changes: 5 additions & 5 deletions crates/tests/src/script_tests/state_validator/revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::script_tests::utils::init_env_log;
use crate::script_tests::utils::layer1::build_simple_tx_with_out_point;
use crate::script_tests::utils::layer1::{always_success_script, random_out_point};
use crate::script_tests::utils::rollup::{
build_always_success_cell, build_rollup_locked_cell, build_type_id_script,
calculate_state_validator_type_id, CellContext, CellContextParam,
build_always_success_cell, build_rollup_locked_cell, calculate_type_id,
named_always_success_script, CellContext, CellContextParam,
};
use crate::testing_tool::chain::{
apply_block_result, construct_block, into_deposit_info_cell, setup_chain_with_config,
Expand Down Expand Up @@ -44,7 +44,7 @@ use gw_types::{packed::StakeLockArgs, prelude::*};
async fn test_revert() {
init_env_log();
let input_out_point = random_out_point();
let type_id = calculate_state_validator_type_id(input_out_point.clone());
let type_id = calculate_type_id(input_out_point.clone());
let rollup_type_script = {
Script::new_builder()
.code_hash(Pack::pack(&*STATE_VALIDATOR_CODE_HASH))
Expand All @@ -62,9 +62,9 @@ async fn test_revert() {
.code_hash(CKBPack::pack(&[0u8; 32]))
.build();
let reward_burn_lock_hash: [u8; 32] = reward_burn_lock.calc_script_hash().unpack().into();
let stake_lock_type = build_type_id_script(b"stake_lock_type_id");
let stake_lock_type = named_always_success_script(b"stake_lock_type_id");
let stake_script_type_hash: [u8; 32] = stake_lock_type.calc_script_hash().unpack().into();
let challenge_lock_type = build_type_id_script(b"challenge_lock_type_id");
let challenge_lock_type = named_always_success_script(b"challenge_lock_type_id");
let challenge_script_type_hash: [u8; 32] =
challenge_lock_type.calc_script_hash().unpack().into();
let finality_blocks = 10;
Expand Down
Loading

0 comments on commit d4e6dc1

Please sign in to comment.