diff --git a/p2p/module_raintree_test.go b/p2p/module_raintree_test.go index e7ed4be8e..3fbb212fe 100644 --- a/p2p/module_raintree_test.go +++ b/p2p/module_raintree_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "log" "os" "path/filepath" "sort" @@ -288,7 +289,7 @@ func prepareBusMock(t *testing.T, wg *sync.WaitGroup, consensusMock *modulesMock busMock.EXPECT().PublishEventToBus(gomock.Any()).Do(func(e *debug.PocketEvent) { wg.Done() - fmt.Println("App specific bus mock publishing event to bus") + log.Println("App specific bus mock publishing event to bus") }).MaxTimes(1) // Using `MaxTimes` rather than `Times` because originator node implicitly handles the message busMock.EXPECT().GetConsensusModule().Return(consensusMock).AnyTimes() diff --git a/persistence/CHANGELOG.md b/persistence/CHANGELOG.md index ac3e52e83..fdf982ca4 100644 --- a/persistence/CHANGELOG.md +++ b/persistence/CHANGELOG.md @@ -7,12 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.0.0.5] - 2022-09-14 + +- Consolidated `PostgresContext` and `PostgresDb` into a single structure + ## [0.0.0.4] - 2022-08-25 + **Encapsulate structures previously in shared [#163](github.com/pokt-network/pocket/issues/163)** -- Renamed schema -> types + +- Renamed schema -> types - Added genesis, config, and unstaking proto files from shared - Ensured proto structures implement shared interfaces -- Populate `PersistenceGenesisState` uses shared interfaces in order to accept `MockPersistenceGenesisState` +- Populate `PersistenceGenesisState` uses shared interfaces in order to accept `MockPersistenceGenesisState` - ^ Same applies for `PersistenceConfig` - Bumped cleanup TODOs to #149 due to scope size of #163 diff --git a/persistence/account.go b/persistence/account.go index aa8790002..cacb86ef4 100644 --- a/persistence/account.go +++ b/persistence/account.go @@ -23,13 +23,13 @@ func (p PostgresContext) GetAccountAmount(address []byte, height int64) (amount } func (p PostgresContext) getAccountAmountStr(address string, height int64) (amount string, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return } amount = defaultAccountAmountStr - if err = txn.QueryRow(ctx, types.GetAccountAmountQuery(address, height)).Scan(&amount); err != pgx.ErrNoRows { + if err = tx.QueryRow(ctx, types.GetAccountAmountQuery(address, height)).Scan(&amount); err != pgx.ErrNoRows { return } @@ -53,7 +53,7 @@ func (p PostgresContext) SubtractAccountAmount(address []byte, amount string) er // DISCUSS(team): If we are okay with `GetAccountAmount` return 0 as a default, this function can leverage // `operationAccountAmount` with `*orig = *delta` and make everything much simpler. func (p PostgresContext) SetAccountAmount(address []byte, amount string) error { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } @@ -62,7 +62,7 @@ func (p PostgresContext) SetAccountAmount(address []byte, amount string) error { return err } // DISCUSS(team): Do we want to panic if `amount < 0` here? - if _, err = txn.Exec(ctx, types.InsertAccountAmountQuery(hex.EncodeToString(address), amount, height)); err != nil { + if _, err = tx.Exec(ctx, types.InsertAccountAmountQuery(hex.EncodeToString(address), amount, height)); err != nil { return err } return nil @@ -76,7 +76,7 @@ func (p *PostgresContext) operationAccountAmount(address []byte, deltaAmount str // TODO(andrew): remove address param func (p PostgresContext) InsertPool(name string, address []byte, amount string) error { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } @@ -84,20 +84,20 @@ func (p PostgresContext) InsertPool(name string, address []byte, amount string) if err != nil { return err } - if _, err = txn.Exec(ctx, types.InsertPoolAmountQuery(name, amount, height)); err != nil { + if _, err = tx.Exec(ctx, types.InsertPoolAmountQuery(name, amount, height)); err != nil { return err } return nil } func (p PostgresContext) GetPoolAmount(name string, height int64) (amount string, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return } amount = defaultAccountAmountStr - if err = txn.QueryRow(ctx, types.GetPoolAmountQuery(name, height)).Scan(&amount); err != pgx.ErrNoRows { + if err = tx.QueryRow(ctx, types.GetPoolAmountQuery(name, height)).Scan(&amount); err != pgx.ErrNoRows { return } @@ -122,7 +122,7 @@ func (p PostgresContext) SubtractPoolAmount(name string, amount string) error { // `operationPoolAmount` with `*orig = *delta` and make everything much simpler. // DISCUSS(team): Do we have a use-case for this function? func (p PostgresContext) SetPoolAmount(name string, amount string) error { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } @@ -130,7 +130,7 @@ func (p PostgresContext) SetPoolAmount(name string, amount string) error { if err != nil { return err } - if _, err = txn.Exec(ctx, types.InsertPoolAmountQuery(name, amount, height)); err != nil { + if _, err = tx.Exec(ctx, types.InsertPoolAmountQuery(name, amount, height)); err != nil { return err } return nil @@ -144,7 +144,7 @@ func (p *PostgresContext) operationPoolOrAccAmount(name, amount string, op func(*big.Int, *big.Int) error, getAmount func(string, int64) (string, error), insert func(name, amount string, height int64) string) error { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } @@ -167,7 +167,7 @@ func (p *PostgresContext) operationPoolOrAccAmount(name, amount string, if err := op(originalAmountBig, amountBig); err != nil { return err } - if _, err = txn.Exec(ctx, insert(name, types.BigIntToString(originalAmountBig), height)); err != nil { + if _, err = tx.Exec(ctx, insert(name, types.BigIntToString(originalAmountBig), height)); err != nil { return err } return nil diff --git a/persistence/block.go b/persistence/block.go index 170f81984..aaee96364 100644 --- a/persistence/block.go +++ b/persistence/block.go @@ -3,30 +3,31 @@ package persistence import ( "encoding/binary" "encoding/hex" - "github.com/pokt-network/pocket/persistence/types" "log" + + "github.com/pokt-network/pocket/persistence/types" ) // OPTIMIZE(team): get from blockstore or keep in memory func (p PostgresContext) GetLatestBlockHeight() (latestHeight uint64, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return 0, err } - err = txn.QueryRow(ctx, types.GetLatestBlockHeightQuery()).Scan(&latestHeight) + err = tx.QueryRow(ctx, types.GetLatestBlockHeightQuery()).Scan(&latestHeight) return } // OPTIMIZE(team): get from blockstore or keep in cache/memory func (p PostgresContext) GetBlockHash(height int64) ([]byte, error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return nil, err } var hexHash string - err = txn.QueryRow(ctx, types.GetBlockHashQuery(height)).Scan(&hexHash) + err = tx.QueryRow(ctx, types.GetBlockHashQuery(height)).Scan(&hexHash) if err != nil { return nil, err } @@ -52,11 +53,11 @@ func (p PostgresContext) StoreBlock(blockProtoBytes []byte) error { // INVESTIGATE: Note that we are writing this directly to the blockStore. Depending on how // the use of the PostgresContext evolves, we may need to write this to `ContextStore` and copy // over to `BlockStore` when the block is committed. - return p.DB.Blockstore.Put(heightToBytes(p.Height), blockProtoBytes) + return p.blockstore.Put(heightToBytes(p.Height), blockProtoBytes) } func (p PostgresContext) InsertBlock(height uint64, hash string, proposerAddr []byte, quorumCert []byte) error { - ctx, tx, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } diff --git a/persistence/context.go b/persistence/context.go index fc2d7f892..fd44e3af1 100644 --- a/persistence/context.go +++ b/persistence/context.go @@ -12,7 +12,7 @@ func (p PostgresContext) NewSavePoint(bytes []byte) error { func (p PostgresContext) RollbackToSavePoint(bytes []byte) error { log.Println("TODO: RollbackToSavePoint not fully implemented") - return p.DB.Tx.Rollback(context.TODO()) + return p.GetTx().Rollback(context.TODO()) } func (p PostgresContext) AppHash() ([]byte, error) { @@ -28,10 +28,10 @@ func (p PostgresContext) Commit() error { log.Printf("About to commit context at height %d.\n", p.Height) ctx := context.TODO() - if err := p.DB.Tx.Commit(context.TODO()); err != nil { + if err := p.GetTx().Commit(context.TODO()); err != nil { return err } - if err := p.DB.conn.Close(ctx); err != nil { + if err := p.conn.Close(ctx); err != nil { log.Println("[TODO][ERROR] Implement connection pooling. Error when closing DB connecting...", err) } @@ -42,10 +42,10 @@ func (p PostgresContext) Release() error { log.Printf("About to release context at height %d.\n", p.Height) ctx := context.TODO() - if err := p.DB.Tx.Rollback(ctx); err != nil { + if err := p.GetTx().Rollback(ctx); err != nil { return err } - if err := p.DB.conn.Close(ctx); err != nil { + if err := p.conn.Close(ctx); err != nil { log.Println("[TODO][ERROR] Implement connection pooling. Error when closing DB connecting...", err) } return nil @@ -54,5 +54,5 @@ func (p PostgresContext) Release() error { func (p PostgresContext) Close() error { log.Printf("About to close context at height %d.\n", p.Height) - return p.DB.conn.Close(context.TODO()) + return p.conn.Close(context.TODO()) } diff --git a/persistence/db.go b/persistence/db.go index 519e17670..3a495596c 100644 --- a/persistence/db.go +++ b/persistence/db.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "github.com/pokt-network/pocket/persistence/types" "github.com/jackc/pgconn" @@ -34,28 +35,19 @@ var protocolActorSchemas = []types.ProtocolActorSchema{ var _ modules.PersistenceRWContext = &PostgresContext{} -// TODO(pocket/issues/149): Consolidate `PostgresContext and PostgresDB` into a single struct and -// avoid exposing it for testing purposes after the consolidation. A helper -// with default context values should be created. -// TODO: These are only externalized for testing purposes, so they should be made private and -// it is trivial to create a helper to initial a context with some values. type PostgresContext struct { - Height int64 - DB PostgresDB -} -type PostgresDB struct { + Height int64 // TODO(olshansky): `Height` is only externalized for testing purposes. Replace with helpers... conn *pgx.Conn - Tx pgx.Tx - Blockstore kvstore.KVStore + tx pgx.Tx + blockstore kvstore.KVStore } -func (pg *PostgresDB) GetCtxAndTxn() (context.Context, pgx.Tx, error) { - tx, err := pg.GetTxn() - return context.TODO(), tx, err +func (pg *PostgresContext) GetCtxAndTx() (context.Context, pgx.Tx, error) { + return context.TODO(), pg.GetTx(), nil } -func (pg *PostgresDB) GetTxn() (pgx.Tx, error) { - return pg.Tx, nil +func (pg *PostgresContext) GetTx() pgx.Tx { + return pg.tx } func (pg *PostgresContext) GetCtx() (context.Context, error) { @@ -171,7 +163,7 @@ func initializeBlockTables(ctx context.Context, db *pgx.Conn) error { // Exposed for testing purposes only func (p PostgresContext) DebugClearAll() error { - ctx, tx, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } diff --git a/persistence/genesis.go b/persistence/genesis.go index e7c6abcaa..1c7e82f14 100644 --- a/persistence/genesis.go +++ b/persistence/genesis.go @@ -158,11 +158,11 @@ func (m *PersistenceModule) populateGenesisState(state *types.PersistenceGenesis // can easily be refactored and condensed into a single function using a generic type or a common // interface. func (p PostgresContext) GetAllAccounts(height int64) (accs []modules.Account, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return nil, err } - rows, err := txn.Query(ctx, types.SelectAccounts(height, types.AccountTableName)) + rows, err := tx.Query(ctx, types.SelectAccounts(height, types.AccountTableName)) if err != nil { return nil, err } @@ -182,11 +182,11 @@ func (p PostgresContext) GetAllAccounts(height int64) (accs []modules.Account, e // CLEANUP: Consolidate with GetAllAccounts. func (p PostgresContext) GetAllPools(height int64) (accs []modules.Account, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return nil, err } - rows, err := txn.Query(ctx, types.SelectPools(height, types.PoolTableName)) + rows, err := tx.Query(ctx, types.SelectPools(height, types.PoolTableName)) if err != nil { return nil, err } @@ -201,11 +201,11 @@ func (p PostgresContext) GetAllPools(height int64) (accs []modules.Account, err } func (p PostgresContext) GetAllApps(height int64) (apps []modules.Actor, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return nil, err } - rows, err := txn.Query(ctx, types.ApplicationActor.GetAllQuery(height)) + rows, err := tx.Query(ctx, types.ApplicationActor.GetAllQuery(height)) if err != nil { return nil, err } @@ -220,7 +220,7 @@ func (p PostgresContext) GetAllApps(height int64) (apps []modules.Actor, err err } rows.Close() for _, actor := range actors { - actor, err = p.GetChainsForActor(ctx, txn, types.ApplicationActor, actor, height) + actor, err = p.GetChainsForActor(ctx, tx, types.ApplicationActor, actor, height) if err != nil { return } @@ -230,11 +230,11 @@ func (p PostgresContext) GetAllApps(height int64) (apps []modules.Actor, err err } func (p PostgresContext) GetAllValidators(height int64) (vals []modules.Actor, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return nil, err } - rows, err := txn.Query(ctx, types.ValidatorActor.GetAllQuery(height)) + rows, err := tx.Query(ctx, types.ValidatorActor.GetAllQuery(height)) if err != nil { return nil, err } @@ -249,7 +249,7 @@ func (p PostgresContext) GetAllValidators(height int64) (vals []modules.Actor, e } rows.Close() for _, actor := range actors { - actor, err = p.GetChainsForActor(ctx, txn, types.ApplicationActor, actor, height) + actor, err = p.GetChainsForActor(ctx, tx, types.ApplicationActor, actor, height) if err != nil { return } @@ -259,11 +259,11 @@ func (p PostgresContext) GetAllValidators(height int64) (vals []modules.Actor, e } func (p PostgresContext) GetAllServiceNodes(height int64) (sn []modules.Actor, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return nil, err } - rows, err := txn.Query(ctx, types.ServiceNodeActor.GetAllQuery(height)) + rows, err := tx.Query(ctx, types.ServiceNodeActor.GetAllQuery(height)) if err != nil { return nil, err } @@ -278,7 +278,7 @@ func (p PostgresContext) GetAllServiceNodes(height int64) (sn []modules.Actor, e } rows.Close() for _, actor := range actors { - actor, err = p.GetChainsForActor(ctx, txn, types.ServiceNodeActor, actor, height) + actor, err = p.GetChainsForActor(ctx, tx, types.ServiceNodeActor, actor, height) if err != nil { return } @@ -288,11 +288,11 @@ func (p PostgresContext) GetAllServiceNodes(height int64) (sn []modules.Actor, e } func (p PostgresContext) GetAllFishermen(height int64) (f []modules.Actor, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return nil, err } - rows, err := txn.Query(ctx, types.FishermanActor.GetAllQuery(height)) + rows, err := tx.Query(ctx, types.FishermanActor.GetAllQuery(height)) if err != nil { return nil, err } @@ -307,7 +307,7 @@ func (p PostgresContext) GetAllFishermen(height int64) (f []modules.Actor, err e } rows.Close() for _, actor := range actors { - actor, err = p.GetChainsForActor(ctx, txn, types.FishermanActor, actor, height) + actor, err = p.GetChainsForActor(ctx, tx, types.FishermanActor, actor, height) if err != nil { return } diff --git a/persistence/gov.go b/persistence/gov.go index ffca99aef..4908c095f 100644 --- a/persistence/gov.go +++ b/persistence/gov.go @@ -3,10 +3,11 @@ package persistence import ( "encoding/hex" "fmt" - "github.com/pokt-network/pocket/persistence/types" - "github.com/pokt-network/pocket/shared/modules" "log" "strconv" + + "github.com/pokt-network/pocket/persistence/types" + "github.com/pokt-network/pocket/shared/modules" ) // TODO (Team) BUG setting parameters twice on the same height causes issues. We need to move the schema away from 'end_height' and @@ -21,11 +22,11 @@ func (p PostgresContext) GetServiceNodesPerSessionAt(height int64) (int, error) } func (p PostgresContext) InitParams() error { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } - _, err = txn.Exec(ctx, types.InsertParams(types.DefaultParams(), p.Height)) + _, err = tx.Exec(ctx, types.InsertParams(types.DefaultParams(), p.Height)) return err } @@ -91,7 +92,7 @@ func (p PostgresContext) setParamOrFlag(name string, value any, enabled *bool) e // setParamOrFlag sets a param or a flag. // If `enabled` is nil, we are dealing with a param, otherwise it's a flag func setParamOrFlag[T types.SupportedParamTypes](p PostgresContext, paramName string, paramValue T, enabled *bool) error { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } @@ -103,20 +104,20 @@ func setParamOrFlag[T types.SupportedParamTypes](p PostgresContext, paramName st if enabled != nil { tableName = types.FlagsTableName } - if _, err = txn.Exec(ctx, types.InsertParamOrFlag(tableName, paramName, height, paramValue, enabled)); err != nil { + if _, err = tx.Exec(ctx, types.InsertParamOrFlag(tableName, paramName, height, paramValue, enabled)); err != nil { return err } return nil } func getParamOrFlag[T int | string | []byte](p PostgresContext, tableName, paramName string, height int64) (i T, enabled bool, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return i, enabled, err } var stringVal string - row := txn.QueryRow(ctx, types.GetParamOrFlagQuery(tableName, paramName, height)) + row := tx.QueryRow(ctx, types.GetParamOrFlagQuery(tableName, paramName, height)) if tableName == types.ParamsTableName { err = row.Scan(&stringVal) } else { diff --git a/persistence/kvstore/kvstore.go b/persistence/kvstore/kvstore.go index 1b5fe2495..24613afc9 100644 --- a/persistence/kvstore/kvstore.go +++ b/persistence/kvstore/kvstore.go @@ -44,22 +44,22 @@ func NewMemKVStore() KVStore { } func (store badgerKVStore) Put(key []byte, value []byte) error { - txn := store.db.NewTransaction(true) - defer txn.Discard() + tx := store.db.NewTransaction(true) + defer tx.Discard() - err := txn.Set(key, value) + err := tx.Set(key, value) if err != nil { return err } - return txn.Commit() + return tx.Commit() } func (store badgerKVStore) Get(key []byte) ([]byte, error) { - txn := store.db.NewTransaction(false) - defer txn.Discard() + tx := store.db.NewTransaction(false) + defer tx.Discard() - item, err := txn.Get(key) + item, err := tx.Get(key) if err != nil { return nil, err } @@ -69,7 +69,7 @@ func (store badgerKVStore) Get(key []byte) ([]byte, error) { return nil, err } - if err := txn.Commit(); err != nil { + if err := tx.Commit(); err != nil { return nil, err } diff --git a/persistence/module.go b/persistence/module.go index a7e434735..33bd827bd 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -145,7 +145,7 @@ func (m *PersistenceModule) GetBus() modules.Bus { } func (m *PersistenceModule) NewRWContext(height int64) (modules.PersistenceRWContext, error) { - if m.writeContext != nil && !m.writeContext.DB.conn.IsClosed() { + if m.writeContext != nil && !m.writeContext.conn.IsClosed() { return nil, fmt.Errorf("write context already exists") } conn, err := connectToDatabase(m.postgresURL, m.nodeSchema) @@ -162,12 +162,10 @@ func (m *PersistenceModule) NewRWContext(height int64) (modules.PersistenceRWCon } m.writeContext = &PostgresContext{ - Height: height, - DB: PostgresDB{ - conn: conn, - Tx: tx, - Blockstore: m.blockStore, - }, + Height: height, + conn: conn, + tx: tx, + blockstore: m.blockStore, } return *m.writeContext, nil @@ -189,18 +187,16 @@ func (m *PersistenceModule) NewReadContext(height int64) (modules.PersistenceRea } return PostgresContext{ - Height: height, - DB: PostgresDB{ - conn: conn, - Tx: tx, - Blockstore: m.blockStore, - }, + Height: height, + conn: conn, + tx: tx, + blockstore: m.blockStore, }, nil } func (m *PersistenceModule) ResetContext() error { if m.writeContext != nil { - if !m.writeContext.DB.Tx.Conn().IsClosed() { + if !m.writeContext.GetTx().Conn().IsClosed() { if err := m.writeContext.Release(); err != nil { log.Println("[TODO][ERROR] Error releasing write context...", err) } diff --git a/persistence/shared_sql.go b/persistence/shared_sql.go index 21c35ce2a..9f2c19a00 100644 --- a/persistence/shared_sql.go +++ b/persistence/shared_sql.go @@ -32,12 +32,12 @@ func UnstakingHeightToStatus(unstakingHeight int64) int32 { } func (p *PostgresContext) GetExists(actorSchema types.ProtocolActorSchema, address []byte, height int64) (exists bool, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return } - if err = txn.QueryRow(ctx, actorSchema.GetExistsQuery(hex.EncodeToString(address), height)).Scan(&exists); err != nil { + if err = tx.QueryRow(ctx, actorSchema.GetExistsQuery(hex.EncodeToString(address), height)).Scan(&exists); err != nil { return } @@ -45,16 +45,16 @@ func (p *PostgresContext) GetExists(actorSchema types.ProtocolActorSchema, addre } func (p *PostgresContext) GetActor(actorSchema types.ProtocolActorSchema, address []byte, height int64) (actor types.BaseActor, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return } - actor, height, err = p.GetActorFromRow(txn.QueryRow(ctx, actorSchema.GetQuery(hex.EncodeToString(address), height))) + actor, height, err = p.GetActorFromRow(tx.QueryRow(ctx, actorSchema.GetQuery(hex.EncodeToString(address), height))) if err != nil { return } - return p.GetChainsForActor(ctx, txn, actorSchema, actor, height) + return p.GetChainsForActor(ctx, tx, actorSchema, actor, height) } func (p *PostgresContext) GetActorFromRow(row pgx.Row) (actor types.BaseActor, height int64, err error) { @@ -67,14 +67,14 @@ func (p *PostgresContext) GetActorFromRow(row pgx.Row) (actor types.BaseActor, h func (p *PostgresContext) GetChainsForActor( ctx context.Context, - txn pgx.Tx, + tx pgx.Tx, actorSchema types.ProtocolActorSchema, actor types.BaseActor, height int64) (a types.BaseActor, err error) { if actorSchema.GetChainsTableName() == "" { return actor, nil } - rows, err := txn.Query(ctx, actorSchema.GetChainsQuery(actor.Address, height)) + rows, err := tx.Query(ctx, actorSchema.GetChainsQuery(actor.Address, height)) if err != nil { return actor, err } @@ -97,7 +97,7 @@ func (p *PostgresContext) GetChainsForActor( } func (p *PostgresContext) InsertActor(actorSchema types.ProtocolActorSchema, actor types.BaseActor) error { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } @@ -107,7 +107,7 @@ func (p *PostgresContext) InsertActor(actorSchema types.ProtocolActorSchema, act return err } - _, err = txn.Exec(ctx, actorSchema.InsertQuery( + _, err = tx.Exec(ctx, actorSchema.InsertQuery( actor.Address, actor.PublicKey, actor.StakedTokens, actor.ActorSpecificParam, actor.OutputAddress, actor.PausedHeight, actor.UnstakingHeight, actor.Chains, height)) @@ -115,7 +115,7 @@ func (p *PostgresContext) InsertActor(actorSchema types.ProtocolActorSchema, act } func (p *PostgresContext) UpdateActor(actorSchema types.ProtocolActorSchema, actor types.BaseActor) error { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } @@ -125,16 +125,16 @@ func (p *PostgresContext) UpdateActor(actorSchema types.ProtocolActorSchema, act return err } - if _, err = txn.Exec(ctx, actorSchema.UpdateQuery(actor.Address, actor.StakedTokens, actor.ActorSpecificParam, height)); err != nil { + if _, err = tx.Exec(ctx, actorSchema.UpdateQuery(actor.Address, actor.StakedTokens, actor.ActorSpecificParam, height)); err != nil { return err } chainsTableName := actorSchema.GetChainsTableName() if chainsTableName != "" && actor.Chains != nil { - if _, err = txn.Exec(ctx, types.NullifyChains(actor.Address, height, chainsTableName)); err != nil { + if _, err = tx.Exec(ctx, types.NullifyChains(actor.Address, height, chainsTableName)); err != nil { return err } - if _, err = txn.Exec(ctx, actorSchema.UpdateChainsQuery(actor.Address, actor.Chains, height)); err != nil { + if _, err = tx.Exec(ctx, actorSchema.UpdateChainsQuery(actor.Address, actor.Chains, height)); err != nil { return err } } @@ -143,12 +143,12 @@ func (p *PostgresContext) UpdateActor(actorSchema types.ProtocolActorSchema, act } func (p *PostgresContext) GetActorsReadyToUnstake(actorSchema types.ProtocolActorSchema, height int64) (actors []modules.IUnstakingActor, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return nil, err } - rows, err := txn.Query(ctx, actorSchema.GetReadyToUnstakeQuery(height)) + rows, err := tx.Query(ctx, actorSchema.GetReadyToUnstakeQuery(height)) if err != nil { return nil, err } @@ -170,12 +170,12 @@ func (p *PostgresContext) GetActorsReadyToUnstake(actorSchema types.ProtocolActo func (p *PostgresContext) GetActorStatus(actorSchema types.ProtocolActorSchema, address []byte, height int64) (int, error) { var unstakingHeight int64 - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return UndefinedStakingStatus, err } - if err := txn.QueryRow(ctx, actorSchema.GetUnstakingHeightQuery(hex.EncodeToString(address), height)).Scan(&unstakingHeight); err != nil { + if err := tx.QueryRow(ctx, actorSchema.GetUnstakingHeightQuery(hex.EncodeToString(address), height)).Scan(&unstakingHeight); err != nil { return UndefinedStakingStatus, err } @@ -190,7 +190,7 @@ func (p *PostgresContext) GetActorStatus(actorSchema types.ProtocolActorSchema, } func (p *PostgresContext) SetActorUnstakingHeightAndStatus(actorSchema types.ProtocolActorSchema, address []byte, unstakingHeight int64) error { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } @@ -200,17 +200,17 @@ func (p *PostgresContext) SetActorUnstakingHeightAndStatus(actorSchema types.Pro return err } - _, err = txn.Exec(ctx, actorSchema.UpdateUnstakingHeightQuery(hex.EncodeToString(address), unstakingHeight, height)) + _, err = tx.Exec(ctx, actorSchema.UpdateUnstakingHeightQuery(hex.EncodeToString(address), unstakingHeight, height)) return err } func (p *PostgresContext) GetActorPauseHeightIfExists(actorSchema types.ProtocolActorSchema, address []byte, height int64) (pausedHeight int64, err error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return types.DefaultBigInt, err } - if err := txn.QueryRow(ctx, actorSchema.GetPausedHeightQuery(hex.EncodeToString(address), height)).Scan(&pausedHeight); err != nil { + if err := tx.QueryRow(ctx, actorSchema.GetPausedHeightQuery(hex.EncodeToString(address), height)).Scan(&pausedHeight); err != nil { return types.DefaultBigInt, err } @@ -218,7 +218,7 @@ func (p *PostgresContext) GetActorPauseHeightIfExists(actorSchema types.Protocol } func (p PostgresContext) SetActorStatusAndUnstakingHeightIfPausedBefore(actorSchema types.ProtocolActorSchema, pausedBeforeHeight, unstakingHeight int64) error { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } @@ -228,12 +228,12 @@ func (p PostgresContext) SetActorStatusAndUnstakingHeightIfPausedBefore(actorSch return err } - _, err = txn.Exec(ctx, actorSchema.UpdateUnstakedHeightIfPausedBeforeQuery(pausedBeforeHeight, unstakingHeight, currentHeight)) + _, err = tx.Exec(ctx, actorSchema.UpdateUnstakedHeightIfPausedBeforeQuery(pausedBeforeHeight, unstakingHeight, currentHeight)) return err } func (p PostgresContext) SetActorPauseHeight(actorSchema types.ProtocolActorSchema, address []byte, pauseHeight int64) error { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } @@ -243,12 +243,12 @@ func (p PostgresContext) SetActorPauseHeight(actorSchema types.ProtocolActorSche return err } - _, err = txn.Exec(ctx, actorSchema.UpdatePausedHeightQuery(hex.EncodeToString(address), pauseHeight, currentHeight)) + _, err = tx.Exec(ctx, actorSchema.UpdatePausedHeightQuery(hex.EncodeToString(address), pauseHeight, currentHeight)) return err } func (p PostgresContext) SetActorStakeAmount(actorSchema types.ProtocolActorSchema, address []byte, stakeAmount string) error { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return err } @@ -257,18 +257,18 @@ func (p PostgresContext) SetActorStakeAmount(actorSchema types.ProtocolActorSche if err != nil { return err } - _, err = txn.Exec(ctx, actorSchema.SetStakeAmountQuery(hex.EncodeToString(address), stakeAmount, currentHeight)) + _, err = tx.Exec(ctx, actorSchema.SetStakeAmountQuery(hex.EncodeToString(address), stakeAmount, currentHeight)) return err } func (p PostgresContext) GetActorOutputAddress(actorSchema types.ProtocolActorSchema, operatorAddr []byte, height int64) ([]byte, error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return nil, err } var outputAddr string - if err := txn.QueryRow(ctx, actorSchema.GetOutputAddressQuery(hex.EncodeToString(operatorAddr), height)).Scan(&outputAddr); err != nil { + if err := tx.QueryRow(ctx, actorSchema.GetOutputAddressQuery(hex.EncodeToString(operatorAddr), height)).Scan(&outputAddr); err != nil { return nil, err } @@ -276,13 +276,13 @@ func (p PostgresContext) GetActorOutputAddress(actorSchema types.ProtocolActorSc } func (p PostgresContext) GetActorStakeAmount(actorSchema types.ProtocolActorSchema, address []byte, height int64) (string, error) { - ctx, txn, err := p.DB.GetCtxAndTxn() + ctx, tx, err := p.GetCtxAndTx() if err != nil { return "", err } var stakeAmount string - if err := txn.QueryRow(ctx, actorSchema.GetStakeAmountQuery(hex.EncodeToString(address), height)).Scan(&stakeAmount); err != nil { + if err := tx.QueryRow(ctx, actorSchema.GetStakeAmountQuery(hex.EncodeToString(address), height)).Scan(&stakeAmount); err != nil { return "", err } return stakeAmount, nil diff --git a/persistence/test/setup_test.go b/persistence/test/setup_test.go index e3162a4d9..459472cf6 100644 --- a/persistence/test/setup_test.go +++ b/persistence/test/setup_test.go @@ -65,32 +65,35 @@ func NewTestPostgresContext(t *testing.T, height int64) *persistence.PostgresCon ctx, err := testPersistenceMod.NewRWContext(height) require.NoError(t, err) - db := &persistence.PostgresContext{ - Height: height, - DB: ctx.(persistence.PostgresContext).DB, - } + db, ok := ctx.(persistence.PostgresContext) + require.True(t, ok) t.Cleanup(func() { require.NoError(t, db.Release()) require.NoError(t, testPersistenceMod.ResetContext()) }) - return db + return &db } -// REFACTOR: Can we leverage using `NewTestPostgresContext`here by creating a common interface? func NewFuzzTestPostgresContext(f *testing.F, height int64) *persistence.PostgresContext { ctx, err := testPersistenceMod.NewRWContext(height) if err != nil { - log.Fatalf("Error creating new context: %s", err) + log.Fatalf("Error creating new context: %v\n", err) } - db := persistence.PostgresContext{ - Height: height, - DB: ctx.(persistence.PostgresContext).DB, + + db, ok := ctx.(persistence.PostgresContext) + if !ok { + log.Fatalf("Error casting RW context to Postgres context") } + f.Cleanup(func() { - db.Release() - testPersistenceMod.ResetContext() + if err := db.Release(); err != nil { + f.FailNow() + } + if err := testPersistenceMod.ResetContext(); err != nil { + f.FailNow() + } }) return &db @@ -204,9 +207,10 @@ func fuzzSingleProtocolActor( require.NoError(t, err) require.ElementsMatch(t, newActor.Chains, newChains, "staked chains not updated") - // TODO(andrew): Use `require.Contains` instead + require.NotContains(t, newActor.StakedTokens, "invalid") + // TODO(andrew): Use `require.Contains` instead. E.g. require.NotContains(t, newActor.StakedTokens, "invalid") if strings.Contains(newActor.StakedTokens, "invalid") { - fmt.Println("") + log.Println("") } require.Equal(t, newActor.StakedTokens, newStakedTokens, "staked tokens not updated") require.Equal(t, newActor.ActorSpecificParam, newActorSpecificParam, "actor specific param not updated") diff --git a/utility/gov.go b/utility/gov.go index d062ef643..fa9c16641 100644 --- a/utility/gov.go +++ b/utility/gov.go @@ -1,11 +1,11 @@ package utility import ( - "fmt" - "github.com/pokt-network/pocket/shared/modules" "log" "math/big" + "github.com/pokt-network/pocket/shared/modules" + typesUtil "github.com/pokt-network/pocket/utility/types" "google.golang.org/protobuf/types/known/wrapperspb" ) @@ -565,7 +565,7 @@ func (u *UtilityContext) getBigIntParam(paramName string) (*big.Int, typesUtil.E } value, err := store.GetStringParam(paramName, height) if err != nil { - fmt.Printf("err: %v\n", err) + log.Printf("err: %v\n", err) return nil, typesUtil.ErrGetParam(paramName, err) } return typesUtil.StringToBigInt(value)