Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add test cases for stake smt tx, delegate smt tx and reward tx #39

Merged
merged 3 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions common/src/traits/ckb_rpc_client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::types::{
BlockNumber, CellWithStatus, JsonBytes, OutPoint, OutputsValidator, Transaction, Uint32,
};
use anyhow::Result;
use async_trait::async_trait;
use ckb_types::H256;

use crate::types::ckb_rpc_client::{Cell, IndexerTip, Order, Pagination, RpcSearchKey, SearchKey};
use crate::types::{
BlockNumber, CellWithStatus, JsonBytes, OutPoint, OutputsValidator, Transaction,
TransactionWithStatusResponse, Uint32,
};

#[async_trait]
pub trait CkbRpc: Send + Sync + Clone {
Expand All @@ -28,6 +29,9 @@ pub trait CkbRpc: Send + Sync + Clone {
tx: &Transaction,
outputs_validator: Option<OutputsValidator>,
) -> Result<H256>;

// Chain
async fn get_transaction(&self, hash: H256) -> Result<Option<TransactionWithStatusResponse>>;
}

#[async_trait]
Expand Down
3 changes: 2 additions & 1 deletion common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ pub use ethereum_types::{
};

pub use ckb_jsonrpc_types::{
BlockNumber, CellWithStatus, JsonBytes, OutPoint, OutputsValidator, Transaction, Uint32,
BlockNumber, CellWithStatus, JsonBytes, OutPoint, OutputsValidator, Transaction,
TransactionWithStatusResponse, Uint32,
};
4 changes: 2 additions & 2 deletions common/src/types/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl From<DelegateRequirement> for ADelegateRequirement {
}
}

#[derive(Clone, Default)]
#[derive(Clone, Default, Debug)]
pub struct StakeItem {
pub is_increase: bool,
pub amount: Amount,
Expand All @@ -122,7 +122,7 @@ pub struct StakeItem {
impl From<StakeItem> for StakeInfoDelta {
fn from(stake: StakeItem) -> Self {
StakeInfoDelta::new_builder()
.is_increase(Byte::new(stake.is_increase.into()))
.is_increase((stake.is_increase as u8).into())
.amount(to_uint128(stake.amount))
.inauguration_epoch(to_uint64(stake.inauguration_epoch))
.build()
Expand Down
2 changes: 2 additions & 0 deletions devtools/tx-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ cargo run -- -s redeem // redeem stake
cargo run -- -s first // first delegate
cargo run -- -s add // add delegate
cargo run -- -s redeem // redeem delegate

cargo run -- -t // stake smt tx
```
16 changes: 8 additions & 8 deletions devtools/tx-tests/src/config/type_ids.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
selection_type_id = "0x705d8dc9b7cd351e29bfb24fb4443e0f7f40b01b13940db5a6f54adfa7b63170"
issue_type_id = "0x3054fe87c40f2ef563bb83e868bab744a173fe37a651d0448116a684e33650d5"
checkpoint_type_id = "0xf451af07b7771925065b3010362c5103cc1809a60de8fc2c9c82d2bec3404d0c"
metadata_type_id = "0xc5387cff490467c52bd2b2352c152a6aa57bbf469e4aa5fe51459ed0647c14c6"
stake_smt_type_id = "0x9b95a2923c775ab42e71314a3110614e86e051fd9c05186f0b0c335fd91e4630"
delegate_smt_type_id = "0xcba604590a06ccc77db8dd827516cb8dbddce2e176cc7ba36d7b4a40dd6b49a9"
reward_smt_type_id = "0x64ec42c5d6de37fc37ed337727ab73c54fef46204cba58bf4b923986b4aef858"
xudt_owner = "0x5bcdd4abdbc6b06a0e949bb469cc6d35e11c443fc9ab0586d5a937c662044964"
selection_type_id = "0xeab0e0bdde4f92982491e677555dd07da40a95ed4f2a69604c134eb4a930dbb2"
issue_type_id = "0x7b3f21c041745249930a8cb44b31874d81fa07bb6e743fefce0000c4c115c279"
checkpoint_type_id = "0xb4189de7fae992a0cc38deda58dd74562a1d2117eb3cdc75b80359928ffa2618"
metadata_type_id = "0xe9af859142be9effff2f5904d927e697e05573b8f3494109bcd81e6355f51085"
stake_smt_type_id = "0x4f241ed011d51fe81558dbb91450fe92f343d37d180689d0fde8f1047e8a92ff"
delegate_smt_type_id = "0x27cd8265a48095f1b23da490ba3571630c2319dce5f758e8cb4dabc1a9386577"
reward_smt_type_id = "0x8ab9b5a16d1a8c36f9db2472587f065c8fe56da879a82a9eb858908e39fb4529"
xudt_owner = "0x5c9c204f74ceb84e55a66c2570fdf825ce4b1cfe38e76c1c6a0c975f758388c3"
36 changes: 36 additions & 0 deletions devtools/tx-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ async fn main() {
.required(false)
.num_args(0)
.help("test checkpoint tx"),
)
.arg(
clap::Arg::new("stake smt")
.short('t')
.required(false)
.num_args(0)
.help("test stake smt tx"),
)
.arg(
clap::Arg::new("delegate smt")
.short('e')
.required(false)
.num_args(0)
.help("test delegate smt tx"),
)
.arg(
clap::Arg::new("reward")
.short('r')
.required(false)
.num_args(0)
.help("test reward tx"),
);

let matches = cmd.get_matches();
Expand All @@ -57,6 +78,9 @@ async fn main() {
let stake = matches.get_one::<String>("stake").unwrap().as_str();
let delegate = matches.get_one::<String>("delegate").unwrap().as_str();
let checkpoint = matches.get_one::<bool>("checkpoint").unwrap().to_owned();
let stake_smt = matches.get_one::<bool>("stake smt").unwrap().to_owned();
let delegate_smt = matches.get_one::<bool>("delegate smt").unwrap().to_owned();
let reward = matches.get_one::<bool>("reward").unwrap().to_owned();

let ckb = CkbRpcClient::new("https://testnet.ckb.dev");

Expand Down Expand Up @@ -89,4 +113,16 @@ async fn main() {
if checkpoint {
checkpoint_tx(&ckb).await;
}

if stake_smt {
stake_smt_tx(&ckb).await;
}

if delegate_smt {
delegate_smt_tx(&ckb).await;
}

if reward {
reward_tx(&ckb).await;
}
}
67 changes: 67 additions & 0 deletions devtools/tx-tests/src/tx/delegate_smt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::{fs, path::PathBuf, vec};

use ckb_types::prelude::Pack;
use rpc_client::ckb_client::ckb_rpc_client::CkbRpcClient;
use storage::SmtManager;

use common::traits::tx_builder::IDelegateSmtTxBuilder;
use common::types::tx_builder::DelegateSmtTypeIds;
use tx_builder::ckb::delegate_smt::DelegateSmtTxBuilder;
use tx_builder::ckb::helper::{Delegate, OmniEth, Tx, Xudt};

use crate::config::parse_file;
use crate::config::types::{PrivKeys, TypeIds as CTypeIds};
use crate::{PRIV_KEYS_PATH, TYPE_IDS_PATH};

static ROCKSDB_PATH: &str = "./free-space/smt/delegate";

pub async fn delegate_smt_tx(ckb: &CkbRpcClient) {
let priv_keys: PrivKeys = parse_file(PRIV_KEYS_PATH);
let test_kicker_key = priv_keys.delegator_privkeys[0].clone().into_h256().unwrap();
let omni_eth = OmniEth::new(test_kicker_key.clone());
println!("kicker ckb addres: {}\n", omni_eth.ckb_address().unwrap());

let type_ids: CTypeIds = parse_file(TYPE_IDS_PATH);
let metadata_type_id = type_ids.metadata_type_id.into_h256().unwrap();
let checkpoint_type_id = type_ids.checkpoint_type_id.into_h256().unwrap();
let delegate_smt_type_id = type_ids.delegate_smt_type_id.into_h256().unwrap();
let xudt_owner = type_ids.xudt_owner.into_h256().unwrap();

let delegate_cell = Delegate::get_cell(
ckb,
Delegate::lock(&metadata_type_id, &omni_eth.address().unwrap()),
Xudt::type_(&xudt_owner.pack()),
)
.await
.unwrap()
.unwrap();

let path = PathBuf::from(ROCKSDB_PATH);
fs::remove_dir_all(path.clone()).unwrap();
let smt = SmtManager::new(path);

let (tx, _) = DelegateSmtTxBuilder::new(
ckb,
test_kicker_key,
0,
DelegateSmtTypeIds {
metadata_type_id,
checkpoint_type_id,
delegate_smt_type_id,
xudt_owner,
},
vec![delegate_cell],
smt,
)
.build_tx()
.await
.unwrap();

let mut tx = Tx::new(ckb, tx);
match tx.send().await {
Ok(tx_hash) => println!("tx hash: 0x{}", tx_hash),
Err(e) => println!("{}", e),
}

println!("\ntx: {}", tx.inner());
}
25 changes: 18 additions & 7 deletions devtools/tx-tests/src/tx/init.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use common::traits::tx_builder::IInitTxBuilder;
use common::types::tx_builder::{Checkpoint, Metadata};
use common::types::tx_builder::{Checkpoint, Metadata, PrivateKey};
use rpc_client::ckb_client::ckb_rpc_client::CkbRpcClient;
use tx_builder::ckb::helper::{OmniEth, Tx};
use tx_builder::ckb::init::InitTxBuilder;
Expand All @@ -16,10 +16,9 @@ pub async fn init_tx(ckb: &CkbRpcClient) {
let omni_eth = OmniEth::new(test_seeder_key.clone());
println!("seeder ckb addres: {}\n", omni_eth.ckb_address().unwrap());

let (tx, type_id_args) = InitTxBuilder::new(
_init_tx(
ckb,
test_seeder_key,
10000,
Checkpoint {
epoch: 0,
period: 0,
Expand All @@ -35,11 +34,21 @@ pub async fn init_tx(ckb: &CkbRpcClient) {
..Default::default()
},
)
.build_tx()
.await
.unwrap();
.await;
}

pub async fn _init_tx(
ckb: &CkbRpcClient,
seeder_key: PrivateKey,
checkpoint: Checkpoint,
metadata: Metadata,
) -> Tx<CkbRpcClient> {
let (tx, type_id_args) = InitTxBuilder::new(ckb, seeder_key, 10000, checkpoint, metadata)
.build_tx()
.await
.unwrap();

let tx = Tx::new(ckb, tx);
let mut tx = Tx::new(ckb, tx);

match tx.send().await {
Ok(tx_hash) => println!("tx hash: 0x{}", tx_hash),
Expand All @@ -48,4 +57,6 @@ pub async fn init_tx(ckb: &CkbRpcClient) {

let type_ids: CTypeIds = type_id_args.into();
write_file(TYPE_IDS_PATH, &type_ids);

tx
}
10 changes: 7 additions & 3 deletions devtools/tx-tests/src/tx/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ pub async fn mint_tx(ckb: &CkbRpcClient) {
println!("seeder ckb addres: {}\n", omni_eth.ckb_address().unwrap());

let mut stakers = vec![];
for staker_privkey in priv_keys.staker_privkeys.into_iter() {
for (i, staker_privkey) in priv_keys.staker_privkeys.into_iter().enumerate() {
let privkey = staker_privkey.clone().into_h256().unwrap();
let omni_eth = OmniEth::new(privkey);
println!("staker ckb addres: {}", omni_eth.ckb_address().unwrap());
println!(
"staker{} ckb addres: {}",
i,
omni_eth.ckb_address().unwrap()
);
stakers.push((omni_eth.address().unwrap(), 200));
}

Expand All @@ -36,7 +40,7 @@ pub async fn mint_tx(ckb: &CkbRpcClient) {
.await
.unwrap();

let tx = Tx::new(ckb, tx);
let mut tx = Tx::new(ckb, tx);

match tx.send().await {
Ok(tx_hash) => println!("tx hash: 0x{}", tx_hash),
Expand Down
6 changes: 6 additions & 0 deletions devtools/tx-tests/src/tx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
mod checkpoint;
mod delegate;
mod delegate_smt;
mod init;
mod mint;
mod reward;
mod stake;
mod stake_smt;

pub use checkpoint::checkpoint_tx;
pub use delegate::{add_delegate_tx, first_delegate_tx, reedem_delegate_tx};
pub use delegate_smt::delegate_smt_tx;
pub use init::init_tx;
pub use mint::mint_tx;
pub use reward::reward_tx;
pub use stake::{add_stake_tx, first_stake_tx, reedem_stake_tx};
pub use stake_smt::stake_smt_tx;