2020-08-05 10:13:35 +00:00
|
|
|
package ethdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-09-08 06:19:06 +00:00
|
|
|
"os"
|
2020-08-05 10:13:35 +00:00
|
|
|
"testing"
|
2020-10-28 03:18:10 +00:00
|
|
|
|
2020-11-28 14:26:28 +00:00
|
|
|
"github.com/ledgerwatch/lmdb-go/lmdb"
|
2020-10-28 03:18:10 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-05 10:13:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBucketCRUD(t *testing.T) {
|
2020-08-14 06:41:18 +00:00
|
|
|
require := require.New(t)
|
2020-08-05 10:13:35 +00:00
|
|
|
kv := NewLMDB().InMem().MustOpen()
|
|
|
|
defer kv.Close()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2021-02-10 17:04:22 +00:00
|
|
|
tx, err := kv.Begin(ctx, RW)
|
2020-08-30 17:34:18 +00:00
|
|
|
require.NoError(err)
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
normalBucket := dbutils.Buckets[15]
|
|
|
|
deprecatedBucket := dbutils.DeprecatedBuckets[0]
|
|
|
|
migrator, ok := tx.(BucketMigrator)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-08-05 10:13:35 +00:00
|
|
|
|
2020-08-30 17:34:18 +00:00
|
|
|
// check thad buckets have unique DBI's
|
2020-10-28 03:18:10 +00:00
|
|
|
uniquness := map[dbutils.DBI]bool{}
|
2020-08-30 17:34:18 +00:00
|
|
|
castedKv := kv.(*LmdbKV)
|
|
|
|
for _, bucketCfg := range castedKv.buckets {
|
|
|
|
if bucketCfg.DBI == NonExistingDBI {
|
|
|
|
continue
|
2020-08-05 10:13:35 +00:00
|
|
|
}
|
2020-08-30 17:34:18 +00:00
|
|
|
_, ok := uniquness[bucketCfg.DBI]
|
|
|
|
require.False(ok)
|
|
|
|
uniquness[bucketCfg.DBI] = true
|
|
|
|
}
|
2020-08-05 10:13:35 +00:00
|
|
|
|
2020-08-30 17:34:18 +00:00
|
|
|
require.True(migrator.ExistsBucket(normalBucket))
|
|
|
|
require.True(errors.Is(migrator.DropBucket(normalBucket), ErrAttemptToDeleteNonDeprecatedBucket))
|
2020-08-05 10:13:35 +00:00
|
|
|
|
2020-08-30 17:34:18 +00:00
|
|
|
require.False(migrator.ExistsBucket(deprecatedBucket))
|
|
|
|
require.NoError(migrator.CreateBucket(deprecatedBucket))
|
|
|
|
require.True(migrator.ExistsBucket(deprecatedBucket))
|
2020-08-05 10:13:35 +00:00
|
|
|
|
2020-08-30 17:34:18 +00:00
|
|
|
require.NoError(migrator.DropBucket(deprecatedBucket))
|
|
|
|
require.False(migrator.ExistsBucket(deprecatedBucket))
|
2020-08-05 10:13:35 +00:00
|
|
|
|
2020-08-30 17:34:18 +00:00
|
|
|
require.NoError(migrator.CreateBucket(deprecatedBucket))
|
|
|
|
require.True(migrator.ExistsBucket(deprecatedBucket))
|
2020-08-05 10:13:35 +00:00
|
|
|
|
2020-08-30 17:34:18 +00:00
|
|
|
err = tx.Cursor(deprecatedBucket).Put([]byte{1}, []byte{1})
|
|
|
|
require.NoError(err)
|
2020-10-24 08:09:20 +00:00
|
|
|
v, err := tx.GetOne(deprecatedBucket, []byte{1})
|
2020-08-30 17:34:18 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal([]byte{1}, v)
|
2020-08-05 10:13:35 +00:00
|
|
|
|
2020-08-30 17:34:18 +00:00
|
|
|
buckets, err := migrator.ExistingBuckets()
|
|
|
|
require.NoError(err)
|
|
|
|
require.True(len(buckets) > 10)
|
2020-08-14 06:41:18 +00:00
|
|
|
|
2020-08-30 17:34:18 +00:00
|
|
|
// check thad buckets have unique DBI's
|
2020-10-28 03:18:10 +00:00
|
|
|
uniquness = map[dbutils.DBI]bool{}
|
2020-08-30 17:34:18 +00:00
|
|
|
for _, bucketCfg := range castedKv.buckets {
|
|
|
|
if bucketCfg.DBI == NonExistingDBI {
|
|
|
|
continue
|
2020-08-05 10:13:35 +00:00
|
|
|
}
|
2020-08-30 17:34:18 +00:00
|
|
|
_, ok := uniquness[bucketCfg.DBI]
|
|
|
|
require.False(ok)
|
|
|
|
uniquness[bucketCfg.DBI] = true
|
2020-08-05 10:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-08 06:19:06 +00:00
|
|
|
|
|
|
|
func TestReadOnlyMode(t *testing.T) {
|
|
|
|
path := os.TempDir() + "/tm1"
|
|
|
|
err := os.RemoveAll(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
db1 := NewLMDB().Path(path).WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
|
|
|
|
return dbutils.BucketsCfg{
|
2021-03-19 12:54:47 +00:00
|
|
|
dbutils.HeadersBucket: dbutils.BucketConfigItem{},
|
2020-09-08 06:19:06 +00:00
|
|
|
}
|
|
|
|
}).MustOpen()
|
|
|
|
db1.Close()
|
|
|
|
|
2020-11-28 14:26:28 +00:00
|
|
|
db2 := NewLMDB().Flags(func(flags uint) uint { return flags | lmdb.Readonly }).Path(path).WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
|
2020-09-08 06:19:06 +00:00
|
|
|
return dbutils.BucketsCfg{
|
2021-03-19 12:54:47 +00:00
|
|
|
dbutils.HeadersBucket: dbutils.BucketConfigItem{},
|
2020-09-08 06:19:06 +00:00
|
|
|
}
|
2020-11-28 14:26:28 +00:00
|
|
|
}).MustOpen()
|
2020-09-08 06:19:06 +00:00
|
|
|
|
2021-02-10 17:04:22 +00:00
|
|
|
tx, err := db2.Begin(context.Background(), RO)
|
2020-09-08 06:19:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-03-19 12:54:47 +00:00
|
|
|
c := tx.Cursor(dbutils.HeadersBucket)
|
2020-09-08 06:19:06 +00:00
|
|
|
_, _, err = c.Seek([]byte("some prefix"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|