Skip to content

Commit

Permalink
[eventv2] make v0/v1 to v1/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
lightmark authored and msmouse committed Aug 21, 2023
1 parent ed99bf0 commit 0ba554c
Show file tree
Hide file tree
Showing 38 changed files with 195 additions and 136 deletions.
24 changes: 12 additions & 12 deletions api/types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,16 +536,16 @@ pub struct Event {
impl From<(&ContractEvent, serde_json::Value)> for Event {
fn from((event, data): (&ContractEvent, serde_json::Value)) -> Self {
match event {
ContractEvent::V0(v0) => Self {
guid: (*v0.key()).into(),
sequence_number: v0.sequence_number().into(),
typ: v0.type_tag().clone().into(),
ContractEvent::V1(v1) => Self {
guid: (*v1.key()).into(),
sequence_number: v1.sequence_number().into(),
typ: v1.type_tag().clone().into(),
data,
},
ContractEvent::V1(v1) => Self {
ContractEvent::V2(v2) => Self {
guid: *DUMMY_GUID,
sequence_number: *DUMMY_SEQUENCE_NUMBER,
typ: v1.type_tag().clone().into(),
typ: v2.type_tag().clone().into(),
data,
},
}
Expand All @@ -570,18 +570,18 @@ pub struct VersionedEvent {
impl From<(&EventWithVersion, serde_json::Value)> for VersionedEvent {
fn from((event, data): (&EventWithVersion, serde_json::Value)) -> Self {
match &event.event {
ContractEvent::V0(v0) => Self {
ContractEvent::V1(v1) => Self {
version: event.transaction_version.into(),
guid: (*v0.key()).into(),
sequence_number: v0.sequence_number().into(),
typ: v0.type_tag().clone().into(),
guid: (*v1.key()).into(),
sequence_number: v1.sequence_number().into(),
typ: v1.type_tag().clone().into(),
data,
},
ContractEvent::V1(v1) => Self {
ContractEvent::V2(v2) => Self {
version: event.transaction_version.into(),
guid: *DUMMY_GUID,
sequence_number: *DUMMY_SEQUENCE_NUMBER,
typ: v1.type_tag().clone().into(),
typ: v2.type_tag().clone().into(),
data,
},
}
Expand Down
4 changes: 2 additions & 2 deletions aptos-move/aptos-debugger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ impl AptosDebugger {
fn is_reconfiguration(vm_output: &TransactionOutput) -> bool {
let new_epoch_event_key = aptos_types::on_chain_config::new_epoch_event_key();
vm_output.events().iter().any(|event| {
if let ContractEvent::V0(v0) = event {
*v0.key() == new_epoch_event_key
if let ContractEvent::V1(v1) = event {
*v1.key() == new_epoch_event_key
} else {
false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -935,11 +935,11 @@ impl<'a> fmt::Display for PrettyEvent<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "{{")?;
match self.0 {
ContractEvent::V0(v0) => {
writeln!(f, " key: {}", v0.key())?;
writeln!(f, " seq_num: {}", v0.sequence_number())?;
ContractEvent::V1(v1) => {
writeln!(f, " key: {}", v1.key())?;
writeln!(f, " seq_num: {}", v1.sequence_number())?;
},
ContractEvent::V1(_v1) => (),
ContractEvent::V2(_v2) => (),
}
writeln!(f, " type: {}", self.0.type_tag())?;
writeln!(f, " data: {:?}", hex::encode(self.0.event_data()))?;
Expand Down
12 changes: 6 additions & 6 deletions aptos-move/aptos-vm/src/aptos_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,15 +1252,15 @@ impl AptosVM {
log_context: &AdapterLogSchema,
) -> Result<(), VMStatus> {
let has_new_block_event = change_set.events().iter().any(|e| {
if let ContractEvent::V0(v0) = e {
*v0.key() == new_block_event_key()
if let ContractEvent::V1(v1) = e {
*v1.key() == new_block_event_key()
} else {
false
}
});
let has_new_epoch_event = change_set.events().iter().any(|e| {
if let ContractEvent::V0(v0) = e {
*v0.key() == new_epoch_event_key()
if let ContractEvent::V1(v1) = e {
*v1.key() == new_epoch_event_key()
} else {
false
}
Expand Down Expand Up @@ -1647,8 +1647,8 @@ impl VMAdapter for AptosVM {
fn should_restart_execution(vm_output: &VMOutput) -> bool {
let new_epoch_event_key = aptos_types::on_chain_config::new_epoch_event_key();
vm_output.change_set().events().iter().any(|event| {
if let ContractEvent::V0(v0) = event {
*v0.key() == new_epoch_event_key
if let ContractEvent::V1(v1) = event {
*v1.key() == new_epoch_event_key
} else {
false
}
Expand Down
2 changes: 2 additions & 0 deletions aptos-move/aptos-vm/src/natives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#[cfg(feature = "testing")]
use aptos_framework::natives::cryptography::algebra::AlgebraContext;
#[cfg(feature = "testing")]
use aptos_framework::natives::event::NativeEventContext;
use aptos_gas_schedule::{MiscGasParameters, NativeGasParameters, LATEST_GAS_FEATURE_VERSION};
use aptos_native_interface::SafeNativeBuilder;
#[cfg(feature = "testing")]
Expand Down
2 changes: 1 addition & 1 deletion aptos-move/e2e-tests/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ impl FakeExecutor {

// Check if we emit the expected event for block metadata, there might be more events for transaction fees.
let event = outputs[0].events()[0]
.v0()
.v1()
.expect("The first event must be a block metadata v0 event")
.clone();
assert_eq!(event.key(), &new_block_event_key());
Expand Down
2 changes: 1 addition & 1 deletion aptos-move/e2e-testsuite/src/tests/peer_to_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn single_peer_to_peer_with_event() {
let sent_ev_path = sender.sent_events_key();
for event in output.events() {
assert!(
rec_ev_path == event.v0().unwrap().key() || sent_ev_path == event.v0().unwrap().key()
rec_ev_path == event.v1().unwrap().key() || sent_ev_path == event.v1().unwrap().key()
);
}
}
Expand Down
27 changes: 27 additions & 0 deletions aptos-move/framework/aptos-framework/doc/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ events emitted to a handle and emit events to the event store.
- [Function `write_to_event_store`](#0x1_event_write_to_event_store)
- [Function `destroy_handle`](#0x1_event_destroy_handle)
- [Specification](#@Specification_1)
- [Function `write_to_module_event_store`](#@Specification_1_write_to_module_event_store)
- [Function `emit_event`](#@Specification_1_emit_event)
- [Function `guid`](#@Specification_1_guid)
- [Function `counter`](#@Specification_1_counter)
Expand All @@ -37,6 +38,9 @@ events emitted to a handle and emit events to the event store.

## Struct `EventHandle`

A handle for an event such that:
1. Other modules can emit events to this handle.
2. Storage can use this handle to prove the total number of events that happened in the past.


<pre><code>#[deprecated]
Expand Down Expand Up @@ -134,6 +138,7 @@ Log <code>msg</code> with the event stream identified by <code>T</code>

## Function `new_event_handle`

Use EventHandleGenerator to generate a unique event handle for <code>sig</code>


<pre><code>#[deprecated]
Expand Down Expand Up @@ -162,6 +167,7 @@ Log <code>msg</code> with the event stream identified by <code>T</code>

## Function `emit_event`

Emit an event with payload <code>msg</code> by using <code>handle_ref</code>'s key and counter.


<pre><code>#[deprecated]
Expand Down Expand Up @@ -191,6 +197,7 @@ Log <code>msg</code> with the event stream identified by <code>T</code>

## Function `guid`

Return the GUID associated with this EventHandle


<pre><code>#[deprecated]
Expand All @@ -216,6 +223,7 @@ Log <code>msg</code> with the event stream identified by <code>T</code>

## Function `counter`

Return the current counter associated with this EventHandle


<pre><code>#[deprecated]
Expand All @@ -241,6 +249,7 @@ Log <code>msg</code> with the event stream identified by <code>T</code>

## Function `write_to_event_store`

Log <code>msg</code> as the <code>count</code>th event associated with the event stream identified by <code><a href="guid.md#0x1_guid">guid</a></code>


<pre><code>#[deprecated]
Expand All @@ -264,6 +273,7 @@ Log <code>msg</code> with the event stream identified by <code>T</code>

## Function `destroy_handle`

Destroy a unique handle.


<pre><code>#[deprecated]
Expand Down Expand Up @@ -297,6 +307,23 @@ Log <code>msg</code> with the event stream identified by <code>T</code>



<a name="@Specification_1_write_to_module_event_store"></a>

### Function `write_to_module_event_store`


<pre><code><b>fun</b> <a href="event.md#0x1_event_write_to_module_event_store">write_to_module_event_store</a>&lt;T: drop, store&gt;(msg: &T)
</code></pre>


Native function use opaque.


<pre><code><b>pragma</b> opaque;
</code></pre>



<a name="@Specification_1_emit_event"></a>

### Function `emit_event`
Expand Down
4 changes: 1 addition & 3 deletions aptos-move/framework/aptos-framework/sources/event.move
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ module aptos_framework::event {
const ENOT_EVENT_STRUCT: u64 = 1;

/// Emit an event with payload `msg` by using `handle_ref`'s key and counter.
public fun emit<T: drop + store>(msg: &T) {
write_to_module_event_store<T>(msg);
}
public fun emit<T: store + drop>(msg: &T) { write_to_module_event_store<T>(msg); }

/// Log `msg` with the event stream identified by `T`
native fun write_to_module_event_store<T: drop + store>(msg: &T);
Expand Down
5 changes: 5 additions & 0 deletions aptos-move/framework/aptos-framework/sources/event.spec.move
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ spec aptos_framework::event {
ensures [concrete] handle_ref.counter == old(handle_ref.counter) + 1;
}

/// Native function use opaque.
spec write_to_module_event_store<T: drop + store>(msg: &T) {
pragma opaque;
}

/// Native function use opaque.
spec write_to_event_store<T: drop + store>(guid: vector<u8>, count: u64, msg: T) {
pragma opaque;
Expand Down
4 changes: 2 additions & 2 deletions aptos-move/framework/src/natives/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl NativeEventContext {
) -> PartialVMResult<Vec<&[u8]>> {
let mut events = vec![];
for event in self.events.iter() {
if let ContractEvent::V0(e) = event {
if let ContractEvent::V1(e) = event {
if e.key() == event_key && e.type_tag() == ty_tag {
events.push(e.event_data());
}
Expand Down Expand Up @@ -101,7 +101,7 @@ fn native_write_to_event_store(

let ctx = context.extensions_mut().get_mut::<NativeEventContext>();
ctx.events
.push(ContractEvent::new_v0(key, seq_num, ty_tag, blob));
.push(ContractEvent::new_v1(key, seq_num, ty_tag, blob));
Ok(smallvec![])
}

Expand Down
10 changes: 5 additions & 5 deletions aptos-move/vm-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use aptos_gas_schedule::{
use aptos_types::{
account_config::{self, aptos_test_root_address, events::NewEpochEvent, CORE_CODE_ADDRESS},
chain_id::ChainId,
contract_event::{ContractEvent, ContractEventV0},
contract_event::{ContractEvent, ContractEventV1},
on_chain_config::{
FeatureFlag, Features, GasScheduleV2, OnChainConsensusConfig, OnChainExecutionConfig,
TimedFeatures, APTOS_MAX_KNOWN_VERSION,
Expand Down Expand Up @@ -630,12 +630,12 @@ fn emit_new_block_and_epoch_event(session: &mut SessionExt) {

/// Verify the consistency of the genesis `WriteSet`
fn verify_genesis_write_set(events: &[ContractEvent]) {
let new_epoch_events: Vec<&ContractEventV0> = events
let new_epoch_events: Vec<&ContractEventV1> = events
.iter()
.filter_map(|e| {
if let ContractEvent::V0(v0) = e {
if v0.key() == &NewEpochEvent::event_key() {
Some(v0)
if let ContractEvent::V1(v1) = e {
if v1.key() == &NewEpochEvent::event_key() {
Some(v1)
} else {
None
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/liveness/leader_reputation_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ impl MockDbReader {

self.events.lock().push(EventWithVersion::new(
*idx,
ContractEvent::new_v0(
ContractEvent::new_v1(
new_block_event_key(),
*idx,
TypeTag::Struct(Box::new(NewBlockEvent::struct_tag())),
Expand Down
2 changes: 1 addition & 1 deletion crates/aptos-rest-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ impl Client {
.into_iter()
.map(|event| {
let version = event.transaction_version;
let event = event.event.v0()?;
let event = event.event.v1()?;
let sequence_number = event.sequence_number();

Ok(VersionedNewBlockEvent {
Expand Down
12 changes: 6 additions & 6 deletions crates/aptos-rosetta/src/types/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1723,8 +1723,8 @@ async fn parse_delegation_pool_resource_changes(
} else {
warn!(
"Failed to parse withdraw undelegated event! Skipping for {}:{}",
e.v0()?.key().get_creator_address(),
e.v0()?.key().get_creation_number()
e.v1()?.key().get_creator_address(),
e.v1()?.key().get_creation_number()
);
continue;
};
Expand Down Expand Up @@ -1817,13 +1817,13 @@ fn filter_events<F: Fn(&EventKey, &ContractEvent) -> Option<T>, T>(
) -> Vec<T> {
events
.iter()
.filter(|event| event.is_v0())
.filter(|event| event.v0().unwrap().key() == event_key)
.filter(|event| event.is_v1())
.filter(|event| event.v1().unwrap().key() == event_key)
.sorted_by(|a, b| {
a.v0()
a.v1()
.unwrap()
.sequence_number()
.cmp(&b.v0().unwrap().sequence_number())
.cmp(&b.v1().unwrap().sequence_number())
})
.filter_map(|event| parser(event_key, event))
.collect()
Expand Down
8 changes: 4 additions & 4 deletions crates/aptos/src/node/analyze/analyze_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,22 @@ impl AnalyzeValidators {
)?;
let end = raw_events.len() < batch;
for raw_event in raw_events {
if cursor <= raw_event.event.v0()?.sequence_number() {
if cursor <= raw_event.event.v1()?.sequence_number() {
println!(
"Duplicate event found for {} : {:?}",
cursor,
raw_event.event.v0()?.sequence_number()
raw_event.event.v1()?.sequence_number()
);
} else {
cursor = raw_event.event.v0()?.sequence_number();
cursor = raw_event.event.v1()?.sequence_number();
let event = bcs::from_bytes::<NewBlockEvent>(raw_event.event.event_data())?;

match epoch.cmp(&event.epoch()) {
Ordering::Equal => {
result.push(VersionedNewBlockEvent {
event,
version: raw_event.transaction_version,
sequence_number: raw_event.event.v0()?.sequence_number(),
sequence_number: raw_event.event.v1()?.sequence_number(),
});
},
Ordering::Greater => {
Expand Down
4 changes: 2 additions & 2 deletions execution/executor-benchmark/src/native_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl NativeExecutor {
];

// TODO(grao): Some values are fake, because I'm lazy.
let events = vec![ContractEvent::new_v0(
let events = vec![ContractEvent::new_v1(
EventKey::new(0, sender_address),
0,
TypeTag::Struct(Box::new(WithdrawEvent::struct_tag())),
Expand Down Expand Up @@ -224,7 +224,7 @@ impl NativeExecutor {
}

let events = vec![
ContractEvent::new_v0(
ContractEvent::new_v1(
EventKey::new(0, recipient_address),
0,
TypeTag::Struct(Box::new(DepositEvent::struct_tag())),
Expand Down
4 changes: 2 additions & 2 deletions execution/executor-test-helpers/src/integration_test_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ pub fn test_execution_with_storage_impl_inner(
assert_eq!(
account3_received_events_batch1[0]
.event
.v0()
.v1()
.unwrap()
.sequence_number(),
16
Expand All @@ -517,7 +517,7 @@ pub fn test_execution_with_storage_impl_inner(
assert_eq!(
account3_received_events_batch2[0]
.event
.v0()
.v1()
.unwrap()
.sequence_number(),
6
Expand Down
Loading

0 comments on commit 0ba554c

Please sign in to comment.