Skip to content

Commit c2c30a7

Browse files
pyth version 2: aggregate price algo and up to 32 publishers
1 parent bb4c4f9 commit c2c30a7

38 files changed

+4151
-219
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ add_executable( test_net pctest/test_net.cpp )
9696
target_link_libraries( test_net ${PC_DEP} )
9797
add_executable( test_publish pctest/test_publish.cpp )
9898
target_link_libraries( test_publish ${PC_DEP} )
99+
add_executable( test_qset pctest/test_qset.cpp )
100+
target_link_libraries( test_qset ${PC_DEP} )
101+
add_executable( test_slot pctest/test_slot.cpp )
102+
target_link_libraries( test_slot ${PC_DEP} )
99103

100104
add_test( test_unit test_unit )
101105
add_test( test_net test_net )

pc/bincode.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace pc
3030

3131
// sign message at position sig for message starting at position msg
3232
void sign( size_t sig, size_t msg, const key_pair& );
33+
void sign( size_t sig, size_t msg, const key_cache& );
3334

3435
// add values to buffer
3536
void add( uint8_t );
@@ -193,4 +194,10 @@ namespace pc
193194
sptr->sign( (const uint8_t*)&buf_[msg], idx_ - msg, kp );
194195
}
195196

197+
inline void bincode::sign( size_t sig, size_t msg, const key_cache& kp )
198+
{
199+
signature *sptr = (signature*)&buf_[sig];
200+
sptr->sign( (const uint8_t*)&buf_[msg], idx_ - msg, kp );
201+
}
202+
196203
}

pc/key_pair.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,31 @@ void key_pair::get_pub_key( pub_key& pk ) const
161161
pk.init_from_buf( &pk_[pub_key::len] );
162162
}
163163

164+
key_cache::key_cache()
165+
: ptr_( nullptr )
166+
{
167+
}
168+
169+
key_cache::~key_cache()
170+
{
171+
if ( ptr_ ) {
172+
EVP_PKEY_free( (EVP_PKEY*)ptr_ );
173+
ptr_ = nullptr;
174+
}
175+
}
176+
177+
void key_cache::set( const key_pair& pk )
178+
{
179+
EVP_PKEY *pkey = EVP_PKEY_new_raw_private_key(
180+
EVP_PKEY_ED25519, NULL, pk.data(), pub_key::len );
181+
ptr_ = (void*)pkey;
182+
}
183+
184+
void *key_cache::get() const
185+
{
186+
return ptr_;
187+
}
188+
164189
void signature::init_from_buf( const uint8_t *buf )
165190
{
166191
__builtin_memcpy( sig_, buf, len );
@@ -204,6 +229,23 @@ bool signature::sign(
204229
return rc != 0;
205230
}
206231

232+
bool signature::sign(
233+
const uint8_t* msg, uint32_t msg_len, const key_cache& kp )
234+
{
235+
EVP_PKEY *pkey = (EVP_PKEY*)kp.get();
236+
if ( !pkey ) {
237+
return false;
238+
}
239+
EVP_MD_CTX *mctx = EVP_MD_CTX_new();
240+
int rc = EVP_DigestSignInit( mctx, NULL, NULL, NULL, pkey );
241+
if ( rc ) {
242+
size_t sig_len[1] = { len };
243+
rc = EVP_DigestSign( mctx, sig_, sig_len, msg, msg_len );
244+
}
245+
EVP_MD_CTX_free( mctx );
246+
return rc != 0;
247+
}
248+
207249
bool signature::verify(
208250
const uint8_t* msg, uint32_t msg_len, const pub_key& pk )
209251
{

pc/key_pair.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ namespace pc
7272
uint8_t pk_[len];
7373
};
7474

75+
// opaque ssl key handle
76+
class key_cache
77+
{
78+
public:
79+
key_cache();
80+
~key_cache();
81+
void set( const key_pair& );
82+
void *get() const;
83+
private:
84+
void *ptr_;
85+
};
86+
7587
// digital signature
7688
class signature
7789
{
@@ -90,6 +102,8 @@ namespace pc
90102
// sign message given key_pair
91103
bool sign( const uint8_t* msg, uint32_t msg_len,
92104
const key_pair& );
105+
bool sign( const uint8_t* msg, uint32_t msg_len,
106+
const key_cache& );
93107

94108
// verify message given public key
95109
bool verify( const uint8_t* msg, uint32_t msg_len,

pc/key_store.cpp

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ key_store::key_store()
4141
has_mkey_( false ),
4242
has_mpub_( false ),
4343
has_gkey_( false ),
44-
has_gpub_( false )
44+
has_gpub_( false ),
45+
has_akey_( false ),
46+
has_apub_( false )
4547
{
4648
}
4749

@@ -126,6 +128,16 @@ std::string key_store::get_program_pub_key_file() const
126128
return dir_ + "program_key.json";
127129
}
128130

131+
std::string key_store::get_param_key_pair_file() const
132+
{
133+
return dir_ + "param_key_pair.json";
134+
}
135+
136+
std::string key_store::get_param_pub_key_file() const
137+
{
138+
return dir_ + "param_key.json";
139+
}
140+
129141
key_pair *key_store::create_publish_key_pair()
130142
{
131143
pkey_.gen();
@@ -146,10 +158,20 @@ key_pair *key_store::get_publish_key_pair()
146158
return nullptr;
147159
}
148160
has_pkey_ = true;
161+
ckey_.set( pkey_ );
149162
pkey_.get_pub_key( ppub_ );
150163
return &pkey_;
151164
}
152165

166+
key_cache *key_store::get_publish_key_cache()
167+
{
168+
if ( get_publish_key_pair() ) {
169+
return &ckey_;
170+
} else {
171+
return nullptr;
172+
}
173+
}
174+
153175
pub_key *key_store::get_publish_pub_key()
154176
{
155177
if ( has_pkey_ ) {
@@ -243,6 +265,47 @@ pub_key *key_store::get_program_pub_key()
243265
return &gpub_;
244266
}
245267

268+
key_pair *key_store::create_param_key_pair()
269+
{
270+
akey_.gen();
271+
if ( !write_key_file( get_param_key_pair_file(), akey_ ) ) {
272+
return nullptr;
273+
}
274+
akey_.get_pub_key( apub_ );
275+
has_akey_ = true;
276+
has_apub_ = true;
277+
return &akey_;
278+
}
279+
280+
key_pair *key_store::get_param_key_pair()
281+
{
282+
if ( has_akey_ ) {
283+
return &akey_;
284+
}
285+
if ( !akey_.init_from_file( get_param_key_pair_file() ) ) {
286+
return nullptr;
287+
}
288+
akey_.get_pub_key( apub_ );
289+
has_akey_ = true;
290+
has_apub_ = true;
291+
return &akey_;
292+
}
293+
294+
pub_key *key_store::get_param_pub_key()
295+
{
296+
if ( has_apub_ ) {
297+
return &apub_;
298+
}
299+
if ( get_param_key_pair() ) {
300+
return &apub_;
301+
}
302+
if ( !apub_.init_from_file( get_param_pub_key_file() ) ) {
303+
return nullptr;
304+
}
305+
has_apub_ = true;
306+
return &apub_;
307+
}
308+
246309
bool key_store::create_account_key_pair( key_pair& res )
247310
{
248311
res.gen();

pc/key_store.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ namespace pc
2929
std::string get_mapping_pub_key_file() const;
3030
std::string get_program_key_pair_file() const;
3131
std::string get_program_pub_key_file() const;
32+
std::string get_param_key_pair_file() const;
33+
std::string get_param_pub_key_file() const;
3234

3335
// primary publishing and funding key
3436
key_pair *create_publish_key_pair();
3537
key_pair *get_publish_key_pair();
38+
key_cache*get_publish_key_cache();
3639
pub_key *get_publish_pub_key();
3740

3841
// get mapping key_pair or public key
@@ -45,6 +48,11 @@ namespace pc
4548
key_pair *get_program_key_pair();
4649
pub_key *get_program_pub_key();
4750

51+
// get parameter account key_pair or public key
52+
key_pair *create_param_key_pair();
53+
key_pair *get_param_key_pair();
54+
pub_key *get_param_pub_key();
55+
4856
// create new mapping or symbol account
4957
bool create_account_key_pair( key_pair& );
5058
bool get_account_key_pair( const pub_key&, key_pair& );
@@ -56,12 +64,17 @@ namespace pc
5664
bool has_mpub_;
5765
bool has_gkey_;
5866
bool has_gpub_;
67+
bool has_akey_;
68+
bool has_apub_;
5969
key_pair pkey_; // primary publishing and funding key
6070
key_pair mkey_; // mapping account key
6171
key_pair gkey_; // program key pair
72+
key_pair akey_; // parameter account key pair
73+
key_cache ckey_; // publishing cache key
6274
pub_key ppub_; // publisher public key
6375
pub_key mpub_; // mapping account public key
6476
pub_key gpub_; // program id
77+
pub_key apub_; // parameter account public key
6578
std::string dir_; // key store directory
6679
};
6780

pc/manager.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ bool manager::init()
248248
if ( gpub ) {
249249
PC_LOG_INF( "program_key" ).add( "key_name", *gpub ).end();
250250
}
251+
pub_key *rkey = get_param_pub_key();
252+
if ( rkey ) {
253+
PC_LOG_INF( "param_key" ).add( "key_name", *rkey ).end();
254+
}
251255

252256
// initialize capture
253257
if ( do_cap_ && !cap_.init() ) {

pc/manager.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ namespace pc
158158
bool get_is_tx_connect() const;
159159

160160
// rpc callbacks
161-
void on_response( rpc::slot_subscribe * );
162-
void on_response( rpc::get_recent_block_hash * );
161+
void on_response( rpc::slot_subscribe * ) override;
162+
void on_response( rpc::get_recent_block_hash * ) override;
163163
void set_status( int );
164164
get_mapping *get_last_mapping() const;
165165

0 commit comments

Comments
 (0)