2020-08-05 10:13:35 +00:00
|
|
|
package ethdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
2020-10-28 03:18:10 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
2020-10-28 03:18:10 +00:00
|
|
|
"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)
|
2021-05-17 09:14:08 +00:00
|
|
|
kv := NewMemKV()
|
2020-08-05 10:13:35 +00:00
|
|
|
defer kv.Close()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2021-03-21 13:15:25 +00:00
|
|
|
tx, err := kv.BeginRw(ctx)
|
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{}
|
2021-05-19 03:47:28 +00:00
|
|
|
castedKv, ok := kv.(*MdbxKV)
|
|
|
|
if !ok {
|
|
|
|
t.Skip()
|
|
|
|
}
|
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-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
|
|
|
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := tx.RwCursor(deprecatedBucket)
|
|
|
|
require.NoError(err)
|
|
|
|
err = c.Put([]byte{1}, []byte{1})
|
2020-08-30 17:34:18 +00:00
|
|
|
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) {
|
2021-05-18 12:13:16 +00:00
|
|
|
path := t.TempDir()
|
2021-05-20 16:46:12 +00:00
|
|
|
db1 := NewMDBX().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
|
|
|
}
|
|
|
|
}).MustOpen()
|
|
|
|
db1.Close()
|
|
|
|
|
2021-05-20 16:46:12 +00:00
|
|
|
db2 := NewMDBX().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()
|
2021-05-20 16:46:12 +00:00
|
|
|
defer db2.Close()
|
2020-09-08 06:19:06 +00:00
|
|
|
|
2021-04-03 06:26:00 +00:00
|
|
|
tx, err := db2.BeginRo(context.Background())
|
2021-04-02 06:36:49 +00:00
|
|
|
require.NoError(t, err)
|
2021-05-19 03:47:28 +00:00
|
|
|
defer tx.Rollback()
|
2020-09-08 06:19:06 +00:00
|
|
|
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := tx.Cursor(dbutils.HeadersBucket)
|
|
|
|
require.NoError(t, err)
|
2020-09-08 06:19:06 +00:00
|
|
|
_, _, err = c.Seek([]byte("some prefix"))
|
2021-04-02 06:36:49 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-08 06:19:06 +00:00
|
|
|
}
|