Skip to content

Commit

Permalink
Merge pull request #710 from radixdlt/feature/round-updates
Browse files Browse the repository at this point in the history
Feature: Epoch Round Updates
  • Loading branch information
talekhinezh authored Jan 5, 2023
2 parents 8ba831e + 6e474b1 commit 7ebab2c
Show file tree
Hide file tree
Showing 22 changed files with 297 additions and 55 deletions.
12 changes: 12 additions & 0 deletions examples/hello-world/Cargo.lock

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

12 changes: 12 additions & 0 deletions examples/no-std/Cargo.lock

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

3 changes: 2 additions & 1 deletion radix-engine-interface/src/api/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ pub trait InvokableModel<E>:
Invokable<ParsedScryptoInvocation, E>
+ Invokable<ScryptoInvocation, E>
+ Invokable<EpochManagerCreateInvocation, E>
+ Invokable<EpochManagerSetEpochInvocation, E>
+ Invokable<EpochManagerNextRoundInvocation, E>
+ Invokable<EpochManagerGetCurrentEpochInvocation, E>
+ Invokable<EpochManagerSetEpochInvocation, E>
+ Invokable<ClockCreateInvocation, E>
+ Invokable<ClockSetCurrentTimeInvocation, E>
+ Invokable<ClockGetCurrentTimeInvocation, E>
Expand Down
1 change: 1 addition & 0 deletions radix-engine-interface/src/api/types/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ pub enum PackageMethod {
#[strum(serialize_all = "snake_case")]
pub enum EpochManagerMethod {
GetCurrentEpoch,
NextRound,
SetEpoch,
}

Expand Down
2 changes: 1 addition & 1 deletion radix-engine-interface/src/crypto/ecdsa_secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::scrypto_type;

/// Represents an ECDSA public key.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct EcdsaSecp256k1PublicKey(
#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] pub [u8; Self::LENGTH],
);
Expand Down
27 changes: 25 additions & 2 deletions radix-engine-interface/src/model/epoch_manager/invocations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use radix_engine_interface::crypto::EcdsaSecp256k1PublicKey;
use sbor::rust::collections::HashSet;
use sbor::rust::fmt::Debug;
use sbor::rust::vec::Vec;
use sbor::*;

use crate::api::api::*;
Expand All @@ -11,7 +11,9 @@ use crate::wasm::*;
#[derive(Debug, Clone, Eq, PartialEq)]
#[scrypto(TypeId, Encode, Decode)]
pub struct EpochManagerCreateInvocation {
pub validator_set: Vec<EcdsaSecp256k1PublicKey>,
pub validator_set: HashSet<EcdsaSecp256k1PublicKey>,
pub initial_epoch: u64,
pub rounds_per_epoch: u64,
}

impl Invocation for EpochManagerCreateInvocation {
Expand Down Expand Up @@ -68,3 +70,24 @@ impl Into<SerializedInvocation> for EpochManagerSetEpochInvocation {
NativeInvocation::EpochManager(EpochManagerInvocation::SetEpoch(self)).into()
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[scrypto(TypeId, Encode, Decode)]
pub struct EpochManagerNextRoundInvocation {
pub receiver: SystemAddress,
pub round: u64,
}

impl Invocation for EpochManagerNextRoundInvocation {
type Output = ();
}

impl SerializableInvocation for EpochManagerNextRoundInvocation {
type ScryptoOutput = ();
}

impl Into<SerializedInvocation> for EpochManagerNextRoundInvocation {
fn into(self) -> SerializedInvocation {
NativeInvocation::EpochManager(EpochManagerInvocation::NextRound(self)).into()
}
}
4 changes: 4 additions & 0 deletions radix-engine-interface/src/model/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub enum EpochManagerInvocation {
Create(EpochManagerCreateInvocation),
GetCurrentEpoch(EpochManagerGetCurrentEpochInvocation),
SetEpoch(EpochManagerSetEpochInvocation),
NextRound(EpochManagerNextRoundInvocation),
}

#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -340,6 +341,9 @@ impl NativeInvocation {
EpochManagerInvocation::GetCurrentEpoch(invocation) => {
refs.insert(RENodeId::Global(GlobalAddress::System(invocation.receiver)));
}
EpochManagerInvocation::NextRound(invocation) => {
refs.insert(RENodeId::Global(GlobalAddress::System(invocation.receiver)));
}
EpochManagerInvocation::SetEpoch(invocation) => {
refs.insert(RENodeId::Global(GlobalAddress::System(invocation.receiver)));
}
Expand Down
10 changes: 1 addition & 9 deletions radix-engine-stores/src/memory_db.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use radix_engine::engine::ScryptoInterpreter;
use radix_engine::ledger::{
bootstrap, OutputValue, QueryableSubstateStore, ReadableSubstateStore, WriteableSubstateStore,
OutputValue, QueryableSubstateStore, ReadableSubstateStore, WriteableSubstateStore,
};
use radix_engine::model::PersistedSubstate;
use radix_engine::types::*;
use radix_engine::wasm::WasmEngine;
use radix_engine_interface::api::types::RENodeId;

/// A substate store that stores all typed substates in host memory.
Expand All @@ -19,12 +17,6 @@ impl SerializedInMemorySubstateStore {
substates: HashMap::new(),
}
}

pub fn with_bootstrap<W: WasmEngine>(scrypto_interpreter: &ScryptoInterpreter<W>) -> Self {
let mut substate_store = Self::new();
bootstrap(&mut substate_store, scrypto_interpreter);
substate_store
}
}

impl Default for SerializedInMemorySubstateStore {
Expand Down
11 changes: 6 additions & 5 deletions radix-engine/src/engine/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ impl<'s> FinalizingTrack<'s> {
// Commit/rollback application state changes
let mut to_persist = HashMap::new();
let mut application_logs = Vec::new();
let mut next_validator_set = None;
let mut next_epoch = None;
let new_global_addresses = if is_success {
for (id, loaded) in self.loaded_substates {
let old_version = match &loaded.metastate {
Expand All @@ -604,9 +604,10 @@ impl<'s> FinalizingTrack<'s> {
state: ExistingMetaState::Updated(..),
..
} => {
let validator_set =
loaded.substate.validator_set().validator_set.clone();
next_validator_set = Some(validator_set);
let validator_set = loaded.substate.validator_set();
let epoch = validator_set.epoch;
let validator_set = validator_set.validator_set.clone();
next_epoch = Some((validator_set, epoch));
}
_ => {}
}
Expand Down Expand Up @@ -748,7 +749,7 @@ impl<'s> FinalizingTrack<'s> {
entity_changes: EntityChanges::new(new_global_addresses),
resource_changes: execution_trace_receipt.resource_changes,
application_logs,
next_validator_set,
next_epoch,
})
}

Expand Down
1 change: 1 addition & 0 deletions radix-engine/src/fee/fee_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ impl FeeTable {
}
NativeMethod::EpochManager(epoch_manager_method) => match epoch_manager_method {
EpochManagerMethod::GetCurrentEpoch => self.fixed_low,
EpochManagerMethod::NextRound => self.fixed_low,
EpochManagerMethod::SetEpoch => self.fixed_low,
},
NativeMethod::Clock(clock_method) => match clock_method {
Expand Down
35 changes: 26 additions & 9 deletions radix-engine/src/ledger/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ pub struct GenesisReceipt {
pub eddsa_ed25519_token: ResourceAddress,
}

pub fn create_genesis(validator_set: Vec<EcdsaSecp256k1PublicKey>) -> SystemTransaction {
pub fn create_genesis(
validator_set: HashSet<EcdsaSecp256k1PublicKey>,
initial_epoch: u64,
rounds_per_epoch: u64,
) -> SystemTransaction {
let mut blobs = Vec::new();
let mut id_allocator = ManifestIdAllocator::new();
let create_faucet_package = {
Expand Down Expand Up @@ -144,7 +148,11 @@ pub fn create_genesis(validator_set: Vec<EcdsaSecp256k1PublicKey>) -> SystemTran
};

let create_epoch_manager = NativeInvocation::EpochManager(EpochManagerInvocation::Create(
EpochManagerCreateInvocation { validator_set },
EpochManagerCreateInvocation {
validator_set,
initial_epoch,
rounds_per_epoch,
},
));

let create_clock = NativeInvocation::Clock(ClockInvocation::Create(ClockCreateInvocation {}));
Expand Down Expand Up @@ -217,13 +225,21 @@ where
S: ReadableSubstateStore + WriteableSubstateStore,
W: WasmEngine,
{
bootstrap_with_validator_set(substate_store, scrypto_interpreter, Vec::new())
bootstrap_with_validator_set(
substate_store,
scrypto_interpreter,
HashSet::new(),
1u64,
1u64,
)
}

pub fn bootstrap_with_validator_set<S, W>(
substate_store: &mut S,
scrypto_interpreter: &ScryptoInterpreter<W>,
validator_set: Vec<EcdsaSecp256k1PublicKey>,
validator_set: HashSet<EcdsaSecp256k1PublicKey>,
initial_epoch: u64,
rounds_per_epoch: u64,
) -> Option<TransactionReceipt>
where
S: ReadableSubstateStore + WriteableSubstateStore,
Expand All @@ -236,7 +252,7 @@ where
))
.is_none()
{
let genesis_transaction = create_genesis(validator_set);
let genesis_transaction = create_genesis(validator_set, initial_epoch, rounds_per_epoch);

let transaction_receipt = execute_transaction(
substate_store,
Expand Down Expand Up @@ -266,8 +282,9 @@ mod tests {
fn bootstrap_receipt_should_match_constants() {
let scrypto_interpreter = ScryptoInterpreter::<DefaultWasmEngine>::default();
let substate_store = TypedInMemorySubstateStore::new();
let initial_validator_set = vec![EcdsaSecp256k1PublicKey([0; 33])];
let genesis_transaction = create_genesis(initial_validator_set.clone());
let mut initial_validator_set = HashSet::new();
initial_validator_set.insert(EcdsaSecp256k1PublicKey([0; 33]));
let genesis_transaction = create_genesis(initial_validator_set.clone(), 1u64, 1u64);

let transaction_receipt = execute_transaction(
&substate_store,
Expand All @@ -282,10 +299,10 @@ mod tests {
let validator_set = transaction_receipt
.result
.expect_commit()
.next_validator_set
.next_epoch
.as_ref()
.expect("Should contain validator set");
assert_eq!(validator_set, &initial_validator_set);
assert_eq!(validator_set, &(initial_validator_set, 1u64));

let genesis_receipt = genesis_result(&transaction_receipt);

Expand Down
Loading

0 comments on commit 7ebab2c

Please sign in to comment.