Skip to content

Commit

Permalink
[force-deploy] Added some modifications to the test flow in test_forc…
Browse files Browse the repository at this point in the history
…e_upgrade_1
  • Loading branch information
welbon committed Dec 26, 2024
1 parent 9b774a8 commit 56c4824
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 15 deletions.
81 changes: 70 additions & 11 deletions chain/tests/test_force_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
use std::str::FromStr;
use std::sync::Arc;

use anyhow::format_err;

use starcoin_accumulator::Accumulator;
use starcoin_chain_api::{ChainReader, ChainWriter};
use starcoin_config::NodeConfig;
use starcoin_consensus::Consensus;
use starcoin_statedb::ChainStateDB;
use starcoin_transaction_builder::{build_transfer_from_association, DEFAULT_EXPIRATION_TIME};
use starcoin_transaction_builder::{
build_burn_illegal_stc_txn_with_association, build_transfer_from_association,
DEFAULT_EXPIRATION_TIME,
};
use starcoin_types::account_address::AccountAddress;
use starcoin_vm_runtime::force_upgrade_management::get_force_upgrade_block_number;
use starcoin_vm_types::on_chain_config::Version;
use starcoin_vm_types::{account_config, state_view::StateReaderExt};
use std::str::FromStr;
use std::sync::Arc;
use test_helper::executor::get_balance;

#[stest::test]
Expand Down Expand Up @@ -130,21 +135,75 @@ pub fn test_force_upgrade_1() -> anyhow::Result<()> {
let txns_num = txns_num + 2;
assert_eq!(miner.get_txn_accumulator().num_leaves(), txns_num);

// let black1_balance = get_balance(black1, miner.chain_state());
// println!("Black 1 balance is: {:?}", black1_balance);
// assert_eq!(
// black1_balance, 0,
// "Upgrade Failed, Balance of black list account not 0"
// );
//
// println!("Black 2 balance is: {:?}", black1_balance);
// let black2_balance = get_balance(black2, miner.chain_state());
// assert_eq!(
// black2_balance, 0,
// "Upgrade Failed, Balance of black list account not 0"
// );

assert_eq!(get_balance(rand3, miner.chain_state()), initial_balance + 3);

block2
};

// fork a new chain, to apply block number 3, this will call StdlibUpgrade::burn_illegal_token_from_frozen_address
{
let burn_black_txn_1 = build_burn_illegal_stc_txn_with_association(
&black1,
association_sequence_num + 3,
initial_balance + 1,
config.net(),
);
let burn_black_txn_2 = build_burn_illegal_stc_txn_with_association(
&black2,
association_sequence_num + 4,
initial_balance + 2,
config.net(),
);

let (block_template, _excluded) = miner
.create_block_template(
account_config::association_address(),
None,
vec![burn_black_txn_1, burn_black_txn_2],
vec![],
Some(block_gas_limit),
)
.unwrap();

let block3 = miner
.consensus()
.create_block(block_template, miner.time_service().as_ref())?;

miner.apply(block3.clone())?;

// 1 meta + 3 txns = 4 txns
let txns_num = txns_num + 4;
let leaves_num = miner.get_txn_accumulator().num_leaves();
assert_eq!(leaves_num, txns_num);

let black1_balance = get_balance(black1, miner.chain_state());
println!("Black 1 balance is: {:?}", black1_balance);
assert_eq!(
get_balance(black1, miner.chain_state()),
0,
black1_balance, 0,
"Upgrade Failed, Balance of black list account not 0"
);

println!("Black 2 balance is: {:?}", black1_balance);
let black2_balance = get_balance(black2, miner.chain_state());
assert_eq!(
get_balance(black2, miner.chain_state()),
0,
black2_balance, 0,
"Upgrade Failed, Balance of black list account not 0"
);

assert_eq!(get_balance(rand3, miner.chain_state()), initial_balance + 3);

block2
block3
};

// apply block number 2 to another chain
Expand Down
56 changes: 52 additions & 4 deletions vm/transaction-builder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use std::convert::TryInto;

use anyhow::Result;
use starcoin_config::{genesis_config::G_TOTAL_STC_AMOUNT, ChainNetwork};
use starcoin_crypto::hash::PlainCryptoHash;
use starcoin_crypto::HashValue;

use starcoin_config::{genesis_config::G_TOTAL_STC_AMOUNT, ChainNetwork};
use starcoin_types::account::Account;
use starcoin_vm_types::access::ModuleAccess;
use starcoin_vm_types::account_address::AccountAddress;
use starcoin_vm_types::account_config;
use starcoin_vm_types::account_config::{core_code_address, genesis_address};
use starcoin_vm_types::account_config::{
association_address, core_code_address, genesis_address, STC_TOKEN_CODE_STR,

Check failure on line 16 in vm/transaction-builder/src/lib.rs

View workflow job for this annotation

GitHub Actions / build and test

unused import: `STC_TOKEN_CODE_STR`
};
use starcoin_vm_types::file_format::CompiledModule;
use starcoin_vm_types::genesis_config::ChainId;
use starcoin_vm_types::identifier::Identifier;
Expand All @@ -25,8 +30,7 @@ use starcoin_vm_types::transaction::{
TransactionPayload,
};
use starcoin_vm_types::value::MoveValue;
use std::convert::TryInto;
use stdlib::{module_to_package, stdlib_package};
use stdlib::{module_to_package, stdlib_package, COMPILED_MOVE_CODE_DIR};

Check failure on line 33 in vm/transaction-builder/src/lib.rs

View workflow job for this annotation

GitHub Actions / build and test

unused import: `COMPILED_MOVE_CODE_DIR`
pub use stdlib::{stdlib_compiled_modules, stdlib_modules, StdLibOptions, StdlibVersion};

pub const DEFAULT_EXPIRATION_TIME: u64 = 40_000;
Expand Down Expand Up @@ -910,3 +914,47 @@ pub fn build_signed_empty_txn(
let signature = prikey.sign(&txn);
SignedUserTransaction::new(txn, signature)
}

// Build a signed user transaction for burning illegal tokens from a frozen address.
// This function creates a signed user transaction that attempts to burn illegal tokens
// from a frozen address. The transaction is signed by the `signer` and is intended for the `recipient`.
//
// # Arguments
// - `signer`: A reference to the `Account` that will sign the transaction.
// - `recipient`: A reference to the `AccountAddress` of the recipient.
// - `seq_num`: The sequence number of the transaction.
// - `amount`: The amount of tokens to be burned, represented as a `u128`.
// - `net`: A reference to the `ChainNetwork` which provides the chain ID for the transaction.
//
// # Returns
// - A `SignedUserTransaction` that is signed by the `signer` with the specified transaction details.
pub fn build_burn_illegal_stc_txn_with_association(
recipient: &AccountAddress,
seq_num: u64,
amount: u128,
net: &ChainNetwork,
) -> SignedUserTransaction {
let raw_txn = RawUserTransaction::new_with_default_gas_token(
association_address(),
seq_num,
TransactionPayload::ScriptFunction(ScriptFunction::new(
ModuleId::new(
core_code_address(),
Identifier::new("StdlibUpgradeScripts").unwrap(),
),
Identifier::new("burn_illegal_token_from_frozen_address").unwrap(),
vec![],
vec![
bcs_ext::to_bytes(&recipient).unwrap(),
bcs_ext::to_bytes(&amount).unwrap(),
],
)),
10000000,
1,
1000 + 60 * 60,
net.chain_id(),
);
net.genesis_config()
.sign_with_association(raw_txn)
.expect("Sign with association failed")
}

0 comments on commit 56c4824

Please sign in to comment.