Skip to content

Commit

Permalink
Specified auction matching, cleaned up TODOs
Browse files Browse the repository at this point in the history
Also no trailing whitespace
  • Loading branch information
Rjected committed May 24, 2019
1 parent af6f6ea commit 181c951
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 152 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ If you don't know what to work on, here are some things that generally increase
* Documentation for packages and other things
* Updates to this document that may help other contributors or developers get started
* Anything that fixes a TODO in the code
- If you find a TODO, feel free to create an issue for it! Sometimes they're just left there and get forgotten. Some TODOs might already be fixed, so if you create an issue and it turns out the TODO was out of date, then you probably shouldn't work on it.
- If you find a TODO, feel free to create an issue for it! Sometimes they're just left there and get forgotten. Some TODOs might already be fixed, so if you create an issue and it turns out the TODO was out of date, then you probably shouldn't work on it.

## Who is the maintainer?

Currently, [rjected](https://github.com/rjected) is the maintainer.
You can contact them by [email!]([email protected])

## How can I get my pull request merged?

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/mit-dci/opencx)](https://goreportcard.com/report/github.com/mit-dci/opencx)
[![GoDoc](https://godoc.org/github.com/mit-dci/opencx?status.svg)](https://godoc.org/github.com/mit-dci/opencx)

OpenCX stands for Open Cryptocurrency eXchange, it's an open-source cryptocurrency exchange toolkit originally built to help understand what a decentralized exchange should be. It's meant to be modular enough so features that increase trustlessness in cryptocurrency exchange can be implemented and experimented with. Included are packages for lightning support, RPC, authentication via the NOISE protocol, and a Golang API supporting multiple forms of authentication.
OpenCX stands for Open Cryptocurrency eXchange, it's an open-source cryptocurrency exchange toolkit originally built to help understand what a decentralized exchange should be. It's meant to be modular enough so features that increase trustlessness in cryptocurrency exchange can be implemented and experimented with. Included are packages for lightning support, RPC, authentication via the NOISE protocol, and a Golang API supporting multiple forms of authentication.
There is also a robust implementation of timed-release cryptography in the `crypto` package.

*Pull requests and issues encouraged!*
Expand Down
1 change: 0 additions & 1 deletion benchclient/channelcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

// GetLitConnection gets the lit con to pass in to lit. Maybe do this more automatically later on
// TODO: in order for all the trading to work properly we need to switch from names to pubkeys
func (cl *BenchClient) GetLitConnection() (getLitConnectionReply *cxrpc.GetLitConnectionReply, err error) {
getLitConnectionReply = new(cxrpc.GetLitConnectionReply)
getLitConnectionArgs := &cxrpc.GetLitConnectionArgs{}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ocx/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ocx

**ocx** is a command-line client for many RPC commands which OpenCX RPC packages support.
**ocx** is a command-line client for many RPC commands which OpenCX RPC packages support.
**ocx** is currently compatible with both commands in `cxrpc` as well as some in `cxauctionrpc`, so it can be used for both servers running `fred` or `opencxd`.
45 changes: 41 additions & 4 deletions crypto/rsw/rswtimelock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ func createSolveConcurrentN(time uint64, n int, t *testing.T) {
return
}

func createSolveConcurrentNBench(time uint64, n int, b *testing.B) {
doneChan := make(chan bool, n)
for i := 0; i < n; i++ {
go createSolveBench2048A2Async(time, doneChan, b)
}

// Wait for our things to return - there may be a better way to do this with select
for i := 0; i < n; i++ {
<-doneChan
}

return
}

func createSolveConcurrent(time uint64, t *testing.T) {
createSolveConcurrentN(time, runtime.NumCPU(), t)
return
Expand Down Expand Up @@ -76,6 +90,29 @@ func createSolveTest2048A2Async(time uint64, doneChan chan bool, t *testing.T) {
return
}

func createSolveBench2048A2Async(time uint64, doneChan chan bool, b *testing.B) {
key := make([]byte, 32)
copy(key[:], []byte(fmt.Sprintf("opencxcreatesolve%d", time)))
rswTimelock, err := New2048A2(key)
if err != nil {
b.Fatalf("There was an error creating a new timelock puzzle: %s", err)
}
puzzle, expectedAns, err := rswTimelock.SetupTimelockPuzzle(time)
if err != nil {
b.Fatalf("There was an error setting up the timelock puzzle: %s\n", err)
}
puzzleAns, err := puzzle.Solve()
if err != nil {
b.Fatalf("Error solving puzzle: %s\n", err)
}
if !bytes.Equal(puzzleAns, expectedAns) {
b.Fatalf("Answer did not equal puzzle for time = %d. Expected %x, got %x\n", time, expectedAns, puzzleAns)
}

doneChan <- true
return
}

func createSolveBench2048A2(time uint64, b *testing.B) {
key := make([]byte, 32)
copy(key[:], []byte(fmt.Sprintf("opencxcreatesolve%d", time)))
Expand Down Expand Up @@ -284,8 +321,8 @@ func TestTenMillion2048A2(t *testing.T) {
return
}

func TestHundredMillion2048A2(t *testing.T) {
createSolveTest2048A2(100000000, t)
func BenchmarkHundredMillion2048A2(b *testing.B) {
createSolveBench2048A2(100000000, b)
return
}

Expand All @@ -299,8 +336,8 @@ func TestConcurrentMillion2048A2(t *testing.T) {
return
}

func TestConcurrentManyMillions2048A2(t *testing.T) {
createSolveConcurrentN(10000000, runtime.NumCPU(), t)
func BenchmarkConcurrentManyMillions2048A2(b *testing.B) {
createSolveConcurrentNBench(10000000, runtime.NumCPU(), b)
return
}

Expand Down
8 changes: 4 additions & 4 deletions cxbenchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Memory Device
Type: DDR4
Speed: 2133 MT/s
Manufacturer: SK Hynix
Part Number: HMA41GS6AFR8N-TF
Part Number: HMA41GS6AFR8N-TF
Configured Memory Speed: 2133 MT/s
Configured Voltage: 1.2 V
Expand All @@ -37,7 +37,7 @@ Memory Device
Type: DDR4
Speed: 2133 MT/s
Manufacturer: SK Hynix
Part Number: HMA41GS6AFR8N-TF
Part Number: HMA41GS6AFR8N-TF
Configured Memory Speed: 2133 MT/s
Configured Voltage: 1.2 V
```
Expand All @@ -47,7 +47,7 @@ All tests are run on the regtest environment as well.
### Currently known limits:

- If you try to use SQL injection you will succeed. The honor system is currently in place to protect against that vulnerability.
- From start to finish, with many thousands of blocks, it takes a while to sync up.
- From start to finish, with many thousands of blocks, it takes a while to sync up.

## Placing orders and matching

Expand Down Expand Up @@ -106,7 +106,7 @@ NOTE: The benchmark requires users `tester` and `othertester` to have a bunch of
Here are the results for combination tests. PlaceAndFill test place then immediately fill orders for multiple prices, meaning the matching loop will be somewhat busy. The PlaceBuyThenSell test place many buy orders, then place many sell orders, like an "all at once" operation. I'm still testing whether or not running matching for the price of an order immediately after the order is placed is a good idea, or any slower. The following only run the matching loop. The matching loop can be optimized as well, maybe it should be run on a time increment when the exchange isn't that busy, and run on an order increment when the exchange is busy.

```
dan@dan-pc  ~/Documents/Projects/opencx/cxbenchmark   master  go test -v -benchtime=10s -bench=.
dan@dan-pc  ~/Documents/Projects/opencx/cxbenchmark   master  go test -v -benchtime=10s -bench=.
goos: linux
goarch: amd64
pkg: github.com/mit-dci/opencx/cxbenchmark
Expand Down
1 change: 0 additions & 1 deletion cxbenchmark/matchingbenchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/mit-dci/opencx/logging"
)

// TODO: fix problems where you do something unexpected network-wise and the entire rpc server crashes!
func BenchmarkPlaceOrders(b *testing.B) {
var err error

Expand Down
63 changes: 57 additions & 6 deletions cxdb/cxdbsql/auctionqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,63 @@ func (db *DB) NewAuction(auctionID [32]byte) (height uint64, err error) {
return
}

// MatchAuction matches the auction with a specific auctionID. This is meant to be the implementation of pro-rata for just the stuff in the auction. We assume that there are orders in the auction orderbook that are ALL valid.
/*
MatchAuction matches the auction with a specific auctionID. This is meant to be the implementation of pro-rata for just the stuff in the auction. We assume that there are orders in the auction orderbook that are ALL valid.
To understand Pro-rata matching on a batch of orders, here is an example of an orderbook, where the "Buy" list represents all of the buy orders
and the "Sell" list represents all of the sell orders.
The pair is BTC/LTC, you "buy" BTC with LTC and "sell" BTC for LTC.
"Sell": [
so1: {
amountWant: 300 LTC,
amountHave: 100 BTC,
// price : 0.33
},
so2: {
amountWant: 400 LTC,
amountHave: 100 BTC,
// price : 0.25
},
so3: {
amountWant: 600 LTC,
amountHave: 100 BTC,
// price : 0.17
},
so4: {
amountWant: 70 LTC,
amountHave: 10 BTC,
// price : 0.15
},
]
"Buy": [
bo1: {
amountWant: 100 BTC,
amountHave: 100 LTC,
// price : 1.00
},
bo2: {
amountWant: 100 BTC,
amountHave: 300 LTC,
// price : 0.33
},
bo3: {
amountWant: 100 BTC,
amountHave: 500 LTC,
// price : 0.20
},
bo4: {
amountWant: 10 BTC,
amountHave: 50 LTC,
// price : 0.20
},
]
We can see here that there's no "nice" way to match these orders, the high/low prices on either end are competitive, nor are there many orders
that are the same price. Pro-rata matching for a single price is trivial.
*/
func (db *DB) MatchAuction(auctionID [32]byte) (height uint64, err error) {

var tx *sql.Tx
Expand Down Expand Up @@ -230,11 +286,6 @@ func (db *DB) MatchAuction(auctionID [32]byte) (height uint64, err error) {
var book map[float64][]*match.AuctionOrder
book = make(map[float64][]*match.AuctionOrder)

// I hate writing matching algorithms!!!!!
// Alright so here's what's going to happen:
// uh two lists? How does crossing work with pro rata
// very annoying, maybe it's time to look at other examples

for _, pair := range db.pairsArray {

// So we get the orders, and they are supposed to be all valid.
Expand Down
2 changes: 1 addition & 1 deletion cxrpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ This will print a description of the order after making it, and prompt the user

`ocx placeorder name {buy|sell} pair amountHave price`

The price is price, amountHave is the amount of the asset you have. If you're on the selling side, that will be the first asset1 in the asset1/asset2 pair. If you're on the buying side, that will be the second, asset2.
The price is price, amountHave is the amount of the asset you have. If you're on the selling side, that will be the first asset1 in the asset1/asset2 pair. If you're on the buying side, that will be the second, asset2.

Arguments:
- Name (string)
Expand Down
2 changes: 0 additions & 2 deletions cxrpc/bankcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ type WithdrawArgs struct {
Signature []byte
}

// TODO: figure out a good way to do this serialize and signature stuff!!

// WithdrawReply holds the reply for Withdraw
type WithdrawReply struct {
Txid string
Expand Down
1 change: 0 additions & 1 deletion cxrpc/channelcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func (cl *OpencxRPC) GetLitConnection(args GetLitConnectionArgs, reply *GetLitCo
return
}

// TODO: figure out how to use the rest of the port list
reply.Ports[i] = uint16(port64)
}

Expand Down
3 changes: 0 additions & 3 deletions cxrpc/ordercmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func (cl *OpencxRPC) SubmitOrder(args SubmitOrderArgs, reply *SubmitOrderReply)
return
}

// TODO: make sure this is a valid way of doing stuff
if !sigPubKey.IsEqual(orderPubkey) {
err = fmt.Errorf("Pubkey used with signature not equal to the one passed")
return
Expand Down Expand Up @@ -156,7 +155,6 @@ func (cl *OpencxRPC) CancelOrder(args CancelOrderArgs, reply *CancelOrderReply)
return
}

// TODO: make sure this is a valid way of doing stuff
if !sigPubKey.IsEqual(orderPubKey) {
err = fmt.Errorf("Pubkey used with signature not equal to the one passed")
return
Expand Down Expand Up @@ -230,7 +228,6 @@ func (cl *OpencxRPC) GetOrder(args GetOrderArgs, reply *GetOrderReply) (err erro
return
}

// TODO: make sure this is a valid way of doing stuff
if !sigPubKey.IsEqual(orderPubKey) {
err = fmt.Errorf("Pubkey used with signature not equal to the one passed")
return
Expand Down
38 changes: 8 additions & 30 deletions cxserver/keymgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/mit-dci/lit/btcutil/base58"
"github.com/mit-dci/lit/coinparam"
"github.com/mit-dci/lit/crypto/koblitz"
"github.com/mit-dci/lit/wallit"
)

// GetAddrForCoin gets an address based on a wallet and pubkey
Expand All @@ -15,38 +14,17 @@ func (server *OpencxServer) GetAddrForCoin(coinType *coinparam.Params, pubkey *k
if !found {
err = fmt.Errorf("Could not find wallet to create address for")
}
if addr, err = GetAddrFunction(wallet)(pubkey); err != nil {
return
}

return
}

// TODO: honestly just delete this at some point. If anyone wants a free pull request just
// make GetAddrFunction a function with 2 parameters.

// GetAddrFunction returns a function that can safely be called by the DB
func GetAddrFunction(wallet *wallit.Wallit) func(*koblitz.PublicKey) (string, error) {
pubKeyHashAddrID := wallet.Param.PubKeyHashAddrID
return func(pubkey *koblitz.PublicKey) (addr string, err error) {
// TODO: in the future this should be deterministic based on public key.
// This is to make it really easy to figure out stuff

defer func() {
if err != nil {
err = fmt.Errorf("Problem with address closure: \n%s", err)
}
}()

// Create a new address
var addrBytes [20]byte
if addrBytes, err = wallet.NewAdr160(); err != nil {
return
}

// encode it to store in own db
addr = base58.CheckEncode(addrBytes[:], pubKeyHashAddrID)

// Create a new address
var addrBytes [20]byte
if addrBytes, err = wallet.NewAdr160(); err != nil {
return
}

// encode it to store in own db
addr = base58.CheckEncode(addrBytes[:], pubKeyHashAddrID)

return
}
2 changes: 0 additions & 2 deletions cxserver/lightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,6 @@ func (server *OpencxServer) CreateSwap(pubkey *koblitz.PublicKey, order *match.L
// }
}

// TODO: once everything is revealed,

return
}

Expand Down
Loading

0 comments on commit 181c951

Please sign in to comment.