Skip to content

Commit

Permalink
removes apply unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott committed Jun 29, 2023
1 parent 0fa5078 commit 9f73b31
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 118 deletions.
31 changes: 6 additions & 25 deletions persistence/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package persistence
import (
"context"
"errors"
"fmt"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
Expand Down Expand Up @@ -37,16 +36,15 @@ type PostgresContext struct {
networkId string
}

// NewSavePoint generates a new Savepoint for the TreeStore.
// NewSavePoint generates a new Savepoint for this context.
func (p *PostgresContext) NewSavePoint() error {
// NB: pgx transaction is started elsewhere so we only need to make a savepoint for each submodule store
if err := p.stateTrees.Savepoint(); err != nil {
return err
}
return nil
}

// RollbackToSavepoint triggers a rollback for the current pgx transaction and the TreeStore
// RollbackToSavepoint triggers a rollback for the current pgx transaction and the underylying submodule stores.
func (p *PostgresContext) RollbackToSavePoint() error {
ctx := context.TODO()
err := p.tx.Rollback(ctx)
Expand All @@ -64,43 +62,26 @@ func (p *PostgresContext) ComputeStateHash() (string, error) {
return p.stateHash, nil
}

// Apply atomically commits a block to a set of AtomicStores. If any of the submodules fail to prepare or commit,
// the store manager triggers a rollback.
func (p *PostgresContext) Apply(block *coreTypes.Block) error {
manager := &storeManager{
stores: []modules.AtomicStore{
p.stateTrees,
// TODO impl for p.txIndexer
// TODO impl for p.blockStore
},
tx: p.tx,
}
if ok := manager.apply(); !ok {
return fmt.Errorf("failed to atomicallyp apply block: %+v", block)
}
return nil
}

func (p *PostgresContext) Commit(proposerAddr, quorumCert []byte) error {
p.logger.Info().Int64("height", p.Height).Msg("About to commit block & context")

// 1. Create a persistence block proto
// Create a persistence block proto
block, err := p.prepareBlock(proposerAddr, quorumCert)
if err != nil {
return err
}

// 2. Save the block in the BlockStore at the current height
// Save the block in the BlockStore at the current height
if err := p.blockStore.StoreBlock(uint64(p.Height), block); err != nil {
return err
}

// 3. Insert the block into the SQL DB
// Insert the block into the SQL DB
if err := p.insertBlock(block); err != nil {
return err
}

// 4. Commit the SQL transaction
// Commit the SQL transaction
ctx := context.TODO()
if err := p.tx.Commit(ctx); err != nil {
return err
Expand Down
67 changes: 0 additions & 67 deletions persistence/transaction.go

This file was deleted.

26 changes: 0 additions & 26 deletions persistence/trees/trees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,6 @@ func TestTreeStore_Update(t *testing.T) {
})
}

func TestPersistenceContext_Apply(t *testing.T) {
pool, resource, dbUrl := test_artifacts.SetupPostgresDocker()
t.Cleanup(func() {
if err := pool.Purge(resource); err != nil {
log.Fatalf("could not purge resource: %s", err)
}
})

t.Run("should apply commit to all stores and return nil", func(t *testing.T) {
pmod := newTestPersistenceModule(t, dbUrl)
context := NewTestPostgresContext(t, 0, pmod)

_, err := createAndInsertDefaultTestApp(context)
assert.NoError(t, err)

testblock := &coreTypes.Block{
BlockHeader: &coreTypes.BlockHeader{
Height: 1,
},
}

err = context.Apply(testblock)
assert.NoError(t, err)
})
}

// createMockBus returns a mock bus with stubbed out functions for bus registration
func createMockBus(t *testing.T, runtimeMgr modules.RuntimeMgr) *mockModules.MockBus {
t.Helper()
Expand Down

0 comments on commit 9f73b31

Please sign in to comment.