From d24dffc0a8ecf7edd1376c2aa0f09224edabfd44 Mon Sep 17 00:00:00 2001
From: "zhanxin.xu" <90450068+1329576606@users.noreply.github.com>
Date: Mon, 23 Oct 2023 15:12:23 +0800
Subject: [PATCH 1/2] fix log.js iterator

fix log.js iterator
---
 src/oplog/log.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/oplog/log.js b/src/oplog/log.js
index 36cc977..506e414 100644
--- a/src/oplog/log.js
+++ b/src/oplog/log.js
@@ -434,7 +434,7 @@ const Log = async (identity, { logId, logHeads, access, entryStorage, headsStora
 
     if (useBuffer) {
       const endIndex = buffer.keys.length
-      const startIndex = endIndex - amount
+      const startIndex = endIndex > amount ? endIndex - amount : 0
       const keys = buffer.keys.slice(startIndex, endIndex)
       for (const key of keys) {
         const hash = buffer.get(key)

From 98594b90812fe940ceb7ab6d0ede3009eb4c9f34 Mon Sep 17 00:00:00 2001
From: Hayden Young <haydenyoung@wijiti.com>
Date: Mon, 13 Nov 2023 22:25:52 +0000
Subject: [PATCH 2/2] test: Amount configured more than total entries being
 returned by gt/gte iterator filter.

---
 test/oplog/iterator.test.js | 39 +++++++++++++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)

diff --git a/test/oplog/iterator.test.js b/test/oplog/iterator.test.js
index 241cc23..ca2da47 100644
--- a/test/oplog/iterator.test.js
+++ b/test/oplog/iterator.test.js
@@ -40,6 +40,7 @@ describe('Log - Iterator', function () {
     let startHash
     const hashes = []
     const logSize = 100
+    const startIndex = 67
 
     beforeEach(async () => {
       log1 = await Log(testIdentity, { logId: 'X' })
@@ -51,8 +52,8 @@ describe('Log - Iterator', function () {
 
       // entry67
       // startHash = 'zdpuAxCuaH2R7AYKZ6ZBeeA94v3FgmHZ8wCfDy7pLVcoc3zSo'
-      startHash = hashes[67][0]
-      strictEqual(startHash, hashes[67][0])
+      startHash = hashes[startIndex][0]
+      strictEqual(startHash, hashes[startIndex][0])
     })
 
     it('returns length with lte and amount', async () => {
@@ -368,6 +369,40 @@ describe('Log - Iterator', function () {
       strictEqual(i, 68)
     })
 
+    it('returns correct entries with gt when amount is more than total entries', async () => {
+      const amount = logSize - startIndex
+      const expectedAmount = logSize - startIndex - 1
+
+      const it = log1.iterator({
+        gt: startHash,
+        amount
+      })
+
+      let i = 0
+      for await (const entry of it) {
+        strictEqual(entry.payload, 'entry' + (logSize - 1 - i++))
+      }
+
+      strictEqual(i, expectedAmount)
+    })
+
+    it('returns correct entries with gte when amount is more than total entries', async () => {
+      const amount = logSize - startIndex + 1
+      const expectedAmount = amount - 1
+
+      const it = log1.iterator({
+        gte: startHash,
+        amount
+      })
+
+      let i = 0
+      for await (const entry of it) {
+        strictEqual(entry.payload, 'entry' + (logSize - 1 - i++))
+      }
+
+      strictEqual(i, expectedAmount)
+    })
+
     it('returns zero entries when amount is 0', async () => {
       const it = log1.iterator({
         amount: 0