Skip to content

Commit

Permalink
OBv2: Implement Event Heap
Browse files Browse the repository at this point in the history
  • Loading branch information
skynetcap committed Dec 21, 2023
1 parent 2d11f42 commit 027dbcc
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.mmorrell.openbook.model;

import lombok.Builder;
import lombok.Data;

import java.util.Arrays;

@Data
@Builder
public class OpenBookAnyEvent {

public static final int SIZE = 144;

// 0 = fill, 1 = out
private byte eventType;
private byte[] padding;

public static OpenBookAnyEvent readOpenBookAnyEvent(byte[] data) {
return OpenBookAnyEvent.builder()
.eventType(data[0])
.padding(Arrays.copyOfRange(data, 1, SIZE))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@
import org.bitcoinj.core.Utils;
import org.p2p.solanaj.utils.ByteUtils;

import java.util.Arrays;
import java.util.List;

@Data
@Builder
public class OpenBookEventHeap {

private static final int FREE_HEAD_OFFSET = 8;
private static final int USED_HEAD_OFFSET = 10;
private static final int COUNT_OFFSET = 12;
private static final int PADDING_OFFSET = 14;
private static final int SEQ_NUM_OFFSET = 16;
private static final int EVENT_NODES_OFFSET = 24;

// EventHeapHeader x 1
private short freeHead;
private short usedHead;
Expand All @@ -18,16 +28,27 @@ public class OpenBookEventHeap {
private long seqNum;

// + EventNode x 600 (starts at offset 24)
private static final int NUM_EVENT_NODES = 600;
private List<OpenBookEventNode> eventNodes;

// + 64 bytes reserved

public static OpenBookEventHeap readOpenBookEventHeap(byte[] data) {
return OpenBookEventHeap.builder()
.freeHead((short) Utils.readUint16(data, 8))
.usedHead((short) Utils.readUint16(data, 10))
.count((short) Utils.readUint16(data, 12))
.padding((short) Utils.readUint16(data, 14))
.seqNum(ByteUtils.readUint64(data, 16).longValue())
.freeHead((short) Utils.readUint16(data, FREE_HEAD_OFFSET))
.usedHead((short) Utils.readUint16(data, USED_HEAD_OFFSET))
.count((short) Utils.readUint16(data, COUNT_OFFSET))
.padding((short) Utils.readUint16(data, PADDING_OFFSET))
.seqNum(ByteUtils.readUint64(data, SEQ_NUM_OFFSET).longValue())
.eventNodes(
OpenBookEventNode.readEventNodes(
Arrays.copyOfRange(
data,
EVENT_NODES_OFFSET,
EVENT_NODES_OFFSET + (NUM_EVENT_NODES * OpenBookEventNode.SIZE)
)
)
)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mmorrell.openbook.model;

import lombok.Builder;
import lombok.Data;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Data
@Builder
public class OpenBookEventNode {

// 2 shorts = 4 bytes
// 4 bytes of padding
// AnyEvent x 1 = 144 bytes
// 152 bytes total

public static final int SIZE = 2 + 2 + 4 + OpenBookAnyEvent.SIZE;
private static final int NEXT_OFFSET = 0;
private static final int PREV_OFFSET = 2;
private static final int EVENT_OFFSET = 8;

private short next;
private short prev;
private OpenBookAnyEvent event;

public static List<OpenBookEventNode> readEventNodes(byte[] bytes) {
List<OpenBookEventNode> results = new ArrayList<>();
int numNodes = bytes.length / SIZE;
for (int i = 0; i < numNodes; i++) {
byte[] nodeBytes = Arrays.copyOfRange(bytes, i * SIZE, (i + 1) * SIZE);
OpenBookEventNode node = OpenBookEventNode.builder()
.next(ByteBuffer.wrap(nodeBytes, NEXT_OFFSET, 2).getShort())
.prev(ByteBuffer.wrap(nodeBytes, PREV_OFFSET, 2).getShort())
.event(
OpenBookAnyEvent.readOpenBookAnyEvent(
Arrays.copyOfRange(
nodeBytes,
EVENT_OFFSET,
nodeBytes.length
)
)
)
.build();
results.add(node);
}

return results;
}
}
8 changes: 8 additions & 0 deletions openbook/src/test/java/OpenBookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mmorrell.openbook.model.BookSide;
import com.mmorrell.openbook.model.LeafNode;
import com.mmorrell.openbook.model.NodeTag;
import com.mmorrell.openbook.model.OpenBookEventHeap;
import com.mmorrell.openbook.model.OpenBookMarket;
import com.mmorrell.openbook.program.OpenbookProgram;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,6 +19,7 @@
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

import static org.junit.Assert.assertFalse;

Expand Down Expand Up @@ -100,4 +102,10 @@ public void eventHeapTest() throws RpcException, IOException {
.getDecodedData();
Files.write(data, new File("eventHeap.bin"));
}

@Test
public void openBookEventHeapTest(){
Optional<OpenBookEventHeap> eventHeap = openBookManager.getEventHeap(PublicKey.valueOf("GY5HKym4yKNUpdHpBBiqLB3DHbrNKhLHDFTSLPK8AbFX"));
eventHeap.ifPresent(heap -> log.info("Event Heap: {}", heap));
}
}

0 comments on commit 027dbcc

Please sign in to comment.