This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
forked from integritee-network/worker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolkadex_orderbook_storage.rs
182 lines (157 loc) · 6.6 KB
/
polkadex_orderbook_storage.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// This file is part of Polkadex.
// Copyright (C) 2020-2021 Polkadex oü and Supercomputing Systems AG
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//use crate::_write_order_to_disk;
use crate::channel_storage::{load_sender, ChannelType};
use crate::polkadex_gateway::GatewayError;
use log::error;
use log::*;
use polkadex_sgx_primitives::types::{Order, OrderUUID, SignedOrder};
use polkadex_sgx_primitives::OrderbookData;
use sgx_types::{sgx_status_t, SgxResult};
use sp_core::ed25519::Signature;
use std::collections::HashMap;
use std::sync::{
atomic::{AtomicPtr, Ordering},
Arc, SgxMutex, SgxMutexGuard,
};
use std::vec::Vec;
use substratee_sgx_crypto::Ed25519Seal;
use substratee_sgx_io::SealedIO;
static GLOBAL_ORDERBOOK_STORAGE: AtomicPtr<()> = AtomicPtr::new(0 as *mut ());
#[derive(Debug)]
pub struct OrderbookStorage {
storage: HashMap<OrderUUID, Order>,
}
impl OrderbookStorage {
pub fn create(verified_orders: Vec<SignedOrder>) -> OrderbookStorage {
let mut storage: HashMap<OrderUUID, Order> = HashMap::new();
for order in verified_orders {
storage.insert(order.order_id, order.order);
}
OrderbookStorage { storage }
}
/// Inserts a order_uid-order pair into the orderbook.
/// If the orderbook did not have this order_uid present, [None] is returned.
/// If the orderbook did have this order_uid present, the order is updated, and the old order is returned.
pub fn add_order(&mut self, order_uid: OrderUUID, order: Order) -> Option<Order> {
debug!("Adding order with uid: {:?}", order_uid);
self.storage.insert(order_uid, order)
}
/// Removes a order_uid from the orderbook,
/// returning the value at the order_uid if the order_uid was previously in the map.
#[allow(clippy::ptr_arg)]
pub fn remove_order(&mut self, order_uid: &OrderUUID) -> Option<Order> {
debug!("Removing order with uid: {:?}", order_uid);
self.storage.remove(order_uid)
}
/// Returns a reference to the order corresponding to the order_uid.
#[allow(clippy::ptr_arg)]
pub fn read_order(&self, order_uid: &OrderUUID) -> Option<&Order> {
debug!("Reading order with uid: {:?}", order_uid);
self.storage.get(order_uid)
}
pub fn _write_orderbook_to_db(order_id: OrderUUID, order: Order) -> SgxResult<()> {
let signer_pair = Ed25519Seal::unseal()?;
let mut signed_order = SignedOrder {
order_id,
order,
signature: Signature::default(),
};
signed_order.sign(&signer_pair);
load_sender()
.map_err(|_| sgx_status_t::SGX_ERROR_UNEXPECTED)?
.send(ChannelType::Order(signed_order))
.map_err(|_| sgx_status_t::SGX_ERROR_UNEXPECTED)?;
//_write_order_to_disk(signed_order)?;
Ok(())
}
pub fn extend_from_disk_data(&mut self, data: Vec<OrderbookData>) {
self.storage.extend(
data.into_iter()
.map(|entry| (entry.signed_order.order_id, entry.signed_order.order)),
);
}
}
/// Creates a Static Atomic Pointer for Orderbook Storage
pub fn create_in_memory_orderbook_storage(signed_orders: Vec<SignedOrder>) -> SgxResult<()> {
let mut verified_orders: Vec<SignedOrder> = vec![];
let signer_pair = Ed25519Seal::unseal()?;
for order in signed_orders {
if !order.verify_signature(&signer_pair) {
error!("Signature Verification Failed");
continue;
}
verified_orders.push(order)
}
let orderbook = OrderbookStorage::create(verified_orders);
let storage_ptr = Arc::new(SgxMutex::<OrderbookStorage>::new(orderbook));
let ptr = Arc::into_raw(storage_ptr);
GLOBAL_ORDERBOOK_STORAGE.store(ptr as *mut (), Ordering::SeqCst);
Ok(())
}
/// Loads and Returns Orderbook under mutex from Static Atomics Pointer
pub fn load_orderbook() -> Result<&'static SgxMutex<OrderbookStorage>, GatewayError> {
let ptr = GLOBAL_ORDERBOOK_STORAGE.load(Ordering::SeqCst) as *mut SgxMutex<OrderbookStorage>;
if ptr.is_null() {
Err(GatewayError::UnableToLoadPointer)
} else {
Ok(unsafe { &*ptr })
}
}
// TODO: Write test cases for this function
#[allow(clippy::ptr_arg)]
pub fn lock_storage_and_remove_order(order_uuid: &OrderUUID) -> Result<Order, GatewayError> {
let mutex = load_orderbook()?;
// TODO: Handle this unwrap
let mut orderbook: SgxMutexGuard<OrderbookStorage> = mutex.lock().unwrap();
if let Some(order) = orderbook.remove_order(order_uuid) {
Ok(order)
} else {
Err(GatewayError::OrderNotFound)
}
}
// TODO: Write test cases for this function
pub fn lock_storage_and_add_order(
order: Order,
order_uuid: OrderUUID,
) -> Result<Option<Order>, GatewayError> {
let mutex = load_orderbook()?;
// TODO: Handle this unwrap
let mut orderbook: SgxMutexGuard<OrderbookStorage> = mutex.lock().unwrap();
Ok(orderbook.add_order(order_uuid, order))
}
// pub fn lock_storage_and_check_order_in_orderbook(
// order_uuid: OrderUUID,
// ) -> Result<bool, GatewayError> {
// let mutex = load_orderbook().unwrap();
// let orderbook: SgxMutexGuard<OrderbookStorage> = mutex.lock().unwrap();
// Ok(orderbook.storage.contains_key(&order_uuid))
// }
// Only for test
// pub fn lock_storage_and_get_order(order_uuid: OrderUUID) -> Result<Order, GatewayError> {
// let mutex = load_orderbook()?;
// let orderbook: SgxMutexGuard<OrderbookStorage> = mutex.lock().unwrap();
// let order = orderbook.read_order(&order_uuid).unwrap().clone();
// Ok(order)
// }
pub fn lock_storage_extend_from_disk(data: Vec<OrderbookData>) -> Result<(), GatewayError> {
// Acquire lock on balance_storage
let mutex = load_orderbook()?;
let mut orderbook_storage: SgxMutexGuard<OrderbookStorage> = mutex.lock().map_err(|_| {
error!("Could not lock mutex of balance storage");
GatewayError::UnableToLock
})?;
orderbook_storage.extend_from_disk_data(data);
Ok(())
}