3
3
4
4
pub mod message;
5
5
6
- use crate :: message:: { ContractEventNotification , Event , Notification , ThinBlock } ;
6
+ use crate :: message:: { ContractEventNotification , Event , Event2 , Notification , ThinBlock } ;
7
7
use anyhow:: { format_err, Result } ;
8
8
use starcoin_logger:: prelude:: * ;
9
9
use starcoin_service_registry:: { ActorService , EventHandler , ServiceContext , ServiceFactory } ;
10
10
use starcoin_storage:: { Storage , Store } ;
11
11
use starcoin_types:: block:: Block ;
12
12
use starcoin_types:: system_events:: NewHeadBlock ;
13
+ use starcoin_vm2_storage:: { Storage as Storage2 , Store as Store2 } ;
13
14
use std:: sync:: Arc ;
14
15
15
16
/// ChainNotify watch `NewHeadBlock` message from bus,
16
17
/// and then reproduce `Notification<ThinBlock>` and `Notification<Arc<[Event]>>` message to bus.
17
18
/// User can subscribe the two notification to watch onchain events.
18
19
pub struct ChainNotifyHandlerService {
19
20
store : Arc < dyn Store > ,
21
+ store2 : Arc < dyn Store2 > ,
20
22
}
21
23
22
24
impl ChainNotifyHandlerService {
23
- pub fn new ( store : Arc < dyn Store > ) -> Self {
24
- Self { store }
25
+ pub fn new ( store : Arc < dyn Store > , store2 : Arc < dyn Store2 > ) -> Self {
26
+ Self { store, store2 }
25
27
}
26
28
}
27
29
@@ -30,7 +32,8 @@ impl ServiceFactory<Self> for ChainNotifyHandlerService {
30
32
ctx : & mut ServiceContext < ChainNotifyHandlerService > ,
31
33
) -> Result < ChainNotifyHandlerService > {
32
34
let storage = ctx. get_shared :: < Arc < Storage > > ( ) ?;
33
- Ok ( Self :: new ( storage) )
35
+ let storage2 = ctx. get_shared :: < Arc < Storage2 > > ( ) ?;
36
+ Ok ( Self :: new ( storage, storage2) )
34
37
}
35
38
}
36
39
@@ -58,7 +61,7 @@ impl EventHandler<Self, NewHeadBlock> for ChainNotifyHandlerService {
58
61
self . notify_new_block ( block, ctx) ;
59
62
60
63
// notify events
61
- if let Err ( e) = self . notify_events ( block, self . store . clone ( ) , ctx) {
64
+ if let Err ( e) = self . notify_events ( block, self . store . clone ( ) , self . store2 . clone ( ) , ctx) {
62
65
error ! ( target: "pubsub" , "fail to notify events to client, err: {}" , & e) ;
63
66
}
64
67
}
@@ -77,18 +80,23 @@ impl ChainNotifyHandlerService {
77
80
& self ,
78
81
block : & Block ,
79
82
store : Arc < dyn Store > ,
83
+ store2 : Arc < dyn Store2 > ,
80
84
ctx : & mut ServiceContext < Self > ,
81
85
) -> Result < ( ) > {
82
86
let block_number = block. header ( ) . number ( ) ;
83
87
let block_id = block. id ( ) ;
84
88
let txn_info_ids = store. get_block_txn_info_ids ( block_id) ?;
85
89
let mut all_events: Vec < Event > = vec ! [ ] ;
90
+ let mut all_events2 = vec ! [ ] ;
86
91
for txn_info_id in txn_info_ids. into_iter ( ) . rev ( ) {
87
92
let txn_info = store
88
93
. get_transaction_info ( txn_info_id) ?
89
94
. ok_or_else ( || format_err ! ( "cannot find txn info by it's id {}" , & txn_info_id) ) ?;
90
95
// get events directly by txn_info_id
91
- let events = store. get_contract_events ( txn_info_id) ?. unwrap_or_default ( ) ;
96
+ let ( in_vm1, events) = store
97
+ . get_contract_events ( txn_info_id) ?
98
+ . map ( |e| ( true , e) )
99
+ . unwrap_or ( ( false , vec ! [ ] ) ) ;
92
100
all_events. extend ( events. into_iter ( ) . enumerate ( ) . map ( |( idx, evt) | {
93
101
Event :: new (
94
102
block_id,
@@ -100,9 +108,26 @@ impl ChainNotifyHandlerService {
100
108
evt,
101
109
)
102
110
} ) ) ;
111
+ if !in_vm1 {
112
+ let events = store2. get_contract_events ( txn_info_id) ?. unwrap_or_default ( ) ;
113
+ all_events2. extend ( events. into_iter ( ) . enumerate ( ) . map ( |( idx, evt) | {
114
+ Event2 :: new (
115
+ block_id,
116
+ block_number,
117
+ txn_info. transaction_hash ( ) ,
118
+ Some ( txn_info. transaction_index ) ,
119
+ Some ( txn_info. transaction_global_index ) ,
120
+ Some ( idx as u32 ) ,
121
+ evt,
122
+ )
123
+ } ) ) ;
124
+ }
103
125
}
104
- let events_notification: ContractEventNotification =
105
- Notification ( ( block. header . state_root ( ) , all_events. into ( ) ) ) ;
126
+ let events_notification: ContractEventNotification = Notification ( (
127
+ block. header . state_root ( ) ,
128
+ all_events. into ( ) ,
129
+ all_events2. into ( ) ,
130
+ ) ) ;
106
131
ctx. broadcast ( events_notification) ;
107
132
Ok ( ( ) )
108
133
}
0 commit comments