Skip to content

Commit fd68bfa

Browse files
committed
replace slot_subscribe with get_slot
1 parent 56fd59a commit fd68bfa

File tree

5 files changed

+77
-13
lines changed

5 files changed

+77
-13
lines changed

pc/manager.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ manager::manager()
6262
do_cap_( false ),
6363
do_tx_( true ),
6464
is_pub_( false ),
65-
cmt_( commitment::e_confirmed )
65+
cmt_( commitment::e_confirmed ),
66+
sreq_{ { commitment::e_processed } }
6667
{
6768
tconn_.set_sub( this );
6869
breq_->set_sub( this );
@@ -433,6 +434,15 @@ void manager::poll( bool do_wait )
433434
// get current time
434435
curr_ts_ = get_now();
435436

437+
// get current slot
438+
if ( curr_ts_ - slot_ts_ > 200 * PC_NSECS_IN_MSEC ) {
439+
if ( sreq_->get_is_recv() ) {
440+
if ( has_status( PC_PYTH_RPC_CONNECTED ) ) {
441+
clnt_.send( sreq_ );
442+
}
443+
}
444+
}
445+
436446
// try to (re)connect to tx proxy
437447
if ( do_tx_ && ( !tconn_.get_is_connect() || tconn_.get_is_err() ) ) {
438448
tconn_.reconnect();
@@ -489,8 +499,9 @@ void manager::reconnect_rpc()
489499
kidx_ = 0;
490500
ctimeout_ = PC_NSECS_IN_SEC;
491501
pub_ts_ = 0L;
492-
slot_cnt_ = 0UL;
493502
slot_ = 0L;
503+
slot_cnt_ = 0UL;
504+
slot_ts_ = 0L;
494505
num_sub_ = 0;
495506
clnt_.reset();
496507
for(;;) {
@@ -634,23 +645,30 @@ void manager::schedule( price_sched *kptr )
634645
}
635646
}
636647

637-
void manager::on_response( rpc::slot_subscribe *res )
648+
void manager::on_response( rpc::get_slot *res )
638649
{
639650
// check error
640651
if ( PC_UNLIKELY( res->get_is_err() ) ) {
641-
set_err_msg( "failed to slot_subscribe ["
652+
set_err_msg( "failed to get slot ["
642653
+ res->get_err_msg() + "]" );
643654
return;
644655
}
645656

646657
// ignore slots that go back in time
647-
uint64_t slot = res->get_slot();
658+
uint64_t slot = res->get_current_slot();
648659
int64_t ts = res->get_recv_time();
649660
if ( slot <= slot_ ) {
650661
return;
651662
}
652663
slot_ = slot;
653-
PC_LOG_DBG( "receive slot" ).add( "slot", slot_ ).end();
664+
slot_ts_ = ts;
665+
666+
int64_t ack_ts = res->get_recv_time() - res->get_sent_time();
667+
668+
PC_LOG_DBG( "received get_slot" )
669+
.add( "slot", slot_ )
670+
.add( "rount_trip_time(ms)", 1e-6*ack_ts )
671+
.end();
654672

655673
// submit block hash every N slots
656674
if ( slot_cnt_++ % PC_BLOCKHASH_TIMEOUT == 0 ) {

pc/manager.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace pc
4848
public net_accept,
4949
public tx_sub,
5050
public rpc_sub,
51-
public rpc_sub_i<rpc::slot_subscribe>,
51+
public rpc_sub_i<rpc::get_slot>,
5252
public rpc_sub_i<rpc::get_recent_block_hash>,
5353
public rpc_sub_i<rpc::program_subscribe>
5454
{
@@ -164,7 +164,7 @@ namespace pc
164164
bool get_is_tx_send() const;
165165

166166
// rpc callbacks
167-
void on_response( rpc::slot_subscribe * ) override;
167+
void on_response( rpc::get_slot * ) override;
168168
void on_response( rpc::get_recent_block_hash * ) override;
169169
void on_response( rpc::program_subscribe * ) override;
170170
void set_status( int );
@@ -228,6 +228,7 @@ namespace pc
228228
int64_t ctimeout_; // connection timeout
229229
uint64_t slot_; // current slot
230230
uint64_t slot_cnt_; // slot count
231+
int64_t slot_ts_; // current slot time
231232
int64_t curr_ts_; // current time
232233
int64_t pub_ts_; // start publish time
233234
int64_t pub_int_; // publish interval
@@ -241,7 +242,7 @@ namespace pc
241242
commitment cmt_; // account get/subscribe commitment
242243

243244
// requests
244-
rpc::slot_subscribe sreq_[1]; // slot subscription
245+
rpc::get_slot sreq_[1]; // slot subscription
245246
rpc::get_recent_block_hash breq_[1]; // block hash request
246247
rpc::program_subscribe preq_[1]; // program account subscription
247248
};

pc/rpc_client.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,38 @@ void rpc::get_cluster_nodes::response( const jtree& jt )
637637
on_response( this );
638638
}
639639

640+
///////////////////////////////////////////////////////////////////////////
641+
// get_slot
642+
643+
rpc::get_slot::get_slot( commitment const cmt )
644+
: cmt_{ cmt }
645+
, cslot_( 0UL )
646+
{
647+
}
648+
649+
uint64_t rpc::get_slot::get_current_slot() const
650+
{
651+
return cslot_;
652+
}
653+
654+
void rpc::get_slot::request( json_wtr& msg )
655+
{
656+
msg.add_key( "method", "getSlot" );
657+
msg.add_key( "params", json_wtr::e_arr );
658+
msg.add_val( json_wtr::e_obj );
659+
msg.add_key( "commitment", commitment_to_str( cmt_ ) );
660+
msg.pop();
661+
msg.pop();
662+
}
663+
664+
void rpc::get_slot::response( const jtree& jt )
665+
{
666+
if ( on_error( jt, this ) ) return;
667+
uint32_t rtok = jt.find_val( 1, "result" );
668+
cslot_ = jt.get_uint( rtok );
669+
on_response( this );
670+
}
671+
640672
///////////////////////////////////////////////////////////////////////////
641673
// get_slot_leaders
642674

pc/rpc_client.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,19 @@ namespace pc
366366
node_map_t nmap_;
367367
};
368368

369+
// get current slot
370+
class get_slot : public rpc_request
371+
{
372+
public:
373+
get_slot( commitment = e_finalized );
374+
uint64_t get_current_slot() const;
375+
void request( json_wtr& ) override;
376+
void response( const jtree& ) override;
377+
private:
378+
commitment const cmt_; // param
379+
uint64_t cslot_; // result
380+
};
381+
369382
// get id of leader node by slot
370383
class get_slot_leaders : public rpc_request
371384
{
@@ -376,7 +389,7 @@ namespace pc
376389
pub_key *get_leader( uint64_t );
377390
uint64_t get_last_slot() const;
378391
void request( json_wtr& ) override;
379-
void response( const jtree&p) override;
392+
void response( const jtree& ) override;
380393
private:
381394
typedef std::vector<pub_key> ldr_vec_t;
382395
uint64_t rslot_;

pctest/slots_info.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class manager_slot : public manager,
1313
{
1414
public:
1515
manager_slot();
16-
void on_response( rpc::slot_subscribe * ) override;
16+
void on_response( rpc::get_slot * ) override;
1717
void on_response( rpc::get_slot_leaders * ) override;
1818
private:
1919
rpc::get_slot_leaders ldr_[1];
@@ -27,10 +27,10 @@ manager_slot::manager_slot()
2727
ldr_->set_limit( PC_LEADER_MAX );
2828
}
2929

30-
void manager_slot::on_response( rpc::slot_subscribe *res )
30+
void manager_slot::on_response( rpc::get_slot *res )
3131
{
3232
manager::on_response( res );
33-
uint64_t slot = get_slot();
33+
uint64_t const slot = get_slot();
3434
if ( slot != last_ ) {
3535
// request next slot leader schedule
3636
if ( PC_UNLIKELY( ldr_->get_is_recv() &&

0 commit comments

Comments
 (0)