Skip to content

Commit

Permalink
trying to get sql to connect
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Jan 25, 2019
1 parent cd223d7 commit d575e00
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 13 deletions.
Binary file modified cmd/ocx/ocx
Binary file not shown.
9 changes: 9 additions & 0 deletions cmd/ocx/ordercmds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func(cl *openCxClient) OrderCommand(args []string) error {
return nil
}

func(cl *openCxClient) ViewOrderbook(args []string) error {
return nil
}
9 changes: 8 additions & 1 deletion cmd/ocx/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ func(cl *openCxClient) parseCommands(commands []string) error {
}
}
if cmd == "vieworderbook" {
// run method that returns orders in json
if len(args) != 2 {
return fmt.Errorf("Must specify 2 currencies to view the pair's orderbook")
}

err := cl.ViewOrderbook(args[0],args[1])
if err != nil {
return fmt.Errorf("Error viewing orderbook: \n%s", err)
}
}
if cmd == "nologinuseless" {
if len(args) > 0 {
Expand Down
21 changes: 21 additions & 0 deletions cxrpc/ordercmds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cxrpc

import (
"github.com/mit-dci/opencx/match"
)

// SubmitOrderArgs holds the args for the submitorder command
type SubmitOrderArgs struct {
BuyOrder *match.Order
SellOrder *match.Order
}

// SubmitOrderReply holds the args for the submitorder command
type SubmitOrderReply struct {
// TODO empty for now
}

// SubmitOrder submits an order to the order book or throws an error
func(cl *OpencxRPC) SubmitOrder(args SubmitOrderArgs, reply *SubmitOrderReply) error {
return nil
}
4 changes: 2 additions & 2 deletions cxserver/server.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cxserver

import "github.com/mit-dci/opencx/db/ocxredis"
import "github.com/mit-dci/opencx/db/ocxsql"

// OpencxServer is how rpc can query the database and whatnot
type OpencxServer struct {
OpencxDB *ocxredis.DB
OpencxDB *ocxsql.DB
OpencxRoot string
OpencxPort int
// TODO: Put TLS stuff here
Expand Down
1 change: 1 addition & 0 deletions db/ocxredis/authqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func(db *DB) CreateStoreToken(username string) ([]byte, error) {

tokenString := fmt.Sprintf("%x", randHash.Sum(nil))

// store token for 20 seconds
status := db.dbClient.Set(qString, tokenString, 20 * time.Second)
err := status.Err()
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions db/ocxredis/bankqueries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ocxredis

// unfinished

// Buyer is a struct containing information needed for a buyer
type Buyer struct {
username string
haveAsset byte
// I think 51 bits is enough to store all the satoshis cause 21,000,000 * 10^8 = a 51 bit number
amountHave int64
}

// Seller is a struct containing information needed for a seller
type Seller struct {
username string
haveAsset byte
// I think 51 bits is enough to store all the satoshis cause 21,000,000 * 10^8 = a 51 bit number
amountHave int64
}

// ExchangeCoins exchanges coins between a buyer and a seller (with a fee of course)
func (db *DB) ExchangeCoins(buyer Buyer, seller Seller) error {
return nil
}

// InitializeAccount initializes all database values for an account with username 'username'
func (db *DB) InitializeAccount(username string) error {
return nil
}
13 changes: 13 additions & 0 deletions db/ocxredis/datacodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,17 @@ const (
Token = 0x00
// Account is used as a prefix for queries for username / account queries
Account = 0x01
// Balance is a prefix used for balance of a specific coin
Balance = 0x02
)

const (
// Bitcoin is a prefix used with Balance for bitcoin tokens
Bitcoin = 0x00
// Litecoin is a prefix used with Balance for Litecoin tokens
Litecoin = 0x01
// Vertcoin is a prefix used with Balance for Vertcoin tokens
Vertcoin = 0x02
// Dogecoin is a prefix used with Balance for Dogecoin tokens
Dogecoin = 0x03
)
15 changes: 15 additions & 0 deletions db/ocxsql/accountqueries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ocxsql

// this is just here so it can be "implemented"

// CreateAccount creates an account
func(db *DB) CreateAccount(username string, password string) (bool, error) {
// TODO later
return true, nil
}

// CheckCredentials checks users username and passwords
func(db *DB) CheckCredentials(username string, password string) (bool, error) {
// TODO later
return true, nil
}
13 changes: 13 additions & 0 deletions db/ocxsql/authqueries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ocxsql

// CreateStoreToken creates a token for username and stores it for a certain amount of time
func(db *DB) CreateStoreToken(username string) ([]byte, error) {
// TODO later
return nil, nil
}

// CheckToken checks the token assigned to a user
func(db *DB) CheckToken(username string, token []byte) (bool, error) {
// TODO maybe never if I use signed stuff
return true, nil
}
21 changes: 21 additions & 0 deletions db/ocxsql/bankqueries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ocxsql

import (
"github.com/mit-dci/opencx/match"
)

// ExchangeCoins exchanges coins between a buyer and a seller (with a fee of course)
func (db *DB) ExchangeCoins(buyOrder *match.Order, sellOrder *match.Order) error {
// check balances
// if balances check out then make the trade, update balances

return nil
}

// InitializeAccount initializes all database values for an account with username 'username'
func (db *DB) InitializeAccount(username string) error {
// Balances table, username is one column and balances are all the other columns
// SELECT username FROM balances...
// tx, err := db.Begin()
return nil
}
93 changes: 93 additions & 0 deletions db/ocxsql/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package ocxsql

import (
"fmt"
"os"
"log"
"io"
"database/sql"

// mysql is just the driver, always interact with database/sql api
_ "github.com/go-sql-driver/mysql"
)

// turn into config options
var (
defaultUsername = "localhost"
defaultPassword = ""
balanceSchema = "balances"
)

// DB contains the sql DB type as well as a logger
type DB struct {
DBHandler *sql.DB
logger *log.Logger
}

// SetupClient sets up the mysql client and driver
func(db *DB) SetupClient() error {

// open db handle
dbHandle, err := sql.Open("mysql", "")
if err != nil {
return fmt.Errorf("Error opening database: \n%s", err)
}

db.DBHandler = dbHandle

err = db.DBHandler.Ping()
if err != nil {
return fmt.Errorf("Could not ping the database, is it running: \n%s", err)
}

// check schema
// if schema not there make it
_, err = db.DBHandler.Exec("CREATE SCHEMA IF NOT EXISTS " + balanceSchema)
if err != nil {
return fmt.Errorf("Could not create balance schema: \n%s", err)
}

// if schema there then we're good
_, err = db.DBHandler.Exec("USE " + balanceSchema)
if err != nil {
return fmt.Errorf("Could not use balance schema: \n%s", err)
}

return nil
}

// SetLogPath sets the log path for the database, and tells it to also print to stdout. This should be changed in the future so only verbose clients log to stdout
func (db *DB) SetLogPath(logPath string) error {
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
panic(err)
}

mw := io.MultiWriter(os.Stdout, logFile)
db.logger = log.New(mw, "OPENCX DATABASE: ", log.LstdFlags)
db.LogPrintf("Logger has been set up at %s\n", logPath)
return nil
}

// These methods can be removed, but these are used frequently so maybe the
// time spent writing these cuts down on the time spent writing logger

// LogPrintf is like printf but you don't have to go db.logger every time
func (db *DB) LogPrintf(format string, v ...interface{}) {
db.logger.Printf(format, v...)
}

// LogPrintln is like println but you don't have to go db.logger every time
func (db *DB) LogPrintln(v ...interface{}) {
db.logger.Println(v...)
}

// LogPrint is like print but you don't have to go db.logger every time
func (db *DB) LogPrint(v ...interface{}) {
db.logger.Print(v...)
}

// LogErrorf is like printf but with error at the beginning
func (db *DB) LogErrorf(format string, v ...interface{}) {
db.logger.Printf("ERROR: "+format, v...)
}
3 changes: 3 additions & 0 deletions features.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

- Accounts

## Important note
There are multiple ways to create an "exchange," one of which is what I'm currently trying to make, but hopefully the APIs I make are robust enough such that the other type can be made as well. There are **Dealer markets** and **Auction markets**. I'm currently making a dealer market, which uses competitive buy and sell orders and a market maker, in this case the exchange, to actually facilitate trades. Auction markets are _very_ similar, in fact the exact same but without a market maker.

## Wallets

Of course users have their own wallets, but there needs to be some way to make a deposit to a certain account.
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module github.com/mit-dci/opencx

require github.com/go-redis/redis v6.15.1+incompatible
require (
github.com/go-redis/redis v6.15.1+incompatible
github.com/go-sql-driver/mysql v1.4.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/dgryski/go-farm v0.0.0-20190104051053-3adb47b1fb0f h1:dDxpBYafY/GYpcl
github.com/dgryski/go-farm v0.0.0-20190104051053-3adb47b1fb0f/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/go-redis/redis v6.15.1+incompatible h1:BZ9s4/vHrIqwOb0OPtTQ5uABxETJ3NRuUNoSUurnkew=
github.com/go-redis/redis v6.15.1+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
Expand Down
27 changes: 27 additions & 0 deletions match/order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package match

// Pair is a struct that represents a trading pair
type Pair struct {
AssetWant byte `json:"assetWant"`
// the AssetHave asset will always be the asset whose balance is checked
AssetHave byte `json:"assetHave"`
}

// Order is a struct that represents a stored side of a trade
type Order struct {
Client string `json:"username"`
Side string `json:"side"`
TradingPair Pair `json:"pair"`
// amount of assetHave the user would like to trade
AmountHave int64 `json:"amount"`
// amount of assetWant the user wants for their assetHave
AmountWant int64 `json:"price"`
}

func(o *Order) isBuySide() bool {
return o.Side == "buy"
}

func(o *Order) isSellSide() bool {
return o.Side == "sell"
}
14 changes: 5 additions & 9 deletions opencx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/mit-dci/opencx/cxserver"

"github.com/mit-dci/opencx/cxrpc"
"github.com/mit-dci/opencx/db/ocxredis"
"github.com/mit-dci/opencx/db/ocxsql"
)

var (
Expand All @@ -33,7 +33,7 @@ func main() {
// Create root directory
createRoot(defaultRoot)

db := new(ocxredis.DB)
db := new(ocxsql.DB)

// Set path where output will be written to for all things database
err = db.SetLogPath(defaultLogPath)
Expand All @@ -46,7 +46,7 @@ func main() {
// Check if DB has saved files, if not then start new DB, if so then load old DB
err = db.SetupClient()
if err != nil {
log.Fatalf("Error setting up redis client: \n%s", err)
log.Fatalf("Error setting up sql client: \n%s", err)
}

// Anyways, here's where we set the server
Expand All @@ -55,12 +55,8 @@ func main() {
ocxServer.OpencxRoot = defaultRoot
ocxServer.OpencxPort = defaultPort

// store in .opencx/
// nevermind redis is really annoying
// err = db.SetDataDirectory(defaultStoreLocation)
// if err != nil {
// log.Fatalf("Error setting data directory: \n%s", err)
// }
// defer the db to when it closes
defer ocxServer.OpencxDB.DBHandler.Close()

// Register RPC Commands and set server
rpc1 := new(cxrpc.OpencxRPC)
Expand Down

0 comments on commit d575e00

Please sign in to comment.