Skip to content

Commit 65d709c

Browse files
committed
set min pub
1 parent 4fa4f9f commit 65d709c

File tree

11 files changed

+406
-5
lines changed

11 files changed

+406
-5
lines changed

pc/request.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,11 @@ int64_t price::get_price_exponent() const
552552
return pptr_->expo_;
553553
}
554554

555+
uint8_t price::get_min_pub() const
556+
{
557+
return pptr_->min_pub_;
558+
}
559+
555560
price_type price::get_price_type() const
556561
{
557562
return(price_type) pptr_->ptype_;

pc/request.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ namespace pc
275275
const pub_key *get_account() const;
276276
price_type get_price_type() const;
277277
int64_t get_price_exponent() const;
278+
uint8_t get_min_pub() const;
278279
uint32_t get_version() const;
279280
int64_t get_price() const;
280281
uint64_t get_conf() const;

pcapps/admin_request.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,105 @@ bool init_price::get_is_done() const
744744
return st_ == e_done;
745745
}
746746

747+
///////////////////////////////////////////////////////////////////////////
748+
// set_min_pub_req
749+
750+
set_min_pub_req::set_min_pub_req()
751+
: st_( e_init_sent ),
752+
cmt_( commitment::e_confirmed )
753+
{
754+
}
755+
756+
void set_min_pub_req::set_min_pub( uint8_t min_pub )
757+
{
758+
req_->set_min_pub( min_pub );
759+
}
760+
761+
void set_min_pub_req::set_price( price *px )
762+
{
763+
price_ = px;
764+
}
765+
766+
void set_min_pub_req::set_commitment( commitment cmt )
767+
{
768+
cmt_ = cmt;
769+
}
770+
771+
bool set_min_pub_req::get_is_ready()
772+
{
773+
manager *cptr = get_manager();
774+
if ( !cptr->get_mapping_pub_key() ) {
775+
return set_err_msg( "missing mapping key pairfile ["
776+
+ cptr->get_mapping_key_pair_file() + "]" );
777+
}
778+
return cptr->has_status( PC_PYTH_RPC_CONNECTED |
779+
PC_PYTH_HAS_BLOCK_HASH |
780+
PC_PYTH_HAS_MAPPING );
781+
}
782+
783+
void set_min_pub_req::submit()
784+
{
785+
manager *cptr = get_manager();
786+
key_pair *pkey = cptr->get_publish_key_pair();
787+
if ( !pkey ) {
788+
on_error_sub( "missing or invalid publish key [" +
789+
cptr->get_publish_key_pair_file() + "]", this );
790+
return;
791+
}
792+
pub_key *gpub = cptr->get_program_pub_key();
793+
if ( !gpub ) {
794+
on_error_sub( "missing or invalid program public key [" +
795+
cptr->get_program_pub_key_file() + "]", this );
796+
return;
797+
}
798+
pub_key *pub = price_->get_account();
799+
if ( !cptr->get_account_key_pair( *pub, key_ ) ) {
800+
std::string knm;
801+
pub->enc_base58( knm );
802+
on_error_sub( "missing key pair for price acct [" + knm + "]", this);
803+
return;
804+
}
805+
req_->set_program( gpub );
806+
req_->set_publish( pkey );
807+
req_->set_account( &key_ );
808+
req_->set_sub( this );
809+
sig_->set_sub( this );
810+
811+
// get recent block hash and submit request
812+
st_ = e_init_sent;
813+
req_->set_block_hash( get_manager()->get_recent_block_hash() );
814+
get_rpc_client()->send( req_ );
815+
}
816+
817+
void set_min_pub_req::on_response( rpc::signature_subscribe *res )
818+
{
819+
if ( res->get_is_err() ) {
820+
on_error_sub( res->get_err_msg(), this );
821+
st_ = e_error;
822+
} else if ( st_ == e_init_sig ) {
823+
st_ = e_done;
824+
on_response_sub( this );
825+
}
826+
}
827+
828+
void set_min_pub_req::on_response( rpc::set_min_pub_rpc *res )
829+
{
830+
if ( res->get_is_err() ) {
831+
on_error_sub( res->get_err_msg(), this );
832+
st_ = e_error;
833+
} else if ( st_ == e_init_sent ) {
834+
st_ = e_init_sig;
835+
sig_->set_commitment( cmt_ );
836+
sig_->set_signature( res->get_signature() );
837+
get_rpc_client()->send( sig_ );
838+
}
839+
}
840+
841+
bool set_min_pub_req::get_is_done() const
842+
{
843+
return st_ == e_done;
844+
}
845+
747846
///////////////////////////////////////////////////////////////////////////
748847
// add_publisher
749848

pcapps/admin_request.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,39 @@ namespace pc
220220
rpc::signature_subscribe sig_[1];
221221
};
222222

223+
// (re)initialize price account to reflect exponent change
224+
class set_min_pub_req : public request,
225+
public rpc_sub_i<rpc::signature_subscribe>,
226+
public rpc_sub_i<rpc::set_min_pub_rpc>
227+
{
228+
public:
229+
set_min_pub_req();
230+
void set_min_pub( uint8_t );
231+
void set_price( price * );
232+
void set_commitment( commitment );
233+
bool get_is_done() const override;
234+
235+
public:
236+
void on_response( rpc::signature_subscribe * ) override;
237+
void on_response( rpc::set_min_pub_rpc * ) override;
238+
bool get_is_ready() override;
239+
void submit() override;
240+
241+
private:
242+
using request::on_response;
243+
244+
typedef enum {
245+
e_init_sent, e_init_sig, e_done, e_error
246+
} state_t;
247+
248+
state_t st_;
249+
commitment cmt_;
250+
price *price_;
251+
key_pair key_;
252+
rpc::set_min_pub_rpc req_[1];
253+
rpc::signature_subscribe sig_[1];
254+
};
255+
223256
// add new price publisher to symbol account
224257
class add_publisher : public request,
225258
public rpc_sub_i<rpc::signature_subscribe>,

pcapps/admin_rpc_client.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,105 @@ void rpc::init_price::response( const jtree& jt )
856856
on_response( this );
857857
}
858858

859+
///////////////////////////////////////////////////////////////////////////
860+
// set_min_pub_rpc
861+
862+
void rpc::set_min_pub_rpc::set_min_pub( uint8_t min_pub )
863+
{
864+
min_pub_ = min_pub;
865+
}
866+
867+
void rpc::set_min_pub_rpc::set_program( pub_key *gkey )
868+
{
869+
gkey_ = gkey;
870+
}
871+
872+
void rpc::set_min_pub_rpc::set_block_hash( hash *bhash )
873+
{
874+
bhash_ = bhash;
875+
}
876+
877+
void rpc::set_min_pub_rpc::set_publish( key_pair *pkey )
878+
{
879+
pkey_ = pkey;
880+
}
881+
882+
void rpc::set_min_pub_rpc::set_account( key_pair *akey )
883+
{
884+
akey_ = akey;
885+
}
886+
887+
signature *rpc::set_min_pub_rpc::get_signature()
888+
{
889+
return &sig_;
890+
}
891+
892+
rpc::set_min_pub_rpc::set_min_pub_rpc()
893+
: min_pub_( 0 ),
894+
bhash_( nullptr ),
895+
pkey_( nullptr ),
896+
gkey_( nullptr ),
897+
akey_( nullptr )
898+
{
899+
}
900+
901+
void rpc::set_min_pub_rpc::request( json_wtr& msg )
902+
{
903+
// construct binary transaction
904+
net_buf *bptr = net_buf::alloc();
905+
bincode tx( bptr->buf_ );
906+
907+
// signatures section
908+
tx.add_len<2>(); // three signatures (publish, price)
909+
size_t pub_idx = tx.reserve_sign();
910+
size_t prc_idx = tx.reserve_sign();
911+
912+
// message header
913+
size_t tx_idx = tx.get_pos();
914+
tx.add( (uint8_t)2 ); // pub and price signing accounts
915+
tx.add( (uint8_t)0 ); // read-only signed accounts
916+
tx.add( (uint8_t)1 ); // program-id are read-only unsigned accounts
917+
918+
// accounts
919+
tx.add_len<3>(); // 3 accounts: publish, price, params, program
920+
tx.add( *pkey_ ); // publish account
921+
tx.add( *akey_ ); // price account
922+
tx.add( *gkey_ ); // programid
923+
924+
// recent block hash
925+
tx.add( *bhash_ ); // recent block hash
926+
927+
// instructions section
928+
tx.add_len<1>(); // one instruction
929+
tx.add( (uint8_t)2); // program_id index
930+
tx.add_len<2>(); // 2 accounts: publish, price
931+
tx.add( (uint8_t)0 ); // index of publish account
932+
tx.add( (uint8_t)1 ); // index of price account
933+
934+
// instruction parameter section
935+
tx.add_len<sizeof(cmd_set_min_pub)>();
936+
tx.add( (uint32_t)PC_VERSION );
937+
tx.add( (int32_t)e_cmd_set_min_pub );
938+
tx.add( min_pub_ );
939+
tx.add( (uint8_t)0 );
940+
tx.add( (uint16_t)0 );
941+
942+
// all accounts need to sign transaction
943+
tx.sign( pub_idx, tx_idx, *pkey_ );
944+
sig_.init_from_buf( (const uint8_t*)(tx.get_buf() + pub_idx) );
945+
tx.sign( prc_idx, tx_idx, *akey_ );
946+
947+
// encode transaction and add to json params
948+
send_transaction( msg, tx );
949+
bptr->dealloc();
950+
}
951+
952+
void rpc::set_min_pub_rpc::response( const jtree& jt )
953+
{
954+
if ( on_error( jt, this ) ) return;
955+
on_response( this );
956+
}
957+
859958
///////////////////////////////////////////////////////////////////////////
860959
// add_publisher
861960

pcapps/admin_rpc_client.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,32 @@ namespace pc {
235235
signature sig_;
236236
};
237237

238+
// set min publishers on price account
239+
class set_min_pub_rpc : public rpc_request
240+
{
241+
public:
242+
void set_min_pub( uint8_t );
243+
void set_program( pub_key * );
244+
void set_block_hash( hash * );
245+
void set_publish( key_pair * );
246+
void set_account( key_pair * );
247+
248+
// results
249+
signature *get_signature();
250+
251+
set_min_pub_rpc();
252+
void request( json_wtr& ) override;
253+
void response( const jtree& ) override;
254+
255+
private:
256+
uint8_t min_pub_;
257+
hash *bhash_;
258+
key_pair *pkey_;
259+
pub_key *gkey_;
260+
key_pair *akey_;
261+
signature sig_;
262+
};
263+
238264
// add new price publisher to symbol account
239265
class add_publisher : public rpc_request
240266
{

pcapps/pyth.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ static void print_product( product *prod )
371371
<< std::endl;
372372
print_val( "price_exponent", 4 );
373373
std::cout << ptr->get_price_exponent() << std::endl;
374+
print_val( "min_pub", 4 );
375+
std::cout << static_cast< unsigned >( ptr->get_min_pub() ) << std::endl;
374376
print_val( "status", 4 );
375377
std::cout << symbol_status_to_str( ptr->get_status() ).as_string()
376378
<< std::endl;

0 commit comments

Comments
 (0)