Skip to content

Commit 6c7e9d4

Browse files
authored
randomness #1: types update from randomnet (aptos-labs#12106)
* types update from randomnet * update * lint * lint
1 parent 122f51f commit 6c7e9d4

File tree

9 files changed

+163
-57
lines changed

9 files changed

+163
-57
lines changed

Cargo.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ event-listener = "2.5.3"
526526
fail = "0.5.0"
527527
ff = "0.13"
528528
field_count = "0.1.1"
529+
fixed = "1.25.1"
529530
flate2 = "1.0.24"
530531
futures = "0.3.29"
531532
futures-channel = "0.3.29"

aptos-move/aptos-vm/src/aptos_vm.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ use aptos_types::{
3838
partitioner::PartitionedTransactions,
3939
},
4040
block_metadata::BlockMetadata,
41-
block_metadata_ext::BlockMetadataExt,
41+
block_metadata_ext::{BlockMetadataExt, BlockMetadataWithRandomness},
4242
chain_id::ChainId,
4343
fee_statement::FeeStatement,
44+
move_utils::as_move_value::AsMoveValue,
4445
on_chain_config::{
4546
new_epoch_event_key, ConfigurationResource, FeatureFlag, Features, OnChainConfig,
4647
TimedFeatureOverride, TimedFeatures, TimedFeaturesBuilder,
4748
},
49+
randomness::Randomness,
4850
state_store::{StateView, TStateView},
4951
transaction::{
5052
authenticator::AnySignature, signature_verified_transaction::SignatureVerifiedTransaction,
@@ -1701,13 +1703,47 @@ impl AptosVM {
17011703
let mut session =
17021704
self.new_session(resolver, SessionId::block_meta_ext(&block_metadata_ext));
17031705

1704-
let args = serialize_values(&block_metadata_ext.get_prologue_ext_move_args());
1706+
let block_metadata_with_randomness = match block_metadata_ext {
1707+
BlockMetadataExt::V0(_) => unreachable!(),
1708+
BlockMetadataExt::V1(v1) => v1,
1709+
};
1710+
1711+
let BlockMetadataWithRandomness {
1712+
id,
1713+
epoch,
1714+
round,
1715+
proposer,
1716+
previous_block_votes_bitvec,
1717+
failed_proposer_indices,
1718+
timestamp_usecs,
1719+
randomness,
1720+
} = block_metadata_with_randomness;
1721+
1722+
let args = vec![
1723+
MoveValue::Signer(AccountAddress::ZERO), // Run as 0x0
1724+
MoveValue::Address(AccountAddress::from_bytes(id.to_vec()).unwrap()),
1725+
MoveValue::U64(epoch),
1726+
MoveValue::U64(round),
1727+
MoveValue::Address(proposer),
1728+
failed_proposer_indices
1729+
.into_iter()
1730+
.map(|i| i as u64)
1731+
.collect::<Vec<_>>()
1732+
.as_move_value(),
1733+
previous_block_votes_bitvec.as_move_value(),
1734+
MoveValue::U64(timestamp_usecs),
1735+
randomness
1736+
.as_ref()
1737+
.map(Randomness::randomness_cloned)
1738+
.as_move_value(),
1739+
];
1740+
17051741
session
17061742
.execute_function_bypass_visibility(
17071743
&BLOCK_MODULE,
17081744
BLOCK_PROLOGUE_EXT,
17091745
vec![],
1710-
args,
1746+
serialize_values(&args),
17111747
&mut gas_meter,
17121748
)
17131749
.map(|_return_vals| ())

types/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ anyhow = { workspace = true }
1717
aptos-bitvec = { workspace = true }
1818
aptos-crypto = { workspace = true }
1919
aptos-crypto-derive = { workspace = true }
20+
aptos-dkg = { workspace = true }
2021
aptos-experimental-runtimes = { workspace = true }
2122
ark-bn254 = { workspace = true }
2223
ark-ff = { workspace = true }
@@ -28,6 +29,7 @@ bcs = { workspace = true }
2829
bytes = { workspace = true }
2930
chrono = { workspace = true }
3031
derivative = { workspace = true }
32+
fixed = { workspace = true }
3133
hex = { workspace = true }
3234
itertools = { workspace = true }
3335
jsonwebtoken = { workspace = true }

types/src/block_metadata_ext.rs

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use crate::{block_metadata::BlockMetadata, randomness::Randomness};
44
use aptos_crypto::HashValue;
5-
use move_core_types::{account_address::AccountAddress, value::MoveValue};
5+
use move_core_types::account_address::AccountAddress;
66
use serde::{Deserialize, Serialize};
77

88
/// The extended block metadata.
@@ -21,15 +21,15 @@ pub enum BlockMetadataExt {
2121

2222
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
2323
pub struct BlockMetadataWithRandomness {
24-
id: HashValue,
25-
epoch: u64,
26-
round: u64,
27-
proposer: AccountAddress,
24+
pub id: HashValue,
25+
pub epoch: u64,
26+
pub round: u64,
27+
pub proposer: AccountAddress,
2828
#[serde(with = "serde_bytes")]
29-
previous_block_votes_bitvec: Vec<u8>,
30-
failed_proposer_indices: Vec<u32>,
31-
timestamp_usecs: u64,
32-
randomness: Option<Randomness>,
29+
pub previous_block_votes_bitvec: Vec<u8>,
30+
pub failed_proposer_indices: Vec<u32>,
31+
pub timestamp_usecs: u64,
32+
pub randomness: Option<Randomness>,
3333
}
3434

3535
impl BlockMetadataExt {
@@ -62,47 +62,6 @@ impl BlockMetadataExt {
6262
}
6363
}
6464

65-
pub fn get_prologue_ext_move_args(self) -> Vec<MoveValue> {
66-
let mut ret = vec![
67-
MoveValue::Signer(AccountAddress::ONE),
68-
MoveValue::Address(AccountAddress::from_bytes(self.id().to_vec()).unwrap()),
69-
MoveValue::U64(self.epoch()),
70-
MoveValue::U64(self.round()),
71-
MoveValue::Address(self.proposer()),
72-
MoveValue::Vector(
73-
self.failed_proposer_indices()
74-
.iter()
75-
.map(|x| MoveValue::U64((*x) as u64))
76-
.collect(),
77-
),
78-
MoveValue::Vector(
79-
self.previous_block_votes_bitvec()
80-
.iter()
81-
.map(|x| MoveValue::U8(*x))
82-
.collect(),
83-
),
84-
MoveValue::U64(self.timestamp_usecs()),
85-
];
86-
87-
match self.randomness() {
88-
None => {
89-
ret.push(MoveValue::Bool(false));
90-
ret.push(MoveValue::Vector(vec![]));
91-
},
92-
Some(randomness) => {
93-
let move_bytes = randomness
94-
.randomness()
95-
.iter()
96-
.copied()
97-
.map(MoveValue::U8)
98-
.collect();
99-
ret.push(MoveValue::Bool(true));
100-
ret.push(MoveValue::Vector(move_bytes));
101-
},
102-
}
103-
ret
104-
}
105-
10665
pub fn timestamp_usecs(&self) -> u64 {
10766
match self {
10867
BlockMetadataExt::V0(obj) => obj.timestamp_usecs(),

types/src/on_chain_config/aptos_features.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
use crate::on_chain_config::OnChainConfig;
55
use serde::{Deserialize, Serialize};
6-
6+
use strum_macros::FromRepr;
77
/// The feature flags define in the Move source. This must stay aligned with the constants there.
8-
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
8+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, FromRepr)]
99
#[allow(non_camel_case_types)]
1010
pub enum FeatureFlag {
1111
CODE_DEPENDENCY_CHECK = 1,
@@ -115,6 +115,17 @@ impl Features {
115115
self.features[byte_index] &= !bit_mask;
116116
}
117117

118+
pub fn into_flag_vec(self) -> Vec<FeatureFlag> {
119+
let Self { features } = self;
120+
features
121+
.into_iter()
122+
.flat_map(|byte| (0..8).map(move |bit_idx| byte & (1 << bit_idx) != 0))
123+
.enumerate()
124+
.filter(|(_feature_idx, enabled)| *enabled)
125+
.map(|(feature_idx, _)| FeatureFlag::from_repr(feature_idx).unwrap())
126+
.collect()
127+
}
128+
118129
pub fn is_enabled(&self, flag: FeatureFlag) -> bool {
119130
let val = flag as u64;
120131
let byte_index = (val / 8) as usize;

types/src/on_chain_config/consensus_config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ impl OnChainConsensusConfig {
237237
OnChainConsensusConfig::V3 { vtxn, .. } => vtxn.clone(),
238238
}
239239
}
240+
241+
pub fn is_vtxn_enabled(&self) -> bool {
242+
self.effective_validator_txn_config().enabled()
243+
}
240244
}
241245

242246
/// This is used when on-chain config is not initialized.

types/src/randomness.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@
22
// Parts of the project are originally copyright © Meta Platforms, Inc.
33
// SPDX-License-Identifier: Apache-2.0
44

5-
use crate::block_info::Round;
5+
use crate::{block_info::Round, on_chain_config::OnChainConfig};
66
use aptos_crypto::HashValue;
7+
use aptos_crypto_derive::SilentDebug;
8+
use aptos_dkg::{weighted_vuf, weighted_vuf::traits::WeightedVUF};
9+
use once_cell::sync::OnceCell;
710
use serde::{Deserialize, Serialize};
811

12+
pub type WVUF = weighted_vuf::pinkas::PinkasWUF;
13+
pub type WvufPP = <WVUF as WeightedVUF>::PublicParameters;
14+
pub type PK = <WVUF as WeightedVUF>::PubKey;
15+
pub type SKShare = <WVUF as WeightedVUF>::SecretKeyShare;
16+
pub type PKShare = <WVUF as WeightedVUF>::PubKeyShare;
17+
pub type ASK = <WVUF as WeightedVUF>::AugmentedSecretKeyShare;
18+
pub type APK = <WVUF as WeightedVUF>::AugmentedPubKeyShare;
19+
pub type ProofShare = <WVUF as WeightedVUF>::ProofShare;
20+
pub type Delta = <WVUF as WeightedVUF>::Delta;
21+
pub type Evaluation = <WVUF as WeightedVUF>::Evaluation;
22+
pub type Proof = <WVUF as WeightedVUF>::Proof;
23+
924
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Hash)]
1025
pub struct RandMetadataToSign {
1126
pub epoch: u64,
@@ -79,6 +94,10 @@ impl Randomness {
7994
pub fn randomness(&self) -> &[u8] {
8095
&self.randomness
8196
}
97+
98+
pub fn randomness_cloned(&self) -> Vec<u8> {
99+
self.randomness.clone()
100+
}
82101
}
83102

84103
impl Default for Randomness {
@@ -91,3 +110,50 @@ impl Default for Randomness {
91110
}
92111
}
93112
}
113+
114+
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
115+
pub struct PerBlockRandomness {
116+
pub epoch: u64,
117+
pub round: u64,
118+
pub seed: Option<Vec<u8>>,
119+
}
120+
121+
impl OnChainConfig for PerBlockRandomness {
122+
const MODULE_IDENTIFIER: &'static str = "randomness";
123+
const TYPE_IDENTIFIER: &'static str = "PerBlockRandomness";
124+
}
125+
126+
#[derive(Clone, SilentDebug)]
127+
pub struct RandKeys {
128+
// augmented secret / public key share of this validator, obtained from the DKG transcript of last epoch
129+
pub ask: ASK,
130+
pub apk: APK,
131+
// certified augmented public key share of all validators,
132+
// obtained from all validators in the new epoch,
133+
// which necessary for verifying randomness shares
134+
pub certified_apks: Vec<OnceCell<APK>>,
135+
// public key share of all validators, obtained from the DKG transcript of last epoch
136+
pub pk_shares: Vec<PKShare>,
137+
}
138+
139+
impl RandKeys {
140+
pub fn new(ask: ASK, apk: APK, pk_shares: Vec<PKShare>, num_validators: usize) -> Self {
141+
let certified_apks = vec![OnceCell::new(); num_validators];
142+
143+
Self {
144+
ask,
145+
apk,
146+
certified_apks,
147+
pk_shares,
148+
}
149+
}
150+
151+
pub fn add_certified_apk(&self, index: usize, apk: APK) -> anyhow::Result<()> {
152+
assert!(index < self.certified_apks.len());
153+
if self.certified_apks[index].get().is_some() {
154+
return Ok(());
155+
}
156+
self.certified_apks[index].set(apk).unwrap();
157+
Ok(())
158+
}
159+
}

types/src/transaction/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1817,7 +1817,14 @@ impl Transaction {
18171817

18181818
pub fn try_as_block_metadata(&self) -> Option<&BlockMetadata> {
18191819
match self {
1820-
Transaction::BlockMetadata(v1) => Some(v1),
1820+
Transaction::BlockMetadata(bm) => Some(bm),
1821+
_ => None,
1822+
}
1823+
}
1824+
1825+
pub fn try_as_block_metadata_ext(&self) -> Option<&BlockMetadataExt> {
1826+
match self {
1827+
Transaction::BlockMetadataExt(bme) => Some(bme),
18211828
_ => None,
18221829
}
18231830
}

0 commit comments

Comments
 (0)