Skip to content

Commit 2409d05

Browse files
committed
add StcContractEvent
1 parent 92c6808 commit 2409d05

File tree

7 files changed

+128
-23
lines changed

7 files changed

+128
-23
lines changed

chain/api/src/message.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use crate::TransactionInfoWithProof;
55
use anyhow::Result;
66
use starcoin_crypto::HashValue;
77
use starcoin_service_registry::ServiceRequest;
8+
use starcoin_types::contract_event::StcContractEventInfo;
89
use starcoin_types::transaction::RichTransactionInfo;
910
use starcoin_types::{
10-
block::{Block, BlockHeader, BlockInfo, BlockNumber},
11-
contract_event::ContractEventInfo,
11+
block::{Block, BlockHeader, BlockInfo, BlockNumber}
12+
,
1213
filter::Filter,
1314
startup_info::{ChainStatus, StartupInfo},
1415
transaction::Transaction,
@@ -83,8 +84,8 @@ pub enum ChainResponse {
8384
BlockHeaderVec(Vec<Option<BlockHeader>>),
8485
TransactionInfos(Vec<RichTransactionInfo>),
8586
TransactionInfo(Option<RichTransactionInfo>),
86-
Events(Vec<ContractEventInfo>),
87-
MainEvents(Vec<ContractEventInfo>),
87+
Events(Vec<StcContractEventInfo>),
88+
MainEvents(Vec<StcContractEventInfo>),
8889
HashVec(Vec<HashValue>),
8990
TransactionProof(Box<Option<TransactionInfoWithProof>>),
9091
BlockInfoVec(Box<Vec<Option<BlockInfo>>>),

chain/api/src/service.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::TransactionInfoWithProof;
66
use anyhow::{bail, Result};
77
use starcoin_crypto::HashValue;
88
use starcoin_service_registry::{ActorService, ServiceHandler, ServiceRef};
9-
use starcoin_types::contract_event::{ContractEvent, ContractEventInfo};
9+
use starcoin_types::contract_event::{ContractEvent, StcContractEventInfo};
1010
use starcoin_types::filter::Filter;
1111
use starcoin_types::startup_info::ChainStatus;
1212
use starcoin_types::transaction::{RichTransactionInfo, Transaction};
@@ -48,7 +48,7 @@ pub trait ReadableChainService {
4848
reverse: bool,
4949
count: u64,
5050
) -> Result<Vec<Block>>;
51-
fn get_main_events(&self, filter: Filter) -> Result<Vec<ContractEventInfo>>;
51+
fn get_main_events(&self, filter: Filter) -> Result<Vec<StcContractEventInfo>>;
5252
fn get_block_ids(
5353
&self,
5454
start_number: BlockNumber,
@@ -101,7 +101,10 @@ pub trait ChainAsyncService:
101101
block_hash: HashValue,
102102
idx: u64,
103103
) -> Result<Option<RichTransactionInfo>>;
104-
async fn get_events_by_txn_hash(&self, txn_hash: HashValue) -> Result<Vec<ContractEventInfo>>;
104+
async fn get_events_by_txn_hash(
105+
&self,
106+
txn_hash: HashValue,
107+
) -> Result<Vec<StcContractEventInfo>>;
105108
/// for main
106109
async fn main_head_header(&self) -> Result<BlockHeader>;
107110
async fn main_head_block(&self) -> Result<Block>;
@@ -116,7 +119,7 @@ pub trait ChainAsyncService:
116119
-> Result<Option<BlockHeader>>;
117120
async fn main_startup_info(&self) -> Result<StartupInfo>;
118121
async fn main_status(&self) -> Result<ChainStatus>;
119-
async fn main_events(&self, filter: Filter) -> Result<Vec<ContractEventInfo>>;
122+
async fn main_events(&self, filter: Filter) -> Result<Vec<StcContractEventInfo>>;
120123
async fn get_block_ids(
121124
&self,
122125
start_number: BlockNumber,
@@ -271,7 +274,10 @@ where
271274
bail!("get txn info by block and idx error.")
272275
}
273276
}
274-
async fn get_events_by_txn_hash(&self, txn_hash: HashValue) -> Result<Vec<ContractEventInfo>> {
277+
async fn get_events_by_txn_hash(
278+
&self,
279+
txn_hash: HashValue,
280+
) -> Result<Vec<StcContractEventInfo>> {
275281
let response = self
276282
.send(ChainRequest::GetEventsByTxnHash { txn_hash })
277283
.await??;
@@ -357,7 +363,7 @@ where
357363
}
358364
}
359365

360-
async fn main_events(&self, filter: Filter) -> Result<Vec<ContractEventInfo>> {
366+
async fn main_events(&self, filter: Filter) -> Result<Vec<StcContractEventInfo>> {
361367
let response = self.send(ChainRequest::MainEvents(filter)).await??;
362368
if let ChainResponse::MainEvents(evts) = response {
363369
Ok(evts)

chain/service/src/chain_service.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use starcoin_service_registry::{
1515
};
1616
use starcoin_storage::{BlockStore, Storage, Store};
1717
use starcoin_types::block::ExecutedBlock;
18-
use starcoin_types::contract_event::ContractEventInfo;
18+
use starcoin_types::contract_event::StcContractEventInfo;
1919
use starcoin_types::filter::Filter;
2020
use starcoin_types::system_events::NewHeadBlock;
2121
use starcoin_types::transaction::RichTransactionInfo;
@@ -186,14 +186,14 @@ impl ServiceHandler<Self, ChainRequest> for ChainReaderService {
186186
events
187187
.into_iter()
188188
.enumerate()
189-
.map(|(idx, evt)| ContractEventInfo {
189+
.map(|(idx, evt)| StcContractEventInfo {
190190
block_hash: txn_info.block_id,
191191
block_number: txn_info.block_number,
192192
transaction_hash: txn_hash,
193193
transaction_index: txn_info.transaction_index,
194194
transaction_global_index: txn_info.transaction_global_index,
195195
event_index: idx as u32,
196-
event: evt,
196+
event: evt.into(),
197197
})
198198
.collect()
199199
};
@@ -389,7 +389,7 @@ impl ReadableChainService for ChainReaderServiceInner {
389389
self.main.get_blocks_by_number(number, reverse, count)
390390
}
391391

392-
fn get_main_events(&self, filter: Filter) -> Result<Vec<ContractEventInfo>> {
392+
fn get_main_events(&self, filter: Filter) -> Result<Vec<StcContractEventInfo>> {
393393
self.main.filter_events(filter)
394394
}
395395

chain/src/chain.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use starcoin_state_api::{AccountStateReader, ChainStateReader, ChainStateWriter}
2525
use starcoin_statedb::ChainStateDB;
2626
use starcoin_storage::Store;
2727
use starcoin_time_service::TimeService;
28-
use starcoin_types::contract_event::ContractEventInfo;
28+
use starcoin_types::contract_event::StcContractEventInfo;
2929
use starcoin_types::filter::Filter;
3030
use starcoin_types::multi_state::MultiState;
3131
use starcoin_types::multi_transaction::MultiSignedUserTransaction;
@@ -1370,7 +1370,7 @@ impl ChainReader for BlockChain {
13701370
}
13711371

13721372
impl BlockChain {
1373-
pub fn filter_events(&self, filter: Filter) -> Result<Vec<ContractEventInfo>> {
1373+
pub fn filter_events(&self, filter: Filter) -> Result<Vec<StcContractEventInfo>> {
13741374
let (storage, _storage2) = &self.storage;
13751375
let reverse = filter.reverse;
13761376
let chain_header = self.current_header();
@@ -1427,14 +1427,14 @@ impl BlockChain {
14271427
})?;
14281428

14291429
let filtered_event_with_info =
1430-
filtered_events.map(|(idx, evt)| ContractEventInfo {
1430+
filtered_events.map(|(idx, evt)| StcContractEventInfo {
14311431
block_hash: block_id,
14321432
block_number: block.header().number(),
14331433
transaction_hash: txn_info.transaction_hash(),
14341434
transaction_index: txn_info.transaction_index,
14351435
transaction_global_index: txn_info.transaction_global_index,
14361436
event_index: idx as u32,
1437-
event: evt,
1437+
event: evt.into(),
14381438
});
14391439
if reverse {
14401440
event_with_infos.extend(filtered_event_with_info.rev())

types/src/contract_event.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) The Starcoin Core Contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
pub use crate::event_info::{ContractEventInfo, StcContractEventInfo};
5+
pub use starcoin_vm_types::contract_event::*;
6+
7+
use crate::event::StcEventKey;
8+
use crate::language_storage::StcTypeTag;
9+
use starcoin_vm2_vm_types::contract_event::ContractEvent as ContractEvent2;
10+
11+
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
12+
pub enum StcContractEvent {
13+
V1(ContractEvent),
14+
V2(ContractEvent2),
15+
}
16+
17+
impl From<ContractEvent> for StcContractEvent {
18+
fn from(event: ContractEvent) -> Self {
19+
Self::V1(event)
20+
}
21+
}
22+
23+
impl From<ContractEvent2> for StcContractEvent {
24+
fn from(event: ContractEvent2) -> Self {
25+
Self::V2(event)
26+
}
27+
}
28+
29+
impl StcContractEvent {
30+
pub fn key(&self) -> StcEventKey {
31+
match self {
32+
Self::V1(event) => StcEventKey::V1(*event.key()),
33+
Self::V2(event) => StcEventKey::V2(event.event_key()),
34+
}
35+
}
36+
37+
pub fn sequence_number(&self) -> u64 {
38+
match self {
39+
Self::V1(event) => event.sequence_number(),
40+
Self::V2(event) => event.sequence_number(),
41+
}
42+
}
43+
44+
pub fn event_data(&self) -> &[u8] {
45+
match self {
46+
Self::V1(event) => event.event_data(),
47+
Self::V2(event) => event.event_data(),
48+
}
49+
}
50+
51+
pub fn type_tag(&self) -> StcTypeTag {
52+
match self {
53+
Self::V1(event) => StcTypeTag::V1(event.type_tag().clone()),
54+
Self::V2(event) => StcTypeTag::V2(event.type_tag().clone()),
55+
}
56+
}
57+
}

types/src/event_info.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::block::BlockNumber;
2-
use crate::contract_event::ContractEvent;
2+
use crate::contract_event::{ContractEvent, StcContractEvent};
33
use starcoin_crypto::HashValue;
44

55
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
@@ -15,3 +15,17 @@ pub struct ContractEventInfo {
1515
pub event_index: u32,
1616
pub event: ContractEvent,
1717
}
18+
19+
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
20+
pub struct StcContractEventInfo {
21+
pub block_hash: HashValue,
22+
pub block_number: BlockNumber,
23+
pub transaction_hash: HashValue,
24+
/// txn index in block
25+
pub transaction_index: u32,
26+
/// txn global index in chain
27+
pub transaction_global_index: u64,
28+
/// event index in the transaction events.
29+
pub event_index: u32,
30+
pub event: StcContractEvent,
31+
}

types/src/lib.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ pub mod block_metadata {
3131
pub use starcoin_vm_types::block_metadata::BlockMetadata;
3232
}
3333

34-
pub mod contract_event {
35-
pub use crate::event_info::ContractEventInfo;
36-
pub use starcoin_vm_types::contract_event::*;
37-
}
34+
pub mod contract_event;
3835

3936
// pub mod time {
4037
// pub use starcoin_vm_types::time::*;
@@ -44,6 +41,15 @@ pub mod error;
4441

4542
pub mod event {
4643
pub use starcoin_vm_types::event::*;
44+
45+
use serde::{Deserialize, Serialize};
46+
use starcoin_vm2_vm_types::event::EventKey as EventKey2;
47+
48+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
49+
pub enum StcEventKey {
50+
V1(EventKey),
51+
V2(EventKey2),
52+
}
4753
}
4854

4955
pub mod filter;
@@ -68,9 +74,30 @@ pub mod vm_error {
6874
}
6975

7076
pub mod language_storage {
77+
use serde::{Deserialize, Serialize};
7178
pub use starcoin_vm_types::language_storage::{
7279
ModuleId, ResourceKey, StructTag, TypeTag, CODE_TAG, CORE_CODE_ADDRESS, RESOURCE_TAG,
7380
};
81+
82+
use starcoin_vm2_vm_types::language_storage::TypeTag as TypeTag2;
83+
84+
#[derive(Serialize, Deserialize, Debug, PartialEq, Hash, Eq, Clone, PartialOrd, Ord)]
85+
pub enum StcTypeTag {
86+
V1(TypeTag),
87+
V2(TypeTag2),
88+
}
89+
90+
impl From<TypeTag> for StcTypeTag {
91+
fn from(tag: TypeTag) -> Self {
92+
StcTypeTag::V1(tag)
93+
}
94+
}
95+
96+
impl From<TypeTag2> for StcTypeTag {
97+
fn from(tag: TypeTag2) -> Self {
98+
StcTypeTag::V2(tag)
99+
}
100+
}
74101
}
75102

76103
pub mod identifier {

0 commit comments

Comments
 (0)