Skip to content

Commit

Permalink
Merge pull request #61 from confio/42-optimize-validators-storage
Browse files Browse the repository at this point in the history
Optimize validators storage
  • Loading branch information
maurolacy authored Feb 3, 2022
2 parents 56844bb + be53abc commit 182afd0
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 51 deletions.
29 changes: 17 additions & 12 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ jobs:
command: rustc --version; cargo --version; rustup --version; rustup target list --installed
- restore_cache:
keys:
- cargocache-v2-bindings:1.54.0-{{ checksum "~/project/Cargo.lock" }}
- cargocache-v2-bindings:1.58.1-{{ checksum "~/project/Cargo.lock" }}
- run:
name: Build library for native target
command: cargo build --locked
Expand All @@ -289,7 +289,7 @@ jobs:
paths:
- /usr/local/cargo/registry
- target
key: cargocache-v2-bindings:1.54.0-{{ checksum "~/project/Cargo.lock" }}
key: cargocache-v2-bindings:1.58.1-{{ checksum "~/project/Cargo.lock" }}

package_bindings_test:
docker:
Expand All @@ -303,7 +303,7 @@ jobs:
command: rustc --version; cargo --version; rustup --version; rustup target list --installed
- restore_cache:
keys:
- cargocache-v2-bindings-test:1.54.0-{{ checksum "~/project/Cargo.lock" }}
- cargocache-v2-bindings-test:1.58.1-{{ checksum "~/project/Cargo.lock" }}
- run:
name: Build library for native target
command: cargo build --locked
Expand All @@ -314,7 +314,7 @@ jobs:
paths:
- /usr/local/cargo/registry
- target
key: cargocache-v2-bindings-test:1.54.0-{{ checksum "~/project/Cargo.lock" }}
key: cargocache-v2-bindings-test:1.58.1-{{ checksum "~/project/Cargo.lock" }}

package_utils:
docker:
Expand All @@ -328,7 +328,7 @@ jobs:
command: rustc --version; cargo --version; rustup --version; rustup target list --installed
- restore_cache:
keys:
- cargocache-v2-utils:1.54.0-{{ checksum "~/project/Cargo.lock" }}
- cargocache-v2-utils:1.58.1-{{ checksum "~/project/Cargo.lock" }}
- run:
name: Build library for native target
command: cargo build --locked
Expand All @@ -339,7 +339,7 @@ jobs:
paths:
- /usr/local/cargo/registry
- target
key: cargocache-v2-utils:1.54.0-{{ checksum "~/project/Cargo.lock" }}
key: cargocache-v2-utils:1.58.1-{{ checksum "~/project/Cargo.lock" }}


package_tg4:
Expand All @@ -354,7 +354,7 @@ jobs:
command: rustc --version; cargo --version; rustup --version; rustup target list --installed
- restore_cache:
keys:
- cargocache-v2-tg4:1.54.0-{{ checksum "~/project/Cargo.lock" }}
- cargocache-v2-tg4:1.58.1-{{ checksum "~/project/Cargo.lock" }}
- run:
name: Build library for native target
command: cargo build --locked
Expand All @@ -368,7 +368,7 @@ jobs:
paths:
- /usr/local/cargo/registry
- target
key: cargocache-v2-tg4:1.54.0-{{ checksum "~/project/Cargo.lock" }}
key: cargocache-v2-tg4:1.58.1-{{ checksum "~/project/Cargo.lock" }}

package_voting_contract:
docker:
Expand All @@ -382,7 +382,7 @@ jobs:
command: rustc --version; cargo --version; rustup --version; rustup target list --installed
- restore_cache:
keys:
- cargocache-v2-tg4:1.54.0-{{ checksum "~/project/Cargo.lock" }}
- cargocache-v2-tg4:1.58.1-{{ checksum "~/project/Cargo.lock" }}
- run:
name: Build library for native target
command: cargo build --locked
Expand All @@ -396,7 +396,7 @@ jobs:
paths:
- /usr/local/cargo/registry
- target
key: cargocache-v2-tg4:1.54.0-{{ checksum "~/project/Cargo.lock" }}
key: cargocache-v2-tg4:1.58.1-{{ checksum "~/project/Cargo.lock" }}

package_test_utils:
docker:
Expand All @@ -410,7 +410,7 @@ jobs:
command: rustc --version; cargo --version; rustup --version; rustup target list --installed
- restore_cache:
keys:
- cargocache-v2-tg4:1.54.0-{{ checksum "~/project/Cargo.lock" }}
- cargocache-v2-tg4:1.58.1-{{ checksum "~/project/Cargo.lock" }}
- run:
name: Build library for native target
command: cargo build --locked
Expand All @@ -421,7 +421,7 @@ jobs:
paths:
- /usr/local/cargo/registry
- target
key: cargocache-v2-tg4:1.54.0-{{ checksum "~/project/Cargo.lock" }}
key: cargocache-v2-tg4:1.58.1-{{ checksum "~/project/Cargo.lock" }}

lint:
docker:
Expand All @@ -440,6 +440,11 @@ jobs:
- run:
name: Add clippy component
command: rustup component add clippy
- run:
name: Create fake wasm files
command: |
mkdir -p ./target/wasm32-unknown-unknown/release/
touch ./target/wasm32-unknown-unknown/release/tgrade_valset.wasm
- run:
name: Clippy linting on workspace
command: cargo clippy --tests -- -D warnings
Expand Down
8 changes: 8 additions & 0 deletions Cargo.lock

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

9 changes: 7 additions & 2 deletions contracts/tgrade-valset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ crate-type = ["cdylib", "rlib"]
backtraces = ["cosmwasm-std/backtraces"]
# use library feature to disable all instantiate/execute/query exports
library = []
integration = ["bech32", "cosmwasm-vm"]

[dependencies]
cw-utils = { version = "0.11.0" }
Expand All @@ -33,13 +34,17 @@ tg-bindings = { version = "0.6.0-alpha1", path = "../../packages/bindings" }
tg-utils = { version = "0.6.0-alpha1", path = "../../packages/utils" }
cw-controllers = { version = "0.11.0" }
cw-storage-plus = { version = "0.11.0" }
cosmwasm-std = { version = "1.0.0-beta" }
cosmwasm-std = { version = "1.0.0-beta4" }
schemars = "0.8"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.21" }

# For integration tests ("integration" feature)
bech32 = { version = "0.8.1", optional = true }
cosmwasm-vm = { version = "1.0.0-beta4", optional = true, default-features = false, features = ["iterator"] }

[dev-dependencies]
cosmwasm-schema = { version = "1.0.0-beta" }
cosmwasm-schema = { version = "1.0.0-beta4" }
cw-multi-test = "0.11.0"
tg4-engagement = { path = "../tg4-engagement", version = "0.6.0-alpha1" }
tg4-stake = { path = "../tg4-stake", version = "0.6.0-alpha1" }
Expand Down
49 changes: 26 additions & 23 deletions contracts/tgrade-valset/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ pub fn execute(
}
ExecuteMsg::Unjail { operator } => execute_unjail(deps, env, info, operator),
ExecuteMsg::Slash { addr, portion } => execute_slash(deps, env, info, addr, portion),
ExecuteMsg::SimulateValidatorSet { validators } => {
execute_simulate_validators(deps, info, validators)
}
}
}

Expand Down Expand Up @@ -367,6 +370,28 @@ fn execute_slash(
Ok(resp)
}

fn execute_simulate_validators(
deps: DepsMut,
_info: MessageInfo,
validators: Vec<ValidatorInfo>,
) -> Result<Response, ContractError> {
// Assert admin is not found (contract not instantiated!) for this to be possible
let admin_res = ADMIN.get(deps.as_ref());
if let Err(err) = admin_res {
let not_found = matches!(err, StdError::NotFound { .. });
if !not_found {
return Err(ContractError::AdminError(AdminError::NotAdmin {}));
}
} else {
return Err(ContractError::AdminError(AdminError::NotAdmin {}));
}

// Store validators
VALIDATORS.save(deps.storage, &validators)?;

Ok(Response::new())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractError> {
use QueryMsg::*;
Expand Down Expand Up @@ -700,7 +725,7 @@ fn calculate_validators(
Err(err) => return Some(Err(err)),
// address not jailed, proceed
Ok(None) => (),
// address jailed, but period axpired and auto unjailing enabled, add to
// address jailed, but period expired and auto unjailing enabled, add to
// auto_unjail list
Ok(Some(expires)) if cfg.auto_unjail && expires.is_expired(&env.block) => {
auto_unjail.push(m_addr.clone())
Expand All @@ -713,7 +738,6 @@ fn calculate_validators(
Ok(ValidatorInfo {
operator: m_addr,
validator_pubkey: op.pubkey.into(),
metadata: op.metadata,
power: m.weight * scaling,
})
})
Expand Down Expand Up @@ -990,25 +1014,11 @@ mod test {
ValidatorInfo {
operator: Addr::unchecked("op1"),
validator_pubkey: Pubkey::Ed25519(b"pubkey1".into()),
metadata: ValidatorMetadata {
moniker: "moniker1".to_owned(),
identity: None,
website: None,
security_contact: None,
details: None,
},
power: 1,
},
ValidatorInfo {
operator: Addr::unchecked("op2"),
validator_pubkey: Pubkey::Ed25519(b"pubkey2".into()),
metadata: ValidatorMetadata {
moniker: "moniker2".to_owned(),
identity: None,
website: None,
security_contact: None,
details: None,
},
power: 2,
},
];
Expand Down Expand Up @@ -1059,13 +1069,6 @@ mod test {
cur.push(ValidatorInfo {
operator: Addr::unchecked("op3"),
validator_pubkey: Pubkey::Ed25519(b"pubkey3".into()),
metadata: ValidatorMetadata {
moniker: "moniker3".to_owned(),
identity: None,
website: None,
security_contact: None,
details: None,
},
power: 3,
});

Expand Down
6 changes: 6 additions & 0 deletions contracts/tgrade-valset/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ pub enum ExecuteMsg {
addr: String,
portion: Decimal,
},

/// This will update the validator set with the passed list.
/// Used for testing validators storage.
SimulateValidatorSet {
validators: Vec<ValidatorInfo>,
},
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
Expand Down
8 changes: 0 additions & 8 deletions contracts/tgrade-valset/src/multitest/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ fn simulate_validators() {
let expected: Vec<_> = vec![ValidatorInfo {
operator: op1_addr.clone(),
validator_pubkey: valid_operator(op1_addr.as_ref()).validator_pubkey,
metadata: valid_operator(op1_addr.as_ref()).metadata,
power: min_weight,
}];
assert_eq!(expected, active);
Expand All @@ -130,13 +129,11 @@ fn simulate_validators() {
ValidatorInfo {
operator: op2_addr.clone(),
validator_pubkey: valid_operator(op2_addr.as_ref()).validator_pubkey,
metadata: valid_operator(op2_addr.as_ref()).metadata,
power: min_weight * 2,
},
ValidatorInfo {
operator: op1_addr.clone(),
validator_pubkey: valid_operator(op1_addr.as_ref()).validator_pubkey,
metadata: valid_operator(op1_addr.as_ref()).metadata,
power: min_weight,
},
];
Expand All @@ -161,19 +158,16 @@ fn simulate_validators() {
ValidatorInfo {
operator: op3_addr.clone(),
validator_pubkey: valid_operator(op3_addr.as_ref()).validator_pubkey,
metadata: valid_operator(op3_addr.as_ref()).metadata,
power: min_weight * 3 - 1,
},
ValidatorInfo {
operator: op2_addr.clone(),
validator_pubkey: valid_operator(op2_addr.as_ref()).validator_pubkey,
metadata: valid_operator(op2_addr.as_ref()).metadata,
power: min_weight * 2,
},
ValidatorInfo {
operator: op1_addr.clone(),
validator_pubkey: valid_operator(op1_addr.as_ref()).validator_pubkey,
metadata: valid_operator(op1_addr.as_ref()).metadata,
power: min_weight,
},
];
Expand All @@ -193,13 +187,11 @@ fn simulate_validators() {
ValidatorInfo {
operator: op3_addr.clone(),
validator_pubkey: valid_operator(op3_addr.as_ref()).validator_pubkey,
metadata: valid_operator(op3_addr.as_ref()).metadata,
power: min_weight * 3 - 1,
},
ValidatorInfo {
operator: op2_addr.clone(),
validator_pubkey: valid_operator(op2_addr.as_ref()).validator_pubkey,
metadata: valid_operator(op2_addr.as_ref()).metadata,
power: min_weight * 2,
},
];
Expand Down
9 changes: 4 additions & 5 deletions contracts/tgrade-valset/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub struct Config {
/// (epoch_reward.amount * 86_400 * 30 / epoch_length) is reward tokens to mint each month.
/// Ensure this is sensible in relation to the total token supply.
pub epoch_reward: Coin,
/// Percentage of total accumulated fees which is substracted from tokens minted as a rewards.
/// 50% as default. To disable this feature just set it to 0 (which efectivelly means that fees
/// Percentage of total accumulated fees which is subtracted from tokens minted as a rewards.
/// 50% as default. To disable this feature just set it to 0 (which effectively means that fees
/// doesn't affect the per epoch reward).
#[serde(default = "default_fee_percentage")]
pub fee_percentage: Decimal,
Expand Down Expand Up @@ -80,7 +80,6 @@ pub struct ValidatorInfo {
/// TODO: better name to specify this is the Tendermint pubkey for consensus?
pub validator_pubkey: Pubkey,
pub operator: Addr,
pub metadata: ValidatorMetadata,
/// The voting power in Tendermint sdk
pub power: u64,
}
Expand All @@ -103,13 +102,13 @@ pub const VALIDATOR_SLASHING: Map<&Addr, Vec<ValidatorSlashing>> = Map::new("val
/// is not jailed
pub const JAIL: Map<&Addr, JailingPeriod> = Map::new("jail");

/// This stores the immutable info for an operator. Both their Tendermint key as well as
/// This stores the info for an operator. Both their Tendermint key as well as
/// their metadata.
#[derive(Serialize, Deserialize, Clone, JsonSchema, Debug, PartialEq)]
pub struct OperatorInfo {
pub pubkey: Ed25519Pubkey,
pub metadata: ValidatorMetadata,
/// Is this an active validator?
/// Is this currently an active validator?
pub active_validator: bool,
}

Expand Down
1 change: 0 additions & 1 deletion contracts/tgrade-valset/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub fn valid_validator(seed: &str, power: u64) -> ValidatorInfo {
ValidatorInfo {
operator: Addr::unchecked(seed),
validator_pubkey: mock_pubkey(seed.as_bytes()),
metadata: mock_metadata(seed),
power,
}
}
Expand Down
Loading

0 comments on commit 182afd0

Please sign in to comment.