Skip to content

Commit

Permalink
add test test_generate_epochs.
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanYuan committed Sep 6, 2023
1 parent f74e8e5 commit 0cca328
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 10 deletions.
9 changes: 4 additions & 5 deletions rpc/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use crate::{RpcServer, ServiceBuilder};
use ckb_app_config::{BlockAssemblerConfig, NetworkConfig, RpcConfig, RpcModule};
use ckb_chain::chain::{ChainController, ChainService};
use ckb_chain_spec::consensus::Consensus;
use ckb_dao::DaoCalculator;
use ckb_jsonrpc_types::ScriptHashType;
use ckb_launcher::SharedBuilder;
use ckb_network::{Flags, NetworkService, NetworkState};
use ckb_reward_calculator::RewardCalculator;
use ckb_shared::{Shared, Snapshot};
use ckb_store::ChainStore;
use ckb_test_chain_utils::{
always_success_cell, always_success_cellbase, always_success_consensus,
};
use ckb_test_chain_utils::{always_success_cell, always_success_cellbase};
use ckb_types::{
core::{
cell::resolve_transaction, BlockBuilder, BlockView, HeaderView, TransactionBuilder,
Expand Down Expand Up @@ -206,9 +205,9 @@ fn always_success_transaction() -> TransactionView {

// setup a chain with 20 blocks and enable `Chain`, `Miner` and `Pool` rpc modules for unit test
// there is a similar fn `setup_rpc_test_suite` which enables all rpc modules, may be refactored into one fn with different paramsters in other PRs
fn setup() -> RpcTestSuite {
fn setup(consensus: Consensus) -> RpcTestSuite {
let (shared, mut pack) = SharedBuilder::with_temp_db()
.consensus(always_success_consensus())
.consensus(consensus)
.block_assembler_config(Some(BlockAssemblerConfig {
code_hash: h256!("0x0"),
args: Default::default(),
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/tests/module/miner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::tests::{always_success_transaction, setup, RpcTestRequest};
use ckb_store::ChainStore;
use ckb_test_chain_utils::always_success_cell;
use ckb_test_chain_utils::{always_success_cell, always_success_consensus};
use ckb_types::{
core::{capacity_bytes, Capacity, TransactionBuilder},
packed::{CellDep, CellInput, CellOutputBuilder, OutPoint},
Expand All @@ -12,7 +12,7 @@ use std::{sync::Arc, thread::sleep, time::Duration};
#[test]
#[ignore]
fn test_get_block_template_cache() {
let suite = setup();
let suite = setup(always_success_consensus());
// block template cache will expire when new uncle block is added to the chain
{
let response_old = suite.rpc(&RpcTestRequest {
Expand Down
1 change: 1 addition & 0 deletions rpc/src/tests/module/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod miner;
mod pool;
mod test;
5 changes: 2 additions & 3 deletions rpc/src/tests/module/pool.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::{thread::sleep, time::Duration};

use ckb_store::ChainStore;
use ckb_test_chain_utils::always_success_cell;
use ckb_test_chain_utils::ckb_testnet_consensus;
use ckb_test_chain_utils::{always_success_cell, always_success_consensus, ckb_testnet_consensus};
use ckb_types::{
core::{self, Capacity, TransactionBuilder},
packed::{self, CellDep, CellInput, CellOutputBuilder, OutPoint},
Expand Down Expand Up @@ -139,7 +138,7 @@ fn test_default_outputs_validator() {
#[test]
#[ignore]
fn test_send_transaction_exceeded_maximum_ancestors_count() {
let suite = setup();
let suite = setup(always_success_consensus());

let store = suite.shared.store();
let tip = store.get_tip_header().unwrap();
Expand Down
112 changes: 112 additions & 0 deletions rpc/src/tests/module/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#![allow(clippy::inconsistent_digit_grouping)]

use ckb_chain_spec::consensus::build_genesis_epoch_ext;
use ckb_store::ChainStore;
use ckb_test_chain_utils::always_success_consensus;
use ckb_types::{
core::{Capacity, EpochNumberWithFraction},
prelude::Unpack,
utilities::DIFF_TWO,
};

use crate::tests::{setup, RpcTestRequest, RpcTestSuite};

const GENESIS_EPOCH_LENGTH: u64 = 30;

#[test]
fn test_generate_epochs() {
let suite = setup_rpc();
assert_eq!(
get_current_epoch(&suite),
EpochNumberWithFraction::new(0, 20, GENESIS_EPOCH_LENGTH)
);

// generate 1 epoch
suite.rpc(&RpcTestRequest {
id: 42,
jsonrpc: "2.0".to_string(),
method: "generate_epochs".to_string(),
params: vec!["0x1".into()],
});
assert_eq!(
get_current_epoch(&suite),
EpochNumberWithFraction::new(1, 20, GENESIS_EPOCH_LENGTH)
);

// generate 1(0/1) epoch
suite.rpc(&RpcTestRequest {
id: 42,
jsonrpc: "2.0".to_string(),
method: "generate_epochs".to_string(),
params: vec!["0x10000000001".into()],
});
assert_eq!(
get_current_epoch(&suite),
EpochNumberWithFraction::new(2, 20, GENESIS_EPOCH_LENGTH)
);

// generate 1/2 epoch
suite.rpc(&RpcTestRequest {
id: 42,
jsonrpc: "2.0".to_string(),
method: "generate_epochs".to_string(),
params: vec!["0x20001000000".into()],
});
assert_eq!(
get_current_epoch(&suite),
EpochNumberWithFraction::new(3, 5, GENESIS_EPOCH_LENGTH)
);

// generate 3/2 epoch
suite.rpc(&RpcTestRequest {
id: 42,
jsonrpc: "2.0".to_string(),
method: "generate_epochs".to_string(),
params: vec!["0x20003000000".into()],
});
assert_eq!(
get_current_epoch(&suite),
EpochNumberWithFraction::new(4, 20, GENESIS_EPOCH_LENGTH)
);

// generate 0/2 epoch
suite.rpc(&RpcTestRequest {
id: 42,
jsonrpc: "2.0".to_string(),
method: "generate_epochs".to_string(),
params: vec!["0x20000000000".into()],
});
assert_eq!(
get_current_epoch(&suite),
EpochNumberWithFraction::new(4, 20, GENESIS_EPOCH_LENGTH)
);
}

// setup a chain for integration test rpc
fn setup_rpc() -> RpcTestSuite {
const INITIAL_PRIMARY_EPOCH_REWARD: Capacity = Capacity::shannons(1_917_808_21917808);
const DEFAULT_EPOCH_DURATION_TARGET: u64 = 240;
const DEFAULT_ORPHAN_RATE_TARGET: (u32, u32) = (1, 40);
let epoch_ext = build_genesis_epoch_ext(
INITIAL_PRIMARY_EPOCH_REWARD,
DIFF_TWO,
GENESIS_EPOCH_LENGTH,
DEFAULT_EPOCH_DURATION_TARGET,
DEFAULT_ORPHAN_RATE_TARGET,
);
let mut consensus = always_success_consensus();
consensus.genesis_epoch_ext = epoch_ext;
consensus.epoch_duration_target = 240;
consensus.permanent_difficulty_in_dummy = true;

setup(consensus)
}

fn get_current_epoch(suite: &RpcTestSuite) -> EpochNumberWithFraction {
let store = suite.shared.store();
let tip_block_number = store.get_tip_header().unwrap().data().raw().number();
store
.get_current_epoch_ext()
.unwrap()
.number_with_fraction(tip_block_number.unpack())
}

0 comments on commit 0cca328

Please sign in to comment.