Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
chain_extraction.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <exception>
7#include <functional>
8#include <map>
9
10namespace sysio { namespace trace_api {
11
13using chain::packed_transaction;
14
15template <typename StoreProvider>
17public:
23 chain_extraction_impl_type( StoreProvider store, exception_handler except_handler )
24 : store(std::move(store))
25 , except_handler(std::move(except_handler))
26 {}
27
30 on_applied_transaction( trace, ptrx );
31 }
32
35 on_accepted_block( bsp );
36 }
37
40 on_irreversible_block( bsp );
41 }
42
44 void signal_block_start( uint32_t block_num ) {
45 on_block_start( block_num );
46 }
47
48private:
49
50 void on_applied_transaction(const chain::transaction_trace_ptr& trace, const chain::packed_transaction_ptr& t) {
51 if( !trace->receipt ) return;
52 // include only executed transactions; soft_fail included so that onerror (and any inlines via onerror) are included
53 if((trace->receipt->status != chain::transaction_receipt_header::executed &&
54 trace->receipt->status != chain::transaction_receipt_header::soft_fail)) {
55 return;
56 }
57 if( chain::is_onblock( *trace )) {
58 onblock_trace.emplace( cache_trace{trace, t} );
59 } else if( trace->failed_dtrx_trace ) {
60 cached_traces[trace->failed_dtrx_trace->id] = {trace, t};
61 } else {
62 cached_traces[trace->id] = {trace, t};
63 }
64 }
65
66 void on_accepted_block(const chain::block_state_ptr& block_state) {
67 store_block_trace( block_state );
68 }
69
70 void on_irreversible_block( const chain::block_state_ptr& block_state ) {
71 store_lib( block_state );
72 }
73
74 void on_block_start( uint32_t block_num ) {
75 clear_caches();
76 }
77
78 void clear_caches() {
79 cached_traces.clear();
80 onblock_trace.reset();
81 }
82
83 void store_block_trace( const chain::block_state_ptr& block_state ) {
84 try {
85 using transaction_trace_t = transaction_trace_v3;
86 auto bt = create_block_trace( block_state );
87
88 std::vector<transaction_trace_t> traces;
89 traces.reserve( block_state->block->transactions.size() + 1 );
90 block_trxs_entry tt;
91 tt.ids.reserve(block_state->block->transactions.size() + 1);
92 if( onblock_trace )
93 traces.emplace_back( to_transaction_trace<transaction_trace_t>( *onblock_trace ));
94 for( const auto& r : block_state->block->transactions ) {
96 if( std::holds_alternative<transaction_id_type>(r.trx)) {
97 id = std::get<transaction_id_type>(r.trx);
98 } else {
99 id = std::get<packed_transaction>(r.trx).id();
100 }
101 const auto it = cached_traces.find( id );
102 if( it != cached_traces.end() ) {
103 traces.emplace_back( to_transaction_trace<transaction_trace_t>( it->second ));
104 }
105 tt.ids.emplace_back(id);
106 }
107 bt.transactions = std::move( traces );
108 clear_caches();
109
110 // tt entry acts as a placeholder in a trx id slice if this block has no transaction
111 tt.block_num = bt.number;
112 store.append_trx_ids( std::move(tt) );
113
114 store.append( std::move( bt ) );
115 } catch( ... ) {
116 except_handler( MAKE_EXCEPTION_WITH_CONTEXT( std::current_exception() ) );
117 }
118 }
119
120 void store_lib( const chain::block_state_ptr& bsp ) {
121 try {
122 store.append_lib( bsp->block_num );
123 } catch( ... ) {
124 except_handler( MAKE_EXCEPTION_WITH_CONTEXT( std::current_exception() ) );
125 }
126 }
127
128private:
129 StoreProvider store;
130 exception_handler except_handler;
131 std::map<transaction_id_type, cache_trace> cached_traces;
132 std::optional<cache_trace> onblock_trace;
133
134};
135
136}}
const mie::Vuint & r
Definition bn.cpp:28
chain_extraction_impl_type(StoreProvider store, exception_handler except_handler)
void signal_block_start(uint32_t block_num)
connect to chain controller block_start signal
void signal_irreversible_block(const chain::block_state_ptr &bsp)
connect to chain controller irreversible_block signal
void signal_accepted_block(const chain::block_state_ptr &bsp)
connect to chain controller accepted_block signal
void signal_applied_transaction(const chain::transaction_trace_ptr &trace, const chain::packed_transaction_ptr &ptrx)
connect to chain controller applied_transaction signal
uint64_t id
Definition code_cache.cpp:0
Definition name.hpp:106
std::shared_ptr< transaction_trace > transaction_trace_ptr
Definition trace.hpp:20
std::shared_ptr< const packed_transaction > packed_transaction_ptr
bool is_onblock(const transaction_trace &tt)
Definition trace.hpp:71
checksum_type transaction_id_type
Definition types.hpp:236
std::shared_ptr< block_state > block_state_ptr
block_trace_v2 create_block_trace(const chain::block_state_ptr &bsp)
TransactionTrace to_transaction_trace(const cache_trace &t)
fc::optional_delegate< void(const exception_with_context &)> exception_handler
Definition common.hpp:41
#define MAKE_EXCEPTION_WITH_CONTEXT(eptr)
Definition common.hpp:56
unsigned int uint32_t
Definition stdint.h:126
@ executed
succeed, no error handler executed
Definition block.hpp:14
@ soft_fail
objectively failed (not executed), error handler executed
Definition block.hpp:15
void bt(const Operand &op, const Reg &reg)