Skip to content

Commit

Permalink
update orderbook to use pointers to orders instead
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott committed Jun 28, 2023
1 parent 4591d04 commit c8b7141
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cmd/golem/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func main() {
accts := &accounts.InMemoryManager{}

// setup channels for wrapping our market
in := make(chan orderbook.Order)
in := make(chan *orderbook.Order)
out := make(chan *orderbook.Match)
status := make(chan []orderbook.Order)
status := make(chan []*orderbook.Order)

// Run the book
go orderbook.Run(ctx, accts, in, out, status)
Expand Down
21 changes: 10 additions & 11 deletions pkg/orderbook/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ type Orderbook interface {
func Run(
ctx context.Context,
accounts accounts.AccountManager,
in chan Order,
in chan *Order,
out chan *Match,
status chan []Order,
status chan []*Order,
) {
// NB: buy and sell are not accessible anywhere but here for safety.
var buy, sell []Order

var buy, sell []*Order
handleMatches(ctx, accounts, buy, sell, in, out, status)
}

Expand All @@ -65,10 +64,10 @@ func Run(
func handleMatches(
ctx context.Context,
accts accounts.AccountManager,
buy, sell []Order,
in chan Order,
buy, sell []*Order,
in chan *Order,
out chan *Match,
status chan []Order,
status chan []*Order,
) {
for {
// feed off the orders that accumulated since the last loop
Expand All @@ -80,7 +79,7 @@ func handleMatches(
}
}
// create the orderlist for state updates
orderlist := []Order{}
orderlist := []*Order{}
orderlist = append(orderlist, buy...)
orderlist = append(orderlist, sell...)
status <- orderlist
Expand All @@ -100,7 +99,7 @@ func handleMatches(
// matching sell options are exhausted,
// * When it exhausts all f it ratchets up the buy index again and finds all matching
// orders.
func MatchOrders(accts accounts.AccountManager, buyOrders []Order, sellOrders []Order) []Match {
func MatchOrders(accts accounts.AccountManager, buyOrders []*Order, sellOrders []*Order) []Match {
sort.Slice(buyOrders, func(i, j int) bool {
return buyOrders[i].Price > buyOrders[j].Price
})
Expand All @@ -118,8 +117,8 @@ func MatchOrders(accts accounts.AccountManager, buyOrders []Order, sellOrders []
// Check if the current Buy order matches the current Sell order
if buyOrders[buyIndex].Price >= sellOrders[sellIndex].Price {
// Create a match and add it to the matches
sell := &sellOrders[sellIndex]
buy := &buyOrders[buyIndex]
sell := sellOrders[sellIndex]
buy := buyOrders[buyIndex]
m := Match{
Buy: buy,
Sell: sell,
Expand Down
8 changes: 4 additions & 4 deletions pkg/orderbook/orderbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ func newTestAccountManager(t *testing.T, num int) (accounts.AccountManager, []st
// price between minPrice and maxPrice, an open quantity between minOpen
// and maxOpen, an equal chance to be owned by foo or bar,
// and with an even chance of being a buy or sell order.
func newTestOrders(count int) (buyOrders []Order, sellOrders []Order) {
func newTestOrders(count int) (buyOrders, sellOrders []*Order) {
log.Printf("count %d", count)
rand.Seed(time.Now().UnixNano())

var minPrice, maxPrice = 100, 10_000
var minOpen, maxOpen = 10, 1_000_000

for i := 0; i < count; i++ {
o := Order{
o := &Order{
ID: fmt.Sprintf("%d", i),
Kind: "market",
Price: uint64(rand.Intn(maxPrice-minPrice) + minPrice),
Expand Down Expand Up @@ -133,9 +133,9 @@ var numTestOrders = 10_000_000
var numTestAccounts = 1_000_000

func TestRunLoad(t *testing.T) {
in := make(chan Order, 1)
in := make(chan *Order, 1)
out := make(chan *Match, 1)
status := make(chan []Order, 1)
status := make(chan []*Order, 1)

// Generate default random accounts for testing
accts, ids := newTestAccountManager(t, numTestAccounts)
Expand Down
16 changes: 8 additions & 8 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ var defaultPort = ":1323"

type Engine struct {
srv *echo.Echo
state []orderbook.Order
in chan orderbook.Order
state []*orderbook.Order
in chan *orderbook.Order
out chan *orderbook.Match
status chan []orderbook.Order
status chan []*orderbook.Order
}

// NewServer returns a new server.Engine that wires together
// API requests to the orderbook.
// startingx
func NewServer(
accounts accounts.AccountManager,
in chan orderbook.Order,
in chan *orderbook.Order,
out chan *orderbook.Match,
status chan []orderbook.Order,
status chan []*orderbook.Order,
) *Engine {
engine := &Engine{
in: in,
Expand All @@ -52,7 +52,7 @@ func NewServer(
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
e.Logger.Infof("order received: %+v", o)
engine.in <- *o
engine.in <- o
return nil
})

Expand All @@ -75,8 +75,8 @@ func (eng *Engine) Run() error {

// handleState updates the Engine's view of the Orderbooks
// so that it can be fetched by the server.
func handleState(e *Engine, status chan []orderbook.Order) {
go func(e *Engine, status chan []orderbook.Order) {
func handleState(e *Engine, status chan []*orderbook.Order) {
go func(e *Engine, status chan []*orderbook.Order) {
for stats := range status {
e.state = stats
e.srv.Logger.Debugf("state: %+v\n", e.state)
Expand Down

0 comments on commit c8b7141

Please sign in to comment.