Skip to content

Commit

Permalink
[AptosFramework] add error code for Stake
Browse files Browse the repository at this point in the history
  • Loading branch information
zekun000 authored and aptos-bot committed Apr 26, 2022
1 parent aedf668 commit 4b306c0
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 151 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": "0xa6562f0e52751bfb2ffde0f6d4e7768208feaf3a0b13ed38b7b780adc8324ec0",
"state_root_hash": "0x164e0dd6c4865a34bf26ae9427a52285a07c564851ecb8711122d106ebcd42a9",
"event_root_hash": "0xd62c85b93234824cf825e9c028c3bcc59b06926039721bd7edd76b56ae9fcecf",
"gas_used": "0",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "0x6ecde3aa44f73ee29cf7c393dad128a2f068ed600bd2587dfb9f9d91244ec41e",
"accumulator_root_hash": "0x80badd4d29e8bdb11cca1e22811531cc562799a16a58706fdb4510fbc5029a0b",
"changes": [
{
"type": "write_resource",
Expand Down Expand Up @@ -56,12 +56,12 @@
"type": "user_transaction",
"version": "2",
"hash": "0x18073cb2536317833aa630d30062383a1f00221dd3604bbce19cd262813e6c36",
"state_root_hash": "0x2e4bcab624f97a0c236357d9b5644f87bcbc9a81f36f3e0c97d8d832f95525dc",
"state_root_hash": "0x3fb13a8a1fc4f0c284e4e7de179fb831aa63a25d7967c0ae32f2fdc50337b043",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "92",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "0x0cbe7dffce77c35e60786ae4ffca07093743c79ab9b6d2a5bb839f3df2db671b",
"accumulator_root_hash": "0xf1a105da25d374b8f9204e8295276059b0aa0dc2c28b752908bbf06155d67d3d",
"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": "0xa440baf7d284e00a14b2d788eebe92fbc4513e92f02cc012b1215e92234df95d",
"state_root_hash": "0x57d403ee832dd459894ce406aa25e5e7b4872d7e72e5e59ce456def80051540d",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Move bytecode deserialization / verification failed, including script function not found or invalid arguments",
"accumulator_root_hash": "0xb13f373a5fe84ac6e4bfaaf2e5dc5ddecddebc1ed898a059fbe084dc65cb5c3d",
"accumulator_root_hash": "0xdc043b2b514eae7669f271799dc20776868f5c7a26b8d4c920868bc86416e012",
"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": "0xa440baf7d284e00a14b2d788eebe92fbc4513e92f02cc012b1215e92234df95d",
"state_root_hash": "0x57d403ee832dd459894ce406aa25e5e7b4872d7e72e5e59ce456def80051540d",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Move bytecode deserialization / verification failed, including script function not found or invalid arguments",
"accumulator_root_hash": "0x36b86baa84b5cbd69144e835bbb9012748427750e6d668d18851a6463931dc24",
"accumulator_root_hash": "0x110d4551838194841714dce5da10b62e816a4adb4d0704059faec9b9f4ffe256",
"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": "0xa440baf7d284e00a14b2d788eebe92fbc4513e92f02cc012b1215e92234df95d",
"state_root_hash": "0x57d403ee832dd459894ce406aa25e5e7b4872d7e72e5e59ce456def80051540d",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Move bytecode deserialization / verification failed, including script function not found or invalid arguments",
"accumulator_root_hash": "0x990ad378635d8dbccdb1ac3d6cf02de637daaa2ea670ced3db45cc723a15c6df",
"accumulator_root_hash": "0x63af08ac203fe45deb8b67fe983bc6a56c3e7816f3e3a37dac30e677f7ae126d",
"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": "0xa440baf7d284e00a14b2d788eebe92fbc4513e92f02cc012b1215e92234df95d",
"state_root_hash": "0x57d403ee832dd459894ce406aa25e5e7b4872d7e72e5e59ce456def80051540d",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Move bytecode deserialization / verification failed, including script function not found or invalid arguments",
"accumulator_root_hash": "0x7018876b0253e8307392c9426eb407f057bccf44f53c8ad8ab20bbf6b4094115",
"accumulator_root_hash": "0x4da4ad6458e13c10730f1e9b8af1439e0cdcca040928d31abd6387bc1255011e",
"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": "0xa440baf7d284e00a14b2d788eebe92fbc4513e92f02cc012b1215e92234df95d",
"state_root_hash": "0x57d403ee832dd459894ce406aa25e5e7b4872d7e72e5e59ce456def80051540d",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Move bytecode deserialization / verification failed, including script function not found or invalid arguments",
"accumulator_root_hash": "0xcb5ce63ed138f2859173333d33445ac2d0f69719f0f3acec3505dfa1fe56778d",
"accumulator_root_hash": "0xf18eb932cac100a663d3949ea03658dd7240b9f29a5ced2503bbbf7c1a219f4a",
"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": "0xa440baf7d284e00a14b2d788eebe92fbc4513e92f02cc012b1215e92234df95d",
"state_root_hash": "0x57d403ee832dd459894ce406aa25e5e7b4872d7e72e5e59ce456def80051540d",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Move bytecode deserialization / verification failed, including script function not found or invalid arguments",
"accumulator_root_hash": "0x066ddd66e1f8527b4f6c2d88c01bb3b7a99c031b7288ca21a15dd79c761bf3d6",
"accumulator_root_hash": "0x25c30a1f25411e60012e07ab720891578036da5701eec6f0b9c47b00a31e57e8",
"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": "0xa440baf7d284e00a14b2d788eebe92fbc4513e92f02cc012b1215e92234df95d",
"state_root_hash": "0x57d403ee832dd459894ce406aa25e5e7b4872d7e72e5e59ce456def80051540d",
"event_root_hash": "0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
"gas_used": "1",
"success": false,
"vm_status": "Move bytecode deserialization / verification failed, including script function not found or invalid arguments",
"accumulator_root_hash": "0xb2ef5b04e4a64e255ac4917e37b9a35ab6fb3600ab3f1f93bd5ed9580639c336",
"accumulator_root_hash": "0xc7046d7d9ff9b1bc66fa295ddfc707eacf2e6664f46f569afef8f32c1236879e",
"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": "0x09b449cbbb040af9d216198af78a09e16a8e6644c0a27aa9f3f9b1083f244a35",
"state_root_hash": "0x03f097eac0bd957967a51c7dadc3bc7f3bcd6f4ec46c8884d12077f97055162e",
"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": "0x8aed0e8a39535fa9d2763892b12792a1b6c354beeaa299ee07093f7968bf92f7",
"accumulator_root_hash": "0xf30c39ed3c8bf98b4bf8b939c5fbd31c515f8b0f6f4c3ca2fcad2a1dd6f28842",
"changes": [
{
"type": "write_resource",
Expand Down
50 changes: 38 additions & 12 deletions aptos-move/framework/aptos-framework/sources/configs/Stake.move
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module AptosFramework::Stake {
use Std::Vector;
use Std::Errors;
use Std::Signer;
use Std::Vector;
use AptosFramework::SystemAddresses;
use AptosFramework::Timestamp;
use AptosFramework::TestCoin::{Self, Coin};
Expand All @@ -10,6 +11,26 @@ module AptosFramework::Stake {

const MINIMUM_LOCK_PERIOD: u64 = 86400;

/// Delegation not found for the account.
const EDELEGATION_NOT_FOUND: u64 = 1;
/// Lock period is shorter than required.
const ELOCK_TIME_TOO_SHORT: u64 = 2;
/// Withdraw not allowed, the stake is still locked.
const EWITHDRAW_NOT_ALLOWED: u64 = 3;
/// Validator Config not published.
const EVALIDATOR_CONFIG: u64 = 4;
/// Not enough stake to join validator set.
const ESTAKE_TOO_LOW: u64 = 5;
/// Too much stake to join validator set.
const ESTAKE_TOO_HIGH: u64 = 6;
/// Account is already a validator or pending validator.
const EALREADY_VALIDATOR: u64 = 7;
/// Account is not a validator.
const ENOT_VALIDATOR: u64 = 8;
/// Can't remove last validator.
const ELAST_VALIDATOR: u64 = 9;


/// Basic unit of stake delegation, it's stored in StakePool.
struct Delegation has store {
coins: Coin,
Expand Down Expand Up @@ -52,6 +73,9 @@ module AptosFramework::Stake {
}

/// Full ValidatorSet, stored in @CoreResource.
/// 1. join_validator_set adds to pending_active queue.
/// 2. leave_valdiator_set moves from active to pending_inactive queue.
/// 3. on_new_epoch processes two pending queues and refresh ValidatorInfo from the owner's address.
struct ValidatorSet has key {
consensus_scheme: u8,
// minimum stakes required to join validator set
Expand All @@ -70,7 +94,7 @@ module AptosFramework::Stake {
public fun delegate_stake(account: &signer, to: address, amount: u64, locked_until_secs: u64) acquires StakePool, ValidatorSet {
let coins = TestCoin::withdraw(account, amount);
let current_time = Timestamp::now_seconds();
assert!(current_time + MINIMUM_LOCK_PERIOD < locked_until_secs, 0);
assert!(current_time + MINIMUM_LOCK_PERIOD < locked_until_secs, Errors::invalid_argument(ELOCK_TIME_TOO_SHORT));
let stake_pool = borrow_global_mut<StakePool>(to);
let delegation = Delegation {
coins,
Expand Down Expand Up @@ -102,8 +126,7 @@ module AptosFramework::Stake {
// move to pending_inactive if it can be unlocked
Vector::push_back(&mut stake_pool.pending_inactive, d);
} else {
// not allowed to withdraw
abort 0
abort Errors::invalid_argument(EWITHDRAW_NOT_ALLOWED)
};
}

Expand Down Expand Up @@ -140,6 +163,7 @@ module AptosFramework::Stake {
/// Rotate the consensus key of the validator, it'll take effect in next epoch.
public fun rotate_consensus_key(account: &signer, consensus_pubkey: vector<u8>) acquires ValidatorConfig {
let addr = Signer::address_of(account);
assert!(exists<ValidatorConfig>(addr), Errors::not_published(EVALIDATOR_CONFIG));
let validator_info = borrow_global_mut<ValidatorConfig>(addr);
validator_info.consensus_pubkey = consensus_pubkey;
}
Expand All @@ -162,14 +186,14 @@ module AptosFramework::Stake {
let addr = Signer::address_of(account);
let stake_pool = borrow_global<StakePool>(addr);
let validator_set = borrow_global_mut<ValidatorSet>(@CoreResources);
assert!(stake_pool.current_stake >= validator_set.minimum_stake, 0);
assert!(stake_pool.current_stake <= validator_set.maximum_stake, 0);
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, 0);
assert!(!exist, Errors::invalid_argument(EALREADY_VALIDATOR));
let (exist, _) = find_validator(&validator_set.pending_inactive, addr);
assert!(!exist, 0);
assert!(!exist, Errors::invalid_argument(EALREADY_VALIDATOR));
let (exist, _) = find_validator(&validator_set.pending_active, addr);
assert!(!exist, 0);
assert!(!exist, Errors::invalid_argument(EALREADY_VALIDATOR));

Vector::push_back(&mut validator_set.pending_active, generate_validator_info(addr));
}
Expand All @@ -180,17 +204,18 @@ module AptosFramework::Stake {
let validator_set = borrow_global_mut<ValidatorSet>(@CoreResources);

let (exist, index) = find_validator(&validator_set.active_validators, addr);
assert!(exist, 0);
assert!(exist, Errors::invalid_argument(ENOT_VALIDATOR));

let validator_info = Vector::swap_remove(&mut validator_set.active_validators, index);
assert!(Vector::length(&validator_set.active_validators) > 0, 0);
assert!(Vector::length(&validator_set.active_validators) > 0, Errors::invalid_argument(ELAST_VALIDATOR));
Vector::push_back(&mut validator_set.pending_inactive, validator_info);
}

/// Triggers at epoch boundary.
/// 1. distribute rewards to stake pool of active and pending inactive validators
/// 2. purge pending queues
/// 3. update the validator info from owners' address
/// This function shouldn't abort.
public(friend) fun on_new_epoch() acquires StakePool, ValidatorConfig, ValidatorSet {
let validator_set = borrow_global_mut<ValidatorSet>(@CoreResources);
// distribute reward
Expand Down Expand Up @@ -225,6 +250,7 @@ module AptosFramework::Stake {
/// 1. distribute rewards to active/pending_inactive delegations
/// 2. process pending_active, pending_inactive correspondingly
/// 3. update the current stake
/// This function shouldn't abort.
fun update_stake_pool(addr: address) acquires StakePool {
let stake_pool = borrow_global_mut<StakePool>(addr);
distribute_reward( &mut stake_pool.active);
Expand Down Expand Up @@ -271,7 +297,7 @@ module AptosFramework::Stake {
};
i = i + 1;
};
abort 0
abort Errors::invalid_argument(EDELEGATION_NOT_FOUND)
}

fun find_validator(v: &vector<ValidatorInfo>, addr: address): (bool, u64) {
Expand Down

0 comments on commit 4b306c0

Please sign in to comment.