From 027dbccdc3dd874c78c0586f6ab4cd320ffd4491 Mon Sep 17 00:00:00 2001 From: skynetcap <100323448+skynetcap@users.noreply.github.com> Date: Thu, 21 Dec 2023 14:55:47 -0800 Subject: [PATCH] OBv2: Implement Event Heap --- .../openbook/model/OpenBookAnyEvent.java | 24 +++++++++ .../openbook/model/OpenBookEventHeap.java | 31 +++++++++-- .../openbook/model/OpenBookEventNode.java | 52 +++++++++++++++++++ openbook/src/test/java/OpenBookTest.java | 8 +++ 4 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 openbook/src/main/java/com/mmorrell/openbook/model/OpenBookAnyEvent.java create mode 100644 openbook/src/main/java/com/mmorrell/openbook/model/OpenBookEventNode.java diff --git a/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookAnyEvent.java b/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookAnyEvent.java new file mode 100644 index 0000000..d285f3a --- /dev/null +++ b/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookAnyEvent.java @@ -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(); + } +} diff --git a/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookEventHeap.java b/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookEventHeap.java index 26de400..3310950 100644 --- a/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookEventHeap.java +++ b/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookEventHeap.java @@ -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; @@ -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 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(); } } diff --git a/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookEventNode.java b/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookEventNode.java new file mode 100644 index 0000000..0eb1cf9 --- /dev/null +++ b/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookEventNode.java @@ -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 readEventNodes(byte[] bytes) { + List 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; + } +} diff --git a/openbook/src/test/java/OpenBookTest.java b/openbook/src/test/java/OpenBookTest.java index 575bb5e..3e08577 100644 --- a/openbook/src/test/java/OpenBookTest.java +++ b/openbook/src/test/java/OpenBookTest.java @@ -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; @@ -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; @@ -100,4 +102,10 @@ public void eventHeapTest() throws RpcException, IOException { .getDecodedData(); Files.write(data, new File("eventHeap.bin")); } + + @Test + public void openBookEventHeapTest(){ + Optional eventHeap = openBookManager.getEventHeap(PublicKey.valueOf("GY5HKym4yKNUpdHpBBiqLB3DHbrNKhLHDFTSLPK8AbFX")); + eventHeap.ifPresent(heap -> log.info("Event Heap: {}", heap)); + } }