-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: recover block curruption on poison error (#28)
* refactor: extract height key related functions * feat: rollback previous block on inject error * style: add comments
- Loading branch information
Jeff Woo
authored
Feb 11, 2022
1 parent
bd24353
commit ecdf9bd
Showing
5 changed files
with
118 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package rollbackable | ||
|
||
import ( | ||
tmdb "github.com/tendermint/tm-db" | ||
) | ||
|
||
type HasRollbackBatch interface { | ||
RollbackBatch() tmdb.Batch | ||
} | ||
|
||
var _ tmdb.Batch = (*RollbackableBatch)(nil) | ||
|
||
type RollbackableBatch struct { | ||
tmdb.Batch | ||
|
||
db tmdb.DB | ||
RollbackBatch tmdb.Batch | ||
} | ||
|
||
func NewRollbackableBatch(db tmdb.DB) *RollbackableBatch { | ||
return &RollbackableBatch{ | ||
db: db, | ||
Batch: db.NewBatch(), | ||
RollbackBatch: db.NewBatch(), | ||
} | ||
} | ||
|
||
// revert value for key to previous state | ||
func (b *RollbackableBatch) backup(key []byte) error { | ||
data, err := b.db.Get(key) | ||
if err != nil { | ||
return err | ||
} | ||
if data == nil { | ||
return b.RollbackBatch.Delete(key) | ||
} else { | ||
return b.RollbackBatch.Set(key, data) | ||
} | ||
} | ||
|
||
func (b *RollbackableBatch) Set(key, value []byte) error { | ||
if err := b.backup(key); err != nil { | ||
return err | ||
} | ||
return b.Batch.Set(key, value) | ||
} | ||
|
||
func (b *RollbackableBatch) Delete(key []byte) error { | ||
if err := b.backup(key); err != nil { | ||
return err | ||
} | ||
return b.Batch.Delete(key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters