Skip to content

Commit 1ade1da

Browse files
committed
Track is_secondary using an explicit boolean flag
1 parent ea3f681 commit 1ade1da

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
@@ -74,7 +74,8 @@ manager::manager()
7474
cmt_( commitment::e_confirmed ),
7575
max_batch_( PC_MAX_BATCH ),
7676
sreq_{ { commitment::e_processed } },
77-
secondary_{ nullptr }
77+
secondary_{ nullptr },
78+
is_secondary_( false )
7879
{
7980
tconn_.set_sub( this );
8081
breq_->set_sub( this );
@@ -117,7 +118,7 @@ void manager::del_map_sub()
117118
{
118119
if ( --num_sub_ <= 0 && !has_status( PC_PYTH_HAS_MAPPING ) ) {
119120
set_status( PC_PYTH_HAS_MAPPING );
120-
PC_LOG_INF( "completed_mapping_init" ).add( "secondary", is_secondary() ).end();
121+
PC_LOG_INF( "completed_mapping_init" ).add( "secondary", get_is_secondary() ).end();
121122
// notify user that initialization is complete
122123
if ( sub_ ) {
123124
sub_->on_init( this );
@@ -267,7 +268,7 @@ uint64_t manager::get_slot() const
267268

268269
void manager::teardown()
269270
{
270-
PC_LOG_INF( "pythd_teardown" ).add( "secondary", is_secondary() ).end();
271+
PC_LOG_INF( "pythd_teardown" ).add( "secondary", get_is_secondary() ).end();
271272

272273
// shutdown listener
273274
lsvr_.close();
@@ -305,15 +306,15 @@ bool manager::init()
305306
// log import key names
306307
key_pair *kp = get_publish_key_pair();
307308
if ( kp ) {
308-
PC_LOG_INF( "publish_key" ).add( "key_name", *kp ).add( "secondary", is_secondary() ).end();
309+
PC_LOG_INF( "publish_key" ).add( "key_name", *kp ).add( "secondary", get_is_secondary() ).end();
309310
}
310311
pub_key *mpub = get_mapping_pub_key();
311312
if ( mpub ) {
312-
PC_LOG_INF( "mapping_key" ).add( "key_name", *mpub ).add( "secondary", is_secondary() ).end();
313+
PC_LOG_INF( "mapping_key" ).add( "key_name", *mpub ).add( "secondary", get_is_secondary() ).end();
313314
}
314315
pub_key *gpub = get_program_pub_key();
315316
if ( gpub ) {
316-
PC_LOG_INF( "program_key" ).add( "key_name", *gpub ).add( "secondary", is_secondary() ).end();
317+
PC_LOG_INF( "program_key" ).add( "key_name", *gpub ).add( "secondary", get_is_secondary() ).end();
317318
}
318319

319320
// initialize capture
@@ -371,12 +372,12 @@ bool manager::init()
371372
return set_err_msg( lsvr_.get_err_msg() );
372373
}
373374
PC_LOG_INF("listening").add("port",lsvr_.get_port())
374-
.add( "secondary", is_secondary() )
375+
.add( "secondary", get_is_secondary() )
375376
.add( "content_dir", get_content_dir() )
376377
.end();
377378
}
378379
PC_LOG_INF( "initialized" )
379-
.add( "secondary", is_secondary() )
380+
.add( "secondary", get_is_secondary() )
380381
.add( "version", PC_VERSION )
381382
.add( "rpc_host", get_rpc_host() )
382383
.add( "tx_host", get_tx_host() )
@@ -405,6 +406,7 @@ void manager::add_secondary( const std::string& rpc_host, const std::string& key
405406
mgr->set_do_tx( do_tx_ );
406407
mgr->set_do_ws( do_ws_ );
407408
mgr->set_commitment( cmt_ );
409+
mgr->set_is_secondary( true );
408410

409411
secondary_ = mgr;
410412

@@ -414,8 +416,12 @@ bool manager::has_secondary() const {
414416
return secondary_ != nullptr;
415417
}
416418

417-
bool manager::is_secondary() const {
418-
return !has_secondary();
419+
void manager::set_is_secondary(bool is_secondary) {
420+
is_secondary_ = is_secondary;
421+
}
422+
423+
bool manager::get_is_secondary() const {
424+
return is_secondary_;
419425
}
420426

421427
manager *manager::get_secondary() {
@@ -616,7 +622,7 @@ void manager::reconnect_rpc()
616622

617623
// check for successful (re)connect
618624
if ( !hconn_.get_is_err() && ( !wconn_ || !wconn_->get_is_err() ) ) {
619-
PC_LOG_INF( "rpc_connected" ).add( "secondary", is_secondary() ).end();
625+
PC_LOG_INF( "rpc_connected" ).add( "secondary", get_is_secondary() ).end();
620626
set_status( PC_PYTH_RPC_CONNECTED );
621627

622628
// reset state
@@ -720,7 +726,7 @@ void manager::log_disconnect()
720726
{
721727
if ( hconn_.get_is_err() ) {
722728
PC_LOG_ERR( "rpc_http_reset")
723-
.add( "secondary", is_secondary() )
729+
.add( "secondary", get_is_secondary() )
724730
.add( "error", hconn_.get_err_msg() )
725731
.add( "host", rhost_ )
726732
.add( "port", hconn_.get_port() )
@@ -729,7 +735,7 @@ void manager::log_disconnect()
729735
}
730736
if ( wconn_ && wconn_->get_is_err() ) {
731737
PC_LOG_ERR( "rpc_websocket_reset" )
732-
.add( "secondary", is_secondary() )
738+
.add( "secondary", get_is_secondary() )
733739
.add( "error", wconn_->get_err_msg() )
734740
.add( "host", rhost_ )
735741
.add( "port", wconn_->get_port() )
@@ -807,7 +813,7 @@ void manager::on_response( rpc::get_slot *res )
807813
PC_LOG_DBG( "received get_slot" )
808814
.add( "slot", slot_ )
809815
.add( "round_trip_time(ms)", 1e-6*ack_ts )
810-
.add( "secondary", is_secondary() )
816+
.add( "secondary", get_is_secondary() )
811817
.end();
812818

813819
// submit block hash every N slots
@@ -843,7 +849,7 @@ void manager::on_response( rpc::get_recent_block_hash *m )
843849
// set initialized status for block hash
844850
set_status( PC_PYTH_HAS_BLOCK_HASH );
845851
PC_LOG_INF( "received_recent_block_hash" )
846-
.add( "secondary", is_secondary() )
852+
.add( "secondary", get_is_secondary() )
847853
.add( "curr_slot", slot_ )
848854
.add( "hash_slot", m->get_slot() )
849855
.add( "round_trip_time(ms)", 1e-6*ack_ts )
@@ -862,15 +868,15 @@ void manager::on_response( rpc::account_update *m )
862868
if ( m->get_is_http() ) {
863869
int64_t ack_ts = m->get_recv_time() - m->get_sent_time();
864870
PC_LOG_DBG( "received account_update" )
865-
.add( "secondary", is_secondary() )
871+
.add( "secondary", get_is_secondary() )
866872
.add( "account", *m->get_account() )
867873
.add( "slot", slot_ )
868874
.add( "round_trip_time(ms)", 1e-6*ack_ts )
869875
.end();
870876
}
871877
else {
872878
PC_LOG_DBG( "received account_update" )
873-
.add( "secondary", is_secondary() )
879+
.add( "secondary", get_is_secondary() )
874880
.add( "account", *m->get_account() )
875881
.add( "slot", slot_ )
876882
.end();
@@ -917,14 +923,14 @@ bool manager::submit_poll( request *req )
917923
}
918924
if ( req->get_is_err() ) {
919925
PC_LOG_ERR( "request error")
920-
.add( "secondary", is_secondary() )
926+
.add( "secondary", get_is_secondary() )
921927
.add( "error", req->get_err_msg() )
922928
.end();
923929
return false;
924930
}
925931
if ( get_is_err() ) {
926932
PC_LOG_ERR( "request error")
927-
.add( "secondary", is_secondary() )
933+
.add( "secondary", get_is_secondary() )
928934
.add( "error", get_err_msg() )
929935
.end();
930936
return false;
@@ -944,7 +950,7 @@ void manager::on_connect()
944950
void manager::on_disconnect()
945951
{
946952
// callback user with connection status
947-
PC_LOG_INF( "pyth_tx_reset" ).add( "secondary", is_secondary() ).end();
953+
PC_LOG_INF( "pyth_tx_reset" ).add( "secondary", get_is_secondary() ).end();
948954
if ( sub_ ) {
949955
sub_->on_tx_disconnect( this );
950956
}

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:
@@ -285,7 +286,8 @@ namespace pc
285286
// Timestamp of the last batch
286287
int64_t last_upd_ts_= 0;
287288

288-
manager *secondary_; // manager for secondary network
289+
manager *secondary_; // manager for secondary network
290+
bool is_secondary_; // flag tracking whether we are a secondary manager
289291
};
290292

291293
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)