Skip to content

Commit 4d6d207

Browse files
authored
remove key from badger db after mempools backup restored (#811)
remove key from badger db after mempools backup restored (#811)
1 parent 48ed2ab commit 4d6d207

File tree

7 files changed

+58
-105
lines changed

7 files changed

+58
-105
lines changed

common/database/badgerdb.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import (
55
"os"
66
"path/filepath"
77

8-
"github.com/zoobc/zoobc-core/common/blocker"
9-
108
"github.com/dgraph-io/badger"
119
"github.com/dgraph-io/badger/options"
10+
"github.com/zoobc/zoobc-core/common/blocker"
1211
)
1312

1413
var (
@@ -17,7 +16,7 @@ var (
1716
)
1817

1918
type (
20-
// SqliteDBInstance as public interface that should implemented
19+
// BadgerDBInstance as public interface that should implemented
2120
BadgerDBInstance interface {
2221
InitializeBadgerDB(dbPath, dbName string) error
2322
OpenBadgerDB(dbPath, dbName string) (*badger.DB, error)
@@ -35,7 +34,7 @@ func NewBadgerDB() *BadgerDB {
3534
}
3635

3736
/*
38-
InitializeDB initialize badger database file from given dbPath and dbName
37+
InitializeBadgerDB initialize badger database file from given dbPath and dbName
3938
if dbPath not exist create given dbPath
4039
if dbName / file not exist, create file with given dbName
4140
return nil if dbPath/dbName exist
@@ -55,6 +54,7 @@ func (bdb *BadgerDB) InitializeBadgerDB(dbPath, dbName string) error {
5554
return nil
5655
}
5756

57+
// OpenBadgerDB will open badger db connection
5858
func (bdb *BadgerDB) OpenBadgerDB(dbPath, dbName string) (*badger.DB, error) {
5959
opts := badger.DefaultOptions(filepath.Join(dbPath, dbName))
6060
// avoid memory-mapping log files

common/kvdb/kvdb.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/**
2-
kvdb is key-value database abstraction of badger db implementation
3-
*/
1+
// Package kvdb is key-value database abstraction of badger db implementation /**
42
package kvdb
53

64
import (
@@ -18,6 +16,7 @@ type (
1816
BatchInsert(updates map[string][]byte) error
1917
Get(key string) ([]byte, error)
2018
GetByPrefix(prefix string) (map[string][]byte, error)
19+
Delete(key string) error
2120
}
2221
KVExecutor struct {
2322
Db *badger.DB
@@ -163,3 +162,10 @@ func (kve *KVExecutor) Rollback(latestBlock, forkingPoint string) error {
163162

164163
return nil
165164
}
165+
166+
// Delete allowing to delete a key with transaction has been wrapped
167+
func (kve *KVExecutor) Delete(key string) error {
168+
return kve.Db.Update(func(txn *badger.Txn) error {
169+
return txn.Delete([]byte(key))
170+
})
171+
}

common/kvdb/kvdb_test.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import (
55
"reflect"
66
"testing"
77

8-
"github.com/zoobc/zoobc-core/common/database"
9-
108
"github.com/dgraph-io/badger"
9+
"github.com/zoobc/zoobc-core/common/database"
1110
)
1211

1312
func getMockedKVDb() *badger.DB {
@@ -172,3 +171,40 @@ func TestNewKVExecutor(t *testing.T) {
172171
})
173172
}
174173
}
174+
175+
func TestKVExecutor_Delete(t *testing.T) {
176+
177+
type fields struct {
178+
Db *badger.DB
179+
}
180+
type args struct {
181+
key string
182+
}
183+
tests := []struct {
184+
name string
185+
fields fields
186+
args args
187+
wantErr bool
188+
}{
189+
{
190+
name: "WantSuccess",
191+
fields: fields{
192+
Db: getMockedKVDb(),
193+
},
194+
args: args{
195+
key: "mempool_backup",
196+
},
197+
},
198+
}
199+
for _, tt := range tests {
200+
t.Run(tt.name, func(t *testing.T) {
201+
kve := &KVExecutor{
202+
Db: tt.fields.Db,
203+
}
204+
if err := kve.Delete(tt.args.key); (err != nil) != tt.wantErr {
205+
t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
206+
}
207+
defer cleanUpTestData()
208+
})
209+
}
210+
}

common/kvdb/kvdbmock.go

Lines changed: 0 additions & 91 deletions
This file was deleted.

core/blockchainsync/processFork.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ func (fp *ForkingProcessor) restoreMempoolsBackup() error {
308308
}
309309
err = fp.MempoolService.ValidateMempoolTransaction(mempoolTX)
310310
if err != nil {
311-
return err
311+
// no need to break the process in this case
312+
fp.Logger.Warnf("Invalid mempool want to restore with ID: %d", tx.GetID())
312313
}
313314

314315
txType, err = fp.ActionTypeSwitcher.GetTransactionType(tx)
@@ -340,6 +341,11 @@ func (fp *ForkingProcessor) restoreMempoolsBackup() error {
340341
if err != nil {
341342
return err
342343
}
344+
// remove restored mempools from badger
345+
err = fp.KVExecutor.Delete(commonUtil.GetKvDbMempoolDBKey(fp.ChainType))
346+
if err != nil {
347+
return err
348+
}
343349
}
344350
return nil
345351
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/btcsuite/btcutil v1.0.1
1010
github.com/dgraph-io/badger v1.6.0
1111
github.com/go-ole/go-ole v1.2.4 // indirect
12-
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129
1312
github.com/golang/protobuf v1.3.3
1413
github.com/google/go-cmp v0.3.1
1514
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
7979
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
8080
github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=
8181
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
82-
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129 h1:tT8iWCYw4uOem71yYA3htfH+LNopJvcqZQshm56G5L4=
83-
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
8482
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
8583
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
8684
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
@@ -295,7 +293,6 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm
295293
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
296294
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
297295
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
298-
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
299296
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 h1:5Beo0mZN8dRzgrMMkDp0jc8YXQKx9DiJ2k1dkvGsn5A=
300297
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
301298
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=

0 commit comments

Comments
 (0)