Skip to content

Commit

Permalink
Removing builder structs from public API of casper-types
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Zajkowski committed Dec 2, 2024
1 parent 76c2ff3 commit a5fcc71
Show file tree
Hide file tree
Showing 40 changed files with 2,089 additions and 2,635 deletions.
8 changes: 4 additions & 4 deletions execution_engine_testing/test_support/src/chainspec_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use casper_execution_engine::engine_state::{EngineConfig, EngineConfigBuilder};
use casper_storage::data_access_layer::GenesisRequest;
use casper_types::{
system::auction::VESTING_SCHEDULE_LENGTH_MILLIS, CoreConfig, FeeHandling, GenesisAccount,
GenesisConfig, GenesisConfigBuilder, MintCosts, PricingHandling, ProtocolVersion,
RefundHandling, StorageCosts, SystemConfig, TimeDiff, WasmConfig,
GenesisConfig, MintCosts, PricingHandling, ProtocolVersion, RefundHandling, StorageCosts,
SystemConfig, TimeDiff, WasmConfig,
};

use crate::{
DEFAULT_ACCOUNTS, DEFAULT_CHAINSPEC_REGISTRY, DEFAULT_GENESIS_CONFIG_HASH,
DEFAULT_GENESIS_TIMESTAMP_MILLIS, DEFAULT_MAX_QUERY_DEPTH,
GenesisConfigBuilder, DEFAULT_ACCOUNTS, DEFAULT_CHAINSPEC_REGISTRY,
DEFAULT_GENESIS_CONFIG_HASH, DEFAULT_GENESIS_TIMESTAMP_MILLIS, DEFAULT_MAX_QUERY_DEPTH,
};

/// The name of the chainspec file on disk.
Expand Down
131 changes: 131 additions & 0 deletions execution_engine_testing/test_support/src/genesis_config_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//! A builder for an [`GenesisConfig`].
use casper_execution_engine::engine_state::engine_config::DEFAULT_ENABLE_ENTITY;
use casper_types::{
GenesisAccount, GenesisConfig, HoldBalanceHandling, StorageCosts, SystemConfig, WasmConfig,
};
use num_rational::Ratio;

use crate::{
DEFAULT_AUCTION_DELAY, DEFAULT_GAS_HOLD_BALANCE_HANDLING, DEFAULT_GAS_HOLD_INTERVAL_MILLIS,
DEFAULT_GENESIS_TIMESTAMP_MILLIS, DEFAULT_LOCKED_FUNDS_PERIOD_MILLIS,
DEFAULT_ROUND_SEIGNIORAGE_RATE, DEFAULT_UNBONDING_DELAY, DEFAULT_VALIDATOR_SLOTS,
};

/// A builder for an [`GenesisConfig`].
///
/// Any field that isn't specified will be defaulted. See [the module docs](index.html) for the set
/// of default values.
#[derive(Default, Debug)]
pub struct GenesisConfigBuilder {
accounts: Option<Vec<GenesisAccount>>,
wasm_config: Option<WasmConfig>,
system_config: Option<SystemConfig>,
validator_slots: Option<u32>,
auction_delay: Option<u64>,
locked_funds_period_millis: Option<u64>,
round_seigniorage_rate: Option<Ratio<u64>>,
unbonding_delay: Option<u64>,
genesis_timestamp_millis: Option<u64>,
gas_hold_balance_handling: Option<HoldBalanceHandling>,
gas_hold_interval_millis: Option<u64>,
enable_addressable_entity: Option<bool>,
storage_costs: Option<StorageCosts>,
}

impl GenesisConfigBuilder {
/// Creates a new `ExecConfig` builder.
pub fn new() -> Self {
GenesisConfigBuilder::default()
}

/// Sets the genesis accounts.
pub fn with_accounts(mut self, accounts: Vec<GenesisAccount>) -> Self {
self.accounts = Some(accounts);
self
}

/// Sets the Wasm config options.
pub fn with_wasm_config(mut self, wasm_config: WasmConfig) -> Self {
self.wasm_config = Some(wasm_config);
self
}

/// Sets the system config options.
pub fn with_system_config(mut self, system_config: SystemConfig) -> Self {
self.system_config = Some(system_config);
self
}

/// Sets the validator slots config option.
pub fn with_validator_slots(mut self, validator_slots: u32) -> Self {
self.validator_slots = Some(validator_slots);
self
}

/// Sets the auction delay config option.
pub fn with_auction_delay(mut self, auction_delay: u64) -> Self {
self.auction_delay = Some(auction_delay);
self
}

/// Sets the locked funds period config option.
pub fn with_locked_funds_period_millis(mut self, locked_funds_period_millis: u64) -> Self {
self.locked_funds_period_millis = Some(locked_funds_period_millis);
self
}

/// Sets the round seigniorage rate config option.
pub fn with_round_seigniorage_rate(mut self, round_seigniorage_rate: Ratio<u64>) -> Self {
self.round_seigniorage_rate = Some(round_seigniorage_rate);
self
}

/// Sets the unbonding delay config option.
pub fn with_unbonding_delay(mut self, unbonding_delay: u64) -> Self {
self.unbonding_delay = Some(unbonding_delay);
self
}

/// Sets the genesis timestamp config option.
pub fn with_genesis_timestamp_millis(mut self, genesis_timestamp_millis: u64) -> Self {
self.genesis_timestamp_millis = Some(genesis_timestamp_millis);
self
}

/// Sets the enable addressable entity flag.
pub fn with_enable_addressable_entity(mut self, enable_addressable_entity: bool) -> Self {
self.enable_addressable_entity = Some(enable_addressable_entity);
self
}

/// Sets the storage_costs handling.
pub fn with_storage_costs(mut self, storage_costs: StorageCosts) -> Self {
self.storage_costs = Some(storage_costs);
self
}

/// Builds a new [`GenesisConfig`] object.
pub fn build(self) -> GenesisConfig {
GenesisConfig::new(
self.accounts.unwrap_or_default(),
self.wasm_config.unwrap_or_default(),
self.system_config.unwrap_or_default(),
self.validator_slots.unwrap_or(DEFAULT_VALIDATOR_SLOTS),
self.auction_delay.unwrap_or(DEFAULT_AUCTION_DELAY),
self.locked_funds_period_millis
.unwrap_or(DEFAULT_LOCKED_FUNDS_PERIOD_MILLIS),
self.round_seigniorage_rate
.unwrap_or(DEFAULT_ROUND_SEIGNIORAGE_RATE),
self.unbonding_delay.unwrap_or(DEFAULT_UNBONDING_DELAY),
self.genesis_timestamp_millis
.unwrap_or(DEFAULT_GENESIS_TIMESTAMP_MILLIS),
self.gas_hold_balance_handling
.unwrap_or(DEFAULT_GAS_HOLD_BALANCE_HANDLING),
self.gas_hold_interval_millis
.unwrap_or(DEFAULT_GAS_HOLD_INTERVAL_MILLIS),
self.enable_addressable_entity
.unwrap_or(DEFAULT_ENABLE_ENTITY),
self.storage_costs.unwrap_or_default(),
)
}
}
10 changes: 8 additions & 2 deletions execution_engine_testing/test_support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ mod chainspec_config;
pub mod deploy_item;
mod deploy_item_builder;
mod execute_request_builder;
pub mod genesis_config_builder;
mod step_request_builder;
mod transfer_request_builder;
mod upgrade_request_builder;
pub mod utils;
mod wasm_test_builder;

pub(crate) use genesis_config_builder::GenesisConfigBuilder;
use num_rational::Ratio;
use once_cell::sync::Lazy;

use casper_storage::data_access_layer::GenesisRequest;
use casper_types::{
account::AccountHash, testing::TestRng, ChainspecRegistry, Digest, GenesisAccount,
GenesisConfig, GenesisConfigBuilder, Motes, ProtocolVersion, PublicKey, SecretKey,
StorageCosts, SystemConfig, WasmConfig, WasmV1Config, U512,
GenesisConfig, HoldBalanceHandling, Motes, ProtocolVersion, PublicKey, SecretKey, StorageCosts,
SystemConfig, WasmConfig, WasmV1Config, U512,
};

pub use chainspec_config::{ChainspecConfig, CHAINSPEC_SYMLINK};
Expand Down Expand Up @@ -69,6 +71,10 @@ pub const DEFAULT_GAS_PRICE: u8 = 1;
pub const ARG_AMOUNT: &str = "amount";
/// Timestamp increment in milliseconds.
pub const TIMESTAMP_MILLIS_INCREMENT: u64 = 30_000; // 30 seconds
/// Default gas hold balance handling.
pub const DEFAULT_GAS_HOLD_BALANCE_HANDLING: HoldBalanceHandling = HoldBalanceHandling::Accrued;
/// Default gas hold interval in milliseconds.
pub const DEFAULT_GAS_HOLD_INTERVAL_MILLIS: u64 = 24 * 60 * 60 * 60;

/// Default value for maximum associated keys configuration option.
pub const DEFAULT_MAX_ASSOCIATED_KEYS: u32 = 100;
Expand Down
9 changes: 5 additions & 4 deletions execution_engine_testing/test_support/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ use once_cell::sync::Lazy;

use casper_execution_engine::engine_state::{Error, WasmV1Result};
use casper_storage::data_access_layer::GenesisRequest;
use casper_types::{bytesrepr::Bytes, GenesisAccount, GenesisConfig, GenesisConfigBuilder};
use casper_types::{bytesrepr::Bytes, GenesisAccount, GenesisConfig};

use super::{DEFAULT_ROUND_SEIGNIORAGE_RATE, DEFAULT_SYSTEM_CONFIG, DEFAULT_UNBONDING_DELAY};
use crate::{
DEFAULT_AUCTION_DELAY, DEFAULT_CHAINSPEC_REGISTRY, DEFAULT_GENESIS_CONFIG_HASH,
DEFAULT_GENESIS_TIMESTAMP_MILLIS, DEFAULT_LOCKED_FUNDS_PERIOD_MILLIS, DEFAULT_PROTOCOL_VERSION,
DEFAULT_STORAGE_COSTS, DEFAULT_VALIDATOR_SLOTS, DEFAULT_WASM_CONFIG,
GenesisConfigBuilder, DEFAULT_AUCTION_DELAY, DEFAULT_CHAINSPEC_REGISTRY,
DEFAULT_GENESIS_CONFIG_HASH, DEFAULT_GENESIS_TIMESTAMP_MILLIS,
DEFAULT_LOCKED_FUNDS_PERIOD_MILLIS, DEFAULT_PROTOCOL_VERSION, DEFAULT_STORAGE_COSTS,
DEFAULT_VALIDATOR_SLOTS, DEFAULT_WASM_CONFIG,
};

static RUST_WORKSPACE_PATH: Lazy<PathBuf> = Lazy::new(|| {
Expand Down
1 change: 1 addition & 0 deletions execution_engine_testing/tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod lmdb_fixture;
pub mod wasm_utils;
pub use casper_engine_test_support::genesis_config_builder::GenesisConfigBuilder;

#[cfg(test)]
mod test;
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::{BTreeMap, BTreeSet};

use crate::lmdb_fixture;
use casper_engine_test_support::{
utils, ExecuteRequestBuilder, LmdbWasmTestBuilder, UpgradeRequestBuilder, DEFAULT_ACCOUNT_ADDR,
Expand All @@ -8,8 +10,11 @@ use casper_execution_engine::{
execution::ExecError,
};
use casper_types::{
ApiError, BlockTime, EraId, InitiatorAddr, Key, PricingMode, ProtocolVersion, RuntimeArgs,
Transaction, TransactionArgs, TransactionEntryPoint, TransactionTarget, TransactionV1Builder,
bytesrepr::{Bytes, ToBytes},
ApiError, BlockTime, Digest, EraId, InitiatorAddr, Key, PricingMode, ProtocolVersion,
PublicKey, RuntimeArgs, SecretKey, TimeDiff, Timestamp, Transaction, TransactionArgs,
TransactionEntryPoint, TransactionRuntime, TransactionScheduling, TransactionTarget,
TransactionV1, TransactionV1Payload,
};

const CONTRACT: &str = "do_nothing_stored.wasm";
Expand All @@ -19,6 +24,7 @@ const BLOCK_TIME: BlockTime = BlockTime::new(10);
pub(crate) const ARGS_MAP_KEY: u16 = 0;
pub(crate) const TARGET_MAP_KEY: u16 = 1;
pub(crate) const ENTRY_POINT_MAP_KEY: u16 = 2;
pub(crate) const SCHEDULING_MAP_KEY: u16 = 3;

#[ignore]
#[test]
Expand All @@ -40,17 +46,13 @@ fn try_add_contract_version(
) {
let module_bytes = utils::read_wasm_file(CONTRACT);

let txn = TransactionV1Builder::new_session(
let txn = new_transaction_v1_session(
is_install_upgrade,
module_bytes,
casper_types::TransactionRuntime::VmCasperV1,
0,
None,
)
.with_secret_key(&DEFAULT_ACCOUNT_SECRET_KEY)
.with_chain_name(CHAIN_NAME)
.build()
.unwrap();
&DEFAULT_ACCOUNT_SECRET_KEY,
);

let txn_request = {
let initiator_addr = txn.initiator_addr().clone();
Expand Down Expand Up @@ -106,6 +108,75 @@ fn try_add_contract_version(
}
}

pub fn new_transaction_v1_session(
is_install_upgrade: bool,
module_bytes: Bytes,
runtime: TransactionRuntime,
transferred_value: u64,
secret_key: &SecretKey,
) -> TransactionV1 {
let timestamp = Timestamp::now();

let target = TransactionTarget::Session {
is_install_upgrade,
module_bytes,
runtime,
transferred_value,
seed: None,
};
let args = TransactionArgs::Named(RuntimeArgs::new());
let entry_point = TransactionEntryPoint::Call;
let scheduling = TransactionScheduling::Standard;
let mut fields: BTreeMap<u16, Bytes> = BTreeMap::new();

fields.insert(ARGS_MAP_KEY, args.to_bytes().unwrap().into());
fields.insert(TARGET_MAP_KEY, target.to_bytes().unwrap().into());
fields.insert(ENTRY_POINT_MAP_KEY, entry_point.to_bytes().unwrap().into());
fields.insert(SCHEDULING_MAP_KEY, scheduling.to_bytes().unwrap().into());

let public_key = PublicKey::from(secret_key);
let initiator_addr = InitiatorAddr::from(public_key);
build_transaction(
CHAIN_NAME.to_string(),
timestamp,
TimeDiff::from_millis(30 * 60 * 1_000),
PricingMode::Fixed {
gas_price_tolerance: 5,
additional_computation_factor: 0,
},
fields,
initiator_addr,
secret_key,
)
}

fn build_transaction(
chain_name: String,
timestamp: Timestamp,
ttl: TimeDiff,
pricing_mode: PricingMode,
fields: BTreeMap<u16, Bytes>,
initiator_addr: InitiatorAddr,
secret_key: &SecretKey,
) -> TransactionV1 {
let transaction_v1_payload = TransactionV1Payload::new(
chain_name,
timestamp,
ttl,
pricing_mode,
initiator_addr,
fields,
);
let hash = Digest::hash(
transaction_v1_payload
.to_bytes()
.unwrap_or_else(|error| panic!("should serialize body: {}", error)),
);
let mut transaction = TransactionV1::new(hash.into(), transaction_v1_payload, BTreeSet::new());
transaction.sign(secret_key);
transaction
}

/// if it becomes necessary to extract deploy session data:
// let data = SessionDataDeploy::new(
// deploy.hash(),
Expand Down
14 changes: 7 additions & 7 deletions execution_engine_testing/tests/src/test/private_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ pub mod management;
mod restricted_auction;
mod unrestricted_transfers;

use std::collections::{BTreeMap, BTreeSet};

use casper_engine_test_support::{
ChainspecConfig, LmdbWasmTestBuilder, DEFAULT_ACCOUNT_INITIAL_BALANCE, DEFAULT_AUCTION_DELAY,
DEFAULT_CHAINSPEC_REGISTRY, DEFAULT_GENESIS_CONFIG_HASH, DEFAULT_GENESIS_TIMESTAMP_MILLIS,
genesis_config_builder::GenesisConfigBuilder, ChainspecConfig, LmdbWasmTestBuilder,
DEFAULT_ACCOUNT_INITIAL_BALANCE, DEFAULT_AUCTION_DELAY, DEFAULT_CHAINSPEC_REGISTRY,
DEFAULT_GENESIS_CONFIG_HASH, DEFAULT_GENESIS_TIMESTAMP_MILLIS,
DEFAULT_LOCKED_FUNDS_PERIOD_MILLIS, DEFAULT_PROPOSER_PUBLIC_KEY, DEFAULT_PROTOCOL_VERSION,
DEFAULT_ROUND_SEIGNIORAGE_RATE, DEFAULT_STORAGE_COSTS, DEFAULT_SYSTEM_CONFIG,
DEFAULT_UNBONDING_DELAY, DEFAULT_VALIDATOR_SLOTS, DEFAULT_WASM_CONFIG,
};
use num_rational::Ratio;
use once_cell::sync::Lazy;
use std::collections::{BTreeMap, BTreeSet};

use casper_storage::data_access_layer::GenesisRequest;
use casper_types::{
account::AccountHash, system::auction::DELEGATION_RATE_DENOMINATOR, AdministratorAccount,
CoreConfig, FeeHandling, GenesisAccount, GenesisConfig, GenesisConfigBuilder, GenesisValidator,
HostFunction, HostFunctionCosts, MessageLimits, Motes, OpcodeCosts, PublicKey, RefundHandling,
SecretKey, StorageCosts, WasmConfig, WasmV1Config, DEFAULT_V1_MAX_STACK_HEIGHT,
CoreConfig, FeeHandling, GenesisAccount, GenesisConfig, GenesisValidator, HostFunction,
HostFunctionCosts, MessageLimits, Motes, OpcodeCosts, PublicKey, RefundHandling, SecretKey,
StorageCosts, WasmConfig, WasmV1Config, DEFAULT_V1_MAX_STACK_HEIGHT,
DEFAULT_V1_WASM_MAX_MEMORY, U512,
};
use tempfile::TempDir;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use casper_types::{
mint,
standard_payment::{self, ARG_AMOUNT},
},
AddressableEntityHash, ApiError, CLType, CLValue, CoreConfig, GenesisAccount,
GenesisConfigBuilder, Key, Package, PackageHash, RuntimeArgs, U512,
AddressableEntityHash, ApiError, CLType, CLValue, CoreConfig, GenesisAccount, Key, Package,
PackageHash, RuntimeArgs, U512,
};
use tempfile::TempDir;

Expand All @@ -27,7 +27,7 @@ use crate::{
self, ACCOUNT_2_ADDR, ADMIN_1_ACCOUNT_ADDR, PRIVATE_CHAIN_ALLOW_AUCTION_BIDS,
PRIVATE_CHAIN_COMPUTE_REWARDS, VALIDATOR_1_PUBLIC_KEY,
},
wasm_utils,
wasm_utils, GenesisConfigBuilder,
};

use super::{
Expand Down
Loading

0 comments on commit a5fcc71

Please sign in to comment.