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

PoS crate refactors #1928

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
196bfec
SQUASHED redelegation
brentstone Jun 23, 2023
90f32b1
pos/SM: fix update_redelegated_bonds in v1
brentstone Sep 15, 2023
4d5a959
fixup! SQUASHED redelegation
tzemanovic Sep 18, 2023
59a6fd8
add redelegate to cli commands
tzemanovic Sep 18, 2023
63e47d5
test/e2e: add redelegation to `pos_bonds` ledger test
tzemanovic Sep 18, 2023
9fef2b6
fix redelegation tx code path
tzemanovic Sep 18, 2023
53b8e12
pos/SM: fix bug in updating map of validator slashed amounts
brentstone Sep 19, 2023
0665d40
test/ethereum_bridge: fix PS initializationin the affected test
tzemanovic Sep 19, 2023
200a27d
fix cli dest validator arg
tzemanovic Sep 19, 2023
c5eb4cd
test/e2e: simplify to only require single node
tzemanovic Sep 19, 2023
6fd0a48
tx_redelegate: refactor
tzemanovic Sep 19, 2023
e00abe3
prohibit redelegation with src == dest validator
tzemanovic Sep 19, 2023
3398066
update wasm tests
brentstone Sep 19, 2023
a9e3f68
fix buggy storage removal of empty unbonds
brentstone Sep 20, 2023
1099ae2
error handling in client redelegation submission
brentstone Sep 20, 2023
d4cff58
use token zero fns everywhere
brentstone Sep 20, 2023
1e57754
fixup! error handling in client redelegation submission
brentstone Sep 20, 2023
cb1abe1
update `bond` client errors and lib fn
brentstone Sep 20, 2023
80959c6
update `unbond` client
brentstone Sep 20, 2023
43734bd
WIP: polishing `unbond_tokens`
brentstone Sep 22, 2023
7475c44
benches: add redelegation tx
tzemanovic Sep 22, 2023
24639f4
wasm/tx_redelegate: update gas cost
tzemanovic Sep 22, 2023
fdfb1bc
fixup! benches: add redelegation tx
tzemanovic Sep 22, 2023
515ceba
PoS: rename mod storage to storage_key
tzemanovic Sep 22, 2023
b93df45
PoS: move storage lazy collection handles into sub-mod
tzemanovic Sep 22, 2023
16201a2
gov: use `fn is_delegator` avoid using PoS storage directly
tzemanovic Sep 22, 2023
b6fb0c9
eth: add higher-level functions to read validator ETH cold and hot key
tzemanovic Sep 22, 2023
4847921
PoS: remove `StorageWrite` trait bound on `fn is_delegator`
tzemanovic Sep 22, 2023
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
47 changes: 13 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ num-traits = "0.2.14"
once_cell = "1.8.0"
orion = "0.16.0"
paste = "1.0.9"
pretty_assertions = "0.7.2"
pretty_assertions = "1.4.0"
primitive-types = "0.12.1"
proptest = "1.2.0"
proptest-state-machine = "0.1.0"
Expand Down Expand Up @@ -147,7 +147,8 @@ tracing-log = "0.1.2"
tracing-subscriber = {version = "0.3.7", default-features = false, features = ["env-filter", "fmt"]}
wasmparser = "0.107.0"
winapi = "0.3.9"
zeroize = {version = "1.5.5", features = ["zeroize_derive"]}
yansi = "0.5.1"
zeroize = { version = "1.5.5", features = ["zeroize_derive"] }

[patch.crates-io]
# TODO temp patch for <https://github.com/near/borsh-rs/issues/82>, <https://github.com/near/borsh-rs/issues/84> and more tba.
Expand Down
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ NAMADA_E2E_DEBUG ?= true
RUST_BACKTRACE ?= 1
NAMADA_MASP_TEST_SEED ?= 0
PROPTEST_CASES ?= 100
# Disable shrinking in `make test-pos-sm` for CI runs. If the test fail in CI,
# we only want to get the seed.
PROPTEST_MAX_SHRINK_ITERS ?= 0

cargo := $(env) cargo
rustup := $(env) rustup
Expand Down Expand Up @@ -206,11 +209,13 @@ test-debug:
--nocapture \
-Z unstable-options --report-time

# Run PoS state machine tests
# Run PoS state machine tests with shrinking disabled by default (can be
# overriden with `PROPTEST_MAX_SHRINK_ITERS`)
test-pos-sm:
cd proof_of_stake && \
RUST_BACKTRACE=1 \
RUST_BACKTRACE=1 \
PROPTEST_CASES=$(PROPTEST_CASES) \
PROPTEST_MAX_SHRINK_ITERS=$(PROPTEST_MAX_SHRINK_ITERS) \
RUSTFLAGS='-C debuginfo=2 -C debug-assertions=true -C overflow-checks=true' \
cargo test pos_state_machine_test --release

Expand Down
88 changes: 87 additions & 1 deletion apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ pub mod cmds {
.subcommand(Bond::def().display_order(2))
.subcommand(Unbond::def().display_order(2))
.subcommand(Withdraw::def().display_order(2))
.subcommand(Redelegate::def().display_order(2))
.subcommand(TxCommissionRateChange::def().display_order(2))
// Ethereum bridge transactions
.subcommand(AddToEthBridgePool::def().display_order(3))
Expand Down Expand Up @@ -283,6 +284,7 @@ pub mod cmds {
let bond = Self::parse_with_ctx(matches, Bond);
let unbond = Self::parse_with_ctx(matches, Unbond);
let withdraw = Self::parse_with_ctx(matches, Withdraw);
let redelegate = Self::parse_with_ctx(matches, Redelegate);
let query_epoch = Self::parse_with_ctx(matches, QueryEpoch);
let query_account = Self::parse_with_ctx(matches, QueryAccount);
let query_transfers = Self::parse_with_ctx(matches, QueryTransfers);
Expand Down Expand Up @@ -326,6 +328,7 @@ pub mod cmds {
.or(bond)
.or(unbond)
.or(withdraw)
.or(redelegate)
.or(add_to_eth_bridge_pool)
.or(tx_update_steward_commission)
.or(tx_resign_steward)
Expand Down Expand Up @@ -400,6 +403,7 @@ pub mod cmds {
Bond(Bond),
Unbond(Unbond),
Withdraw(Withdraw),
Redelegate(Redelegate),
AddToEthBridgePool(AddToEthBridgePool),
TxUpdateStewardCommission(TxUpdateStewardCommission),
TxResignSteward(TxResignSteward),
Expand Down Expand Up @@ -1423,6 +1427,27 @@ pub mod cmds {
}
}

#[derive(Clone, Debug)]
pub struct Redelegate(pub args::Redelegate<args::CliTypes>);

impl SubCmd for Redelegate {
const CMD: &'static str = "redelegate";

fn parse(matches: &ArgMatches) -> Option<Self> {
matches
.subcommand_matches(Self::CMD)
.map(|matches| Redelegate(args::Redelegate::parse(matches)))
}

fn def() -> App {
App::new(Self::CMD)
.about(
"Redelegate bonded tokens from one validator to another.",
)
.add_args::<args::Redelegate<args::CliTypes>>()
}
}

#[derive(Clone, Debug)]
pub struct QueryEpoch(pub args::Query<args::CliTypes>);

Expand Down Expand Up @@ -2494,6 +2519,7 @@ pub mod args {
pub const TX_TRANSFER_WASM: &str = "tx_transfer.wasm";
pub const TX_UNBOND_WASM: &str = "tx_unbond.wasm";
pub const TX_UNJAIL_VALIDATOR_WASM: &str = "tx_unjail_validator.wasm";
pub const TX_REDELEGATE_WASM: &str = "tx_redelegate.wasm";
pub const TX_UPDATE_VP_WASM: &str = "tx_update_vp.wasm";
pub const TX_UPDATE_STEWARD_COMMISSION: &str =
"tx_update_steward_commission.wasm";
Expand Down Expand Up @@ -2524,7 +2550,7 @@ pub mod args {
arg_default(
"pool-gas-amount",
DefaultFn(|| token::DenominatedAmount {
amount: token::Amount::default(),
amount: token::Amount::zero(),
denom: NATIVE_MAX_DECIMAL_PLACES.into(),
}),
);
Expand Down Expand Up @@ -2557,6 +2583,8 @@ pub mod args {
pub const DATA_PATH: Arg<PathBuf> = arg("data-path");
pub const DECRYPT: ArgFlag = flag("decrypt");
pub const DISPOSABLE_SIGNING_KEY: ArgFlag = flag("disposable-gas-payer");
pub const DESTINATION_VALIDATOR: Arg<WalletAddress> =
arg("destination-validator");
pub const DONT_ARCHIVE: ArgFlag = flag("dont-archive");
pub const DONT_PREFETCH_WASM: ArgFlag = flag("dont-prefetch-wasm");
pub const DRY_RUN_TX: ArgFlag = flag("dry-run");
Expand Down Expand Up @@ -2653,6 +2681,7 @@ pub mod args {
pub const SOURCE: Arg<WalletAddress> = arg("source");
pub const SOURCE_OPT: ArgOpt<WalletAddress> = SOURCE.opt();
pub const STEWARD: Arg<WalletAddress> = arg("steward");
pub const SOURCE_VALIDATOR: Arg<WalletAddress> = arg("source-validator");
pub const STORAGE_KEY: Arg<storage::Key> = arg("storage-key");
pub const SUSPEND_ACTION: ArgFlag = flag("suspend");
pub const TIMEOUT_HEIGHT: ArgOpt<u64> = arg_opt("timeout-height");
Expand Down Expand Up @@ -3928,6 +3957,63 @@ pub mod args {
}
}

impl CliToSdk<Redelegate<SdkTypes>> for Redelegate<CliTypes> {
fn to_sdk(self, ctx: &mut Context) -> Redelegate<SdkTypes> {
Redelegate::<SdkTypes> {
tx: self.tx.to_sdk(ctx),
src_validator: ctx.get(&self.src_validator),
dest_validator: ctx.get(&self.dest_validator),
owner: ctx.get(&self.owner),
amount: self.amount,
tx_code_path: self.tx_code_path.to_path_buf(),
}
}
}

impl Args for Redelegate<CliTypes> {
fn parse(matches: &ArgMatches) -> Self {
let tx = Tx::parse(matches);
let src_validator = SOURCE_VALIDATOR.parse(matches);
let dest_validator = DESTINATION_VALIDATOR.parse(matches);
let owner = OWNER.parse(matches);
let amount = AMOUNT.parse(matches);
let amount = amount
.canonical()
.increase_precision(NATIVE_MAX_DECIMAL_PLACES.into())
.unwrap_or_else(|e| {
println!("Could not parse bond amount: {:?}", e);
safe_exit(1);
})
.amount;
let tx_code_path = PathBuf::from(TX_REDELEGATE_WASM);
Self {
tx,
src_validator,
dest_validator,
owner,
amount,
tx_code_path,
}
}

fn def(app: App) -> App {
app.add_args::<Tx<CliTypes>>()
.arg(
SOURCE_VALIDATOR
.def()
.help("Source validator address for the redelegation."),
)
.arg(DESTINATION_VALIDATOR.def().help(
"Destination validator address for the redelegation.",
))
.arg(OWNER.def().help(
"Delegator (owner) address of the bonds that are being \
redelegated.",
))
.arg(AMOUNT.def().help("Amount of tokens to redelegate."))
}
}

impl CliToSdk<InitProposal<SdkTypes>> for InitProposal<CliTypes> {
fn to_sdk(self, ctx: &mut Context) -> InitProposal<SdkTypes> {
InitProposal::<SdkTypes> {
Expand Down
13 changes: 13 additions & 0 deletions apps/src/lib/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ impl<IO> CliApi<IO> {
let args = args.to_sdk(&mut ctx);
tx::submit_withdraw(&client, ctx, args).await?;
}
Sub::Redelegate(Redelegate(mut args)) => {
let client = client.unwrap_or_else(|| {
C::from_tendermint_address(
&mut args.tx.ledger_address,
)
});
client
.wait_until_node_is_synced()
.await
.proceed_or_else(error)?;
let args = args.to_sdk(&mut ctx);
tx::submit_redelegate(&client, ctx, args).await?;
}
Sub::TxCommissionRateChange(TxCommissionRateChange(
mut args,
)) => {
Expand Down
22 changes: 11 additions & 11 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ use namada::ledger::masp::{
Conversions, MaspAmount, MaspChange, ShieldedContext, ShieldedUtils,
};
use namada::ledger::parameters::{storage as param_storage, EpochDuration};
use namada::ledger::pos::{CommissionPair, PosParams, Slash};
use namada::ledger::pos::types::{CommissionPair, Slash};
use namada::ledger::pos::PosParams;
use namada::ledger::queries::RPC;
use namada::ledger::rpc::{
self, enriched_bonds_and_unbonds, format_denominated_amount, query_epoch,
Expand Down Expand Up @@ -1277,7 +1278,7 @@ pub async fn query_and_print_unbonds<
let unbonds = query_unbond_with_slashing(client, source, validator).await;
let current_epoch = query_epoch(client).await.unwrap();

let mut total_withdrawable = token::Amount::default();
let mut total_withdrawable = token::Amount::zero();
let mut not_yet_withdrawable = HashMap::<Epoch, token::Amount>::new();
for ((_start_epoch, withdraw_epoch), amount) in unbonds.into_iter() {
if withdraw_epoch <= current_epoch {
Expand All @@ -1288,7 +1289,7 @@ pub async fn query_and_print_unbonds<
*withdrawable_amount += amount;
}
}
if total_withdrawable != token::Amount::default() {
if !total_withdrawable.is_zero() {
println!(
"Total withdrawable now: {}.",
total_withdrawable.to_string_native()
Expand Down Expand Up @@ -1358,7 +1359,7 @@ pub async fn query_bonds<C: namada::ledger::queries::Client + Sync>(
bond.amount.to_string_native()
)?;
}
if details.bonds_total != token::Amount::zero() {
if !details.bonds_total.is_zero() {
writeln!(
w,
"Active (slashed) bonds total: {}",
Expand Down Expand Up @@ -2082,13 +2083,12 @@ pub async fn get_bond_amount_at<C: namada::ledger::queries::Client + Sync>(
validator: &Address,
epoch: Epoch,
) -> Option<token::Amount> {
let (_total, total_active) =
unwrap_client_response::<C, (token::Amount, token::Amount)>(
RPC.vp()
.pos()
.bond_with_slashing(client, delegator, validator, &Some(epoch))
.await,
);
let total_active = unwrap_client_response::<C, token::Amount>(
RPC.vp()
.pos()
.bond_with_slashing(client, delegator, validator, &Some(epoch))
.await,
);
Some(total_active)
}

Expand Down
Loading
Loading