Skip to content

Commit bf61565

Browse files
clean-up error handling around price submission
1 parent c3066d7 commit bf61565

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
lines changed

pc/request.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,11 @@ void price::reset()
11541154
reset_err();
11551155
}
11561156

1157+
bool price::has_publisher()
1158+
{
1159+
return has_publisher( *pkey_ );
1160+
}
1161+
11571162
bool price::has_publisher( const pub_key& key )
11581163
{
11591164
pc_pub_key_t *pk = (pc_pub_key_t*)key.data();
@@ -1192,18 +1197,16 @@ bool price::update(
11921197
if ( PC_UNLIKELY( !init_ && !init_publish() ) ) {
11931198
return false;
11941199
}
1195-
if ( PC_UNLIKELY( !has_publisher( *pkey_ ) ) ) {
1196-
return set_err_msg( "not permissioned to update price" );
1200+
if ( PC_UNLIKELY( !has_publisher() ) ) {
1201+
return false;
11971202
}
1198-
manager *cptr = get_manager();
1199-
if ( cptr && st_ == e_publish ) {
1200-
st_ = e_pend_publish;
1201-
preq_->set_price( price, conf, st, is_agg );
1202-
cptr->submit( this );
1203-
return true;
1204-
} else {
1203+
if ( PC_UNLIKELY( st_ != e_publish ) ) {
12051204
return false;
12061205
}
1206+
st_ = e_pend_publish;
1207+
preq_->set_price( price, conf, st, is_agg );
1208+
get_manager()->submit( this );
1209+
return true;
12071210
}
12081211

12091212
void price::submit()
@@ -1449,7 +1452,5 @@ void price_sched::submit()
14491452

14501453
void price_sched::schedule()
14511454
{
1452-
if ( ptr_->get_is_ready_publish() ) {
1453-
on_response_sub( this );
1454-
}
1455+
on_response_sub( this );
14551456
}

pc/request.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ namespace pc
400400

401401
price( const pub_key& );
402402

403+
// is current publisher authorized to publish on this symbol
404+
bool has_publisher();
405+
403406
// is publisher authorized to publish on this symbol
404407
bool has_publisher( const pub_key& );
405408

@@ -409,6 +412,7 @@ namespace pc
409412
// submit new price update and update aggregate
410413
// will fail with false if in error (check get_is_err() )
411414
// or because symbol is not ready to publish (get_is_ready_publish())
415+
// or because publisher does not have permission (has_publisher())
412416
bool update( int64_t price, uint64_t conf, symbol_status );
413417

414418
// update aggregate price only

pc/user.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#define PC_JSON_UNKNOWN_METHOD -32601
1010
#define PC_JSON_INVALID_PARAMS -32602
1111
#define PC_JSON_UNKNOWN_SYMBOL -32000
12+
#define PC_JSON_MISSING_PERMS -32001
13+
#define PC_JSON_NOT_READY -32002
1214

1315
using namespace pc;
1416

@@ -189,8 +191,14 @@ void user::parse_upd_price( uint32_t tok, uint32_t itok )
189191
add_header();
190192
jw_.add_key( "result", 0UL );
191193
add_tail( itok );
194+
} else if ( !sptr->get_is_ready_publish() ) {
195+
add_error( itok, PC_JSON_NOT_READY, "not ready to publish" );
196+
} else if ( !sptr->has_publisher() ) {
197+
add_error( itok, PC_JSON_MISSING_PERMS, "missing publish permission" );
192198
} else if ( sptr->get_is_err() ) {
193199
add_error( itok, PC_JSON_INVALID_REQUEST, sptr->get_err_msg() );
200+
} else {
201+
add_error( itok, PC_JSON_INVALID_REQUEST, "unknown error" );
194202
}
195203
return;
196204
} while( 0 );

pctest/test_publish.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,7 @@ void test_publish::on_response( pc::price_sched *ptr, uint64_t sub_id )
176176
}
177177

178178
// submit next price to block chain for this symbol
179-
if ( !sym->update( px_, sprd_, pc::symbol_status::e_trading ) ) {
180-
PC_LOG_ERR( "failed to submit price" )
181-
.add( "symbol", *sym->get_symbol() )
182-
.add( "price_type", pc::price_type_to_str( sym->get_price_type() ) )
183-
.add( "err_msg", sym->get_err_msg() )
184-
.end();
185-
} else {
179+
if ( sym->update( px_, sprd_, pc::symbol_status::e_trading ) ) {
186180
double price = expo_ * (double)px_;
187181
double spread = expo_ * (double)sprd_;
188182
PC_LOG_INF( "submit price to block-chain" )
@@ -195,6 +189,27 @@ void test_publish::on_response( pc::price_sched *ptr, uint64_t sub_id )
195189
.end();
196190
// increase price
197191
px_ += sprd_;
192+
} else if ( !sym->has_publisher() ) {
193+
PC_LOG_WRN( "missing publish permission" )
194+
.add( "symbol", *sym->get_symbol() )
195+
.add( "price_type", pc::price_type_to_str( sym->get_price_type() ) )
196+
.end();
197+
// should work once publisher has been permissioned
198+
} else if ( !sym->get_is_ready_publish() ) {
199+
PC_LOG_WRN( "not ready to publish next price" )
200+
.add( "symbol", *sym->get_symbol() )
201+
.add( "price_type", pc::price_type_to_str( sym->get_price_type() ) )
202+
.end();
203+
// could be delay in confirmation - try again next time
204+
} else if ( sym->get_is_err() ) {
205+
PC_LOG_WRN( "block-chain error" )
206+
.add( "symbol", *sym->get_symbol() )
207+
.add( "price_type", pc::price_type_to_str( sym->get_price_type() ) )
208+
.add( "err_msg", sym->get_err_msg() )
209+
.end();
210+
unsubscribe();
211+
// either bad config or on-chain program problem - cant continue as is
212+
// could try calling reset_err() and continue once error is resolved
198213
}
199214
}
200215

pctest/test_pyth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ async def test_2(uri1,uri2):
146146
await ws2.send( '{"id":18,"method":"update_price", "params":'
147147
'{"symbol":"US.EQ.SYMBOL3", "price_type": "price", "price": 1, '
148148
'"conf":2, "status": "trading"}}' )
149-
check_error_obj( -32600, await ws2.recv(), idval=18 )
149+
check_error_obj( -32001, await ws2.recv(), idval=18 )
150150

151151
print('submitting subscriptions for symbols1, 2, 3')
152152
req = [ gen_sub( 'US.EQ.SYMBOL1', 1 ),

0 commit comments

Comments
 (0)