-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/consensus/tendermint: Use MKVS for storing application state
- Loading branch information
Showing
76 changed files
with
2,482 additions
and
2,022 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
go/consensus/tendermint: Use MKVS for storing application state |
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,82 @@ | ||
package abci | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestContext(t *testing.T) { | ||
require := require.New(t) | ||
|
||
now := time.Unix(1580461674, 0) | ||
appState := NewMockApplicationState(MockApplicationStateConfig{}) | ||
ctx := appState.NewContext(ContextBeginBlock, now) | ||
defer ctx.Close() | ||
|
||
// Add some state. | ||
tree := ctx.State() | ||
err := tree.Insert(ctx, []byte("key"), []byte("value")) | ||
require.NoError(err, "Insert") | ||
|
||
// Test checkpoints. | ||
cp := ctx.StartCheckpoint() | ||
// Should panic on nested checkpoints. | ||
require.Panics(func() { ctx.StartCheckpoint() }) | ||
overlay := ctx.State() | ||
require.NotEqual(&tree, &overlay, "new State() should return the overlay") | ||
|
||
// Existing state should be there. | ||
value, err := overlay.Get(ctx, []byte("key")) | ||
require.NoError(err, "Get") | ||
require.EqualValues([]byte("value"), value) | ||
|
||
// Add some state to the overlay. | ||
err = overlay.Insert(ctx, []byte("blah"), []byte("value2")) | ||
require.NoError(err, "Insert") | ||
err = overlay.Remove(ctx, []byte("key")) | ||
require.NoError(err, "Remove") | ||
|
||
// Make sure updates didn't leak. | ||
value, err = tree.Get(ctx, []byte("key")) | ||
require.NoError(err, "Get") | ||
require.EqualValues([]byte("value"), value, "updates should not leak outside checkpoint") | ||
value, err = tree.Get(ctx, []byte("blah")) | ||
require.NoError(err, "Get") | ||
require.Nil(value, "updates should not leak outside checkpoint") | ||
|
||
// Commit checkpoint. | ||
cp.Commit() | ||
newTree := ctx.State() | ||
require.Equal(&tree, &newTree, "new State() should return the original tree") | ||
|
||
// Make sure updates were applied. | ||
value, err = tree.Get(ctx, []byte("key")) | ||
require.NoError(err, "Get") | ||
require.Nil(value, "updates should have been applied") | ||
value, err = tree.Get(ctx, []byte("blah")) | ||
require.NoError(err, "Get") | ||
require.EqualValues([]byte("value2"), value, "updates should have been applied") | ||
|
||
ctx.Close() | ||
} | ||
|
||
type testBlockContextKey struct{} | ||
|
||
func (k testBlockContextKey) NewDefault() interface{} { | ||
return 42 | ||
} | ||
|
||
func TestBlockContext(t *testing.T) { | ||
require := require.New(t) | ||
|
||
bc := NewBlockContext() | ||
|
||
value := bc.Get(testBlockContextKey{}) | ||
require.EqualValues(42, value, "block context key should have default value") | ||
|
||
bc.Set(testBlockContextKey{}, 21) | ||
value = bc.Get(testBlockContextKey{}) | ||
require.EqualValues(21, value, "block context key should have correct value") | ||
} |
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,32 @@ | ||
package abci | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
type errorUnavailableState struct { | ||
inner error | ||
} | ||
|
||
func (e *errorUnavailableState) Error() string { | ||
return fmt.Sprintf("unavailable/corrupted state: %s", e.inner.Error()) | ||
} | ||
|
||
func (e *errorUnavailableState) Unwrap() error { | ||
return e.inner | ||
} | ||
|
||
// UnavailableStateError wraps an error in an unavailable state error. | ||
func UnavailableStateError(err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
return &errorUnavailableState{err} | ||
} | ||
|
||
// IsUnavailableStateError returns true if any error in err's chain is an unavailable state error. | ||
func IsUnavailableStateError(err error) bool { | ||
var e *errorUnavailableState | ||
return errors.As(err, &e) | ||
} |
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
Oops, something went wrong.