-
Notifications
You must be signed in to change notification settings - Fork 4
Add State Hash #39
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
Closed
Closed
Add State Hash #39
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,95 @@ | ||
| package db | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/0xsoniclabs/substate/types" | ||
| "github.com/syndtr/goleveldb/leveldb" | ||
| "github.com/syndtr/goleveldb/leveldb/opt" | ||
| ) | ||
|
|
||
| const StateHashDBPrefix = "dbh" // StateHashDBPrefix + 0x + blockNum(hex) -> StateHash | ||
|
|
||
| // StateHashDB is a wrapper around CodeDB. It extends it with Has/Get/Put/DeleteSubstate functions. | ||
| // | ||
| //go:generate mockgen -source=substate_db.go -destination=./substate_db_mock.go -package=db | ||
| type StateHashDB interface { | ||
| BaseDB | ||
|
|
||
| PutStateHash(blockNumber uint64, stateRoot []byte) error | ||
| } | ||
|
|
||
| // NewDefaultStateHashDB creates new instance of StateHashDB with default options. | ||
| func NewDefaultStateHashDB(path string) (StateHashDB, error) { | ||
| return newStateHashDB(path, nil, nil, nil) | ||
| } | ||
|
|
||
| // NewStateHashDB creates new instance of StateHashDB with customizable options. | ||
| // Note: Any of three options is nillable. If that's the case a default value for the option is set. | ||
| func NewStateHashDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (StateHashDB, error) { | ||
| return newStateHashDB(path, o, wo, ro) | ||
| } | ||
|
|
||
| func MakeDefaultStateHashDB(db *leveldb.DB) StateHashDB { | ||
| return &stateHashDB{&baseDB{backend: db}} | ||
| } | ||
|
|
||
| func MakeDefaultStateHashDBFromBaseDB(db BaseDB) StateHashDB { | ||
| return &stateHashDB{&baseDB{backend: db.getBackend()}} | ||
| } | ||
|
|
||
| // NewReadOnlyStateHashDB creates a new instance of read-only StateHashDB. | ||
| func NewReadOnlyStateHashDB(path string) (StateHashDB, error) { | ||
| return newStateHashDB(path, &opt.Options{ReadOnly: true}, nil, nil) | ||
| } | ||
|
|
||
| func MakeStateHashDB(db *leveldb.DB, wo *opt.WriteOptions, ro *opt.ReadOptions) StateHashDB { | ||
| return &stateHashDB{&baseDB{backend: db, wo: wo, ro: ro}} | ||
| } | ||
|
|
||
| func newStateHashDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*stateHashDB, error) { | ||
| base, err := newBaseDB(path, o, wo, ro) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &stateHashDB{base}, nil | ||
| } | ||
|
|
||
| type stateHashDB struct { | ||
| BaseDB | ||
| } | ||
|
|
||
| func (db *stateHashDB) PutStateHash(blockNumber uint64, stateRoot []byte) error { | ||
| hex := strconv.FormatUint(blockNumber, 16) | ||
| fullPrefix := StateHashDBPrefix + "0x" + hex | ||
| err := db.Put([]byte(fullPrefix), stateRoot) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to put state hash for block %d: %v", blockNumber, err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (db *stateHashDB) PutStateHashString(blockNumber string, stateRoot string) error { | ||
| fullPrefix := StateHashDBPrefix + blockNumber | ||
| err := db.Put([]byte(fullPrefix), types.Hex2Bytes(strings.TrimPrefix(stateRoot, "0x"))) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to put state hash for block %s: %v", blockNumber, err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (db *stateHashDB) GetStateHash(number int) (types.Hash, error) { | ||
| hex := strconv.FormatUint(uint64(number), 16) | ||
| stateRoot, err := db.Get([]byte(StateHashDBPrefix + "0x" + hex)) | ||
| if err != nil { | ||
| return types.Hash{}, err | ||
| } | ||
|
|
||
| if stateRoot == nil { | ||
| return types.Hash{}, nil | ||
| } | ||
|
|
||
| return types.Hash(stateRoot), nil | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is taken over from Aida: block encoding into hex is different from bigEndian encoding we use everywhere else. Maybe we could consider to make a migration to another prefix to unify this.