From 31897a1b2ef2afaed1f1800008750b2e002c5ba4 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 16 Aug 2022 15:01:50 -0400 Subject: [PATCH] refactor: Change exch-open-order record_id to auto-incremented int To insulate ourselves from potential duplicate-id bugs at the exchange --- src/lib/api/services/OrderService.proto | 4 ++-- src/lib/orderman/OrderServiceServer.cpp | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index cf723882e..77dfbf2f6 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -726,7 +726,7 @@ message RetrievedExchangePosition{ } message RetrievedExchangeOpenOrder{ - string record_id = 1 [(bts.api.key)=true]; + uint32 record_id = 1 [(bts.api.key)=true]; string exchange_sec_desc = 10; string exchange_account = 20; string exchange_order_id = 30; @@ -744,4 +744,4 @@ message RetrievedExchangeOpenOrder{ uint64 last_update_timestamp = 140; bool api = 150; bool web = 160; -} \ No newline at end of file +} diff --git a/src/lib/orderman/OrderServiceServer.cpp b/src/lib/orderman/OrderServiceServer.cpp index 955899332..63931c9c5 100644 --- a/src/lib/orderman/OrderServiceServer.cpp +++ b/src/lib/orderman/OrderServiceServer.cpp @@ -35,6 +35,7 @@ #include #include +#include namespace bts { namespace orderman { @@ -99,6 +100,22 @@ ActorThread *find_thread_for_actor( return nullptr; } +class OpenOrderRecordIDStore { + public: + std::uint32_t get_rec_id(std::string exch_order_id) { + auto [it, is_new] = + rec_ids_.try_emplace(exch_order_id, rec_id_counter_); + if (is_new) { + rec_id_counter_++; + } + return it->second; + } + + private: + std::uint32_t rec_id_counter_ = 1; + std::unordered_map rec_ids_; +}; + struct OrderServiceServer::PImpl { PImpl( bts::api::ServiceContext &ctx, @@ -141,6 +158,7 @@ struct OrderServiceServer::PImpl { std::unordered_map> in_flight_order_ops; uint32_t current_req_op_id; + OpenOrderRecordIDStore open_order_id_store; }; OrderServiceServer::OrderServiceServer( @@ -495,8 +513,8 @@ void OrderServiceServer::_on_exchange_open_orders( ev.open_orders.all_open_orders.size()); for (const auto &o : ev.open_orders.all_open_orders) { bts::api::orderman::RetrievedExchangeOpenOrder o_ord = {}; - std::string id = o.exchange_order_id; - o_ord.set_record_id(id); + auto rec_id = me->open_order_id_store.get_rec_id(o.exchange_order_id); + o_ord.set_record_id(rec_id); o_ord.set_exchange_order_id(o.exchange_order_id); o_ord.set_exchange_sec_desc(o.exchange_sec_desc); o_ord.set_exchange_account(o.exchange_account);