Skip to content

Commit b3421b0

Browse files
coderbradleezjshen14
authored andcommitted
Reduplicative ations with same hash shows in action list 1386 (#1404)
* Reduplicative ations with same hash shows in action list 1386
1 parent 51cf380 commit b3421b0

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

blockchain/indexbuilder.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ func (ib *IndexBuilder) initAndLoadActions() error {
181181
if err != nil {
182182
return err
183183
}
184+
} else {
185+
if err = ib.dao.kvstore.Delete(blockAddressActionMappingNS, nil); err != nil {
186+
return err
187+
}
188+
if err = ib.dao.kvstore.Delete(blockAddressActionCountMappingNS, nil); err != nil {
189+
return err
190+
}
184191
}
185192
zap.L().Info("Loading actions", zap.Uint64("startHeight", startHeight), zap.Uint64("startIndex", startIndex))
186193
batch := db.NewBatch()

db/db_bolt.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,26 @@ func (b *boltDB) Get(namespace string, key []byte) ([]byte, error) {
9090
return nil, errors.Wrap(ErrIO, err.Error())
9191
}
9292

93-
// Delete deletes a record
93+
// Delete deletes a record,if key is nil,this will delete the whole bucket
9494
func (b *boltDB) Delete(namespace string, key []byte) (err error) {
9595
numRetries := b.config.NumRetries
9696
for c := uint8(0); c < numRetries; c++ {
97-
err = b.db.Update(func(tx *bolt.Tx) error {
98-
bucket := tx.Bucket([]byte(namespace))
99-
if bucket == nil {
97+
if key == nil {
98+
err = b.db.Update(func(tx *bolt.Tx) error {
99+
if err := tx.DeleteBucket([]byte(namespace)); err != bolt.ErrBucketNotFound {
100+
return err
101+
}
100102
return nil
101-
}
102-
return bucket.Delete(key)
103-
})
103+
})
104+
} else {
105+
err = b.db.Update(func(tx *bolt.Tx) error {
106+
bucket := tx.Bucket([]byte(namespace))
107+
if bucket == nil {
108+
return nil
109+
}
110+
return bucket.Delete(key)
111+
})
112+
}
104113
if err == nil {
105114
break
106115
}

db/db_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,40 @@ func TestCacheKV(t *testing.T) {
323323
testFunc(NewOnDiskDB(cfg), t)
324324
})
325325
}
326+
327+
func TestDeleteBucket(t *testing.T) {
328+
testFunc := func(kv KVStore, t *testing.T) {
329+
require := require.New(t)
330+
331+
require.Nil(kv.Start(context.Background()))
332+
defer func() {
333+
err := kv.Stop(context.Background())
334+
require.Nil(err)
335+
}()
336+
337+
require.NoError(kv.Put(bucket1, testK1[0], testV1[0]))
338+
v, _ := kv.Get(bucket1, testK1[0])
339+
require.Equal(testV1[0], v)
340+
341+
require.NoError(kv.Put(bucket2, testK1[0], testV1[0]))
342+
v, _ = kv.Get(bucket2, testK1[0])
343+
require.Equal(testV1[0], v)
344+
345+
require.NoError(kv.Delete(bucket1, nil))
346+
v, _ = kv.Get(bucket1, testK1[0])
347+
require.Equal([]uint8([]byte(nil)), v)
348+
349+
v, _ = kv.Get(bucket2, testK1[0])
350+
require.Equal(testV1[0], v)
351+
}
352+
353+
path := "test-cache-kv.bolt"
354+
testFile, _ := ioutil.TempFile(os.TempDir(), path)
355+
testPath := testFile.Name()
356+
cfg.DbPath = testPath
357+
t.Run("Bolt DB", func(t *testing.T) {
358+
testutil.CleanupPath(t, testPath)
359+
defer testutil.CleanupPath(t, testPath)
360+
testFunc(NewBoltDB(cfg), t)
361+
})
362+
}

0 commit comments

Comments
 (0)