Skip to content

Commit eb4ddbd

Browse files
authored
Minimal bids in delegations, the skeleton (#577)
* Validator Info Api * Validator Info with minimum delegation rate. TODO: Make delegation rate work in OdraVM * Fix delegation * Rewards rework --------- Co-authored-by: Kuba <kuba@odra.dev>
1 parent 0cf3903 commit eb4ddbd

File tree

15 files changed

+201
-31
lines changed

15 files changed

+201
-31
lines changed

core/src/consts.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,7 @@ pub const ACCOUNTS_NUMBER: u8 = 20;
6565

6666
pub const DEFAULT_BALANCE: u64 = 10_000_000_000_000_000_000;
6767

68+
pub const DEFAULT_MINIMUM_DELEGATION_AMOUNT: u64 = 500_000_000_000u64;
69+
6870
/// Amount of random bytes returned by pseudorandom_bytes
6971
pub const RANDOM_BYTES_COUNT: usize = 32;

core/src/contract_context.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use casper_types::{CLValue, PublicKey};
2-
31
use crate::call_def::CallDef;
42
use crate::casper_types::bytesrepr::Bytes;
53
use crate::casper_types::U512;
64
use crate::consts::RANDOM_BYTES_COUNT;
75
use crate::prelude::*;
6+
use crate::validator::ValidatorInfo;
7+
use casper_types::{CLValue, PublicKey};
88

99
/// Trait representing the context of a smart contract.
1010
#[cfg_attr(test, allow(unreachable_code))]
@@ -202,6 +202,15 @@ pub trait ContractContext {
202202
/// The amount of tokens delegated to the validator as a `U512` value.
203203
fn delegated_amount(&self, validator: PublicKey) -> U512;
204204

205+
/// Returns information about the validator
206+
///
207+
/// # Arguments
208+
/// - validator - The validator to query
209+
///
210+
/// # Returns
211+
/// Option<ValidatorBid>
212+
fn get_validator_info(&self, validator: PublicKey) -> Option<ValidatorInfo>;
213+
205214
/// Returns a vector of pseudorandom bytes of the specified size.
206215
/// There is no guarantee that the returned bytes are in any way cryptographically secure.
207216
fn pseudorandom_bytes(&self) -> [u8; RANDOM_BYTES_COUNT];

core/src/contract_env.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::casper_types::bytesrepr::{deserialize_from_slice, Bytes, FromBytes, T
44
use crate::casper_types::crypto::PublicKey;
55
use crate::casper_types::{CLTyped, CLValue, BLAKE2B_DIGEST_LENGTH, U512};
66
use crate::module::Revertible;
7+
use crate::validator::ValidatorInfo;
78
pub use crate::ContractContext;
89
use crate::VmError::{Serialization, TypeMismatch};
910
use crate::{consts, prelude::*, utils};
@@ -315,6 +316,17 @@ impl ContractEnv {
315316
self.backend.borrow().delegated_amount(validator)
316317
}
317318

319+
/// Returns information about the validator
320+
///
321+
/// # Arguments
322+
/// - validator - The validator to query
323+
///
324+
/// # Returns
325+
/// Option<ValidatorBid>
326+
pub fn get_validator_info(&self, validator: PublicKey) -> Option<ValidatorInfo> {
327+
self.backend.borrow().get_validator_info(validator)
328+
}
329+
318330
/// Returns a vector of pseudorandom bytes of the specified size.
319331
/// There is no guarantee that the returned bytes are in any way cryptographically secure.
320332
pub fn pseudorandom_bytes(&self, size: usize) -> Vec<u8> {

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ mod sequence;
3434
pub mod uints;
3535
mod unwrap_or_revert;
3636
pub mod utils;
37+
pub mod validator;
3738
mod var;
3839

3940
pub use call_def::CallDef;

core/src/validator.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! Validator related types.
2+
use casper_types::U512;
3+
4+
/// ValidatorInfo contains information about a validator.
5+
#[derive(Clone, Debug)]
6+
pub struct ValidatorInfo {
7+
/// The amount of tokens staked.
8+
pub staked_amount: U512,
9+
/// The minimum amount of tokens that must be delegated to the validator.
10+
pub minimum_delegation_amount: u64
11+
}
12+
13+
impl ValidatorInfo {
14+
/// Creates a new ValidatorInfo with the given staked amount and minimum delegation amount.
15+
pub fn new(staked_amount: U512, minimum_delegation_amount: u64) -> Self {
16+
ValidatorInfo {
17+
staked_amount,
18+
minimum_delegation_amount
19+
}
20+
}
21+
22+
/// Sets the ValidatorInfo with the staked amount set to the given value.
23+
pub fn set_staked_amount(&mut self, staked_amount: U512) {
24+
self.staked_amount = staked_amount;
25+
}
26+
27+
/// Sets the ValidatorInfo with the minimum delegation amount set to the given value.
28+
pub fn set_minimum_delegation_amount(&mut self, minimum_delegation_amount: u64) {
29+
self.minimum_delegation_amount = minimum_delegation_amount;
30+
}
31+
}

examples/src/features/validators.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ impl ValidatorsContract {
4848
pub fn current_casper_balance(&self) -> U512 {
4949
self.env().self_balance()
5050
}
51+
52+
/// Get minimum delegation amount
53+
pub fn get_minimum_delegation_amount(&self) -> u64 {
54+
self.env()
55+
.get_validator_info(self.validator.get().unwrap())
56+
.unwrap()
57+
.minimum_delegation_amount
58+
}
5159
}
5260

5361
/// Error enum for the ValidatorsContract
@@ -233,4 +241,24 @@ mod tests {
233241
assert_eq!(staking.currently_delegated_amount(), U512::zero());
234242
assert_eq!(test_env.balance_of(&staking.address()), staking_amount);
235243
}
244+
245+
#[test]
246+
fn test_validator_info() {
247+
use crate::features::validators::{ValidatorsContract, ValidatorsContractInitArgs};
248+
use odra::host::Deployer;
249+
let test_env = odra_test::env();
250+
let validator = test_env.get_validator(0);
251+
252+
test_env.set_caller(test_env.get_account(0));
253+
let staking = ValidatorsContract::deploy(
254+
&test_env,
255+
ValidatorsContractInitArgs {
256+
validator: validator.clone()
257+
}
258+
);
259+
260+
let minimum_delegation_amount = staking.get_minimum_delegation_amount();
261+
262+
assert_eq!(minimum_delegation_amount, 500_000_000_000u64);
263+
}
236264
}

odra-casper/livenet-env/src/livenet_contract_env.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use odra_core::callstack::{Callstack, CallstackElement};
66
use odra_core::casper_types::bytesrepr::Bytes;
77
use odra_core::casper_types::{CLValue, PublicKey, U512};
88
use odra_core::prelude::*;
9+
use odra_core::validator::ValidatorInfo;
910
use odra_core::{CallDef, ContractContext, ContractRegister};
1011
use std::io::Write;
1112
use std::sync::RwLock;
@@ -187,6 +188,10 @@ impl ContractContext for LivenetContractEnv {
187188
.block_on(async { client.delegated_amount(address, _validator).await })
188189
}
189190

191+
fn get_validator_info(&self, _validator: PublicKey) -> Option<ValidatorInfo> {
192+
todo!()
193+
}
194+
190195
fn pseudorandom_bytes(&self) -> [u8; 32] {
191196
panic!(
192197
"pseudorandom_bytes is not supported for LivenetContractEnv, it should be run\

odra-casper/rpc-client/src/casper_client.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use casper_client::{
2323
};
2424
use casper_types::bytesrepr::{deserialize_from_slice, Bytes, ToBytes};
2525
use casper_types::execution::ExecutionResultV1::{Failure, Success};
26-
use casper_types::system::auction::BidAddr;
26+
use casper_types::system::auction::{BidAddr, ValidatorBid};
2727
use casper_types::StoredValue::CLValue;
2828
use casper_types::{
2929
execution::ExecutionResult, runtime_args, sign, CLTyped, Digest, EntityAddr, Key, PricingMode,
@@ -189,6 +189,10 @@ impl CasperClient {
189189
}
190190
}
191191

192+
pub async fn get_validator_info(&self, _validator: PublicKey) -> Option<ValidatorBid> {
193+
todo!("Implement get_validator_info")
194+
}
195+
192196
pub async fn auction_delay(&self) -> u64 {
193197
let chainspec = self.chainspec().await;
194198

odra-casper/test-vm/src/vm/casper_vm.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl CasperVm {
136136

137137
let step_request = step_request_builder.build();
138138
self.context.step(step_request);
139-
self.context.advance_eras_by_default_auction_delay();
139+
self.context.advance_era();
140140
self.advance_block_time(time_between_auctions);
141141
self.context
142142
.distribute(None, DEFAULT_PROTOCOL_VERSION, rewards, self.block_time);
@@ -710,7 +710,6 @@ impl CasperVm {
710710
builder.exec(bid_request).commit().expect_success();
711711
}
712712

713-
builder.advance_eras_by(20);
714713
Self {
715714
active_account: accounts[0],
716715
context: builder,

odra-casper/wasm-env/src/host_functions.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use odra_core::casper_types::account::AccountHash;
2929
use odra_core::casper_types::bytesrepr::deserialize;
3030
use odra_core::casper_types::contract_messages::{MessagePayload, MessageTopicOperation};
3131
use odra_core::casper_types::contracts::{ContractHash, ContractPackageHash, ContractVersion};
32-
use odra_core::casper_types::system::auction::{self, BidAddr, BidKind};
32+
use odra_core::casper_types::system::auction::{self, BidAddr, BidKind, ValidatorBid};
3333
use odra_core::casper_types::system::{Caller, CallerInfo};
3434
use odra_core::casper_types::StoredValue;
3535
use odra_core::casper_types::{
@@ -41,6 +41,7 @@ use odra_core::casper_types::{
4141
use odra_core::consts::{
4242
ALLOW_KEY_OVERRIDE_ARG, IS_UPGRADABLE_ARG, PACKAGE_HASH_KEY_NAME_ARG, RANDOM_BYTES_COUNT
4343
};
44+
use odra_core::validator::ValidatorInfo;
4445
use odra_core::{
4546
args::EntrypointArgument,
4647
casper_event_standard::{self, Schema, Schemas}
@@ -832,3 +833,23 @@ pub fn delegated_amount(public_key: PublicKey) -> U512 {
832833
pub fn pseudorandom_bytes() -> [u8; RANDOM_BYTES_COUNT] {
833834
runtime::random_bytes()
834835
}
836+
837+
/// Retrieves ValidatorBid from the storage
838+
pub fn get_validator_info(validator: PublicKey) -> Option<ValidatorInfo> {
839+
let account_hash = validator.to_account_hash();
840+
let key = Key::BidAddr(BidAddr::Validator(account_hash));
841+
842+
read_from_key(key)
843+
.ok()
844+
.and_then(|stored_value| stored_value)
845+
.and_then(|bid_kind| match bid_kind {
846+
BidKind::Validator(bid) => Some(*bid),
847+
_ => None
848+
})
849+
.map(|validator_bid| {
850+
ValidatorInfo::new(
851+
validator_bid.staked_amount(),
852+
validator_bid.minimum_delegation_amount()
853+
)
854+
})
855+
}

0 commit comments

Comments
 (0)