-
Notifications
You must be signed in to change notification settings - Fork 810
feat: add in memory implementation of HeightIndex Database #4212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6eb6764
feat: added an in-memory mock implementation of blockdb for testing
DracoLi 5cf1948
make copies of the block
DracoLi d8d93e1
refactor memory database
DracoLi c5e218c
remove Inspect
DracoLi 7645992
move memdb into package and add simple tests
DracoLi 9997910
fix naming & separate tests
DracoLi 0ce9c20
chore: implement database.HeightIndex
DracoLi cde510e
feat: update to use database.HeightIndex and move out of blockdb
DracoLi be762aa
fix lint issues
DracoLi edeb8ff
Merge branch 'master' into dl/blockdb-memory-mock
DracoLi e70e213
create dbtest package for heightindexdb
DracoLi 88f2394
update close behaviour on close
DracoLi 0f9a5d9
clarify Put behaviour
DracoLi 1769a5c
fix test case
DracoLi 5454574
fix: pr feedback
DracoLi 344b0e1
update interface description
DracoLi bc432c3
refactor testCase struct
DracoLi bbeb41f
Change the return value req for nil or empty value puts
DracoLi ac5ca8f
update doc wording
DracoLi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package dbtest | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/database" | ||
) | ||
|
||
// Tests is a list of all database tests | ||
var Tests = []struct { | ||
Name string | ||
Test func(t *testing.T, newDB func() database.HeightIndex) | ||
}{ | ||
{"TestPutGet", TestPutGet}, | ||
{"TestHas", TestHas}, | ||
{"TestCloseAndPut", TestCloseAndPut}, | ||
{"TestCloseAndGet", TestCloseAndGet}, | ||
{"TestCloseAndHas", TestCloseAndHas}, | ||
{"TestClose", TestClose}, | ||
} | ||
|
||
type putArgs struct { | ||
height uint64 | ||
data []byte | ||
} | ||
|
||
func TestPutGet(t *testing.T, newDB func() database.HeightIndex) { | ||
tests := []struct { | ||
name string | ||
puts []putArgs | ||
queryHeight uint64 | ||
want []byte | ||
wantErr error | ||
}{ | ||
{ | ||
name: "normal operation", | ||
puts: []putArgs{ | ||
{1, []byte("test data 1")}, | ||
}, | ||
queryHeight: 1, | ||
want: []byte("test data 1"), | ||
}, | ||
{ | ||
name: "not found error when getting on non-existing height", | ||
puts: []putArgs{ | ||
{1, []byte("test data")}, | ||
}, | ||
queryHeight: 2, | ||
wantErr: database.ErrNotFound, | ||
}, | ||
{ | ||
name: "overwriting data on same height", | ||
puts: []putArgs{ | ||
{1, []byte("original data")}, | ||
{1, []byte("overwritten data")}, | ||
}, | ||
queryHeight: 1, | ||
want: []byte("overwritten data"), | ||
}, | ||
{ | ||
name: "put and get nil data", | ||
puts: []putArgs{ | ||
{1, nil}, | ||
}, | ||
queryHeight: 1, | ||
want: nil, | ||
}, | ||
{ | ||
name: "put and get empty bytes", | ||
puts: []putArgs{ | ||
{1, []byte{}}, | ||
}, | ||
queryHeight: 1, | ||
want: []byte{}, | ||
}, | ||
{ | ||
name: "put and get large data", | ||
puts: []putArgs{ | ||
{1, make([]byte, 1000)}, | ||
}, | ||
queryHeight: 1, | ||
want: make([]byte, 1000), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
db := newDB() | ||
defer func() { | ||
require.NoError(t, db.Close()) | ||
}() | ||
|
||
// Perform all puts | ||
for _, write := range tt.puts { | ||
require.NoError(t, db.Put(write.height, write.data)) | ||
} | ||
|
||
// modify the original value of the put data to ensure the saved | ||
// value won't be changed after Get | ||
if len(tt.puts) > int(tt.queryHeight) && tt.puts[tt.queryHeight].data != nil { | ||
copy(tt.puts[tt.queryHeight].data, []byte("modified data")) | ||
} | ||
|
||
// Query the specific height | ||
retrievedData, err := db.Get(tt.queryHeight) | ||
require.ErrorIs(t, err, tt.wantErr) | ||
require.Equal(t, tt.want, retrievedData) | ||
|
||
// modify the data returned from Get and ensure it won't change the | ||
// data from a second Get | ||
copy(retrievedData, []byte("modified data")) | ||
newData, err := db.Get(tt.queryHeight) | ||
require.ErrorIs(t, err, tt.wantErr) | ||
require.Equal(t, tt.want, newData) | ||
}) | ||
} | ||
} | ||
|
||
func TestHas(t *testing.T, newDB func() database.HeightIndex) { | ||
tests := []struct { | ||
name string | ||
puts []putArgs | ||
queryHeight uint64 | ||
want bool | ||
}{ | ||
{ | ||
name: "non-existent item", | ||
queryHeight: 1, | ||
}, | ||
{ | ||
name: "existing item with data", | ||
puts: []putArgs{{1, []byte("test data")}}, | ||
queryHeight: 1, | ||
want: true, | ||
}, | ||
{ | ||
name: "existing item with nil data", | ||
puts: []putArgs{{1, nil}}, | ||
queryHeight: 1, | ||
want: true, | ||
}, | ||
{ | ||
name: "existing item with empty bytes", | ||
puts: []putArgs{{1, []byte{}}}, | ||
queryHeight: 1, | ||
want: true, | ||
}, | ||
{ | ||
name: "has returns true on overridden height", | ||
puts: []putArgs{ | ||
{1, []byte("original data")}, | ||
{1, []byte("overridden data")}, | ||
}, | ||
queryHeight: 1, | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
db := newDB() | ||
defer func() { | ||
require.NoError(t, db.Close()) | ||
}() | ||
|
||
// Perform all puts | ||
for _, write := range tt.puts { | ||
require.NoError(t, db.Put(write.height, write.data)) | ||
} | ||
|
||
ok, err := db.Has(tt.queryHeight) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.want, ok) | ||
}) | ||
} | ||
} | ||
|
||
func TestCloseAndPut(t *testing.T, newDB func() database.HeightIndex) { | ||
db := newDB() | ||
require.NoError(t, db.Close()) | ||
|
||
// Try to put after close - should return error | ||
err := db.Put(1, []byte("test")) | ||
require.ErrorIs(t, err, database.ErrClosed) | ||
} | ||
|
||
func TestCloseAndGet(t *testing.T, newDB func() database.HeightIndex) { | ||
db := newDB() | ||
require.NoError(t, db.Close()) | ||
|
||
// Try to get after close - should return error | ||
_, err := db.Get(1) | ||
require.ErrorIs(t, err, database.ErrClosed) | ||
} | ||
|
||
func TestCloseAndHas(t *testing.T, newDB func() database.HeightIndex) { | ||
db := newDB() | ||
require.NoError(t, db.Close()) | ||
|
||
// Try to has after close - should return error | ||
_, err := db.Has(1) | ||
require.ErrorIs(t, err, database.ErrClosed) | ||
} | ||
|
||
func TestClose(t *testing.T, newDB func() database.HeightIndex) { | ||
db := newDB() | ||
require.NoError(t, db.Close()) | ||
|
||
// Second close should return error | ||
err := db.Close() | ||
require.ErrorIs(t, err, database.ErrClosed) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package memdb | ||
|
||
import ( | ||
"slices" | ||
"sync" | ||
|
||
"github.com/ava-labs/avalanchego/database" | ||
) | ||
|
||
var _ database.HeightIndex = (*Database)(nil) | ||
|
||
// Database is an in-memory implementation of database.HeightIndex | ||
type Database struct { | ||
mu sync.RWMutex | ||
data map[uint64][]byte | ||
closed bool | ||
} | ||
|
||
func (db *Database) Put(height uint64, data []byte) error { | ||
db.mu.Lock() | ||
defer db.mu.Unlock() | ||
|
||
if db.closed { | ||
return database.ErrClosed | ||
} | ||
|
||
if db.data == nil { | ||
db.data = make(map[uint64][]byte) | ||
} | ||
|
||
db.data[height] = slices.Clone(data) | ||
return nil | ||
} | ||
|
||
func (db *Database) Get(height uint64) ([]byte, error) { | ||
db.mu.RLock() | ||
defer db.mu.RUnlock() | ||
|
||
if db.closed { | ||
return nil, database.ErrClosed | ||
} | ||
|
||
data, ok := db.data[height] | ||
if !ok { | ||
return nil, database.ErrNotFound | ||
} | ||
|
||
return slices.Clone(data), nil | ||
} | ||
|
||
func (db *Database) Has(height uint64) (bool, error) { | ||
db.mu.RLock() | ||
defer db.mu.RUnlock() | ||
|
||
if db.closed { | ||
return false, database.ErrClosed | ||
} | ||
|
||
_, ok := db.data[height] | ||
return ok, nil | ||
} | ||
|
||
func (db *Database) Close() error { | ||
db.mu.Lock() | ||
defer db.mu.Unlock() | ||
|
||
if db.closed { | ||
return database.ErrClosed | ||
} | ||
|
||
db.closed = true | ||
db.data = nil | ||
return nil | ||
} |
DracoLi marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package memdb | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ava-labs/avalanchego/database" | ||
"github.com/ava-labs/avalanchego/database/heightindexdb/dbtest" | ||
) | ||
|
||
func TestInterface(t *testing.T) { | ||
for _, test := range dbtest.Tests { | ||
t.Run("memdb_"+test.Name, func(t *testing.T) { | ||
test.Test(t, func() database.HeightIndex { return &Database{} }) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.