From 2eec0b83c768479dd9683752c1ec624a819c52c0 Mon Sep 17 00:00:00 2001 From: dongcool <95206510+dongcool@users.noreply.github.com> Date: Tue, 26 Mar 2024 11:08:15 +0800 Subject: [PATCH] make sync balance public --- README.md | 2 +- contracts/linear/src/epoch_actions.rs | 22 +++--------- contracts/linear/src/internal.rs | 4 --- contracts/linear/src/validator_pool.rs | 4 --- makefile | 5 +-- tests/__tests__/linear/helper.ts | 10 +++--- tests/__tests__/linear/sync-balance.ava.ts | 42 ++++++---------------- 7 files changed, 23 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index da66ebad..7b678cf7 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ We adopt unit tests and heavily used the [`workspace-js`](https://github.com/nea - Run LiNEAR simulation tests: - Run all: `make test-linear` - Run specific test file: `TEST_FILE={filename} make test-linear` - - Print contract logs: `LOGS=1 make test-linear` + - Print contract logs: `NO_LOGS=false make test-linear` ### Deploy diff --git a/contracts/linear/src/epoch_actions.rs b/contracts/linear/src/epoch_actions.rs index d05af780..48398bf9 100644 --- a/contracts/linear/src/epoch_actions.rs +++ b/contracts/linear/src/epoch_actions.rs @@ -8,7 +8,6 @@ use crate::utils::*; const MIN_AMOUNT_TO_PERFORM_STAKE: Balance = ONE_NEAR; const MAX_SYNC_BALANCE_DIFF: Balance = 100; -const MANAGER_SYNC_BALANCE_DIFF_THRESHOLD: Balance = 1_000_000; /// Actions that should be called by off-chain actors /// during each epoch. @@ -266,11 +265,7 @@ trait EpochActionCallbacks { fn validator_get_balance_callback(&mut self, validator_id: AccountId); - fn validator_get_account_callback( - &mut self, - validator_id: AccountId, - post_action: bool, - ) -> bool; + fn validator_get_account_callback(&mut self, validator_id: AccountId) -> bool; fn validator_withdraw_callback(&mut self, validator_id: AccountId, amount: U128); } @@ -307,7 +302,6 @@ impl LiquidStakingContract { .sync_account_balance(&mut self.validator_pool, true) .then(ext_self_action_cb::validator_get_account_callback( validator_id, - true, env::current_account_id(), NO_DEPOSIT, GAS_CB_VALIDATOR_SYNC_BALANCE, @@ -357,7 +351,6 @@ impl LiquidStakingContract { .sync_account_balance(&mut self.validator_pool, true) .then(ext_self_action_cb::validator_get_account_callback( validator_id, - true, env::current_account_id(), NO_DEPOSIT, GAS_CB_VALIDATOR_SYNC_BALANCE, @@ -421,7 +414,6 @@ impl LiquidStakingContract { pub fn validator_get_account_callback( &mut self, validator_id: AccountId, - post_action: bool, #[callback_result] result: Result, ) -> bool { let mut validator = self @@ -429,12 +421,6 @@ impl LiquidStakingContract { .get_validator(&validator_id) .unwrap_or_else(|| panic!("{}: {}", ERR_VALIDATOR_NOT_EXIST, &validator_id)); - let max_sync_balance_diff = if !post_action && self.signed_by_manager() { - MANAGER_SYNC_BALANCE_DIFF_THRESHOLD - } else { - MAX_SYNC_BALANCE_DIFF - }; - match result { Ok(account) => { // allow at most max_sync_balance_diff diff in total balance, staked balance and unstake balance @@ -442,15 +428,15 @@ impl LiquidStakingContract { if abs_diff_eq( new_total_balance, validator.total_balance(), - max_sync_balance_diff, + MAX_SYNC_BALANCE_DIFF, ) && abs_diff_eq( account.staked_balance.0, validator.staked_amount, - max_sync_balance_diff, + MAX_SYNC_BALANCE_DIFF, ) && abs_diff_eq( account.unstaked_balance.0, validator.unstaked_amount, - max_sync_balance_diff, + MAX_SYNC_BALANCE_DIFF, ) { Event::SyncValidatorBalanceSuccess { validator_id: &validator_id, diff --git a/contracts/linear/src/internal.rs b/contracts/linear/src/internal.rs index aa969e85..66081d0d 100644 --- a/contracts/linear/src/internal.rs +++ b/contracts/linear/src/internal.rs @@ -358,8 +358,4 @@ impl LiquidStakingContract { ERR_NOT_MANAGER ); } - - pub(crate) fn signed_by_manager(&self) -> bool { - self.managers.contains(&env::signer_account_id()) - } } diff --git a/contracts/linear/src/validator_pool.rs b/contracts/linear/src/validator_pool.rs index cf8407a2..fa3df223 100644 --- a/contracts/linear/src/validator_pool.rs +++ b/contracts/linear/src/validator_pool.rs @@ -587,10 +587,8 @@ impl LiquidStakingContract { } /// Sync contract staked and unstaked balance from validator - /// - Only allowed by manager pub fn sync_balance_from_validator(&mut self, validator_id: AccountId) { self.assert_running(); - self.assert_manager(); let min_gas = GAS_SYNC_BALANCE + GAS_EXT_GET_ACCOUNT + GAS_CB_VALIDATOR_SYNC_BALANCE; require!( @@ -607,7 +605,6 @@ impl LiquidStakingContract { .sync_account_balance(&mut self.validator_pool, false) .then(ext_self_action_cb::validator_get_account_callback( validator.account_id, - false, env::current_account_id(), NO_DEPOSIT, GAS_CB_VALIDATOR_SYNC_BALANCE, @@ -798,7 +795,6 @@ impl LiquidStakingContract { .sync_account_balance(&mut self.validator_pool, true) .then(ext_self_action_cb::validator_get_account_callback( validator_id, - true, env::current_account_id(), NO_DEPOSIT, GAS_CB_VALIDATOR_SYNC_BALANCE, diff --git a/makefile b/makefile index e2b3e4f8..f9b7b801 100644 --- a/makefile +++ b/makefile @@ -57,7 +57,8 @@ test-unit: cargo test --features "test" TEST_FILE ?= ** -LOGS ?= +NO_LOGS=true + test-contracts: linear_test mock-staking-pool mock-fungible-token mock-dex mock-lockup mock-whitelist @mkdir -p ./tests/compiled-contracts/ @cp ./res/linear_test.wasm ./tests/compiled-contracts/linear.wasm @@ -68,7 +69,7 @@ test-contracts: linear_test mock-staking-pool mock-fungible-token mock-dex mock- @cp ./res/mock_whitelist.wasm ./tests/compiled-contracts/mock_whitelist.wasm test-linear: test-contracts - cd tests && NEAR_PRINT_LOGS=$(LOGS) npx ava --timeout=2m __tests__/linear/$(TEST_FILE).ava.ts --verbose + cd tests && NEAR_WORKSPACES_NO_LOGS=$(NO_LOGS) npx ava --timeout=2m __tests__/linear/$(TEST_FILE).ava.ts --verbose test-mock-staking-pool: mock-staking-pool @mkdir -p ./tests/compiled-contracts/ diff --git a/tests/__tests__/linear/helper.ts b/tests/__tests__/linear/helper.ts index 7677557e..2723bab0 100644 --- a/tests/__tests__/linear/helper.ts +++ b/tests/__tests__/linear/helper.ts @@ -1,19 +1,19 @@ +import anyTest, { TestFn } from 'ava'; import { - Worker, - NEAR, - NearAccount, BN, Gas, + NEAR, + NearAccount, TransactionResult, + Worker, } from 'near-workspaces'; -import anyTest, { TestFn } from 'ava'; export const test = anyTest as TestFn; export const ONE_YOCTO = '1'; export const NUM_EPOCHS_TO_UNLOCK = 4; export const MAX_SYNC_BALANCE_DIFF = NEAR.from(100); -export const MANAGER_SYNC_BALANCE_DIFF_THRESHOLD = NEAR.from(1_000_000); +export const SYNC_BALANCE_DIFF_THRESHOLD = NEAR.from(100); interface RewardFee { numerator: number; diff --git a/tests/__tests__/linear/sync-balance.ava.ts b/tests/__tests__/linear/sync-balance.ava.ts index b07abd3c..86cfc235 100644 --- a/tests/__tests__/linear/sync-balance.ava.ts +++ b/tests/__tests__/linear/sync-balance.ava.ts @@ -1,16 +1,14 @@ +import { Gas, NEAR, NearAccount } from 'near-workspaces'; import { - MANAGER_SYNC_BALANCE_DIFF_THRESHOLD, MAX_SYNC_BALANCE_DIFF, - assertFailure, + SYNC_BALANCE_DIFF_THRESHOLD, createStakingPool, epochStake, epochUnstake, getValidator, initWorkspace, - setManager, - test, + test } from './helper'; -import { Gas, NEAR, NearAccount } from 'near-workspaces'; function assertValidatorAmountHelper( test: any, @@ -191,11 +189,9 @@ test( ); test( - 'sync balance by manager failure', + 'sync balance failure', async (t) => { const { root, contract, alice, bob, owner } = t.context; - // set bob as manager - await setManager(root, contract, owner, bob); const assertValidator = assertValidatorAmountHelper(t, contract, owner); const v1 = await createStakingPool(root, 'v1'); @@ -232,8 +228,8 @@ test( }, ); - // -- 1. total balance diff > MANAGER_SYNC_BALANCE_DIFF_THRESHOLD - const diff = MANAGER_SYNC_BALANCE_DIFF_THRESHOLD.addn(1); + // -- 1. total balance diff > SYNC_BALANCE_DIFF_THRESHOLD + const diff = SYNC_BALANCE_DIFF_THRESHOLD.addn(1); await owner.call(v1, 'set_balance_delta', { staked_delta: '0', unstaked_delta: diff.toString(10), @@ -243,22 +239,6 @@ test( await epochStake(owner, contract); } - // sync balance only allowed by manager - await assertFailure( - t, - alice.call( - contract, - 'sync_balance_from_validator', - { - validator_id: v1.accountId, - }, - { - gas: Gas.parse('200 Tgas'), - }, - ), - 'Only manager can perform this action', - ); - await bob.call( contract, 'sync_balance_from_validator', @@ -273,7 +253,7 @@ test( // v1 amount should not change await assertValidator(v1, '30000000000000000000000000', '0'); - // -- 2. amount balance diff > MANAGER_SYNC_BALANCE_DIFF_THRESHOLD + // -- 2. amount balance diff > SYNC_BALANCE_DIFF_THRESHOLD await owner.call(v2, 'set_balance_delta', { staked_delta: diff.toString(10), unstaked_delta: diff.toString(10), @@ -308,11 +288,9 @@ test( ); test( - 'sync balance by manager', + 'sync balance', async (t) => { const { root, contract, alice, bob, owner } = t.context; - // set bob as manager - await setManager(root, contract, owner, bob); const assertValidator = assertValidatorAmountHelper(t, contract, owner); const v1 = await createStakingPool(root, 'v1'); @@ -349,8 +327,8 @@ test( }, ); - // -- amount balance diff < MANAGER_SYNC_BALANCE_DIFF_THRESHOLD - const diff = MANAGER_SYNC_BALANCE_DIFF_THRESHOLD.subn(1); + // -- amount balance diff < SYNC_BALANCE_DIFF_THRESHOLD + const diff = SYNC_BALANCE_DIFF_THRESHOLD.subn(1); await owner.call(v1, 'set_balance_delta', { staked_delta: diff.toString(10), unstaked_delta: diff.toString(10),