2021-08-24 11:51:22 +00:00
|
|
|
package mailserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
waku "github.com/status-im/status-go/waku/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLevelDB_BuildIteratorWithTopic(t *testing.T) {
|
|
|
|
topic := []byte{0x01, 0x02, 0x03, 0x04}
|
|
|
|
|
2023-01-03 14:40:11 +00:00
|
|
|
dir, err := os.MkdirTemp("/tmp", "status-go-test-level-db")
|
2021-08-24 11:51:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
db, err := NewLevelDB(dir)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
_ = os.Remove(dir)
|
|
|
|
}()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
envelope, err := newTestEnvelope(topic)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = db.SaveEnvelope(envelope)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
iter, err := db.BuildIterator(CursorQuery{
|
|
|
|
start: NewDBKey(uint32(time.Now().Add(-time.Hour).Unix()), types.BytesToTopic(topic), types.Hash{}).Bytes(),
|
|
|
|
end: NewDBKey(uint32(time.Now().Add(time.Second).Unix()), types.BytesToTopic(topic), types.Hash{}).Bytes(),
|
|
|
|
topics: [][]byte{topic},
|
|
|
|
limit: 10,
|
|
|
|
})
|
|
|
|
topicsMap := make(map[types.TopicType]bool)
|
|
|
|
topicsMap[types.BytesToTopic(topic)] = true
|
|
|
|
require.NoError(t, err)
|
|
|
|
hasNext := iter.Next()
|
|
|
|
require.True(t, hasNext)
|
|
|
|
rawValue, err := iter.GetEnvelopeByTopicsMap(topicsMap)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, rawValue)
|
|
|
|
var receivedEnvelope waku.Envelope
|
|
|
|
err = rlp.DecodeBytes(rawValue, &receivedEnvelope)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, waku.BytesToTopic(topic), receivedEnvelope.Topic)
|
|
|
|
|
|
|
|
err = iter.Release()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, iter.Error())
|
|
|
|
}
|