Skip to content

Remove key from badger db after mempools backup restored #811

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 2 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions common/database/badgerdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"os"
"path/filepath"

"github.com/zoobc/zoobc-core/common/blocker"

"github.com/dgraph-io/badger"
"github.com/dgraph-io/badger/options"
"github.com/zoobc/zoobc-core/common/blocker"
)

var (
Expand All @@ -17,7 +16,7 @@ var (
)

type (
// SqliteDBInstance as public interface that should implemented
// BadgerDBInstance as public interface that should implemented
BadgerDBInstance interface {
InitializeBadgerDB(dbPath, dbName string) error
OpenBadgerDB(dbPath, dbName string) (*badger.DB, error)
Expand All @@ -35,7 +34,7 @@ func NewBadgerDB() *BadgerDB {
}

/*
InitializeDB initialize badger database file from given dbPath and dbName
InitializeBadgerDB initialize badger database file from given dbPath and dbName
if dbPath not exist create given dbPath
if dbName / file not exist, create file with given dbName
return nil if dbPath/dbName exist
Expand All @@ -55,6 +54,7 @@ func (bdb *BadgerDB) InitializeBadgerDB(dbPath, dbName string) error {
return nil
}

// OpenBadgerDB will open badger db connection
func (bdb *BadgerDB) OpenBadgerDB(dbPath, dbName string) (*badger.DB, error) {
opts := badger.DefaultOptions(filepath.Join(dbPath, dbName))
// avoid memory-mapping log files
Expand Down
12 changes: 9 additions & 3 deletions common/kvdb/kvdb.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
kvdb is key-value database abstraction of badger db implementation
*/
// Package kvdb is key-value database abstraction of badger db implementation /**
package kvdb

import (
Expand All @@ -18,6 +16,7 @@ type (
BatchInsert(updates map[string][]byte) error
Get(key string) ([]byte, error)
GetByPrefix(prefix string) (map[string][]byte, error)
Delete(key string) error
}
KVExecutor struct {
Db *badger.DB
Expand Down Expand Up @@ -163,3 +162,10 @@ func (kve *KVExecutor) Rollback(latestBlock, forkingPoint string) error {

return nil
}

// Delete allowing to delete a key with transaction has been wrapped
func (kve *KVExecutor) Delete(key string) error {
return kve.Db.Update(func(txn *badger.Txn) error {
return txn.Delete([]byte(key))
})
}
40 changes: 38 additions & 2 deletions common/kvdb/kvdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"reflect"
"testing"

"github.com/zoobc/zoobc-core/common/database"

"github.com/dgraph-io/badger"
"github.com/zoobc/zoobc-core/common/database"
)

func getMockedKVDb() *badger.DB {
Expand Down Expand Up @@ -172,3 +171,40 @@ func TestNewKVExecutor(t *testing.T) {
})
}
}

func TestKVExecutor_Delete(t *testing.T) {

type fields struct {
Db *badger.DB
}
type args struct {
key string
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "WantSuccess",
fields: fields{
Db: getMockedKVDb(),
},
args: args{
key: "mempool_backup",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
kve := &KVExecutor{
Db: tt.fields.Db,
}
if err := kve.Delete(tt.args.key); (err != nil) != tt.wantErr {
t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
}
defer cleanUpTestData()
})
}
}
91 changes: 0 additions & 91 deletions common/kvdb/kvdbmock.go

This file was deleted.

8 changes: 7 additions & 1 deletion core/blockchainsync/processFork.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ func (fp *ForkingProcessor) restoreMempoolsBackup() error {
}
err = fp.MempoolService.ValidateMempoolTransaction(mempoolTX)
if err != nil {
return err
// no need to break the process in this case
fp.Logger.Warnf("Invalid mempool want to restore with ID: %d", tx.GetID())
}

txType, err = fp.ActionTypeSwitcher.GetTransactionType(tx)
Expand Down Expand Up @@ -340,6 +341,11 @@ func (fp *ForkingProcessor) restoreMempoolsBackup() error {
if err != nil {
return err
}
// remove restored mempools from badger
err = fp.KVExecutor.Delete(commonUtil.GetKvDbMempoolDBKey(fp.ChainType))
if err != nil {
return err
}
}
return nil
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/btcsuite/btcutil v1.0.1
github.com/dgraph-io/badger v1.6.0
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129
github.com/golang/protobuf v1.3.3
github.com/google/go-cmp v0.3.1
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129 h1:tT8iWCYw4uOem71yYA3htfH+LNopJvcqZQshm56G5L4=
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
Expand Down Expand Up @@ -295,7 +293,6 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 h1:5Beo0mZN8dRzgrMMkDp0jc8YXQKx9DiJ2k1dkvGsn5A=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down