Skip to content
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

[Chore][WIP] Adds assertions to rollback error path test #944

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions persistence/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ func (p *PostgresContext) SetSavePoint() error {

// RollbackToSavepoint triggers a rollback for the current pgx transaction and the underylying submodule stores.
func (p *PostgresContext) RollbackToSavePoint() error {
ctx, _ := p.getCtxAndTx()
pgErr := p.tx.Rollback(ctx)
treesErr := p.stateTrees.Rollback()
return errors.Join(pgErr, treesErr)
err := p.stateTrees.Rollback()
p.Release()
return err
}

// Full details in the thread from the PR review: https://github.com/pokt-network/pocket/pull/285#discussion_r1018471719
Expand Down
10 changes: 9 additions & 1 deletion persistence/trees/trees.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package trees
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"hash"
"log"
Expand Down Expand Up @@ -322,7 +323,14 @@ func (t *treeStore) Rollback() error {
return nil
}
t.logger.Err(ErrFailedRollback)
return ErrFailedRollback
// emergency shut down to prevent improper commit to trees
errs := []error{ErrFailedRollback}
for _, st := range t.merkleTrees {
if err := st.nodeStore.Stop(); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}

// save commits any pending changes to the trees and creates a copy of the current worldState,
Expand Down
35 changes: 29 additions & 6 deletions persistence/trees/trees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@
require.NoError(t, pool.Purge(resource))
})

t.Run("should update actor trees, commit, and modify the state hash", func(t *testing.T) {
pmod := newTestPersistenceModule(t, dbUrl)
t.Run("should update application trees, commit, and modify the state hash", func(t *testing.T) {
cfg := newTestDefaultConfig(t, dbUrl)
pmod := newTestPersistenceModule(t, cfg)
context := newTestPostgresContext(t, 0, pmod)

require.NoError(t, context.SetSavePoint())
Expand All @@ -69,21 +70,43 @@
})

t.Run("should fail to rollback when no treestore savepoint is set", func(t *testing.T) {
pmod := newTestPersistenceModule(t, dbUrl)
// NB: test needs a file to persist against for test purposes
tmpdir := t.TempDir()
cfg := newTestDefaultConfig(t, dbUrl)
cfg.Persistence.TreesStoreDir = tmpdir
pmod := newTestPersistenceModule(t, cfg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pmod := newTestPersistenceModule(t, cfg)
pMod := newTestPersistenceModule(t, cfg)

context := newTestPostgresContext(t, 0, pmod)

err := context.RollbackToSavePoint()
// calculate first hash
hash1, err := context.ComputeStateHash()
require.NoError(t, err)
require.NotEmpty(t, hash1)

// insert default app
_, err = createAndInsertDefaultTestApp(t, context)
require.NoError(t, err)

// trigger a rollback without creating a savepoint
err = context.RollbackToSavePoint()
require.Error(t, err)
require.ErrorIs(t, err, trees.ErrFailedRollback)

// initialize a new context with the same persistence module
context2 := newTestPostgresContext(t, 0, pmod)
hash3, err := context2.ComputeStateHash()
require.NoError(t, err)
require.NotEmpty(t, hash3)

// unsuccessful rollback should not violate treeStore integrity
require.Equal(t, hash3, hash1)
})
}

func newTestPersistenceModule(t *testing.T, databaseURL string) modules.PersistenceModule {
func newTestPersistenceModule(t *testing.T, cfg *configs.Config) modules.PersistenceModule {
t.Helper()
teardownDeterministicKeygen := keygen.GetInstance().SetSeed(42)
defer teardownDeterministicKeygen()

cfg := newTestDefaultConfig(t, databaseURL)
genesisState, _ := test_artifacts.NewGenesisState(
genesisStateNumValidators,
genesisStateNumServicers,
Expand Down Expand Up @@ -121,7 +144,7 @@
}
return cfg
}
func createAndInsertDefaultTestApp(t *testing.T, db *persistence.PostgresContext) (*core_types.Actor, error) {

Check failure on line 147 in persistence/trees/trees_test.go

View workflow job for this annotation

GitHub Actions / lint

createAndInsertDefaultTestApp - result 0 (*github.com/pokt-network/pocket/shared/core/types.Actor) is never used (unparam)
t.Helper()
app := newTestApp(t)

Expand Down
Loading