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: make sync balance public #195

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 4 additions & 18 deletions contracts/linear/src/epoch_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -421,36 +414,29 @@ impl LiquidStakingContract {
pub fn validator_get_account_callback(
&mut self,
validator_id: AccountId,
post_action: bool,
#[callback_result] result: Result<HumanReadableAccount, PromiseError>,
) -> bool {
let mut validator = self
.validator_pool
.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
let new_total_balance = account.staked_balance.0 + account.unstaked_balance.0;
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,
Expand Down
4 changes: 0 additions & 4 deletions contracts/linear/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,4 @@ impl LiquidStakingContract {
ERR_NOT_MANAGER
);
}

pub(crate) fn signed_by_manager(&self) -> bool {
self.managers.contains(&env::signer_account_id())
}
}
4 changes: 0 additions & 4 deletions contracts/linear/src/validator_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/
Expand Down
10 changes: 5 additions & 5 deletions tests/__tests__/linear/helper.ts
Original file line number Diff line number Diff line change
@@ -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<Workspace>;

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;
Expand Down
42 changes: 10 additions & 32 deletions tests/__tests__/linear/sync-balance.ava.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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),
Expand All @@ -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',
Expand All @@ -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),
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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),
Expand Down
Loading