Skip to content

Commit

Permalink
randomness #1: types update from randomnet (aptos-labs#12106)
Browse files Browse the repository at this point in the history
* types update from randomnet

* update

* lint

* lint
  • Loading branch information
zjma authored Feb 21, 2024
1 parent 122f51f commit 6c7e9d4
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 57 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ event-listener = "2.5.3"
fail = "0.5.0"
ff = "0.13"
field_count = "0.1.1"
fixed = "1.25.1"
flate2 = "1.0.24"
futures = "0.3.29"
futures-channel = "0.3.29"
Expand Down
42 changes: 39 additions & 3 deletions aptos-move/aptos-vm/src/aptos_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ use aptos_types::{
partitioner::PartitionedTransactions,
},
block_metadata::BlockMetadata,
block_metadata_ext::BlockMetadataExt,
block_metadata_ext::{BlockMetadataExt, BlockMetadataWithRandomness},
chain_id::ChainId,
fee_statement::FeeStatement,
move_utils::as_move_value::AsMoveValue,
on_chain_config::{
new_epoch_event_key, ConfigurationResource, FeatureFlag, Features, OnChainConfig,
TimedFeatureOverride, TimedFeatures, TimedFeaturesBuilder,
},
randomness::Randomness,
state_store::{StateView, TStateView},
transaction::{
authenticator::AnySignature, signature_verified_transaction::SignatureVerifiedTransaction,
Expand Down Expand Up @@ -1701,13 +1703,47 @@ impl AptosVM {
let mut session =
self.new_session(resolver, SessionId::block_meta_ext(&block_metadata_ext));

let args = serialize_values(&block_metadata_ext.get_prologue_ext_move_args());
let block_metadata_with_randomness = match block_metadata_ext {
BlockMetadataExt::V0(_) => unreachable!(),
BlockMetadataExt::V1(v1) => v1,
};

let BlockMetadataWithRandomness {
id,
epoch,
round,
proposer,
previous_block_votes_bitvec,
failed_proposer_indices,
timestamp_usecs,
randomness,
} = block_metadata_with_randomness;

let args = vec![
MoveValue::Signer(AccountAddress::ZERO), // Run as 0x0
MoveValue::Address(AccountAddress::from_bytes(id.to_vec()).unwrap()),
MoveValue::U64(epoch),
MoveValue::U64(round),
MoveValue::Address(proposer),
failed_proposer_indices
.into_iter()
.map(|i| i as u64)
.collect::<Vec<_>>()
.as_move_value(),
previous_block_votes_bitvec.as_move_value(),
MoveValue::U64(timestamp_usecs),
randomness
.as_ref()
.map(Randomness::randomness_cloned)
.as_move_value(),
];

session
.execute_function_bypass_visibility(
&BLOCK_MODULE,
BLOCK_PROLOGUE_EXT,
vec![],
args,
serialize_values(&args),
&mut gas_meter,
)
.map(|_return_vals| ())
Expand Down
2 changes: 2 additions & 0 deletions types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ anyhow = { workspace = true }
aptos-bitvec = { workspace = true }
aptos-crypto = { workspace = true }
aptos-crypto-derive = { workspace = true }
aptos-dkg = { workspace = true }
aptos-experimental-runtimes = { workspace = true }
ark-bn254 = { workspace = true }
ark-ff = { workspace = true }
Expand All @@ -28,6 +29,7 @@ bcs = { workspace = true }
bytes = { workspace = true }
chrono = { workspace = true }
derivative = { workspace = true }
fixed = { workspace = true }
hex = { workspace = true }
itertools = { workspace = true }
jsonwebtoken = { workspace = true }
Expand Down
59 changes: 9 additions & 50 deletions types/src/block_metadata_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{block_metadata::BlockMetadata, randomness::Randomness};
use aptos_crypto::HashValue;
use move_core_types::{account_address::AccountAddress, value::MoveValue};
use move_core_types::account_address::AccountAddress;
use serde::{Deserialize, Serialize};

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

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlockMetadataWithRandomness {
id: HashValue,
epoch: u64,
round: u64,
proposer: AccountAddress,
pub id: HashValue,
pub epoch: u64,
pub round: u64,
pub proposer: AccountAddress,
#[serde(with = "serde_bytes")]
previous_block_votes_bitvec: Vec<u8>,
failed_proposer_indices: Vec<u32>,
timestamp_usecs: u64,
randomness: Option<Randomness>,
pub previous_block_votes_bitvec: Vec<u8>,
pub failed_proposer_indices: Vec<u32>,
pub timestamp_usecs: u64,
pub randomness: Option<Randomness>,
}

impl BlockMetadataExt {
Expand Down Expand Up @@ -62,47 +62,6 @@ impl BlockMetadataExt {
}
}

pub fn get_prologue_ext_move_args(self) -> Vec<MoveValue> {
let mut ret = vec![
MoveValue::Signer(AccountAddress::ONE),
MoveValue::Address(AccountAddress::from_bytes(self.id().to_vec()).unwrap()),
MoveValue::U64(self.epoch()),
MoveValue::U64(self.round()),
MoveValue::Address(self.proposer()),
MoveValue::Vector(
self.failed_proposer_indices()
.iter()
.map(|x| MoveValue::U64((*x) as u64))
.collect(),
),
MoveValue::Vector(
self.previous_block_votes_bitvec()
.iter()
.map(|x| MoveValue::U8(*x))
.collect(),
),
MoveValue::U64(self.timestamp_usecs()),
];

match self.randomness() {
None => {
ret.push(MoveValue::Bool(false));
ret.push(MoveValue::Vector(vec![]));
},
Some(randomness) => {
let move_bytes = randomness
.randomness()
.iter()
.copied()
.map(MoveValue::U8)
.collect();
ret.push(MoveValue::Bool(true));
ret.push(MoveValue::Vector(move_bytes));
},
}
ret
}

pub fn timestamp_usecs(&self) -> u64 {
match self {
BlockMetadataExt::V0(obj) => obj.timestamp_usecs(),
Expand Down
15 changes: 13 additions & 2 deletions types/src/on_chain_config/aptos_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

use crate::on_chain_config::OnChainConfig;
use serde::{Deserialize, Serialize};

use strum_macros::FromRepr;
/// The feature flags define in the Move source. This must stay aligned with the constants there.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, FromRepr)]
#[allow(non_camel_case_types)]
pub enum FeatureFlag {
CODE_DEPENDENCY_CHECK = 1,
Expand Down Expand Up @@ -115,6 +115,17 @@ impl Features {
self.features[byte_index] &= !bit_mask;
}

pub fn into_flag_vec(self) -> Vec<FeatureFlag> {
let Self { features } = self;
features
.into_iter()
.flat_map(|byte| (0..8).map(move |bit_idx| byte & (1 << bit_idx) != 0))
.enumerate()
.filter(|(_feature_idx, enabled)| *enabled)
.map(|(feature_idx, _)| FeatureFlag::from_repr(feature_idx).unwrap())
.collect()
}

pub fn is_enabled(&self, flag: FeatureFlag) -> bool {
let val = flag as u64;
let byte_index = (val / 8) as usize;
Expand Down
4 changes: 4 additions & 0 deletions types/src/on_chain_config/consensus_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ impl OnChainConsensusConfig {
OnChainConsensusConfig::V3 { vtxn, .. } => vtxn.clone(),
}
}

pub fn is_vtxn_enabled(&self) -> bool {
self.effective_validator_txn_config().enabled()
}
}

/// This is used when on-chain config is not initialized.
Expand Down
68 changes: 67 additions & 1 deletion types/src/randomness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@
// Parts of the project are originally copyright © Meta Platforms, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::block_info::Round;
use crate::{block_info::Round, on_chain_config::OnChainConfig};
use aptos_crypto::HashValue;
use aptos_crypto_derive::SilentDebug;
use aptos_dkg::{weighted_vuf, weighted_vuf::traits::WeightedVUF};
use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};

pub type WVUF = weighted_vuf::pinkas::PinkasWUF;
pub type WvufPP = <WVUF as WeightedVUF>::PublicParameters;
pub type PK = <WVUF as WeightedVUF>::PubKey;
pub type SKShare = <WVUF as WeightedVUF>::SecretKeyShare;
pub type PKShare = <WVUF as WeightedVUF>::PubKeyShare;
pub type ASK = <WVUF as WeightedVUF>::AugmentedSecretKeyShare;
pub type APK = <WVUF as WeightedVUF>::AugmentedPubKeyShare;
pub type ProofShare = <WVUF as WeightedVUF>::ProofShare;
pub type Delta = <WVUF as WeightedVUF>::Delta;
pub type Evaluation = <WVUF as WeightedVUF>::Evaluation;
pub type Proof = <WVUF as WeightedVUF>::Proof;

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Hash)]
pub struct RandMetadataToSign {
pub epoch: u64,
Expand Down Expand Up @@ -79,6 +94,10 @@ impl Randomness {
pub fn randomness(&self) -> &[u8] {
&self.randomness
}

pub fn randomness_cloned(&self) -> Vec<u8> {
self.randomness.clone()
}
}

impl Default for Randomness {
Expand All @@ -91,3 +110,50 @@ impl Default for Randomness {
}
}
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct PerBlockRandomness {
pub epoch: u64,
pub round: u64,
pub seed: Option<Vec<u8>>,
}

impl OnChainConfig for PerBlockRandomness {
const MODULE_IDENTIFIER: &'static str = "randomness";
const TYPE_IDENTIFIER: &'static str = "PerBlockRandomness";
}

#[derive(Clone, SilentDebug)]
pub struct RandKeys {
// augmented secret / public key share of this validator, obtained from the DKG transcript of last epoch
pub ask: ASK,
pub apk: APK,
// certified augmented public key share of all validators,
// obtained from all validators in the new epoch,
// which necessary for verifying randomness shares
pub certified_apks: Vec<OnceCell<APK>>,
// public key share of all validators, obtained from the DKG transcript of last epoch
pub pk_shares: Vec<PKShare>,
}

impl RandKeys {
pub fn new(ask: ASK, apk: APK, pk_shares: Vec<PKShare>, num_validators: usize) -> Self {
let certified_apks = vec![OnceCell::new(); num_validators];

Self {
ask,
apk,
certified_apks,
pk_shares,
}
}

pub fn add_certified_apk(&self, index: usize, apk: APK) -> anyhow::Result<()> {
assert!(index < self.certified_apks.len());
if self.certified_apks[index].get().is_some() {
return Ok(());
}
self.certified_apks[index].set(apk).unwrap();
Ok(())
}
}
9 changes: 8 additions & 1 deletion types/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,14 @@ impl Transaction {

pub fn try_as_block_metadata(&self) -> Option<&BlockMetadata> {
match self {
Transaction::BlockMetadata(v1) => Some(v1),
Transaction::BlockMetadata(bm) => Some(bm),
_ => None,
}
}

pub fn try_as_block_metadata_ext(&self) -> Option<&BlockMetadataExt> {
match self {
Transaction::BlockMetadataExt(bme) => Some(bme),
_ => None,
}
}
Expand Down

0 comments on commit 6c7e9d4

Please sign in to comment.