Skip to content

Commit

Permalink
[Stake] add a simple smoke-test
Browse files Browse the repository at this point in the history
  • Loading branch information
zekun000 authored and aptos-bot committed May 1, 2022
1 parent b40baa2 commit 3d6410d
Show file tree
Hide file tree
Showing 20 changed files with 450 additions and 195 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"type": "block_metadata_transaction",
"version": "1",
"hash": "0xa8fc186c28fc0f95b8233651438093e8e215a51dcd16475f5e68c0a421f6305c",
"state_root_hash": "0x36266c9178229c20082c86058a0ff15d7e8281d564c0396199c40b9837c094e5",
"state_root_hash": "0x1be1aa05c82c263c78dec7f0b316c754c938366f07a9e58ffa90ae570ffada6e",
"event_root_hash": "0xd62c85b93234824cf825e9c028c3bcc59b06926039721bd7edd76b56ae9fcecf",
"gas_used": "0",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "0x5c3815107d2d38bf0918f1f0102c7eadaff311e727d720f99beb049386365806",
"accumulator_root_hash": "0x028afd47a483934ecdfe402ee0b30b212e4fd84da8229c796d53e1afabad4d63",
"changes": [
{
"type": "write_resource",
Expand Down Expand Up @@ -56,12 +56,12 @@
"type": "user_transaction",
"version": "2",
"hash": "0x18073cb2536317833aa630d30062383a1f00221dd3604bbce19cd262813e6c36",
"state_root_hash": "0xfd956cd03081bd9e5a82e6ae4c20b5d0bf44669368ab1002d1d3393b80e85d87",
"state_root_hash": "0x224b85b6e19f3f0e5c5b4e58c33cd97e4f2bab6b5f5a5350b1a703dda5b2ecc5",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "92",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "0x2617051da37d49dfd32aa91dd94bd1286feed4f72a653cb954a587eb6e1c0c85",
"accumulator_root_hash": "0x712b88b3891a57fd83d1a044fd93586f58542b139ff35f4b86fa82e5a2a4c96d",
"changes": [
{
"type": "write_resource",
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"type": "user_transaction",
"version": "2",
"hash": "0xf1d9819d9c3890f1d3e515be30cf920bd5bba8a7616dc02f21438c3ce08e27fa",
"state_root_hash": "0x673c46549e3cd269480e807065bbb5e239ce0019d335f546272b42c8b024a0d6",
"state_root_hash": "0xbc906005d541bd194d15f0f2b743c92114948ff1d0677fb1f5d3ac8ed3369e03",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Transaction Executed and Committed with Error MALFORMED",
"accumulator_root_hash": "0xed295d37eb4c5230ce3b16b1c350490a749640678acab6b67ab4eeb850a6e44f",
"accumulator_root_hash": "0xfbdac2be064c5702ccbde9b1f71e2ac69d7a660b96cb54fbaaa53f7de656a7ce",
"changes": [
{
"type": "write_resource",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"type": "user_transaction",
"version": "2",
"hash": "0x97053ab47cdf9d0bee6a38b61d4ca5366090474be9191c589377dad77b3fab66",
"state_root_hash": "0x673c46549e3cd269480e807065bbb5e239ce0019d335f546272b42c8b024a0d6",
"state_root_hash": "0xbc906005d541bd194d15f0f2b743c92114948ff1d0677fb1f5d3ac8ed3369e03",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Transaction Executed and Committed with Error LINKER_ERROR",
"accumulator_root_hash": "0x1c117522be906f5ae57d1b64c0bfc68a321cf939cc01cbb2fd651c4d3f940496",
"accumulator_root_hash": "0xadff5723e66a00eb1fb403396069a06b4238106c2831116a013f8ff178a21ebe",
"changes": [
{
"type": "write_resource",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"type": "user_transaction",
"version": "2",
"hash": "0x0760cc0dfba7e747fdcfda4ffeffbe4fa42061482758e14803535f5babfb09a0",
"state_root_hash": "0x673c46549e3cd269480e807065bbb5e239ce0019d335f546272b42c8b024a0d6",
"state_root_hash": "0xbc906005d541bd194d15f0f2b743c92114948ff1d0677fb1f5d3ac8ed3369e03",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Transaction Executed and Committed with Error FAILED_TO_DESERIALIZE_ARGUMENT",
"accumulator_root_hash": "0xcc86fac7123f562fa1be03253303b1e29c1431c1c1bcd7d61d1bf630201581fe",
"accumulator_root_hash": "0xe41cde18f2a04733fc6778f3a3117f379c2e226d344c01be199c2e3105a1c81c",
"changes": [
{
"type": "write_resource",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"type": "user_transaction",
"version": "2",
"hash": "0x454dcd76c7a06eebaf8674a72bb4ecd991b268047a6af9bba9fc15a8baa49af2",
"state_root_hash": "0x673c46549e3cd269480e807065bbb5e239ce0019d335f546272b42c8b024a0d6",
"state_root_hash": "0xbc906005d541bd194d15f0f2b743c92114948ff1d0677fb1f5d3ac8ed3369e03",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Transaction Executed and Committed with Error LINKER_ERROR",
"accumulator_root_hash": "0xd3e3f6fc14545d96c82e250bf96a0ee769cbb0b4f477dc294c920784921e9d45",
"accumulator_root_hash": "0x9645c2493acc4beb7fbb948fc9e1b10dfef0430a93a65cde19d0d17751309bd5",
"changes": [
{
"type": "write_resource",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"type": "user_transaction",
"version": "2",
"hash": "0xc00c694596b002f38c4a5eb5b8b20a772cb2bc7e5aacd3b2637e73bec802796e",
"state_root_hash": "0x673c46549e3cd269480e807065bbb5e239ce0019d335f546272b42c8b024a0d6",
"state_root_hash": "0xbc906005d541bd194d15f0f2b743c92114948ff1d0677fb1f5d3ac8ed3369e03",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Transaction Executed and Committed with Error FUNCTION_RESOLUTION_FAILURE",
"accumulator_root_hash": "0xb31e4d152eaf93c2410c3704b31f1a1d4981679715e69a878b95f5b6c3a2a862",
"accumulator_root_hash": "0xb2e9557afeb170c712c96f5a4bdb563510c6a1b24dbcffff9974501793698e4e",
"changes": [
{
"type": "write_resource",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"type": "user_transaction",
"version": "2",
"hash": "0xd99666ee3f06d5d564d1e8bf5955a020e7634fd9b539c2b6bab071670548f625",
"state_root_hash": "0x673c46549e3cd269480e807065bbb5e239ce0019d335f546272b42c8b024a0d6",
"state_root_hash": "0xbc906005d541bd194d15f0f2b743c92114948ff1d0677fb1f5d3ac8ed3369e03",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Transaction Executed and Committed with Error CODE_DESERIALIZATION_ERROR",
"accumulator_root_hash": "0x56f1ee2717a18370b4cebafd42a120dcd6a329b06b1f2a6cca98eb450d1e26b1",
"accumulator_root_hash": "0xdae5d544f6d05c673b3ddcf03f8623861693cf3980c1beec4ffaa0afa4de8372",
"changes": [
{
"type": "write_resource",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"type": "user_transaction",
"version": "2",
"hash": "0x8df66affad9c571750829820259721f96af4d7ae787bd7089bea259ff0f82cb1",
"state_root_hash": "0x673c46549e3cd269480e807065bbb5e239ce0019d335f546272b42c8b024a0d6",
"state_root_hash": "0xbc906005d541bd194d15f0f2b743c92114948ff1d0677fb1f5d3ac8ed3369e03",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Transaction Executed and Committed with Error NUMBER_OF_ARGUMENTS_MISMATCH",
"accumulator_root_hash": "0x193cdf5a5449c17ee0750befed25ce39ae385dc51f08f9c601efd44e20148e73",
"accumulator_root_hash": "0xaf8fe0b0c606b73e11fb1a8a00b0f76f0474f84d5b90d2584680b55bd88253f8",
"changes": [
{
"type": "write_resource",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"type": "user_transaction",
"version": "4",
"hash": "0x67d3d83ebf873c54ae08380b8984ee3bef0227c284117384b9e086498c52fb90",
"state_root_hash": "0x9a1a9ce94c511eab057cde74bdbae3638ae88e9edb355c11863d0ddb2880926f",
"state_root_hash": "0xf939fc41ff4a9f74b1a1faab8df208e30546bbd27a23d5b504f01138f9f4b8c8",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "21",
"success": false,
"vm_status": "Move abort by LIMIT_EXCEEDED - EINSUFFICIENT_BALANCE\n A limit on an amount, e.g. a currency, is exceeded. Example: withdrawal of money after account limits window\n is exhausted.\n Error codes",
"accumulator_root_hash": "0xd82837542c44bf4f285a1a0b73db10c184ad04724ad35617d0713261267f8f10",
"accumulator_root_hash": "0x927610a130bdb8411c52970d4faadfa00fde666dbdeb7e1296f0f18c88b8f7af",
"changes": [
{
"type": "write_resource",
Expand Down
57 changes: 22 additions & 35 deletions aptos-move/framework/aptos-framework/sources/configs/Stake.move
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module AptosFramework::Stake {
use Std::Errors;
use Std::Option::{Self, Option};
use Std::Signer;
use Std::Vector;
use AptosFramework::SystemAddresses;
Expand Down Expand Up @@ -191,11 +192,9 @@ module AptosFramework::Stake {
let validator_set = borrow_global_mut<ValidatorSet>(@CoreResources);
assert!(stake_pool.current_stake >= validator_set.minimum_stake, Errors::invalid_argument(ESTAKE_TOO_LOW));
assert!(stake_pool.current_stake <= validator_set.maximum_stake, Errors::invalid_argument(ESTAKE_TOO_HIGH));
let (exist, _) = find_validator(&validator_set.active_validators, addr);
assert!(!exist, Errors::invalid_argument(EALREADY_VALIDATOR));
let (exist, _) = find_validator(&validator_set.pending_inactive, addr);
assert!(!exist, Errors::invalid_argument(EALREADY_VALIDATOR));
let (exist, _) = find_validator(&validator_set.pending_active, addr);
let exist = Option::is_some(&find_validator(&validator_set.active_validators, addr)) ||
Option::is_some(&find_validator(&validator_set.pending_inactive, addr)) ||
Option::is_some(&find_validator(&validator_set.pending_active, addr));
assert!(!exist, Errors::invalid_argument(EALREADY_VALIDATOR));

Vector::push_back(&mut validator_set.pending_active, generate_validator_info(addr));
Expand All @@ -206,8 +205,9 @@ module AptosFramework::Stake {
let addr = Signer::address_of(account);
let validator_set = borrow_global_mut<ValidatorSet>(@CoreResources);

let (exist, index) = find_validator(&validator_set.active_validators, addr);
assert!(exist, Errors::invalid_argument(ENOT_VALIDATOR));
let maybe_index = find_validator(&validator_set.active_validators, addr);
assert!(Option::is_some(&maybe_index), Errors::invalid_argument(ENOT_VALIDATOR));
let index = Option::extract(&mut maybe_index);

let validator_info = Vector::swap_remove(&mut validator_set.active_validators, index);
assert!(Vector::length(&validator_set.active_validators) > 0, Errors::invalid_argument(ELAST_VALIDATOR));
Expand All @@ -216,12 +216,8 @@ module AptosFramework::Stake {

public fun is_current_validator(addr: address): bool acquires ValidatorSet{
let validator_set = borrow_global<ValidatorSet>(@CoreResources);
let (exist_1, _) = find_validator(&validator_set.active_validators, addr);
if (exist_1) {
return true
};
let (exist_2, _) = find_validator(&validator_set.pending_inactive, addr);
exist_2
Option::is_some(&find_validator(&validator_set.active_validators, addr)) ||
Option::is_some(&find_validator(&validator_set.pending_inactive, addr))
}

/// Triggers at epoch boundary.
Expand Down Expand Up @@ -301,52 +297,43 @@ module AptosFramework::Stake {
}

fun find_delegation_from_pool(pool: &StakePool, addr: address): bool {
let (exist, _) = find_delegation(&pool.active, addr);
if (exist) {
return true
};
let (exist, _) = find_delegation(&pool.pending_active, addr);
if (exist) {
return true
};
let (exist, _) = find_delegation(&pool.pending_inactive, addr);
if (exist) {
return true
};
let (exist, _) = find_delegation(&pool.inactive, addr);
exist
Option::is_some(&find_delegation(&pool.active, addr)) ||
Option::is_some(&find_delegation(&pool.pending_active, addr)) ||
Option::is_some(&find_delegation(&pool.pending_inactive, addr)) ||
Option::is_some(&find_delegation(&pool.inactive, addr))
}

fun find_delegation(v: &vector<Delegation>, addr: address): (bool, u64) {
fun find_delegation(v: &vector<Delegation>, addr: address): Option<u64> {
let i = 0;
let len = Vector::length(v);
while (i < len) {
let d = Vector::borrow(v, i);
if (d.from == addr) {
return (true, i)
return Option::some(i)
};
i = i + 1;
};
(false, 0)
Option::none()
}

fun find_validator(v: &vector<ValidatorInfo>, addr: address): (bool, u64) {
fun find_validator(v: &vector<ValidatorInfo>, addr: address): Option<u64> {
let i = 0;
let len = Vector::length(v);
while (i < len) {
if (Vector::borrow(v, i).addr == addr) {
return (true, i)
return Option::some(i)
};
i = i + 1;
};
(false, 0)
Option::none()
}

fun withdraw_internal(v: &mut vector<Delegation>, addr: address): Delegation {
let (exist, index) = find_delegation(v, addr);
if (!exist) {
let maybe_index = find_delegation(v, addr);
if (Option::is_none(&maybe_index)) {
abort Errors::invalid_argument(EDELEGATION_NOT_FOUND)
};
let index = Option::extract(&mut maybe_index);
Vector::swap_remove(v, index)
}

Expand Down
6 changes: 4 additions & 2 deletions aptos-move/framework/cached-packages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ pub fn load_abis(path: &str) -> Vec<ScriptABI> {
.find("**/*abis/*.abi")
.unwrap()
.filter_map(|file_module| match file_module {
DirEntry::Dir(_) => None,
DirEntry::File(file) => Some(bcs::from_bytes::<ScriptABI>(file.contents()).unwrap()),
DirEntry::File(file) if !file.path().to_str().unwrap().contains("Genesis") => {
Some(bcs::from_bytes::<ScriptABI>(file.contents()).unwrap())
}
_ => None,
})
.collect::<Vec<_>>()
}
Expand Down
2 changes: 2 additions & 0 deletions testsuite/smoke-test/src/aptos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ mod module_publish;
pub use module_publish::*;
mod error_report;
pub use error_report::*;
mod staking;
pub use staking::*;
101 changes: 101 additions & 0 deletions testsuite/smoke-test/src/aptos/staking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

use aptos_transaction_builder::aptos_stdlib;
use aptos_types::{account_address::AccountAddress, account_config::aptos_root_address};
use forge::{AptosContext, AptosTest, Result, Test};

pub struct Staking;

impl Test for Staking {
fn name(&self) -> &'static str {
"smoke-test::aptos::staking"
}
}

#[async_trait::async_trait]
impl AptosTest for Staking {
async fn run<'t>(&self, ctx: &mut AptosContext<'t>) -> Result<()> {
// created by root account
let mut accounts = vec![];
for _ in 0..10 {
let local_account = ctx.random_account();
ctx.create_user_account(local_account.public_key()).await?;
ctx.mint(local_account.address(), 10000).await?;
accounts.push(local_account);
}
let validator_addr = AccountAddress::from_hex_literal(
validator_set(ctx).await["active_validators"][0]["addr"]
.as_str()
.unwrap(),
)
.unwrap();
let txn_factory = ctx.aptos_transaction_factory();
let locked_period = 86400 * 2;
let current_time = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs()
+ locked_period;
for account in &mut accounts {
let txn = account.sign_with_transaction_builder(txn_factory.payload(
aptos_stdlib::encode_delegate_stake_script_function(
validator_addr,
100,
current_time,
),
));
ctx.client().submit_and_wait(&txn).await?;
}
let stake_pool = stake_pool_from_addr(ctx, validator_addr).await;
assert_eq!(
stake_pool["pending_active"].as_array().unwrap().len(),
accounts.len()
);
// force epoch change to make pending changes take effect
let txn = ctx.root_account().sign_with_transaction_builder(
txn_factory.payload(aptos_stdlib::encode_force_reconfigure_script_function()),
);
ctx.client().submit_and_wait(&txn).await?;
let stake_pool = stake_pool_from_addr(ctx, validator_addr).await;
assert_eq!(
stake_pool["active"].as_array().unwrap().len(),
accounts.len() + 1
);
assert_eq!(
stake_pool["current_stake"]
.as_str()
.unwrap()
.parse::<u64>()
.unwrap(),
accounts.len() as u64 * 100 + 1
);
let voting_power = validator_set(ctx).await["active_validators"][0]["voting_power"]
.as_str()
.unwrap()
.parse::<u64>()
.unwrap();
assert_eq!(voting_power, accounts.len() as u64 * 100 + 1);
Ok(())
}
}

async fn stake_pool_from_addr(ctx: &AptosContext<'_>, addr: AccountAddress) -> serde_json::Value {
ctx.client()
.get_account_resource(addr, "0x1::Stake::StakePool")
.await
.unwrap()
.into_inner()
.unwrap()
.data
}

async fn validator_set(ctx: &AptosContext<'_>) -> serde_json::Value {
ctx.client()
.get_account_resource(aptos_root_address(), "0x1::Stake::ValidatorSet")
.await
.unwrap()
.into_inner()
.unwrap()
.data
}
Loading

0 comments on commit 3d6410d

Please sign in to comment.