Changes
This commit is contained in:
129
src/main.rs
129
src/main.rs
@@ -1,10 +1,20 @@
|
|||||||
|
//
|
||||||
|
// When inserting a new Order into the royalties tree, we must make sure the existing
|
||||||
|
// royalties don't get shared with that new node. To do this, we must "shave" down
|
||||||
|
// the tree, pushing royalties from the root down to all nodes to the left of the
|
||||||
|
// new node.
|
||||||
|
//
|
||||||
|
// Case to think about:
|
||||||
|
// Selling 140000 USD to buy 2 BTC. Weight is ===140k USD
|
||||||
|
// Selling 50000 GBP to buy 1 BTC. Weight is === 50k GBP
|
||||||
|
//
|
||||||
#![allow(unsafe_code)]
|
#![allow(unsafe_code)]
|
||||||
|
#![allow(unused_variables)]
|
||||||
|
#![allow(dead_code)]
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::cmp::min;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::ops::DerefMut;
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use rand::rngs::StdRng;
|
use rand::rngs::StdRng;
|
||||||
@@ -13,25 +23,25 @@ use finum::FiNum;
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct Trader {
|
struct Trader {
|
||||||
name: String,
|
name: String,
|
||||||
id: i32,
|
id: usize,
|
||||||
balances: HashMap<i32,FiNum>, // Maps Currency to Amount
|
balances: HashMap<usize,FiNum>, // Maps Currency to Amount
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Trader {
|
impl Trader {
|
||||||
fn new(name:&str,id:i32) -> Self {
|
fn new(name:&str,id:usize) -> Self {
|
||||||
Trader {
|
Trader {
|
||||||
name: String::from(name),
|
name: String::from(name),
|
||||||
id: id,
|
id: id,
|
||||||
balances: HashMap::new()
|
balances: HashMap::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn add_balance(&mut self, cur:i32, delta:FiNum) {
|
fn add_balance(&mut self, cur:usize, delta:FiNum) {
|
||||||
self.balances.entry(cur).and_modify(|ent| *ent+=delta ).or_insert_with(|| delta);
|
self.balances.entry(cur).and_modify(|ent| *ent+=delta ).or_insert_with(|| delta);
|
||||||
}
|
}
|
||||||
fn sub_balance(&mut self, cur:i32, delta:FiNum) {
|
fn sub_balance(&mut self, cur:usize, delta:FiNum) {
|
||||||
self.balances.entry(cur).and_modify(|ent| *ent-=delta );
|
self.balances.entry(cur).and_modify(|ent| *ent-=delta );
|
||||||
}
|
}
|
||||||
fn get_balance(&self, cur:i32) -> FiNum {
|
fn get_balance(&self, cur:usize) -> FiNum {
|
||||||
*self.balances.get(&cur).map(|bal| bal).unwrap_or(&FiNum::new(0u64))
|
*self.balances.get(&cur).map(|bal| bal).unwrap_or(&FiNum::new(0u64))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,14 +51,56 @@ struct Order {
|
|||||||
sell_qty: FiNum,
|
sell_qty: FiNum,
|
||||||
sell_remain: FiNum,
|
sell_remain: FiNum,
|
||||||
buy_qty: FiNum,
|
buy_qty: FiNum,
|
||||||
owner: i32,
|
owner: usize,
|
||||||
|
rt_loc: usize, // Location in the Royalty Tree
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct RoyaltyTree {
|
||||||
|
tree: Vec<Royalty>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RoyaltyTree {
|
||||||
|
fn new() -> Self {
|
||||||
|
RoyaltyTree { tree:Vec::new() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Royalty {
|
||||||
|
weight: FiNum, // Here and below
|
||||||
|
acc: FiNum, // Here and Below
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Royalty {
|
||||||
|
fn new(weight: FiNum) -> Self {
|
||||||
|
Royalty { weight:weight, acc:FiNum::new(0u64) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RoyaltyTree {
|
||||||
|
fn insert(&mut self, weight: FiNum) -> usize {
|
||||||
|
self.tree.push(Royalty::new(weight));
|
||||||
|
let last=self.tree.len()-1;
|
||||||
|
let forefather=wt_forefather(last);
|
||||||
|
let mut pivot=last;
|
||||||
|
while pivot!=forefather {
|
||||||
|
pivot=wt_parent(pivot);
|
||||||
|
for i in self.tree.len()-1..=pivot {
|
||||||
|
self.tree.push(Royalty::new(0.into()));
|
||||||
|
self.tree[i].weight=if let Some(v)=wt_left (i) { self.tree[v].weight } else { FiNum::zero() }
|
||||||
|
+if let Some(v)=wt_right(i) { self.tree[v].weight } else { FiNum::zero() };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.tree[last].weight+=weight;
|
||||||
|
last
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
trait Dumpable {
|
trait Dumpable {
|
||||||
fn dump(&self);
|
fn dump(&self);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Dumpable for i32 {
|
impl Dumpable for usize {
|
||||||
fn dump(&self) {
|
fn dump(&self) {
|
||||||
println!("Dump Integer: {}",self);
|
println!("Dump Integer: {}",self);
|
||||||
}
|
}
|
||||||
@@ -70,19 +122,19 @@ impl Dumpable for Order {
|
|||||||
|
|
||||||
|
|
||||||
struct Market {
|
struct Market {
|
||||||
asset_name2num: HashMap<String,i32>,
|
asset_name2num: HashMap<String,usize>,
|
||||||
asset_num2name: HashMap<i32,String>,
|
asset_num2name: HashMap<usize,String>,
|
||||||
asset_count:i32,
|
asset_count:usize,
|
||||||
money_supply: HashMap<i32,FiNum>,
|
money_supply: HashMap<usize,FiNum>,
|
||||||
traders: Vec<Trader>,
|
traders: Vec<Trader>,
|
||||||
trader_name2num: HashMap<String,i32>,
|
trader_name2num: HashMap<String,usize>,
|
||||||
orders: HashMap<(i32,i32),PQueue<Rc<RefCell<Order>>>>,
|
orders: HashMap<(usize,usize),PQueue<Rc<RefCell<Order>>>>,
|
||||||
royalties: HashMap<i32,Vec<Rc<RefCell<Order>>>>, // Active orders that are accepting asset X. They receive royalties when someone makes an order to sell X
|
royalties: HashMap<usize,RoyaltyTree>, // Active orders that are accepting asset X. They receive royalties when someone makes an order to sell X
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Market {
|
impl Market {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Market {
|
let mut rval=Market {
|
||||||
asset_name2num: HashMap::new(),
|
asset_name2num: HashMap::new(),
|
||||||
asset_num2name: HashMap::new(),
|
asset_num2name: HashMap::new(),
|
||||||
asset_count:0,
|
asset_count:0,
|
||||||
@@ -91,9 +143,12 @@ impl Market {
|
|||||||
royalties: HashMap::new(),
|
royalties: HashMap::new(),
|
||||||
traders: Vec::new(),
|
traders: Vec::new(),
|
||||||
trader_name2num: HashMap::new(),
|
trader_name2num: HashMap::new(),
|
||||||
}
|
};
|
||||||
|
rval.register_trader("*NONE*");
|
||||||
|
rval
|
||||||
}
|
}
|
||||||
fn distribute_royalty(&self, amount:FiNum) {
|
fn distribute_royalty(&self, amount:FiNum) {
|
||||||
|
|
||||||
}
|
}
|
||||||
fn sanity_check(&self) {
|
fn sanity_check(&self) {
|
||||||
println!("Sanity Checking Market...");
|
println!("Sanity Checking Market...");
|
||||||
@@ -109,32 +164,32 @@ impl Market {
|
|||||||
println!(" {}: Orders {} Traders {} Total {} Should Be {}",self.number_to_name(*cur),acc_orders,acc_traders,acc,*amt);
|
println!(" {}: Orders {} Traders {} Total {} Should Be {}",self.number_to_name(*cur),acc_orders,acc_traders,acc,*amt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn register_trader(&mut self, name:&str) -> i32 { // Add error checking for inserting a trader twice
|
fn register_trader(&mut self, name:&str) -> usize { // Add error checking for inserting a trader twice
|
||||||
let rval=self.traders.len() as i32;
|
let rval=self.traders.len();
|
||||||
self.trader_name2num.insert(String::from(name),self.traders.len() as i32);
|
self.trader_name2num.insert(String::from(name),self.traders.len());
|
||||||
self.traders.push(Trader::new(name,rval));
|
self.traders.push(Trader::new(name,rval));
|
||||||
rval
|
rval
|
||||||
}
|
}
|
||||||
// These are the only ways to get money into or out of the market.
|
// These are the only ways to get money into or out of the market.
|
||||||
fn add_trader_balance(&mut self, who:i32, cur:i32, delta: FiNum) {
|
fn add_trader_balance(&mut self, who:usize, cur:usize, delta: FiNum) {
|
||||||
self.traders[who as usize].add_balance(cur,delta);
|
self.traders[who as usize].add_balance(cur,delta);
|
||||||
*self.money_supply.get_mut(&cur).unwrap()+=delta;
|
*self.money_supply.get_mut(&cur).unwrap()+=delta;
|
||||||
}
|
}
|
||||||
fn sub_trader_balance(&mut self, who:i32, cur:i32, delta: FiNum) {
|
fn sub_trader_balance(&mut self, who:usize, cur:usize, delta: FiNum) {
|
||||||
self.traders[who as usize].sub_balance(cur,delta);
|
self.traders[who as usize].sub_balance(cur,delta);
|
||||||
*self.money_supply.get_mut(&cur).unwrap()-=delta;
|
*self.money_supply.get_mut(&cur).unwrap()-=delta;
|
||||||
}
|
}
|
||||||
fn register_asset(&mut self, name:&str) -> i32 {
|
fn register_asset(&mut self, name:&str) -> usize {
|
||||||
self.asset_count+=1;
|
self.asset_count+=1;
|
||||||
self.asset_name2num.insert(String::from(name),self.asset_count);
|
self.asset_name2num.insert(String::from(name),self.asset_count);
|
||||||
self.asset_num2name.insert(self.asset_count,String::from(name));
|
self.asset_num2name.insert(self.asset_count,String::from(name));
|
||||||
self.money_supply.insert(self.asset_count,FiNum::new(0));
|
self.money_supply.insert(self.asset_count,FiNum::new(0));
|
||||||
self.asset_count
|
self.asset_count
|
||||||
}
|
}
|
||||||
fn name_to_number(&self, name:&str) -> i32 {
|
fn name_to_number(&self, name:&str) -> usize {
|
||||||
*self.asset_name2num.get(name).unwrap()
|
*self.asset_name2num.get(name).unwrap()
|
||||||
}
|
}
|
||||||
fn number_to_name(&self, num:i32) -> &str {
|
fn number_to_name(&self, num:usize) -> &str {
|
||||||
&*self.asset_num2name.get(&num).unwrap()
|
&*self.asset_num2name.get(&num).unwrap()
|
||||||
}
|
}
|
||||||
fn dump(&self) {
|
fn dump(&self) {
|
||||||
@@ -159,7 +214,7 @@ impl Market {
|
|||||||
pq.dump();
|
pq.dump();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn make_order(&mut self, owner:i32, sell_type:i32, buy_type:i32, sell_qty_initial:FiNum, buy_qty_initial:FiNum) -> bool // Dollars, Bitcoin, 64000, 1
|
fn make_order(&mut self, owner:usize, sell_type:usize, buy_type:usize, sell_qty_initial:FiNum, buy_qty_initial:FiNum) -> bool // Dollars, Bitcoin, 64000, 1
|
||||||
{
|
{
|
||||||
let initial_balance=self.traders[owner as usize].get_balance(sell_type);
|
let initial_balance=self.traders[owner as usize].get_balance(sell_type);
|
||||||
if initial_balance<sell_qty_initial { return false; }
|
if initial_balance<sell_qty_initial { return false; }
|
||||||
@@ -185,7 +240,9 @@ impl Market {
|
|||||||
let bids=self.orders.get_mut(&ap).unwrap();
|
let bids=self.orders.get_mut(&ap).unwrap();
|
||||||
let sell_qty_remain=sell_qty_initial*buy_qty/buy_qty_initial;
|
let sell_qty_remain=sell_qty_initial*buy_qty/buy_qty_initial;
|
||||||
if sell_qty_remain>0.into() {
|
if sell_qty_remain>0.into() {
|
||||||
let neworder=Rc::new(RefCell::new(Order { owner:owner, sell_qty:sell_qty_remain, sell_remain:sell_qty_remain, buy_qty:buy_qty } ));
|
let rt_loc=self.royalties.entry(sell_type).or_insert(RoyaltyTree::new()).insert(sell_qty_remain);
|
||||||
|
let neworder=Rc::new(RefCell::new(
|
||||||
|
Order { owner:owner, sell_qty:sell_qty_remain, sell_remain:sell_qty_remain, buy_qty:buy_qty, rt_loc: rt_loc } ));
|
||||||
bids.insert(neworder);
|
bids.insert(neworder);
|
||||||
self.traders[owner as usize].sub_balance(sell_type,sell_qty_remain);
|
self.traders[owner as usize].sub_balance(sell_type,sell_qty_remain);
|
||||||
}
|
}
|
||||||
@@ -284,7 +341,7 @@ impl Market {
|
|||||||
let mut tries=0;
|
let mut tries=0;
|
||||||
for _i in 1..=10000000 {
|
for _i in 1..=10000000 {
|
||||||
let seller=if rng.gen_bool(0.5) { teppy } else { luni };
|
let seller=if rng.gen_bool(0.5) { teppy } else { luni };
|
||||||
let (buy_type,sell_type,buy_qty,sell_qty):(i32,i32,FiNum,FiNum);
|
let (buy_type,sell_type,buy_qty,sell_qty):(usize,usize,FiNum,FiNum);
|
||||||
if rng.gen_bool(0.5) {
|
if rng.gen_bool(0.5) {
|
||||||
sell_type=btc;
|
sell_type=btc;
|
||||||
buy_type=usd;
|
buy_type=usd;
|
||||||
@@ -306,21 +363,21 @@ impl Market {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wt_level(index:u64) -> u32 {
|
fn wt_level(index:usize) -> usize {
|
||||||
(index^(index+1)).trailing_ones()-1
|
(index^(index+1)).trailing_ones() as usize-1
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wt_left(index:u64) -> Option<u64> {
|
fn wt_left(index:usize) -> Option<usize> {
|
||||||
let level=wt_level(index);
|
let level=wt_level(index);
|
||||||
if level>0 { Some(index-(1<<(wt_level(index)-1))) } else { None }
|
if level>0 { Some(index-(1<<(wt_level(index)-1))) } else { None }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wt_right(index:u64) -> Option<u64> {
|
fn wt_right(index:usize) -> Option<usize> {
|
||||||
let level=wt_level(index);
|
let level=wt_level(index);
|
||||||
if level>0 { Some(index+(1<<(wt_level(index)-1))) } else { None }
|
if level>0 { Some(index+(1<<(wt_level(index)-1))) } else { None }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wt_parent(index:u64) -> u64 {
|
fn wt_parent(index:usize) -> usize {
|
||||||
let lev=wt_level(index);
|
let lev=wt_level(index);
|
||||||
let first_in_row=index%(1<<lev);
|
let first_in_row=index%(1<<lev);
|
||||||
let skip=2<<lev;
|
let skip=2<<lev;
|
||||||
@@ -331,7 +388,7 @@ fn wt_parent(index:u64) -> u64 {
|
|||||||
first_in_parent_row+nth_in_parent_row*skip_in_parent_row
|
first_in_parent_row+nth_in_parent_row*skip_in_parent_row
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wt_forefather(max_index:u64) -> u64 {
|
fn wt_forefather(max_index:usize) -> usize {
|
||||||
let mut rval=max_index;
|
let mut rval=max_index;
|
||||||
rval=rval|(rval>>1);
|
rval=rval|(rval>>1);
|
||||||
rval=rval|(rval>>2);
|
rval=rval|(rval>>2);
|
||||||
|
|||||||
Reference in New Issue
Block a user