Skip to content

Commit 07b0ff5

Browse files
committed
Track is_secondary using an explicit boolean flag
1 parent e653b77 commit 07b0ff5

File tree

3 files changed

+43
-35
lines changed

3 files changed

+43
-35
lines changed

pc/manager.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ manager::manager()
7777
cmt_( commitment::e_confirmed ),
7878
max_batch_( PC_MAX_BATCH ),
7979
sreq_{ { commitment::e_processed } },
80-
secondary_{ nullptr }
80+
secondary_{ nullptr },
81+
is_secondary_( false )
8182
{
8283
tconn_.set_sub( this );
8384
breq_->set_sub( this );
@@ -120,7 +121,7 @@ void manager::del_map_sub()
120121
{
121122
if ( --num_sub_ <= 0 && !has_status( PC_PYTH_HAS_MAPPING ) ) {
122123
set_status( PC_PYTH_HAS_MAPPING );
123-
PC_LOG_INF( "completed_mapping_init" ).add( "secondary", is_secondary() ).end();
124+
PC_LOG_INF( "completed_mapping_init" ).add( "secondary", get_is_secondary() ).end();
124125
// notify user that initialization is complete
125126
if ( sub_ ) {
126127
sub_->on_init( this );
@@ -270,7 +271,7 @@ uint64_t manager::get_slot() const
270271

271272
void manager::teardown()
272273
{
273-
PC_LOG_INF( "pythd_teardown" ).add( "secondary", is_secondary() ).end();
274+
PC_LOG_INF( "pythd_teardown" ).add( "secondary", get_is_secondary() ).end();
274275

275276
// shutdown listener
276277
lsvr_.close();
@@ -308,15 +309,15 @@ bool manager::init()
308309
// log import key names
309310
key_pair *kp = get_publish_key_pair();
310311
if ( kp ) {
311-
PC_LOG_INF( "publish_key" ).add( "key_name", *kp ).add( "secondary", is_secondary() ).end();
312+
PC_LOG_INF( "publish_key" ).add( "key_name", *kp ).add( "secondary", get_is_secondary() ).end();
312313
}
313314
pub_key *mpub = get_mapping_pub_key();
314315
if ( mpub ) {
315-
PC_LOG_INF( "mapping_key" ).add( "key_name", *mpub ).add( "secondary", is_secondary() ).end();
316+
PC_LOG_INF( "mapping_key" ).add( "key_name", *mpub ).add( "secondary", get_is_secondary() ).end();
316317
}
317318
pub_key *gpub = get_program_pub_key();
318319
if ( gpub ) {
319-
PC_LOG_INF( "program_key" ).add( "key_name", *gpub ).add( "secondary", is_secondary() ).end();
320+
PC_LOG_INF( "program_key" ).add( "key_name", *gpub ).add( "secondary", get_is_secondary() ).end();
320321
}
321322

322323
// initialize capture
@@ -374,12 +375,12 @@ bool manager::init()
374375
return set_err_msg( lsvr_.get_err_msg() );
375376
}
376377
PC_LOG_INF("listening").add("port",lsvr_.get_port())
377-
.add( "secondary", is_secondary() )
378+
.add( "secondary", get_is_secondary() )
378379
.add( "content_dir", get_content_dir() )
379380
.end();
380381
}
381382
PC_LOG_INF( "initialized" )
382-
.add( "secondary", is_secondary() )
383+
.add( "secondary", get_is_secondary() )
383384
.add( "version", PC_VERSION )
384385
.add( "rpc_host", get_rpc_host() )
385386
.add( "tx_host", get_tx_host() )
@@ -408,6 +409,7 @@ void manager::add_secondary( const std::string& rpc_host, const std::string& key
408409
mgr->set_do_tx( do_tx_ );
409410
mgr->set_do_ws( do_ws_ );
410411
mgr->set_commitment( cmt_ );
412+
mgr->set_is_secondary( true );
411413

412414
secondary_ = mgr;
413415

@@ -417,8 +419,12 @@ bool manager::has_secondary() const {
417419
return secondary_ != nullptr;
418420
}
419421

420-
bool manager::is_secondary() const {
421-
return !has_secondary();
422+
void manager::set_is_secondary(bool is_secondary) {
423+
is_secondary_ = is_secondary;
424+
}
425+
426+
bool manager::get_is_secondary() const {
427+
return is_secondary_;
422428
}
423429

424430
manager *manager::get_secondary() {
@@ -628,7 +634,7 @@ void manager::reconnect_rpc()
628634

629635
// check for successful (re)connect
630636
if ( !hconn_.get_is_err() && ( !wconn_ || !wconn_->get_is_err() ) ) {
631-
PC_LOG_INF( "rpc_connected" ).add( "secondary", is_secondary() ).end();
637+
PC_LOG_INF( "rpc_connected" ).add( "secondary", get_is_secondary() ).end();
632638
set_status( PC_PYTH_RPC_CONNECTED );
633639

634640
// reset state
@@ -735,7 +741,7 @@ void manager::log_disconnect()
735741
{
736742
if ( hconn_.get_is_err() ) {
737743
PC_LOG_ERR( "rpc_http_reset")
738-
.add( "secondary", is_secondary() )
744+
.add( "secondary", get_is_secondary() )
739745
.add( "error", hconn_.get_err_msg() )
740746
.add( "host", rhost_ )
741747
.add( "port", hconn_.get_port() )
@@ -744,7 +750,7 @@ void manager::log_disconnect()
744750
}
745751
if ( wconn_ && wconn_->get_is_err() ) {
746752
PC_LOG_ERR( "rpc_websocket_reset" )
747-
.add( "secondary", is_secondary() )
753+
.add( "secondary", get_is_secondary() )
748754
.add( "error", wconn_->get_err_msg() )
749755
.add( "host", rhost_ )
750756
.add( "port", wconn_->get_port() )
@@ -822,7 +828,7 @@ void manager::on_response( rpc::get_slot *res )
822828
PC_LOG_DBG( "received get_slot" )
823829
.add( "slot", slot_ )
824830
.add( "round_trip_time(ms)", 1e-6*ack_ts )
825-
.add( "secondary", is_secondary() )
831+
.add( "secondary", get_is_secondary() )
826832
.end();
827833

828834
// submit block hash every N slots
@@ -865,7 +871,7 @@ void manager::on_response( rpc::get_recent_block_hash *m )
865871
// set initialized status for block hash
866872
set_status( PC_PYTH_HAS_BLOCK_HASH );
867873
PC_LOG_INF( "received_recent_block_hash" )
868-
.add( "secondary", is_secondary() )
874+
.add( "secondary", get_is_secondary() )
869875
.add( "curr_slot", slot_ )
870876
.add( "hash_slot", m->get_slot() )
871877
.add( "round_trip_time(ms)", 1e-6*ack_ts )
@@ -884,15 +890,15 @@ void manager::on_response( rpc::account_update *m )
884890
if ( m->get_is_http() ) {
885891
int64_t ack_ts = m->get_recv_time() - m->get_sent_time();
886892
PC_LOG_DBG( "received account_update" )
887-
.add( "secondary", is_secondary() )
893+
.add( "secondary", get_is_secondary() )
888894
.add( "account", *m->get_account() )
889895
.add( "slot", slot_ )
890896
.add( "round_trip_time(ms)", 1e-6*ack_ts )
891897
.end();
892898
}
893899
else {
894900
PC_LOG_DBG( "received account_update" )
895-
.add( "secondary", is_secondary() )
901+
.add( "secondary", get_is_secondary() )
896902
.add( "account", *m->get_account() )
897903
.add( "slot", slot_ )
898904
.end();
@@ -939,14 +945,14 @@ bool manager::submit_poll( request *req )
939945
}
940946
if ( req->get_is_err() ) {
941947
PC_LOG_ERR( "request error")
942-
.add( "secondary", is_secondary() )
948+
.add( "secondary", get_is_secondary() )
943949
.add( "error", req->get_err_msg() )
944950
.end();
945951
return false;
946952
}
947953
if ( get_is_err() ) {
948954
PC_LOG_ERR( "request error")
949-
.add( "secondary", is_secondary() )
955+
.add( "secondary", get_is_secondary() )
950956
.add( "error", get_err_msg() )
951957
.end();
952958
return false;
@@ -966,7 +972,7 @@ void manager::on_connect()
966972
void manager::on_disconnect()
967973
{
968974
// callback user with connection status
969-
PC_LOG_INF( "pyth_tx_reset" ).add( "secondary", is_secondary() ).end();
975+
PC_LOG_INF( "pyth_tx_reset" ).add( "secondary", get_is_secondary() ).end();
970976
if ( sub_ ) {
971977
sub_->on_tx_disconnect( this );
972978
}

pc/manager.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ namespace pc
193193
bool get_is_rpc_send() const;
194194

195195
bool has_secondary() const;
196-
bool is_secondary() const;
196+
void set_is_secondary(bool is_secondary);
197+
bool get_is_secondary() const;
197198
manager *get_secondary();
198199

199200
private:
@@ -287,7 +288,8 @@ namespace pc
287288
// Timestamp of the last batch
288289
int64_t last_upd_ts_= 0;
289290

290-
manager *secondary_; // manager for secondary network
291+
manager *secondary_; // manager for secondary network
292+
bool is_secondary_; // flag tracking whether we are a secondary manager
291293
};
292294

293295
inline bool manager::get_is_tx_connect() const

pc/request.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ void get_mapping::update( T *res )
235235
// check and get any new product accounts in mapping table
236236
num_sym_ = tab->num_;
237237
PC_LOG_INF( "add_mapping" )
238-
.add( "secondary", cptr->is_secondary() )
238+
.add( "secondary", cptr->get_is_secondary() )
239239
.add( "account", mkey_ )
240240
.add( "num_products", num_sym_ )
241241
.end();
@@ -382,7 +382,7 @@ void product::update( T *res )
382382
net_buf *jhd, *jtl;
383383
wtr.detach( jhd, jtl );
384384
PC_LOG_INF( st_ != e_done ? "add_product" : "upd_product" )
385-
.add( "secondary", cptr->is_secondary() )
385+
.add( "secondary", cptr->get_is_secondary() )
386386
.add( "account", acc_ )
387387
.add( "attr", str( jhd->buf_, jhd->size_) )
388388
.end();
@@ -693,7 +693,7 @@ bool price::update(
693693
tvec_.emplace_back( std::string( 100, '\0' ), preq_->get_sent_time() );
694694
preq_->get_signature()->enc_base58( tvec_.back().first );
695695
PC_LOG_DBG( "sent price update transaction" )
696-
.add( "secondary", mgr->is_secondary() )
696+
.add( "secondary", mgr->get_is_secondary() )
697697
.add( "price_account", *get_account() )
698698
.add( "product_account", *prod_->get_account() )
699699
.add( "symbol", get_symbol() )
@@ -703,7 +703,7 @@ bool price::update(
703703
.end();
704704
if ( PC_UNLIKELY( tvec_.size() >= 100 ) ) {
705705
PC_LOG_WRN( "too many unacked price update transactions" )
706-
.add( "secondary", mgr->is_secondary() )
706+
.add( "secondary", mgr->get_is_secondary() )
707707
.add( "price_account", *get_account() )
708708
.add( "product_account", *prod_->get_account() )
709709
.add( "symbol", get_symbol() )
@@ -739,7 +739,7 @@ bool price::send( price *prices[], const unsigned n )
739739
manager *const mgr = p->get_manager();
740740
if ( PC_UNLIKELY( ! p->init_ && ! p->init_publish() ) ) {
741741
PC_LOG_ERR( "failed to initialize publisher" )
742-
.add( "secondary", mgr->is_secondary() )
742+
.add( "secondary", mgr->get_is_secondary() )
743743
.add( "price_account", *p->get_account() )
744744
.add( "product_account", *p->prod_->get_account() )
745745
.add( "symbol", p->get_symbol() )
@@ -748,7 +748,7 @@ bool price::send( price *prices[], const unsigned n )
748748
}
749749
if ( PC_UNLIKELY( ! p->has_publisher() ) ) {
750750
PC_LOG_ERR( "missing publish permission" )
751-
.add( "secondary", mgr->is_secondary() )
751+
.add( "secondary", mgr->get_is_secondary() )
752752
.add( "price_account", *p->get_account() )
753753
.add( "product_account", *p->prod_->get_account() )
754754
.add( "symbol", p->get_symbol() )
@@ -757,7 +757,7 @@ bool price::send( price *prices[], const unsigned n )
757757
}
758758
if ( PC_UNLIKELY( ! p->get_is_ready_publish() ) ) {
759759
PC_LOG_ERR( "not ready to publish - check rpc / pyth_tx connection" )
760-
.add( "secondary", mgr->is_secondary() )
760+
.add( "secondary", mgr->get_is_secondary() )
761761
.add( "price_account", *p->get_account() )
762762
.add( "product_account", *p->prod_->get_account() )
763763
.add( "symbol", p->get_symbol() )
@@ -770,7 +770,7 @@ bool price::send( price *prices[], const unsigned n )
770770
}
771771
else if ( mgr != mgr1 ) {
772772
PC_LOG_ERR( "unexpected manager" )
773-
.add( "secondary", mgr->is_secondary() )
773+
.add( "secondary", mgr->get_is_secondary() )
774774
.add( "price_account", *p->get_account() )
775775
.add( "product_account", *p->prod_->get_account() )
776776
.add( "symbol", p->get_symbol() )
@@ -795,7 +795,7 @@ bool price::send( price *prices[], const unsigned n )
795795
}
796796
else {
797797
PC_LOG_ERR( "failed to build msg" )
798-
.add( "secondary", mgr->is_secondary() )
798+
.add( "secondary", mgr->get_is_secondary() )
799799
.add( "price_account", *p->get_account() )
800800
.add( "product_account", *p->prod_->get_account() )
801801
.add( "symbol", p->get_symbol() )
@@ -811,7 +811,7 @@ bool price::send( price *prices[], const unsigned n )
811811
);
812812
p1->preq_->get_signature()->enc_base58( p1->tvec_.back().first );
813813
PC_LOG_DBG( "sent price update" )
814-
.add( "secondary", mgr->is_secondary() )
814+
.add( "secondary", mgr->get_is_secondary() )
815815
.add( "price_account", *p1->get_account() )
816816
.add( "product_account", *p1->prod_->get_account() )
817817
.add( "symbol", p1->get_symbol() )
@@ -821,7 +821,7 @@ bool price::send( price *prices[], const unsigned n )
821821
.end();
822822
if ( PC_UNLIKELY( p1->tvec_.size() >= 100 ) ) {
823823
PC_LOG_WRN( "too many unacked price update transactions" )
824-
.add( "secondary", mgr->is_secondary() )
824+
.add( "secondary", mgr->get_is_secondary() )
825825
.add( "price_account", *p1->get_account() )
826826
.add( "product_account", *p1->prod_->get_account() )
827827
.add( "symbol", p1->get_symbol() )
@@ -872,7 +872,7 @@ void price::on_response( rpc::upd_price *res )
872872
const int64_t ack_dur = res->get_recv_time() - it->second;
873873
tvec_.erase( it );
874874
PC_LOG_DBG( "received price update transaction ack" )
875-
.add( "secondary", get_manager()->is_secondary() )
875+
.add( "secondary", get_manager()->get_is_secondary() )
876876
.add( "price_account", *get_account() )
877877
.add( "product_account", *prod_->get_account() )
878878
.add( "symbol", get_symbol() )
@@ -898,7 +898,7 @@ void price::on_response( rpc::account_update *res )
898898
void price::log_update( const char *title )
899899
{
900900
PC_LOG_INF( title )
901-
.add( "secondary", get_manager()->is_secondary() )
901+
.add( "secondary", get_manager()->get_is_secondary() )
902902
.add( "account", *get_account() )
903903
.add( "product", *prod_->get_account() )
904904
.add( "symbol", get_symbol() )

0 commit comments

Comments
 (0)