From 9ea627383953b02458a25a60b053ef4a3093a0ed Mon Sep 17 00:00:00 2001 From: Emma Zhong Date: Fri, 13 Jan 2023 21:11:43 -0800 Subject: [PATCH] validator: add voting power (#7392) --- ..._populated_genesis_snapshot_matches-2.snap | 4 +- ...cal_transaction_cost__good_snapshot-2.snap | 12 +- crates/sui-framework/docs/validator.md | 64 +++++++++ crates/sui-framework/docs/validator_set.md | 135 ++++++++++++++---- .../sources/governance/validator.move | 19 +++ .../sources/governance/validator_set.move | 78 +++++++--- crates/sui-open-rpc/spec/openrpc.json | 18 ++- crates/sui-types/src/committee.rs | 3 + crates/sui-types/src/sui_system_state.rs | 8 +- crates/test-utils/src/sui_system_state.rs | 4 +- narwhal/config/src/lib.rs | 4 + 11 files changed, 286 insertions(+), 63 deletions(-) diff --git a/crates/sui-config/tests/snapshots/snapshot_tests__populated_genesis_snapshot_matches-2.snap b/crates/sui-config/tests/snapshots/snapshot_tests__populated_genesis_snapshot_matches-2.snap index 1f833939c1d5f..4b2fdab82ae1f 100644 --- a/crates/sui-config/tests/snapshots/snapshot_tests__populated_genesis_snapshot_matches-2.snap +++ b/crates/sui-config/tests/snapshots/snapshot_tests__populated_genesis_snapshot_matches-2.snap @@ -9,7 +9,8 @@ epoch: 0 validators: validator_stake: 1 delegation_stake: 0 - quorum_stake_threshold: 1 + total_voting_power: 1 + quorum_threshold: 1 active_validators: - metadata: sui_address: "0x21b60aa9a8cb189ccbe20461dbfad2202fdef55b" @@ -237,6 +238,7 @@ validators: next_epoch_delegation: 0 next_epoch_gas_price: 1 next_epoch_commission_rate: 0 + voting_power: 1 stake_amount: 1 pending_stake: 0 pending_withdraw: 0 diff --git a/crates/sui-cost/tests/snapshots/empirical_transaction_cost__good_snapshot-2.snap b/crates/sui-cost/tests/snapshots/empirical_transaction_cost__good_snapshot-2.snap index aa2523bca535e..7ccb35a0194f2 100644 --- a/crates/sui-cost/tests/snapshots/empirical_transaction_cost__good_snapshot-2.snap +++ b/crates/sui-cost/tests/snapshots/empirical_transaction_cost__good_snapshot-2.snap @@ -4,13 +4,13 @@ expression: common_costs_estimate --- { "MergeCoin": { - "computation_cost": 7112, - "storage_cost": 10209, + "computation_cost": 7159, + "storage_cost": 10279, "storage_rebate": 0 }, "Publish": { - "computation_cost": 7971, - "storage_cost": 11412, + "computation_cost": 8018, + "storage_cost": 11482, "storage_rebate": 0 }, "SharedCounterAssertValue": { @@ -29,8 +29,8 @@ expression: common_costs_estimate "storage_rebate": 0 }, "SplitCoin": { - "computation_cost": 7090, - "storage_cost": 10177, + "computation_cost": 7137, + "storage_cost": 10246, "storage_rebate": 0 }, "TransferPortionSuiCoin": { diff --git a/crates/sui-framework/docs/validator.md b/crates/sui-framework/docs/validator.md index 9b8255ce2b24c..50b00dd02d657 100644 --- a/crates/sui-framework/docs/validator.md +++ b/crates/sui-framework/docs/validator.md @@ -27,6 +27,8 @@ - [Function `stake_amount`](#0x2_validator_stake_amount) - [Function `delegate_amount`](#0x2_validator_delegate_amount) - [Function `total_stake`](#0x2_validator_total_stake) +- [Function `voting_power`](#0x2_validator_voting_power) +- [Function `set_voting_power`](#0x2_validator_set_voting_power) - [Function `pending_stake_amount`](#0x2_validator_pending_stake_amount) - [Function `pending_withdraw`](#0x2_validator_pending_withdraw) - [Function `gas_price`](#0x2_validator_gas_price) @@ -195,6 +197,13 @@ Summary of the validator.
+voting_power: u64 +
+
+ The voting power of this validator, which might be different from its + stake amount. +
+
stake_amount: u64
@@ -355,6 +364,10 @@ next_epoch_gas_price: gas_price, next_epoch_commission_rate: commission_rate, }, + // Initialize the voting power to be the same as the stake amount. + // At the epoch change where this validator is actually added to the + // active validator set, the voting power will be updated accordingly. + voting_power: stake_amount, stake_amount, pending_stake: 0, pending_withdraw: 0, @@ -387,6 +400,7 @@
public(friend) fun destroy(self: Validator, ctx: &mut TxContext) {
     let Validator {
         metadata: _,
+        voting_power: _,
         stake_amount: _,
         pending_stake: _,
         pending_withdraw: _,
@@ -843,6 +857,56 @@ Return the total amount staked with this validator, including both validator sta
 
 
 
+
+
+
+
+## Function `voting_power`
+
+Return the voting power of this validator.
+
+
+
public fun voting_power(self: &validator::Validator): u64
+
+ + + +
+Implementation + + +
public fun voting_power(self: &Validator): u64 {
+    self.voting_power
+}
+
+ + + +
+ + + +## Function `set_voting_power` + +Set the voting power of this validator, called only from validator_set. + + +
public(friend) fun set_voting_power(self: &mut validator::Validator, new_voting_power: u64)
+
+ + + +
+Implementation + + +
public(friend) fun set_voting_power(self: &mut Validator, new_voting_power: u64) {
+    self.voting_power = new_voting_power;
+}
+
+ + +
diff --git a/crates/sui-framework/docs/validator_set.md b/crates/sui-framework/docs/validator_set.md index 0bb72ac5a7ae6..16103d8e168b8 100644 --- a/crates/sui-framework/docs/validator_set.md +++ b/crates/sui-framework/docs/validator_set.md @@ -21,7 +21,9 @@ - [Function `request_set_gas_price`](#0x2_validator_set_request_set_gas_price) - [Function `request_set_commission_rate`](#0x2_validator_set_request_set_commission_rate) - [Function `advance_epoch`](#0x2_validator_set_advance_epoch) +- [Function `update_validator_voting_power`](#0x2_validator_set_update_validator_voting_power) - [Function `derive_reference_gas_price`](#0x2_validator_set_derive_reference_gas_price) +- [Function `total_voting_power`](#0x2_validator_set_total_voting_power) - [Function `total_validator_stake`](#0x2_validator_set_total_validator_stake) - [Function `total_delegation_stake`](#0x2_validator_set_total_delegation_stake) - [Function `validator_stake_amount`](#0x2_validator_set_validator_stake_amount) @@ -37,8 +39,8 @@ - [Function `sort_removal_list`](#0x2_validator_set_sort_removal_list) - [Function `process_pending_delegation_switches`](#0x2_validator_set_process_pending_delegation_switches) - [Function `process_pending_delegations_and_withdraws`](#0x2_validator_set_process_pending_delegations_and_withdraws) -- [Function `calculate_total_stake_and_quorum_threshold`](#0x2_validator_set_calculate_total_stake_and_quorum_threshold) -- [Function `calculate_quorum_threshold`](#0x2_validator_set_calculate_quorum_threshold) +- [Function `calculate_total_stakes`](#0x2_validator_set_calculate_total_stakes) +- [Function `calculate_total_voting_power_and_quorum_threshold`](#0x2_validator_set_calculate_total_voting_power_and_quorum_threshold) - [Function `adjust_stake_and_gas_price`](#0x2_validator_set_adjust_stake_and_gas_price) - [Function `compute_reward_distribution`](#0x2_validator_set_compute_reward_distribution) - [Function `distribute_reward`](#0x2_validator_set_distribute_reward) @@ -92,14 +94,20 @@ total_delegation_stake: u64
- Total amount of stake from delegation, at the beginning of the epoch. + Total amount of stake from delegation, at the beginning of the epoch.
-quorum_stake_threshold: u64 +total_voting_power: u64
- The amount of accumulated stake to reach a quorum among all active validators. - This is always 2/3 of total stake. Keep it here to reduce potential inconsistencies + Sum of voting power of validators. +
+
+quorum_threshold: u64 +
+
+ The amount of accumulated voting power to reach a quorum among all active validators. + This is always 2/3 of total voting power. Keep it here to reduce potential inconsistencies among validators.
@@ -334,11 +342,15 @@ each validator, emitted during epoch advancement.
public(friend) fun new(init_active_validators: vector<Validator>): ValidatorSet {
-    let (total_validator_stake, total_delegation_stake, quorum_stake_threshold) = calculate_total_stake_and_quorum_threshold(&init_active_validators);
+    let (total_validator_stake, total_delegation_stake) =
+        calculate_total_stakes(&init_active_validators);
+    let (total_voting_power, quorum_threshold) =
+        calculate_total_voting_power_and_quorum_threshold(&init_active_validators);
     let validators = ValidatorSet {
         total_validator_stake,
         total_delegation_stake,
-        quorum_stake_threshold,
+        total_voting_power,
+        quorum_threshold,
         active_validators: init_active_validators,
         pending_validators: vector::empty(),
         pending_removals: vector::empty(),
@@ -346,6 +358,7 @@ each validator, emitted during epoch advancement.
         pending_delegation_switches: vec_map::empty(),
     };
     validators.next_epoch_validators = derive_next_epoch_validators(&validators);
+    update_validator_voting_power(&mut validators);
     validators
 }
 
@@ -784,12 +797,51 @@ It does the following things: process_pending_removals(self, ctx); + // Update the voting power of each validator, now that the pending validator additions + // and the removals have been processed. + update_validator_voting_power(self); + self.next_epoch_validators = derive_next_epoch_validators(self); - let (validator_stake, delegation_stake, quorum_stake_threshold) = calculate_total_stake_and_quorum_threshold(&self.active_validators); + let (validator_stake, delegation_stake) = calculate_total_stakes(&self.active_validators); self.total_validator_stake = validator_stake; self.total_delegation_stake = delegation_stake; - self.quorum_stake_threshold = quorum_stake_threshold; + + let (total_voting_power, quorum_threshold) = + calculate_total_voting_power_and_quorum_threshold(&self.active_validators); + self.total_voting_power = total_voting_power; + self.quorum_threshold = quorum_threshold; +} +
+ + + + + + + +## Function `update_validator_voting_power` + + + +
fun update_validator_voting_power(self: &mut validator_set::ValidatorSet)
+
+ + + +
+Implementation + + +
fun update_validator_voting_power(self: &mut ValidatorSet) {
+    let num_validators = vector::length(&self.active_validators);
+    let i = 0;
+    while (i < num_validators) {
+        let validator_mut = vector::borrow_mut(&mut self.active_validators, i);
+        let updated_voting_power = validator::total_stake(validator_mut);
+        validator::set_voting_power(validator_mut, updated_voting_power);
+        i = i + 1;
+    };
 }
 
@@ -833,7 +885,7 @@ gas price, weighted by stake. // Build a priority queue that will pop entries with gas price from the highest to the lowest. let pq = pq::new(entries); let sum = 0; - let threshold = (total_validator_stake(self) + total_delegation_stake(self)) / 3; + let threshold = self.total_voting_power - self.quorum_threshold; let result = 0; while (sum < threshold) { let (gas_price, stake) = pq::pop_max(&mut pq); @@ -846,6 +898,30 @@ gas price, weighted by stake. +
+ + + +## Function `total_voting_power` + + + +
public fun total_voting_power(self: &validator_set::ValidatorSet): u64
+
+ + + +
+Implementation + + +
public fun total_voting_power(self: &ValidatorSet): u64 {
+    self.total_voting_power
+}
+
+ + +
@@ -1317,14 +1393,14 @@ Process all active validators' pending delegation deposits and withdraws. - + -## Function `calculate_total_stake_and_quorum_threshold` +## Function `calculate_total_stakes` -Calculate the total active stake, and the amount of stake to reach quorum. +Calculate the total active validator and delegated stake. -
fun calculate_total_stake_and_quorum_threshold(validators: &vector<validator::Validator>): (u64, u64, u64)
+
fun calculate_total_stakes(validators: &vector<validator::Validator>): (u64, u64)
 
@@ -1333,7 +1409,7 @@ Calculate the total active stake, and the amount of stake to reach quorum. Implementation -
fun calculate_total_stake_and_quorum_threshold(validators: &vector<Validator>): (u64, u64, u64) {
+
fun calculate_total_stakes(validators: &vector<Validator>): (u64, u64) {
     let validator_state = 0;
     let delegate_stake = 0;
     let length = vector::length(validators);
@@ -1344,8 +1420,7 @@ Calculate the total active stake, and the amount of stake to reach quorum.
         delegate_stake = delegate_stake + validator::delegate_amount(v);
         i = i + 1;
     };
-    let total_stake = validator_state + delegate_stake;
-    (validator_state, delegate_stake, (total_stake + 1) * 2 / 3)
+    (validator_state, delegate_stake)
 }
 
@@ -1353,16 +1428,14 @@ Calculate the total active stake, and the amount of stake to reach quorum. - + -## Function `calculate_quorum_threshold` +## Function `calculate_total_voting_power_and_quorum_threshold` -Calculate the required percentage threshold to reach quorum. -With 3f + 1 validators, we can tolerate up to f byzantine ones. -Hence (2f + 1) / total is our threshold. +Calculate the total voting power, and the amount of voting power to reach quorum. -
fun calculate_quorum_threshold(validators: &vector<validator::Validator>): u8
+
fun calculate_total_voting_power_and_quorum_threshold(validators: &vector<validator::Validator>): (u64, u64)
 
@@ -1371,10 +1444,16 @@ Hence (2f + 1) / total is our threshold. Implementation -
fun calculate_quorum_threshold(validators: &vector<Validator>): u8 {
-    let count = vector::length(validators);
-    let threshold = (2 * count / 3 + 1) * 100 / count;
-    (threshold as u8)
+
fun calculate_total_voting_power_and_quorum_threshold(validators: &vector<Validator>): (u64, u64) {
+    let total_voting_power = 0;
+    let length = vector::length(validators);
+    let i = 0;
+    while (i < length) {
+        let v = vector::borrow(validators, i);
+        total_voting_power = total_voting_power + validator::voting_power(v);
+        i = i + 1;
+    };
+    (total_voting_power, (total_voting_power + 1) * 2 / 3)
 }
 
diff --git a/crates/sui-framework/sources/governance/validator.move b/crates/sui-framework/sources/governance/validator.move index e11e3cc30d1f1..f7cf111761151 100644 --- a/crates/sui-framework/sources/governance/validator.move +++ b/crates/sui-framework/sources/governance/validator.move @@ -67,6 +67,9 @@ module sui::validator { struct Validator has store { /// Summary of the validator. metadata: ValidatorMetadata, + /// The voting power of this validator, which might be different from its + /// stake amount. + voting_power: u64, /// The current active stake amount. This will not change during an epoch. It can only /// be updated at the end of epoch. stake_amount: u64, @@ -154,6 +157,10 @@ module sui::validator { next_epoch_gas_price: gas_price, next_epoch_commission_rate: commission_rate, }, + // Initialize the voting power to be the same as the stake amount. + // At the epoch change where this validator is actually added to the + // active validator set, the voting power will be updated accordingly. + voting_power: stake_amount, stake_amount, pending_stake: 0, pending_withdraw: 0, @@ -166,6 +173,7 @@ module sui::validator { public(friend) fun destroy(self: Validator, ctx: &mut TxContext) { let Validator { metadata: _, + voting_power: _, stake_amount: _, pending_stake: _, pending_withdraw: _, @@ -299,6 +307,16 @@ module sui::validator { stake_amount(self) + delegate_amount(self) } + /// Return the voting power of this validator. + public fun voting_power(self: &Validator): u64 { + self.voting_power + } + + /// Set the voting power of this validator, called only from validator_set. + public(friend) fun set_voting_power(self: &mut Validator, new_voting_power: u64) { + self.voting_power = new_voting_power; + } + public fun pending_stake_amount(self: &Validator): u64 { self.pending_stake } @@ -379,6 +397,7 @@ module sui::validator { next_epoch_commission_rate: commission_rate, }, stake_amount, + voting_power: stake_amount, pending_stake: 0, pending_withdraw: 0, gas_price, diff --git a/crates/sui-framework/sources/governance/validator_set.move b/crates/sui-framework/sources/governance/validator_set.move index e5573afd8c1cd..081203b5be5ac 100644 --- a/crates/sui-framework/sources/governance/validator_set.move +++ b/crates/sui-framework/sources/governance/validator_set.move @@ -28,13 +28,16 @@ module sui::validator_set { /// at the beginning of the epoch. total_validator_stake: u64, - /// Total amount of stake from delegation, at the beginning of the epoch. + /// Total amount of stake from delegation, at the beginning of the epoch. total_delegation_stake: u64, - /// The amount of accumulated stake to reach a quorum among all active validators. - /// This is always 2/3 of total stake. Keep it here to reduce potential inconsistencies + /// Sum of voting power of validators. + total_voting_power: u64, + + /// The amount of accumulated voting power to reach a quorum among all active validators. + /// This is always 2/3 of total voting power. Keep it here to reduce potential inconsistencies /// among validators. - quorum_stake_threshold: u64, + quorum_threshold: u64, /// The current list of active validators. active_validators: vector, @@ -90,11 +93,15 @@ module sui::validator_set { // ==== initialization at genesis ==== public(friend) fun new(init_active_validators: vector): ValidatorSet { - let (total_validator_stake, total_delegation_stake, quorum_stake_threshold) = calculate_total_stake_and_quorum_threshold(&init_active_validators); + let (total_validator_stake, total_delegation_stake) = + calculate_total_stakes(&init_active_validators); + let (total_voting_power, quorum_threshold) = + calculate_total_voting_power_and_quorum_threshold(&init_active_validators); let validators = ValidatorSet { total_validator_stake, total_delegation_stake, - quorum_stake_threshold, + total_voting_power, + quorum_threshold, active_validators: init_active_validators, pending_validators: vector::empty(), pending_removals: vector::empty(), @@ -102,6 +109,7 @@ module sui::validator_set { pending_delegation_switches: vec_map::empty(), }; validators.next_epoch_validators = derive_next_epoch_validators(&validators); + update_validator_voting_power(&mut validators); validators } @@ -351,12 +359,32 @@ module sui::validator_set { process_pending_removals(self, ctx); + // Update the voting power of each validator, now that the pending validator additions + // and the removals have been processed. + update_validator_voting_power(self); + self.next_epoch_validators = derive_next_epoch_validators(self); - let (validator_stake, delegation_stake, quorum_stake_threshold) = calculate_total_stake_and_quorum_threshold(&self.active_validators); + let (validator_stake, delegation_stake) = calculate_total_stakes(&self.active_validators); self.total_validator_stake = validator_stake; self.total_delegation_stake = delegation_stake; - self.quorum_stake_threshold = quorum_stake_threshold; + + let (total_voting_power, quorum_threshold) = + calculate_total_voting_power_and_quorum_threshold(&self.active_validators); + self.total_voting_power = total_voting_power; + self.quorum_threshold = quorum_threshold; + } + + // TODO: implement this to correctly cap the voting power. + fun update_validator_voting_power(self: &mut ValidatorSet) { + let num_validators = vector::length(&self.active_validators); + let i = 0; + while (i < num_validators) { + let validator_mut = vector::borrow_mut(&mut self.active_validators, i); + let updated_voting_power = validator::total_stake(validator_mut); + validator::set_voting_power(validator_mut, updated_voting_power); + i = i + 1; + }; } /// Called by `sui_system` to derive reference gas price for the new epoch. @@ -380,7 +408,7 @@ module sui::validator_set { // Build a priority queue that will pop entries with gas price from the highest to the lowest. let pq = pq::new(entries); let sum = 0; - let threshold = (total_validator_stake(self) + total_delegation_stake(self)) / 3; + let threshold = self.total_voting_power - self.quorum_threshold; let result = 0; while (sum < threshold) { let (gas_price, stake) = pq::pop_max(&mut pq); @@ -392,6 +420,10 @@ module sui::validator_set { // ==== getter functions ==== + public fun total_voting_power(self: &ValidatorSet): u64 { + self.total_voting_power + } + public fun total_validator_stake(self: &ValidatorSet): u64 { self.total_validator_stake } @@ -564,8 +596,8 @@ module sui::validator_set { } } - /// Calculate the total active stake, and the amount of stake to reach quorum. - fun calculate_total_stake_and_quorum_threshold(validators: &vector): (u64, u64, u64) { + /// Calculate the total active validator and delegated stake. + fun calculate_total_stakes(validators: &vector): (u64, u64) { let validator_state = 0; let delegate_stake = 0; let length = vector::length(validators); @@ -576,17 +608,20 @@ module sui::validator_set { delegate_stake = delegate_stake + validator::delegate_amount(v); i = i + 1; }; - let total_stake = validator_state + delegate_stake; - (validator_state, delegate_stake, (total_stake + 1) * 2 / 3) + (validator_state, delegate_stake) } - /// Calculate the required percentage threshold to reach quorum. - /// With 3f + 1 validators, we can tolerate up to f byzantine ones. - /// Hence (2f + 1) / total is our threshold. - fun calculate_quorum_threshold(validators: &vector): u8 { - let count = vector::length(validators); - let threshold = (2 * count / 3 + 1) * 100 / count; - (threshold as u8) + /// Calculate the total voting power, and the amount of voting power to reach quorum. + fun calculate_total_voting_power_and_quorum_threshold(validators: &vector): (u64, u64) { + let total_voting_power = 0; + let length = vector::length(validators); + let i = 0; + while (i < length) { + let v = vector::borrow(validators, i); + total_voting_power = total_voting_power + validator::voting_power(v); + i = i + 1; + }; + (total_voting_power, (total_voting_power + 1) * 2 / 3) } /// Process the pending stake changes for each validator. @@ -753,7 +788,8 @@ module sui::validator_set { let ValidatorSet { total_validator_stake: _, total_delegation_stake: _, - quorum_stake_threshold: _, + total_voting_power: _, + quorum_threshold: _, active_validators, pending_validators, pending_removals: _, diff --git a/crates/sui-open-rpc/spec/openrpc.json b/crates/sui-open-rpc/spec/openrpc.json index bb32c4a5a11b9..6050ff7c82faa 100644 --- a/crates/sui-open-rpc/spec/openrpc.json +++ b/crates/sui-open-rpc/spec/openrpc.json @@ -6413,7 +6413,8 @@ "metadata", "pending_stake", "pending_withdraw", - "stake_amount" + "stake_amount", + "voting_power" ], "properties": { "commission_rate": { @@ -6446,6 +6447,11 @@ "type": "integer", "format": "uint64", "minimum": 0.0 + }, + "voting_power": { + "type": "integer", + "format": "uint64", + "minimum": 0.0 } } }, @@ -6609,7 +6615,8 @@ "pending_delegation_switches", "pending_removals", "pending_validators", - "quorum_stake_threshold", + "quorum_threshold", + "total_voting_power", "validator_stake" ], "properties": { @@ -6647,7 +6654,12 @@ "$ref": "#/components/schemas/Validator" } }, - "quorum_stake_threshold": { + "quorum_threshold": { + "type": "integer", + "format": "uint64", + "minimum": 0.0 + }, + "total_voting_power": { "type": "integer", "format": "uint64", "minimum": 0.0 diff --git a/crates/sui-types/src/committee.rs b/crates/sui-types/src/committee.rs index d94116c6cf67e..42758053eb68d 100644 --- a/crates/sui-types/src/committee.rs +++ b/crates/sui-types/src/committee.rs @@ -20,6 +20,9 @@ use std::hash::{Hash, Hasher}; pub type EpochId = u64; +// TODO: the stake and voting power of a validator can be different so +// in some places when we are actually referring to the voting power, we +// should use a different type alias, field name, etc. pub type StakeUnit = u64; pub type CommitteeDigest = [u8; 32]; diff --git a/crates/sui-types/src/sui_system_state.rs b/crates/sui-types/src/sui_system_state.rs index 885b5bb2496d0..60391bed817af 100644 --- a/crates/sui-types/src/sui_system_state.rs +++ b/crates/sui-types/src/sui_system_state.rs @@ -73,6 +73,7 @@ impl ValidatorMetadata { #[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, JsonSchema)] pub struct Validator { pub metadata: ValidatorMetadata, + pub voting_power: u64, pub stake_amount: u64, pub pending_stake: u64, pub pending_withdraw: u64, @@ -89,7 +90,7 @@ impl Validator { // TODO: Make sure we are actually verifying this on-chain. AuthorityPublicKeyBytes::from_bytes(self.metadata.pubkey_bytes.as_ref()) .expect("Validity of public key bytes should be verified on-chain"), - self.stake_amount + self.delegation_staking_pool.sui_balance, + self.voting_power, self.metadata.net_address.clone(), ) } @@ -161,7 +162,8 @@ pub struct ValidatorPair { pub struct ValidatorSet { pub validator_stake: u64, pub delegation_stake: u64, - pub quorum_stake_threshold: u64, + pub total_voting_power: u64, + pub quorum_threshold: u64, pub active_validators: Vec, pub pending_validators: Vec, pub pending_removals: Vec, @@ -237,7 +239,7 @@ impl SuiSystemState { Multiaddr::try_from(validator.metadata.consensus_address.clone()) .expect("Can't get narwhal primary address"); let authority = narwhal_config::Authority { - stake: validator.stake_amount as narwhal_config::Stake, + stake: validator.voting_power as narwhal_config::Stake, primary_address, network_key, }; diff --git a/crates/test-utils/src/sui_system_state.rs b/crates/test-utils/src/sui_system_state.rs index c14a5f3204f80..8230d1251c3da 100644 --- a/crates/test-utils/src/sui_system_state.rs +++ b/crates/test-utils/src/sui_system_state.rs @@ -64,6 +64,7 @@ pub fn test_validator( let sui_address = SuiAddress::from(&pubkey_bytes); Validator { metadata: test_validatdor_metadata(sui_address, pubkey_bytes, net_address), + voting_power: stake_amount, stake_amount, pending_stake: 1, pending_withdraw: 1, @@ -77,7 +78,8 @@ pub fn test_sui_system_state(epoch: EpochId, validators: Vec) -> SuiS let validator_set = ValidatorSet { validator_stake: 1, delegation_stake: 1, - quorum_stake_threshold: 1, + total_voting_power: 1, + quorum_threshold: 1, active_validators: validators, pending_validators: vec![], pending_removals: vec![], diff --git a/narwhal/config/src/lib.rs b/narwhal/config/src/lib.rs index 9495fc1018265..007c4b16574b3 100644 --- a/narwhal/config/src/lib.rs +++ b/narwhal/config/src/lib.rs @@ -96,6 +96,10 @@ pub trait Export: Serialize { impl Export for S {} +// TODO: the stake and voting power of a validator can be different so +// in some places when we are actually referring to the voting power, we +// should use a different type alias, field name, etc. +// Also, consider unify this with `StakeUnit` on Sui side. pub type Stake = u64; pub type WorkerId = u32;