From 1762efb756ab205687d71be4ce5b43113f932b07 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:11:46 +0000 Subject: [PATCH 01/59] chore(utility): normalize imports --- utility/account.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/utility/account.go b/utility/account.go index 19988904f..2595d2511 100644 --- a/utility/account.go +++ b/utility/account.go @@ -10,7 +10,6 @@ import ( "github.com/pokt-network/pocket/shared/utils" "github.com/pokt-network/pocket/utility/types" - typesUtil "github.com/pokt-network/pocket/utility/types" ) // Accounts specific functionality @@ -18,11 +17,11 @@ import ( func (u *utilityContext) getAccountAmount(address []byte) (*big.Int, types.Error) { amountStr, err := u.store.GetAccountAmount(address, u.height) if err != nil { - return nil, typesUtil.ErrGetAccountAmount(err) + return nil, types.ErrGetAccountAmount(err) } amount, err := utils.StringToBigInt(amountStr) if err != nil { - return nil, typesUtil.ErrStringToBigInt(err) + return nil, types.ErrStringToBigInt(err) } return amount, nil } From 305e96a01faec0638a88088bc7c3e9ef1728fa70 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:12:18 +0000 Subject: [PATCH 02/59] chore(utility): setBus (from embedded base_module) --- utility/context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utility/context.go b/utility/context.go index 11df37706..94857fd2b 100644 --- a/utility/context.go +++ b/utility/context.go @@ -43,7 +43,7 @@ func (u *utilityModule) NewContext(height int64) (modules.UtilityContext, error) savePointsList: make([][]byte, 0), savePointsSet: make(map[string]struct{}), } - ctx.IntegratableModule.SetBus(u.GetBus()) + ctx.SetBus(u.GetBus()) return ctx, nil } From cc9f7db96b4fbc30fb9bf7756947247ce4cdaab4 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:15:31 +0000 Subject: [PATCH 03/59] refactor(shared): utility module interface changes --- shared/modules/utility_module.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/shared/modules/utility_module.go b/shared/modules/utility_module.go index b3843c2a8..177da70d0 100644 --- a/shared/modules/utility_module.go +++ b/shared/modules/utility_module.go @@ -17,8 +17,12 @@ type UtilityModule interface { Module // NewContext creates a `utilityContext` with an underlying read-write `persistenceContext` (only 1 of which can exist at a time) + // TODO - @deblasis - deprecate this NewContext(height int64) (UtilityContext, error) + // NewUnitOfWork creates a `utilityUnitOfWork` used to allow atomicity and commit/rollback functionality (https://martinfowler.com/eaaCatalog/unitOfWork.html) + NewUnitOfWork(height int64) (UtilityUnitOfWork, error) + // HandleTransaction does basic `Transaction` validation & adds it to the utility's module mempool if valid HandleTransaction(tx []byte) error From c859dbb3e883df8e0610493eab98a909327e2173 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:15:54 +0000 Subject: [PATCH 04/59] chore(shared): utilitycontext marked for deprecation --- shared/modules/utility_module.go | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/modules/utility_module.go b/shared/modules/utility_module.go index 177da70d0..7e2256135 100644 --- a/shared/modules/utility_module.go +++ b/shared/modules/utility_module.go @@ -39,6 +39,7 @@ type UtilityModule interface { // `GetProposalBlock` and `ApplyProposalBlock` functions // The context within which the node can operate with the utility layer. +// TODO: @deblasis - deprecate this type UtilityContext interface { // SetProposalBlock updates the utility context with the proposed state transition. // It does not apply, validate or commit the changes. From 771b95eb1f4fa398bd64feb4646f0009f004246a Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:17:13 +0000 Subject: [PATCH 05/59] refactor(shared): utility unitofwork, catering for #508 as well --- shared/modules/utility_module.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/shared/modules/utility_module.go b/shared/modules/utility_module.go index 7e2256135..c128f6a7f 100644 --- a/shared/modules/utility_module.go +++ b/shared/modules/utility_module.go @@ -71,3 +71,26 @@ type UnstakingActor interface { GetAddress() []byte GetOutputAddress() []byte } + +type UtilityUnitOfWork interface { + IntegratableModule + + // SetProposalBlock updates the utility context with the proposed state transition. + // It does not apply, validate or commit the changes. + // For example, it can be use during state sync to set a proposed state transition before validation. + // TODO: Investigate a way to potentially simplify the interface by removing this function. + // TODO: @deblasis: there's still some mix and match between blockHash and stateHash + SetProposalBlock(blockHash string, proposerAddr []byte, txs [][]byte) error + + // ApplyBlock applies the context's in-memory proposed state (i.e. the txs in this context). + // Only intended to be used by the block verifiers (i.e. replicas). + // NOTE: this is called by the replica OR by the leader when `prepareQc` is not `nil` + ApplyBlock(beforeApplyBlock, afterApplyBlock func(UtilityUnitOfWork) error) (stateHash string, txs [][]byte, err error) + + // Release releases this utility context and any underlying contexts it references + Release() error + + // Commit commits this utility context along with any underlying contexts (e.g. persistenceContext) it references + Commit(quorumCert []byte) error +} + From a1d7aaba30137aa264e6bebb4019e3ac2533a39b Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:17:59 +0000 Subject: [PATCH 06/59] feat(shared): utility unitofwork for leader and replica for #508 --- shared/modules/utility_module.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/shared/modules/utility_module.go b/shared/modules/utility_module.go index c128f6a7f..56d45dca8 100644 --- a/shared/modules/utility_module.go +++ b/shared/modules/utility_module.go @@ -94,3 +94,15 @@ type UtilityUnitOfWork interface { Commit(quorumCert []byte) error } +type LeaderUtilityUnitOfWork interface { + UtilityUnitOfWork + + // CreateAndApplyProposalBlock reaps the mempool for txs to be proposed in a new block, and + // applies them to this context after validation. + // TODO: @deblasis: new signature? + CreateProposalBlock(proposer []byte, maxTxBytes int, beforeApplyBlock, afterApplyBlock func(UtilityUnitOfWork) error) (stateHash string, txs [][]byte, err error) +} + +type ReplicaUtilityUnitOfWork interface { + UtilityUnitOfWork +} From 06a5694451b40a8e50c9c4ad08199518b3495569 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:21:49 +0000 Subject: [PATCH 07/59] refactor(shared): mv ReleaseUtilityContext->ReleaseUtilityUnitOfWork --- shared/modules/consensus_module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/modules/consensus_module.go b/shared/modules/consensus_module.go index deff25dba..4d5dd81d4 100644 --- a/shared/modules/consensus_module.go +++ b/shared/modules/consensus_module.go @@ -47,7 +47,7 @@ type ConsensusPacemaker interface { ResetRound() ResetForNewHeight() ClearLeaderMessagesPool() - ReleaseUtilityContext() error + ReleaseUtilityUnitOfWork() error // Setters SetHeight(uint64) From f93827491115aca1809c665ac06f828e0c6c0205 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:22:08 +0000 Subject: [PATCH 08/59] chore(shared): TODO in consensus (ReleaseUtilityUnitOfWork) --- shared/modules/consensus_module.go | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/modules/consensus_module.go b/shared/modules/consensus_module.go index 4d5dd81d4..9cb97c87f 100644 --- a/shared/modules/consensus_module.go +++ b/shared/modules/consensus_module.go @@ -47,6 +47,7 @@ type ConsensusPacemaker interface { ResetRound() ResetForNewHeight() ClearLeaderMessagesPool() + // TODO: @deblasis - remove this and implement an event based approach ReleaseUtilityUnitOfWork() error // Setters From 2b2469b89accdb02aa66e39437410ea2da73da59 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:22:37 +0000 Subject: [PATCH 09/59] refactor(shared): refactored ConsensusDebugModule for uow --- shared/modules/consensus_module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/modules/consensus_module.go b/shared/modules/consensus_module.go index 9cb97c87f..afe99ebca 100644 --- a/shared/modules/consensus_module.go +++ b/shared/modules/consensus_module.go @@ -85,5 +85,5 @@ type ConsensusDebugModule interface { // REFACTOR: This should accept typesCons.HotstuffStep. SetStep(uint8) SetBlock(*types.Block) - SetUtilityContext(UtilityContext) + SetUtilityUnitOfWork(UtilityUnitOfWork) } From 324ae5e24a0e2372e1fc8d20b4a7d1b6a72d7653 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:28:25 +0000 Subject: [PATCH 10/59] feat(utility): base utilityUnitOfWork --- utility/unit_of_work/base.go | 77 ++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 utility/unit_of_work/base.go diff --git a/utility/unit_of_work/base.go b/utility/unit_of_work/base.go new file mode 100644 index 000000000..f73158255 --- /dev/null +++ b/utility/unit_of_work/base.go @@ -0,0 +1,77 @@ +package unit_of_work + +import ( + "github.com/pokt-network/pocket/shared/modules" + "github.com/pokt-network/pocket/shared/modules/base_modules" +) + +const ( + leaderUtilityUOWModuleName = "leader_utility_unit_of_work" + replicaUtilityUOWModuleName = "replica_utility_unit_of_work" +) + +var _ modules.UtilityUnitOfWork = &baseUtilityUnitOfWork{} + +type baseUtilityUnitOfWork struct { + base_modules.IntegratableModule + + logger *modules.Logger + + persistenceReadContext modules.PersistenceReadContext + persistenceRWContext modules.PersistenceRWContext + + // TECHDEBT: Consolidate all these types with the shared Protobuf struct and create a `proposalBlock` + proposalStateHash string + proposalProposerAddr []byte + proposalBlockTxs [][]byte +} + +func (uow *baseUtilityUnitOfWork) SetProposalBlock(blockHash string, proposerAddr []byte, txs [][]byte) error { + uow.proposalStateHash = blockHash + uow.proposalProposerAddr = proposerAddr + uow.proposalBlockTxs = txs + return nil +} + +func (uow *baseUtilityUnitOfWork) ApplyBlock(beforeApplyBlock, afterApplyBlock func(modules.UtilityUnitOfWork) error) (stateHash string, txs [][]byte, err error) { + if beforeApplyBlock != nil { + uow.logger.Debug().Msg("running beforeApplyBlock...") + if err := beforeApplyBlock(uow); err != nil { + return "", nil, err + } + } + + if afterApplyBlock != nil { + uow.logger.Debug().Msg("running afterApplyBlock...") + if err := afterApplyBlock(uow); err != nil { + return "", nil, err + } + } + + panic("unimplemented") +} + +func (uow *baseUtilityUnitOfWork) Commit(quorumCert []byte) error { + // TODO: @deblasis - change tracking here + + uow.logger.Debug().Msg("committing the rwPersistenceContext...") + if err := uow.persistenceRWContext.Commit(uow.proposalProposerAddr, quorumCert); err != nil { + return err + } + return uow.Release() +} + +func (uow *baseUtilityUnitOfWork) Release() error { + // TODO: @deblasis - change tracking reset here + + if uow.persistenceRWContext == nil { + return nil + } + if err := uow.persistenceRWContext.Release(); err != nil { + return err + } + if err := uow.persistenceReadContext.Close(); err != nil { + return err + } + return nil +} From 69a8fe1ff457740e7e8e1ef77f738ac95b104359 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:32:32 +0000 Subject: [PATCH 11/59] feat(utility): leader utilityUnitOfWork --- utility/unit_of_work/leader.go | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 utility/unit_of_work/leader.go diff --git a/utility/unit_of_work/leader.go b/utility/unit_of_work/leader.go new file mode 100644 index 000000000..23e79581f --- /dev/null +++ b/utility/unit_of_work/leader.go @@ -0,0 +1,42 @@ +package unit_of_work + +import ( + "github.com/pokt-network/pocket/logger" + "github.com/pokt-network/pocket/shared/modules" +) + +var ( + _ modules.UtilityUnitOfWork = &leaderUtilityUnitOfWork{} + _ modules.LeaderUtilityUnitOfWork = &leaderUtilityUnitOfWork{} +) + +type leaderUtilityUnitOfWork struct { + baseUtilityUnitOfWork +} + +func NewForLeader(height int64, readContext modules.PersistenceReadContext, rwPersistenceContext modules.PersistenceRWContext) *leaderUtilityUnitOfWork { + return &leaderUtilityUnitOfWork{ + baseUtilityUnitOfWork: baseUtilityUnitOfWork{ + persistenceReadContext: readContext, + persistenceRWContext: rwPersistenceContext, + logger: logger.Global.CreateLoggerForModule(leaderUtilityUOWModuleName), + }, + } +} + +func (uow *leaderUtilityUnitOfWork) CreateProposalBlock(proposer []byte, maxTxBytes int, beforeApplyBlock, afterApplyBlock func(modules.UtilityUnitOfWork) error) (stateHash string, txs [][]byte, err error) { + if beforeApplyBlock != nil { + uow.logger.Debug().Msg("running beforeApplyBlock...") + if err := beforeApplyBlock(uow); err != nil { + return "", nil, err + } + } + + if afterApplyBlock != nil { + uow.logger.Debug().Msg("running afterApplyBlock...") + if err := afterApplyBlock(uow); err != nil { + return "", nil, err + } + } + panic("unimplemented") +} From c151bbfe3c8866f53fb7760d044cc901388d1f54 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:33:03 +0000 Subject: [PATCH 12/59] feat(utility): replicaUtilityUnitOfWork --- utility/unit_of_work/replica.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 utility/unit_of_work/replica.go diff --git a/utility/unit_of_work/replica.go b/utility/unit_of_work/replica.go new file mode 100644 index 000000000..d9e44f2bb --- /dev/null +++ b/utility/unit_of_work/replica.go @@ -0,0 +1,25 @@ +package unit_of_work + +import ( + "github.com/pokt-network/pocket/logger" + "github.com/pokt-network/pocket/shared/modules" +) + +var ( + _ modules.UtilityUnitOfWork = &replicaUtilityUnitOfWork{} + _ modules.ReplicaUtilityUnitOfWork = &replicaUtilityUnitOfWork{} +) + +type replicaUtilityUnitOfWork struct { + baseUtilityUnitOfWork +} + +func NewForReplica(height int64, readContext modules.PersistenceReadContext, rwPersistenceContext modules.PersistenceRWContext) *replicaUtilityUnitOfWork { + return &replicaUtilityUnitOfWork{ + baseUtilityUnitOfWork: baseUtilityUnitOfWork{ + persistenceReadContext: readContext, + persistenceRWContext: rwPersistenceContext, + logger: logger.Global.CreateLoggerForModule(replicaUtilityUOWModuleName), + }, + } +} From d28d2e78a315662153d647d9d88474642551202a Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:33:57 +0000 Subject: [PATCH 13/59] feat(utility): unitOfWork constructor --- utility/uow.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 utility/uow.go diff --git a/utility/uow.go b/utility/uow.go new file mode 100644 index 000000000..ad15e6253 --- /dev/null +++ b/utility/uow.go @@ -0,0 +1,27 @@ +package utility + +import ( + "github.com/pokt-network/pocket/shared/modules" + "github.com/pokt-network/pocket/utility/unit_of_work" +) + +func (u *utilityModule) NewUnitOfWork(height int64) (modules.UtilityUnitOfWork, error) { + readContext, err := u.GetBus().GetPersistenceModule().NewReadContext(height) + if err != nil { + return nil, err + } + rwContext, err := u.GetBus().GetPersistenceModule().NewRWContext(height) + if err != nil { + return nil, err + } + + var utilityUow modules.UtilityUnitOfWork + if u.GetBus().GetConsensusModule().IsLeader() { + utilityUow = unit_of_work.NewForLeader(height, readContext, rwContext) + } else { + utilityUow = unit_of_work.NewForReplica(height, readContext, rwContext) + } + + utilityUow.SetBus(u.GetBus()) + return utilityUow, nil +} From a8d7792dbaef753e52dc9a5225b2cc56bf3ab919 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:44:29 +0000 Subject: [PATCH 14/59] refactor(consensus): utilityUnitOfWork --- consensus/debugging.go | 4 ++-- consensus/hotstuff_replica.go | 10 +++++----- consensus/module.go | 4 ++-- consensus/pacemaker_consensus.go | 12 ++++++------ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/consensus/debugging.go b/consensus/debugging.go index 3fb26af85..4f91bf9e2 100644 --- a/consensus/debugging.go +++ b/consensus/debugging.go @@ -32,8 +32,8 @@ func (m *consensusModule) SetBlock(block *coreTypes.Block) { m.block = block } -func (m *consensusModule) SetUtilityContext(utilityContext modules.UtilityContext) { - m.utilityContext = utilityContext +func (m *consensusModule) SetUtilityUnitOfWork(utilityUnitOfWork modules.UtilityUnitOfWork) { + m.utilityUnitOfWork = utilityUnitOfWork } func (m *consensusModule) HandleDebugMessage(debugMessage *messaging.DebugMessage) error { diff --git a/consensus/hotstuff_replica.go b/consensus/hotstuff_replica.go index 904342d49..cb7202f08 100644 --- a/consensus/hotstuff_replica.go +++ b/consensus/hotstuff_replica.go @@ -33,9 +33,9 @@ func (handler *HotstuffReplicaMessageHandler) HandleNewRoundMessage(m *consensus return } - // Clear the previous utility context, if it exists, and create a new one - if err := m.refreshUtilityContext(); err != nil { - m.logger.Error().Err(err).Msg("Could not refresh utility context") + // Clear the previous utility unitOfWork, if it exists, and create a new one + if err := m.refreshUtilityUnitOfWork(); err != nil { + m.logger.Error().Err(err).Msg("Could not refresh utility unitOfWork") return } @@ -230,12 +230,12 @@ func (m *consensusModule) validateProposal(msg *typesCons.HotstuffMessage) error func (m *consensusModule) applyBlock(block *coreTypes.Block) error { blockHeader := block.BlockHeader // Set the proposal block in the persistence context - if err := m.utilityContext.SetProposalBlock(blockHeader.StateHash, blockHeader.ProposerAddress, block.Transactions); err != nil { + if err := m.utilityUnitOfWork.SetProposalBlock(blockHeader.StateHash, blockHeader.ProposerAddress, block.Transactions); err != nil { return err } // Apply all the transactions in the block and get the stateHash - stateHash, err := m.utilityContext.ApplyBlock() + stateHash, _, err := m.utilityUnitOfWork.ApplyBlock(nil, nil) if err != nil { return err } diff --git a/consensus/module.go b/consensus/module.go index 644398d0d..6e9b5bbbd 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -66,7 +66,7 @@ type consensusModule struct { // managed or changed. Also consider exposing a function that exposes the context // to streamline how its accessed in the module (see the ticket). - utilityContext modules.UtilityContext + utilityUnitOfWork modules.UtilityUnitOfWork paceMaker pacemaker.Pacemaker leaderElectionMod leader_election.LeaderElectionModule @@ -132,7 +132,7 @@ func (*consensusModule) Create(bus modules.Bus, options ...modules.ModuleOption) leaderId: nil, - utilityContext: nil, + utilityUnitOfWork: nil, logPrefix: DefaultLogPrefix, diff --git a/consensus/pacemaker_consensus.go b/consensus/pacemaker_consensus.go index c9d4212b2..75ff9aca0 100644 --- a/consensus/pacemaker_consensus.go +++ b/consensus/pacemaker_consensus.go @@ -24,14 +24,14 @@ func (m *consensusModule) ResetForNewHeight() { m.lockedQC = nil } -// This function releases consensus module's utility context, called by pacemaker module -func (m *consensusModule) ReleaseUtilityContext() error { - if m.utilityContext != nil { - if err := m.utilityContext.Release(); err != nil { - log.Println("[WARN] Failed to release utility context: ", err) +// This function releases consensus module's utility unitOfWork, called by pacemaker module +func (m *consensusModule) ReleaseUtilityUnitOfWork() error { + if m.utilityUnitOfWork != nil { + if err := m.utilityUnitOfWork.Release(); err != nil { + log.Println("[WARN] Failed to release utility unitOfWork: ", err) return err } - m.utilityContext = nil + m.utilityUnitOfWork = nil } return nil From 2b93b37be2722f5ec0d35407203f90af64f91efe Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:44:47 +0000 Subject: [PATCH 15/59] refactor(consensus): utilityUnitOfWork in pacemaker --- consensus/pacemaker/module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/pacemaker/module.go b/consensus/pacemaker/module.go index 45e696f8c..a5f42e3aa 100644 --- a/consensus/pacemaker/module.go +++ b/consensus/pacemaker/module.go @@ -243,7 +243,7 @@ func (m *pacemaker) startNextView(qc *typesCons.QuorumCertificate, forceNextView consensusMod := m.GetBus().GetConsensusModule() consensusMod.SetStep(uint8(newRound)) consensusMod.ResetRound() - if err := consensusMod.ReleaseUtilityContext(); err != nil { + if err := consensusMod.ReleaseUtilityUnitOfWork(); err != nil { log.Println("[WARN] NewHeight: Failed to release utility context: ", err) return } From 54ca863e097374170a55eddf842d3eac692271e2 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:45:01 +0000 Subject: [PATCH 16/59] chore(consensus): TODO in tests --- consensus/e2e_tests/pacemaker_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/consensus/e2e_tests/pacemaker_test.go b/consensus/e2e_tests/pacemaker_test.go index 85449447a..1a0a70570 100644 --- a/consensus/e2e_tests/pacemaker_test.go +++ b/consensus/e2e_tests/pacemaker_test.go @@ -182,6 +182,7 @@ func TestPacemakerCatchupSameStepDifferentRounds(t *testing.T) { consensusModImpl.MethodByName("SetHeight").Call([]reflect.Value{reflect.ValueOf(testHeight)}) consensusModImpl.MethodByName("SetStep").Call([]reflect.Value{reflect.ValueOf(testStep)}) + // TODO: @deblasis - refactor using uow // utilityContext is only set on new rounds, which is skipped in this test utilityContext, err := pocketNode.GetBus().GetUtilityModule().NewContext(int64(testHeight)) require.NoError(t, err) From 0d6de1bb734cb1a8bf50f2e08610f0f3073b1858 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:45:52 +0000 Subject: [PATCH 17/59] refactor(consensus): refactored block application to use uow --- consensus/block.go | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/consensus/block.go b/consensus/block.go index ec7c37228..7b35f68e3 100644 --- a/consensus/block.go +++ b/consensus/block.go @@ -10,7 +10,7 @@ import ( func (m *consensusModule) commitBlock(block *coreTypes.Block) error { // Commit the context - if err := m.utilityContext.Commit(block.BlockHeader.QuorumCertificate); err != nil { + if err := m.utilityUnitOfWork.Commit(block.BlockHeader.QuorumCertificate); err != nil { return err } @@ -22,12 +22,7 @@ func (m *consensusModule) commitBlock(block *coreTypes.Block) error { }). Msg("🧱🧱🧱 Committing block 🧱🧱🧱") - // Release the context - if err := m.utilityContext.Release(); err != nil { - m.logger.Warn().Err(err).Msg("Error releasing utility context") - } - - m.utilityContext = nil + m.utilityUnitOfWork = nil return nil } @@ -71,16 +66,16 @@ func (m *consensusModule) isValidMessageBlock(msg *typesCons.HotstuffMessage) (b return true, nil } -// Creates a new Utility context and clears/nullifies any previous contexts if they exist -func (m *consensusModule) refreshUtilityContext() error { +// Creates a new Utility Unit Of Work and clears/nullifies any previous U.O.W. if they exist +func (m *consensusModule) refreshUtilityUnitOfWork() error { // Catch-all structure to release the previous utility context if it wasn't properly cleaned up. // Ideally, this should not be called. - if m.utilityContext != nil { + if m.utilityUnitOfWork != nil { m.logger.Warn().Msg(typesCons.NilUtilityContextWarning) - if err := m.utilityContext.Release(); err != nil { + if err := m.utilityUnitOfWork.Release(); err != nil { m.logger.Warn().Err(err).Msg("Error releasing utility context") } - m.utilityContext = nil + m.utilityUnitOfWork = nil } // Only one write context can exist at a time, and the utility context needs to instantiate @@ -89,11 +84,11 @@ func (m *consensusModule) refreshUtilityContext() error { m.logger.Warn().Err(err).Msg("Error releasing persistence write context") } - utilityContext, err := m.GetBus().GetUtilityModule().NewContext(int64(m.height)) + utilityUow, err := m.GetBus().GetUtilityModule().NewUnitOfWork(int64(m.height)) if err != nil { return err } - m.utilityContext = utilityContext + m.utilityUnitOfWork = utilityUow return nil } From 1092e6b2138b02c78cfd07bd56bdecac90a434f9 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:46:19 +0000 Subject: [PATCH 18/59] chore(consensus): removed already handled TODO --- consensus/helpers.go | 1 - 1 file changed, 1 deletion(-) diff --git a/consensus/helpers.go b/consensus/helpers.go index 9986edf6a..149dc2731 100644 --- a/consensus/helpers.go +++ b/consensus/helpers.go @@ -229,7 +229,6 @@ func (m *consensusModule) broadcastToValidators(msg *typesCons.HotstuffMessage) /*** Persistence Helpers ***/ -// TECHDEBT(#388): Integrate this with the `persistence` module or a real mempool. func (m *consensusModule) clearMessagesPool() { for _, step := range HotstuffSteps { m.hotstuffMempool[step].Clear() From 4692611a464e75cca13be531954454545878dc3a Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:46:54 +0000 Subject: [PATCH 19/59] chore(consensus): removed already handled TODO --- consensus/hotstuff_leader.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/consensus/hotstuff_leader.go b/consensus/hotstuff_leader.go index 78ee46661..8284e2e0b 100644 --- a/consensus/hotstuff_leader.go +++ b/consensus/hotstuff_leader.go @@ -355,11 +355,6 @@ func (m *consensusModule) validateMessageSignature(msg *typesCons.HotstuffMessag address, valAddrToIdMap[address], msg, pubKey) } -// TODO(#388): Utilize the shared mempool implementation for consensus messages. -// -// It doesn't actually work because SizeOf returns the size of the map pointer, -// and does not recursively determine the size of all the underlying elements -// Add proper tests and implementation once the mempool is implemented. func (m *consensusModule) indexHotstuffMessage(msg *typesCons.HotstuffMessage) error { if m.consCfg.MaxMempoolBytes < uint64(m.hotstuffMempool[typesCons.HotstuffStep(msg.Type)].TotalMsgBytes()) { m.logger.Error().Err(typesCons.ErrConsensusMempoolFull).Msg(typesCons.DisregardHotstuffMessage) From 676e7103926c4cfa87973d79c91f4fb353f0fb9c Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:48:03 +0000 Subject: [PATCH 20/59] feat(consensus): techdebt (maxmempoolbytes from consensus config) --- consensus/hotstuff_leader.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/consensus/hotstuff_leader.go b/consensus/hotstuff_leader.go index 8284e2e0b..121282fe3 100644 --- a/consensus/hotstuff_leader.go +++ b/consensus/hotstuff_leader.go @@ -378,8 +378,8 @@ func (m *consensusModule) prepareAndApplyBlock(qc *typesCons.QuorumCertificate) return nil, typesCons.ErrReplicaPrepareBlock } - // TECHDEBT: Retrieve this from consensus consensus config - maxTxBytes := 90000 + maxTxBytes := int(m.consCfg.MaxMempoolBytes) + // Reap the mempool for transactions to be applied in this block stateHash, txs, err := m.utilityContext.CreateAndApplyProposalBlock(m.privateKey.Address(), maxTxBytes) From 68cb452ab3c4a346bd1077af286f34cd9abdd0ae Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 9 Mar 2023 21:48:43 +0000 Subject: [PATCH 21/59] feat(consensus): using refactored uow for leader logic --- consensus/hotstuff_leader.go | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/consensus/hotstuff_leader.go b/consensus/hotstuff_leader.go index 121282fe3..590aaa87e 100644 --- a/consensus/hotstuff_leader.go +++ b/consensus/hotstuff_leader.go @@ -1,10 +1,13 @@ package consensus import ( + "errors" + consensusTelemetry "github.com/pokt-network/pocket/consensus/telemetry" typesCons "github.com/pokt-network/pocket/consensus/types" "github.com/pokt-network/pocket/shared/codec" coreTypes "github.com/pokt-network/pocket/shared/core/types" + "github.com/pokt-network/pocket/shared/modules" ) // CONSOLIDATE: Last/Prev & AppHash/StateHash/BlockHash @@ -48,9 +51,9 @@ func (handler *HotstuffLeaderMessageHandler) HandleNewRoundMessage(m *consensusM }, ).Msg("📬 Received enough 📬 votes") - // Clear the previous utility context, if it exists, and create a new one - if err := m.refreshUtilityContext(); err != nil { - m.logger.Error().Err(err).Msg("Could not refresh utility context") + // Clear the previous utility unitOfWork, if it exists, and create a new one + if err := m.refreshUtilityUnitOfWork(); err != nil { + m.logger.Error().Err(err).Msg("Could not refresh utility unitOfWork") return } @@ -62,7 +65,7 @@ func (handler *HotstuffLeaderMessageHandler) HandleNewRoundMessage(m *consensusM // TODO: Add test to make sure same block is not applied twice if round is interrupted after being 'Applied'. // TODO: Add more unit tests for these checks... if m.shouldPrepareNewBlock(highPrepareQC) { - block, err := m.prepareAndApplyBlock(highPrepareQC) + block, err := m.prepareBlock(highPrepareQC) if err != nil { m.logger.Error().Err(err).Msg(typesCons.ErrPrepareBlock.Error()) m.paceMaker.InterruptRound("failed to prepare & apply block") @@ -72,14 +75,15 @@ func (handler *HotstuffLeaderMessageHandler) HandleNewRoundMessage(m *consensusM } else { // Leader acts like a replica if `prepareQC` is not `nil` // TODO: Do we need to call `validateProposal` here similar to how replicas does it - if err := m.applyBlock(highPrepareQC.Block); err != nil { - m.logger.Error().Err(err).Msg(typesCons.ErrApplyBlock.Error()) - m.paceMaker.InterruptRound("failed to apply block") - return - } m.block = highPrepareQC.Block } + if err := m.applyBlock(m.block); err != nil { + m.logger.Error().Err(err).Msg(typesCons.ErrApplyBlock.Error()) + m.paceMaker.InterruptRound("failed to apply block") + return + } + m.step = Prepare m.hotstuffMempool[NewRound].Clear() @@ -372,17 +376,20 @@ func (m *consensusModule) indexHotstuffMessage(msg *typesCons.HotstuffMessage) e // This is a helper function intended to be called by a leader/validator during a view change // to prepare a new block that is applied to the new underlying context. -// TODO: Split this into atomic & functional `prepareBlock` and `applyBlock` methods -func (m *consensusModule) prepareAndApplyBlock(qc *typesCons.QuorumCertificate) (*coreTypes.Block, error) { +func (m *consensusModule) prepareBlock(qc *typesCons.QuorumCertificate) (*coreTypes.Block, error) { if m.isReplica() { return nil, typesCons.ErrReplicaPrepareBlock } maxTxBytes := int(m.consCfg.MaxMempoolBytes) + leaderUow, ok := m.utilityUnitOfWork.(modules.LeaderUtilityUnitOfWork) + if !ok { + return nil, errors.New("invalid utility unitOfWork, should be of type LeaderUtilityUnitOfWork") + } // Reap the mempool for transactions to be applied in this block - stateHash, txs, err := m.utilityContext.CreateAndApplyProposalBlock(m.privateKey.Address(), maxTxBytes) + stateHash, txs, err := leaderUow.CreateProposalBlock(m.privateKey.Address(), maxTxBytes, nil, nil) if err != nil { return nil, err } @@ -417,7 +424,7 @@ func (m *consensusModule) prepareAndApplyBlock(qc *typesCons.QuorumCertificate) } // Set the proposal block in the persistence context - if err := m.utilityContext.SetProposalBlock(blockHeader.StateHash, blockHeader.ProposerAddress, block.Transactions); err != nil { + if err := m.utilityUnitOfWork.SetProposalBlock(blockHeader.StateHash, blockHeader.ProposerAddress, block.Transactions); err != nil { return nil, err } From 46f918184ce58700ba514cc492c7707aaaadb005 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Fri, 10 Mar 2023 00:13:09 +0000 Subject: [PATCH 22/59] fix(consensus): consolidated maxTxBytes --- consensus/e2e_tests/utils_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/consensus/e2e_tests/utils_test.go b/consensus/e2e_tests/utils_test.go index d50f61890..13a6eb136 100644 --- a/consensus/e2e_tests/utils_test.go +++ b/consensus/e2e_tests/utils_test.go @@ -40,9 +40,11 @@ func TestMain(m *testing.M) { const ( numValidators = 4 stateHash = "42" - maxTxBytes = 90000 + maxTxBytes = 500000000 ) +var maxTxBytes = defaults.DefaultConsensusMaxMempoolBytes + type IdToNodeMapping map[typesCons.NodeId]*shared.Node /*** Node Generation Helpers ***/ @@ -55,7 +57,7 @@ func GenerateNodeRuntimeMgrs(_ *testing.T, validatorCount int, clockMgr clock.Cl for i, config := range cfgs { config.Consensus = &configs.ConsensusConfig{ PrivateKey: config.PrivateKey, - MaxMempoolBytes: 500000000, + MaxMempoolBytes: maxTxBytes, PacemakerConfig: &configs.PacemakerConfig{ TimeoutMsec: 5000, Manual: false, From 4f86ceb8ad7c9fc67dfe075567ea8ec92b5fc482 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Fri, 10 Mar 2023 00:13:40 +0000 Subject: [PATCH 23/59] refactor(consensus): pacemaker_test uow --- consensus/e2e_tests/pacemaker_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/consensus/e2e_tests/pacemaker_test.go b/consensus/e2e_tests/pacemaker_test.go index 1a0a70570..8176fd979 100644 --- a/consensus/e2e_tests/pacemaker_test.go +++ b/consensus/e2e_tests/pacemaker_test.go @@ -182,11 +182,10 @@ func TestPacemakerCatchupSameStepDifferentRounds(t *testing.T) { consensusModImpl.MethodByName("SetHeight").Call([]reflect.Value{reflect.ValueOf(testHeight)}) consensusModImpl.MethodByName("SetStep").Call([]reflect.Value{reflect.ValueOf(testStep)}) - // TODO: @deblasis - refactor using uow - // utilityContext is only set on new rounds, which is skipped in this test - utilityContext, err := pocketNode.GetBus().GetUtilityModule().NewContext(int64(testHeight)) + // utilityUnitOfWork is only set on new rounds, which is skipped in this test + utilityUnitOfWork, err := pocketNode.GetBus().GetUtilityModule().NewUnitOfWork(int64(testHeight)) require.NoError(t, err) - consensusModImpl.MethodByName("SetUtilityContext").Call([]reflect.Value{reflect.ValueOf(utilityContext)}) + consensusModImpl.MethodByName("SetUtilityUnitOfWork").Call([]reflect.Value{reflect.ValueOf(utilityUnitOfWork)}) } // Set the leader to be in the highest round. From b5893966ec6022d09f9fed56939ddc4462b378f4 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Fri, 10 Mar 2023 00:15:20 +0000 Subject: [PATCH 24/59] refactor(shared): updated type for maxTxBytes --- consensus/hotstuff_leader.go | 2 +- shared/modules/utility_module.go | 2 +- utility/unit_of_work/leader.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/consensus/hotstuff_leader.go b/consensus/hotstuff_leader.go index 590aaa87e..6d34daf12 100644 --- a/consensus/hotstuff_leader.go +++ b/consensus/hotstuff_leader.go @@ -381,7 +381,7 @@ func (m *consensusModule) prepareBlock(qc *typesCons.QuorumCertificate) (*coreTy return nil, typesCons.ErrReplicaPrepareBlock } - maxTxBytes := int(m.consCfg.MaxMempoolBytes) + maxTxBytes := m.consCfg.MaxMempoolBytes leaderUow, ok := m.utilityUnitOfWork.(modules.LeaderUtilityUnitOfWork) if !ok { diff --git a/shared/modules/utility_module.go b/shared/modules/utility_module.go index 56d45dca8..2f6a56a8f 100644 --- a/shared/modules/utility_module.go +++ b/shared/modules/utility_module.go @@ -100,7 +100,7 @@ type LeaderUtilityUnitOfWork interface { // CreateAndApplyProposalBlock reaps the mempool for txs to be proposed in a new block, and // applies them to this context after validation. // TODO: @deblasis: new signature? - CreateProposalBlock(proposer []byte, maxTxBytes int, beforeApplyBlock, afterApplyBlock func(UtilityUnitOfWork) error) (stateHash string, txs [][]byte, err error) + CreateProposalBlock(proposer []byte, maxTxBytes uint64, beforeApplyBlock, afterApplyBlock func(UtilityUnitOfWork) error) (stateHash string, txs [][]byte, err error) } type ReplicaUtilityUnitOfWork interface { diff --git a/utility/unit_of_work/leader.go b/utility/unit_of_work/leader.go index 23e79581f..dab0e1fd7 100644 --- a/utility/unit_of_work/leader.go +++ b/utility/unit_of_work/leader.go @@ -24,7 +24,7 @@ func NewForLeader(height int64, readContext modules.PersistenceReadContext, rwPe } } -func (uow *leaderUtilityUnitOfWork) CreateProposalBlock(proposer []byte, maxTxBytes int, beforeApplyBlock, afterApplyBlock func(modules.UtilityUnitOfWork) error) (stateHash string, txs [][]byte, err error) { +func (uow *leaderUtilityUnitOfWork) CreateProposalBlock(proposer []byte, maxTxBytes uint64, beforeApplyBlock, afterApplyBlock func(modules.UtilityUnitOfWork) error) (stateHash string, txs [][]byte, err error) { if beforeApplyBlock != nil { uow.logger.Debug().Msg("running beforeApplyBlock...") if err := beforeApplyBlock(uow); err != nil { From 097787aeaa00ceb461f710654ec622fc5a534722 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Fri, 10 Mar 2023 00:15:49 +0000 Subject: [PATCH 25/59] =?UTF-8?q?refactor(consensus):=20tests=20are=20?= =?UTF-8?q?=E2=9C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consensus/e2e_tests/utils_test.go | 65 ++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/consensus/e2e_tests/utils_test.go b/consensus/e2e_tests/utils_test.go index 13a6eb136..6af978878 100644 --- a/consensus/e2e_tests/utils_test.go +++ b/consensus/e2e_tests/utils_test.go @@ -16,6 +16,7 @@ import ( mocksPer "github.com/pokt-network/pocket/persistence/types/mocks" "github.com/pokt-network/pocket/runtime" "github.com/pokt-network/pocket/runtime/configs" + "github.com/pokt-network/pocket/runtime/defaults" "github.com/pokt-network/pocket/runtime/genesis" "github.com/pokt-network/pocket/runtime/test_artifacts" "github.com/pokt-network/pocket/shared" @@ -40,7 +41,6 @@ func TestMain(m *testing.M) { const ( numValidators = 4 stateHash = "42" - maxTxBytes = 500000000 ) var maxTxBytes = defaults.DefaultConsensusMaxMempoolBytes @@ -104,14 +104,14 @@ func CreateTestConsensusPocketNode( persistenceMock := basePersistenceMock(t, eventsChannel, bus) bus.RegisterModule(persistenceMock) - _, err := consensus.Create(bus) + consensusModule, err := consensus.Create(bus) require.NoError(t, err) runtimeMgr := (bus).GetRuntimeMgr() // TODO(olshansky): At the moment we are using the same base mocks for all the tests, // but note that they will need to be customized on a per test basis. p2pMock := baseP2PMock(t, eventsChannel) - utilityMock := baseUtilityMock(t, eventsChannel, runtimeMgr.GetGenesis()) + utilityMock := baseUtilityMock(t, eventsChannel, runtimeMgr.GetGenesis(), consensusModule.(modules.ConsensusModule)) telemetryMock := baseTelemetryMock(t, eventsChannel) loggerMock := baseLoggerMock(t, eventsChannel) rpcMock := baseRpcMock(t, eventsChannel) @@ -431,44 +431,71 @@ func baseP2PMock(t *testing.T, eventsChannel modules.EventsChannel) *mockModules } // Creates a utility module mock with mock implementations of some basic functionality -func baseUtilityMock(t *testing.T, _ modules.EventsChannel, genesisState *genesis.GenesisState) *mockModules.MockUtilityModule { +func baseUtilityMock(t *testing.T, _ modules.EventsChannel, genesisState *genesis.GenesisState, consensusMod modules.ConsensusModule) *mockModules.MockUtilityModule { ctrl := gomock.NewController(t) utilityMock := mockModules.NewMockUtilityModule(ctrl) - utilityContextMock := baseUtilityContextMock(t, genesisState) - utilityMock.EXPECT().Start().Return(nil).AnyTimes() utilityMock.EXPECT().SetBus(gomock.Any()).Return().AnyTimes() utilityMock.EXPECT(). - NewContext(gomock.Any()). - Return(utilityContextMock, nil). + NewUnitOfWork(gomock.Any()). + DoAndReturn( + // mimicking the behavior of the utility module's NewUnitOfWork method + func(height int64) (modules.UtilityUnitOfWork, error) { + if consensusMod.IsLeader() { + return baseLeaderUtilityUnitOfWorkMock(t, genesisState), nil + } + return baseReplicaUtilityUnitOfWorkMock(t, genesisState), nil + }). MaxTimes(4) utilityMock.EXPECT().GetModuleName().Return(modules.UtilityModuleName).AnyTimes() return utilityMock } -func baseUtilityContextMock(t *testing.T, genesisState *genesis.GenesisState) *mockModules.MockUtilityContext { +func baseLeaderUtilityUnitOfWorkMock(t *testing.T, genesisState *genesis.GenesisState) *mockModules.MockLeaderUtilityUnitOfWork { ctrl := gomock.NewController(t) - utilityContextMock := mockModules.NewMockUtilityContext(ctrl) + utilityLeaderUnitOfWorkMock := mockModules.NewMockLeaderUtilityUnitOfWork(ctrl) + persistenceContextMock := mockModules.NewMockPersistenceRWContext(ctrl) persistenceContextMock.EXPECT().GetAllValidators(gomock.Any()).Return(genesisState.GetValidators(), nil).AnyTimes() persistenceContextMock.EXPECT().GetBlockHash(gomock.Any()).Return("", nil).AnyTimes() - utilityContextMock.EXPECT(). - CreateAndApplyProposalBlock(gomock.Any(), maxTxBytes). + utilityLeaderUnitOfWorkMock.EXPECT(). + CreateProposalBlock(gomock.Any(), maxTxBytes, gomock.Any(), gomock.Any()). // TODO: @deblasis - review before/after + Return(stateHash, make([][]byte, 0), nil). + AnyTimes() + utilityLeaderUnitOfWorkMock.EXPECT(). + ApplyBlock(gomock.Any(), gomock.Any()). // TODO: @deblasis - review before/after Return(stateHash, make([][]byte, 0), nil). AnyTimes() - utilityContextMock.EXPECT(). - ApplyBlock(). - Return(stateHash, nil). + utilityLeaderUnitOfWorkMock.EXPECT().SetProposalBlock(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() + utilityLeaderUnitOfWorkMock.EXPECT().Commit(gomock.Any()).Return(nil).AnyTimes() + utilityLeaderUnitOfWorkMock.EXPECT().Release().Return(nil).AnyTimes() + + persistenceContextMock.EXPECT().Release().Return(nil).AnyTimes() + + return utilityLeaderUnitOfWorkMock +} + +func baseReplicaUtilityUnitOfWorkMock(t *testing.T, genesisState *genesis.GenesisState) *mockModules.MockReplicaUtilityUnitOfWork { + ctrl := gomock.NewController(t) + utilityReplicaUnitOfWorkMock := mockModules.NewMockReplicaUtilityUnitOfWork(ctrl) + + persistenceContextMock := mockModules.NewMockPersistenceRWContext(ctrl) + persistenceContextMock.EXPECT().GetAllValidators(gomock.Any()).Return(genesisState.GetValidators(), nil).AnyTimes() + persistenceContextMock.EXPECT().GetBlockHash(gomock.Any()).Return("", nil).AnyTimes() + + utilityReplicaUnitOfWorkMock.EXPECT(). + ApplyBlock(gomock.Any(), gomock.Any()). // TODO: @deblasis - review before/after + Return(stateHash, make([][]byte, 0), nil). AnyTimes() - utilityContextMock.EXPECT().SetProposalBlock(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() - utilityContextMock.EXPECT().Commit(gomock.Any()).Return(nil).AnyTimes() - utilityContextMock.EXPECT().Release().Return(nil).AnyTimes() + utilityReplicaUnitOfWorkMock.EXPECT().SetProposalBlock(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() + utilityReplicaUnitOfWorkMock.EXPECT().Commit(gomock.Any()).Return(nil).AnyTimes() + utilityReplicaUnitOfWorkMock.EXPECT().Release().Return(nil).AnyTimes() persistenceContextMock.EXPECT().Release().Return(nil).AnyTimes() - return utilityContextMock + return utilityReplicaUnitOfWorkMock } func baseTelemetryMock(t *testing.T, _ modules.EventsChannel) *mockModules.MockTelemetryModule { From 5e2dca41995447313a79a2f865580110916d8ce7 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sat, 11 Mar 2023 00:37:58 +0000 Subject: [PATCH 26/59] refactor(consensus): simplified create / apply --- consensus/e2e_tests/utils_test.go | 6 +++--- consensus/hotstuff_leader.go | 2 +- consensus/hotstuff_replica.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/consensus/e2e_tests/utils_test.go b/consensus/e2e_tests/utils_test.go index 6af978878..593bb5b3d 100644 --- a/consensus/e2e_tests/utils_test.go +++ b/consensus/e2e_tests/utils_test.go @@ -461,11 +461,11 @@ func baseLeaderUtilityUnitOfWorkMock(t *testing.T, genesisState *genesis.Genesis persistenceContextMock.EXPECT().GetBlockHash(gomock.Any()).Return("", nil).AnyTimes() utilityLeaderUnitOfWorkMock.EXPECT(). - CreateProposalBlock(gomock.Any(), maxTxBytes, gomock.Any(), gomock.Any()). // TODO: @deblasis - review before/after + CreateProposalBlock(gomock.Any(), maxTxBytes). Return(stateHash, make([][]byte, 0), nil). AnyTimes() utilityLeaderUnitOfWorkMock.EXPECT(). - ApplyBlock(gomock.Any(), gomock.Any()). // TODO: @deblasis - review before/after + ApplyBlock(). Return(stateHash, make([][]byte, 0), nil). AnyTimes() utilityLeaderUnitOfWorkMock.EXPECT().SetProposalBlock(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() @@ -486,7 +486,7 @@ func baseReplicaUtilityUnitOfWorkMock(t *testing.T, genesisState *genesis.Genesi persistenceContextMock.EXPECT().GetBlockHash(gomock.Any()).Return("", nil).AnyTimes() utilityReplicaUnitOfWorkMock.EXPECT(). - ApplyBlock(gomock.Any(), gomock.Any()). // TODO: @deblasis - review before/after + ApplyBlock(). Return(stateHash, make([][]byte, 0), nil). AnyTimes() utilityReplicaUnitOfWorkMock.EXPECT().SetProposalBlock(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() diff --git a/consensus/hotstuff_leader.go b/consensus/hotstuff_leader.go index 6d34daf12..6e8faa23d 100644 --- a/consensus/hotstuff_leader.go +++ b/consensus/hotstuff_leader.go @@ -389,7 +389,7 @@ func (m *consensusModule) prepareBlock(qc *typesCons.QuorumCertificate) (*coreTy } // Reap the mempool for transactions to be applied in this block - stateHash, txs, err := leaderUow.CreateProposalBlock(m.privateKey.Address(), maxTxBytes, nil, nil) + stateHash, txs, err := leaderUow.CreateProposalBlock(m.privateKey.Address(), maxTxBytes) if err != nil { return nil, err } diff --git a/consensus/hotstuff_replica.go b/consensus/hotstuff_replica.go index cb7202f08..7b51538a0 100644 --- a/consensus/hotstuff_replica.go +++ b/consensus/hotstuff_replica.go @@ -235,7 +235,7 @@ func (m *consensusModule) applyBlock(block *coreTypes.Block) error { } // Apply all the transactions in the block and get the stateHash - stateHash, _, err := m.utilityUnitOfWork.ApplyBlock(nil, nil) + stateHash, _, err := m.utilityUnitOfWork.ApplyBlock() if err != nil { return err } From 7ab68c5937b36825b188ffa7967c5506919a922f Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sat, 11 Mar 2023 00:38:30 +0000 Subject: [PATCH 27/59] fix(persistence): techdebt: log statement --- persistence/block.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/persistence/block.go b/persistence/block.go index 02b0d48e0..38f95a0dd 100644 --- a/persistence/block.go +++ b/persistence/block.go @@ -3,7 +3,6 @@ package persistence import ( "encoding/hex" "fmt" - "log" "github.com/pokt-network/pocket/persistence/kvstore" "github.com/pokt-network/pocket/persistence/types" @@ -100,6 +99,6 @@ func (p *PostgresContext) storeBlock(block *coreTypes.Block) error { if err != nil { return err } - log.Printf("Storing block %d in block store.\n", block.BlockHeader.Height) + p.logger.Info().Uint64("height", block.BlockHeader.Height).Msg("Storing block in block store") return p.blockStore.Set(utils.HeightToBytes(uint64(p.Height)), blockBz) } From 15365fe4614797ea173c646dd21b813d52df2ff6 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sat, 11 Mar 2023 00:39:08 +0000 Subject: [PATCH 28/59] refactor(utility): updated create / apply --- shared/modules/utility_module.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/shared/modules/utility_module.go b/shared/modules/utility_module.go index 2f6a56a8f..f382b2e2b 100644 --- a/shared/modules/utility_module.go +++ b/shared/modules/utility_module.go @@ -72,6 +72,9 @@ type UnstakingActor interface { GetOutputAddress() []byte } +// TECHDEBT: @deblasis - `CreateAndApplyProposalBlock` and `ApplyBlock` should be be refactored into a +// +// `GetProposalBlock` and `ApplyProposalBlock` functions type UtilityUnitOfWork interface { IntegratableModule @@ -85,7 +88,7 @@ type UtilityUnitOfWork interface { // ApplyBlock applies the context's in-memory proposed state (i.e. the txs in this context). // Only intended to be used by the block verifiers (i.e. replicas). // NOTE: this is called by the replica OR by the leader when `prepareQc` is not `nil` - ApplyBlock(beforeApplyBlock, afterApplyBlock func(UtilityUnitOfWork) error) (stateHash string, txs [][]byte, err error) + ApplyBlock() (stateHash string, txs [][]byte, err error) // Release releases this utility context and any underlying contexts it references Release() error @@ -100,7 +103,7 @@ type LeaderUtilityUnitOfWork interface { // CreateAndApplyProposalBlock reaps the mempool for txs to be proposed in a new block, and // applies them to this context after validation. // TODO: @deblasis: new signature? - CreateProposalBlock(proposer []byte, maxTxBytes uint64, beforeApplyBlock, afterApplyBlock func(UtilityUnitOfWork) error) (stateHash string, txs [][]byte, err error) + CreateProposalBlock(proposer []byte, maxTxBytes uint64) (stateHash string, txs [][]byte, err error) } type ReplicaUtilityUnitOfWork interface { From bdf0a5cb46df53718e652612d0355892ba75bd10 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sat, 11 Mar 2023 00:39:35 +0000 Subject: [PATCH 29/59] refactor(utility): deprecating utilityContext --- shared/modules/utility_module.go | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/shared/modules/utility_module.go b/shared/modules/utility_module.go index f382b2e2b..d43066ad0 100644 --- a/shared/modules/utility_module.go +++ b/shared/modules/utility_module.go @@ -16,10 +16,6 @@ const ( type UtilityModule interface { Module - // NewContext creates a `utilityContext` with an underlying read-write `persistenceContext` (only 1 of which can exist at a time) - // TODO - @deblasis - deprecate this - NewContext(height int64) (UtilityContext, error) - // NewUnitOfWork creates a `utilityUnitOfWork` used to allow atomicity and commit/rollback functionality (https://martinfowler.com/eaaCatalog/unitOfWork.html) NewUnitOfWork(height int64) (UtilityUnitOfWork, error) @@ -35,34 +31,6 @@ type UtilityModule interface { HandleUtilityMessage(*anypb.Any) error } -// TECHDEBT: `CreateAndApplyProposalBlock` and `ApplyBlock` should be be refactored into a -// `GetProposalBlock` and `ApplyProposalBlock` functions - -// The context within which the node can operate with the utility layer. -// TODO: @deblasis - deprecate this -type UtilityContext interface { - // SetProposalBlock updates the utility context with the proposed state transition. - // It does not apply, validate or commit the changes. - // For example, it can be use during state sync to set a proposed state transition before validation. - // TODO: Investigate a way to potentially simplify the interface by removing this function. - SetProposalBlock(blockHash string, proposerAddr []byte, txs [][]byte) error - - // CreateAndApplyProposalBlock reaps the mempool for txs to be proposed in a new block, and - // applies them to this context after validation. - // Only intended to be used by the block proposer. - CreateAndApplyProposalBlock(proposer []byte, maxTxBytes int) (stateHash string, txs [][]byte, err error) - - // ApplyBlock applies the context's in-memory proposed state (i.e. the txs in this context). - // Only intended to be used by the block verifiers (i.e. replicas). - ApplyBlock() (stateHash string, err error) - - // Release releases this utility context and any underlying contexts it references - Release() error - - // Commit commits this utility context along with any underlying contexts (e.g. persistenceContext) it references - Commit(quorumCert []byte) error -} - // TECHDEBT: Remove this interface from `shared/modules` and use the `Actor` protobuf type instead // There will need to be some documentation or indicator that the Actor struct returned may not be // fully hydrated. Alternatively, we could eat the performance cost and just hydrate the entire struct From 43294c38b7b2f13c4af489ecd0b34d1263d212dc Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sat, 11 Mar 2023 00:41:04 +0000 Subject: [PATCH 30/59] refactor(utility): WIP separation of concerns module/uow --- utility/account.go | 92 --- utility/block.go | 331 ---------- utility/context.go | 176 +++--- utility/gov.go | 576 ------------------ utility/transaction.go | 84 --- utility/unit_of_work/account.go | 92 +++ utility/{ => unit_of_work}/account_test.go | 14 +- utility/{ => unit_of_work}/actor.go | 102 ++-- utility/{ => unit_of_work}/actor_test.go | 34 +- utility/{ => unit_of_work}/application.go | 4 +- .../{ => unit_of_work}/application_test.go | 2 +- utility/unit_of_work/base.go | 77 --- utility/unit_of_work/block.go | 218 +++++++ utility/{ => unit_of_work}/block_test.go | 8 +- utility/unit_of_work/gov.go | 576 ++++++++++++++++++ utility/{ => unit_of_work}/gov_test.go | 2 +- utility/{ => unit_of_work}/message_test.go | 2 +- utility/unit_of_work/module.go | 209 +++++++ utility/{ => unit_of_work}/module_test.go | 42 +- utility/unit_of_work/transaction.go | 90 +++ .../{ => unit_of_work}/transaction_test.go | 56 +- .../{ => unit_of_work}/tx_message_handler.go | 48 +- .../unit_of_work/{leader.go => uow_leader.go} | 27 +- .../{replica.go => uow_replica.go} | 1 + utility/{ => unit_of_work}/validator.go | 14 +- 25 files changed, 1454 insertions(+), 1423 deletions(-) delete mode 100644 utility/account.go delete mode 100644 utility/block.go delete mode 100644 utility/gov.go create mode 100644 utility/unit_of_work/account.go rename utility/{ => unit_of_work}/account_test.go (89%) rename utility/{ => unit_of_work}/actor.go (60%) rename utility/{ => unit_of_work}/actor_test.go (94%) rename utility/{ => unit_of_work}/application.go (88%) rename utility/{ => unit_of_work}/application_test.go (96%) delete mode 100644 utility/unit_of_work/base.go create mode 100644 utility/unit_of_work/block.go rename utility/{ => unit_of_work}/block_test.go (97%) create mode 100644 utility/unit_of_work/gov.go rename utility/{ => unit_of_work}/gov_test.go (99%) rename utility/{ => unit_of_work}/message_test.go (99%) create mode 100644 utility/unit_of_work/module.go rename utility/{ => unit_of_work}/module_test.go (78%) create mode 100644 utility/unit_of_work/transaction.go rename utility/{ => unit_of_work}/transaction_test.go (75%) rename utility/{ => unit_of_work}/tx_message_handler.go (72%) rename utility/unit_of_work/{leader.go => uow_leader.go} (64%) rename utility/unit_of_work/{replica.go => uow_replica.go} (95%) rename utility/{ => unit_of_work}/validator.go (84%) diff --git a/utility/account.go b/utility/account.go deleted file mode 100644 index 2595d2511..000000000 --- a/utility/account.go +++ /dev/null @@ -1,92 +0,0 @@ -package utility - -// Internal business logic for `Accounts` & `Pools` (i.e. autonomous accounts owned by the protocol) -// -// Accounts are utility module structures that resemble currency holding vehicles; e.g. a bank account. -// Pools are autonomous accounts owned by the protocol; e.g. an account for a fee pool that gets distributed - -import ( - "math/big" - - "github.com/pokt-network/pocket/shared/utils" - "github.com/pokt-network/pocket/utility/types" -) - -// Accounts specific functionality - -func (u *utilityContext) getAccountAmount(address []byte) (*big.Int, types.Error) { - amountStr, err := u.store.GetAccountAmount(address, u.height) - if err != nil { - return nil, types.ErrGetAccountAmount(err) - } - amount, err := utils.StringToBigInt(amountStr) - if err != nil { - return nil, types.ErrStringToBigInt(err) - } - return amount, nil -} - -func (u *utilityContext) addAccountAmount(address []byte, amountToAdd *big.Int) types.Error { - if err := u.store.AddAccountAmount(address, utils.BigIntToString(amountToAdd)); err != nil { - return types.ErrAddAccountAmount(err) - } - return nil -} - -func (u *utilityContext) subtractAccountAmount(address []byte, amountToSubtract *big.Int) types.Error { - if err := u.store.SubtractAccountAmount(address, utils.BigIntToString(amountToSubtract)); err != nil { - return types.ErrSetAccountAmount(err) - } - return nil -} - -func (u *utilityContext) setAccountAmount(address []byte, amount *big.Int) types.Error { - if err := u.store.SetAccountAmount(address, utils.BigIntToString(amount)); err != nil { - return types.ErrSetAccountAmount(err) - } - return nil -} - -// Pools specific functionality - -// IMPROVE: Pool function should accept the actual pool types rather than the `FriendlyName` string - -func (u *utilityContext) insertPool(name string, amount *big.Int) types.Error { - if err := u.store.InsertPool(name, utils.BigIntToString(amount)); err != nil { - return types.ErrSetPool(name, err) - } - return nil -} - -func (u *utilityContext) getPoolAmount(name string) (*big.Int, types.Error) { - amountStr, err := u.store.GetPoolAmount(name, u.height) - if err != nil { - return nil, types.ErrGetPoolAmount(name, err) - } - amount, err := utils.StringToBigInt(amountStr) - if err != nil { - return nil, types.ErrStringToBigInt(err) - } - return amount, nil -} - -func (u *utilityContext) addPoolAmount(name string, amountToAdd *big.Int) types.Error { - if err := u.store.AddPoolAmount(name, utils.BigIntToString(amountToAdd)); err != nil { - return types.ErrAddPoolAmount(name, err) - } - return nil -} - -func (u *utilityContext) subPoolAmount(name string, amountToSub *big.Int) types.Error { - if err := u.store.SubtractPoolAmount(name, utils.BigIntToString(amountToSub)); err != nil { - return types.ErrSubPoolAmount(name, err) - } - return nil -} - -func (u *utilityContext) setPoolAmount(name string, amount *big.Int) types.Error { - if err := u.store.SetPoolAmount(name, utils.BigIntToString(amount)); err != nil { - return types.ErrSetPoolAmount(name, err) - } - return nil -} diff --git a/utility/block.go b/utility/block.go deleted file mode 100644 index fb72f9ac4..000000000 --- a/utility/block.go +++ /dev/null @@ -1,331 +0,0 @@ -package utility - -// Internal business logic containing the lifecycle of Block-related operations - -import ( - "encoding/hex" - "math/big" - - coreTypes "github.com/pokt-network/pocket/shared/core/types" - moduleTypes "github.com/pokt-network/pocket/shared/modules/types" - "github.com/pokt-network/pocket/shared/utils" - typesUtil "github.com/pokt-network/pocket/utility/types" -) - -// CreateAndApplyProposalBlock implements the exposed functionality of the shared UtilityContext interface. -func (u *utilityContext) CreateAndApplyProposalBlock(proposer []byte, maxTransactionBytes int) (stateHash string, txs [][]byte, err error) { - prevBlockByzantineVals, err := u.prevBlockByzantineValidators() - if err != nil { - return "", nil, err - } - - // begin block lifecycle phase - if err := u.beginBlock(prevBlockByzantineVals); err != nil { - return "", nil, err - } - txs = make([][]byte, 0) - txsTotalBz := 0 - txIdx := 0 - - mempool := u.GetBus().GetUtilityModule().GetMempool() - for !mempool.IsEmpty() { - // NB: In order for transactions to have entered the mempool, `HandleTransaction` must have - // been called which handles basic checks & validation. - txBz, err := mempool.PopTx() - if err != nil { - return "", nil, err - } - - tx, err := coreTypes.TxFromBytes(txBz) - if err != nil { - return "", nil, err - } - - txBzSize := len(txBz) - txsTotalBz += txBzSize - - // Exceeding maximum transaction bytes to be added in this block - if txsTotalBz >= maxTransactionBytes { - // Add back popped tx to be applied in a future block - if err := mempool.AddTx(txBz); err != nil { - return "", nil, err - } - break // we've reached our max - } - - txResult, err := u.hydrateTxResult(tx, txIdx) - if err != nil { - u.logger.Err(err).Msg("Error in ApplyTransaction") - // TODO(#327): Properly implement 'unhappy path' for save points - if err := u.revertLastSavePoint(); err != nil { - return "", nil, err - } - txsTotalBz -= txBzSize - continue - } - - // Index the transaction - if err := u.store.IndexTransaction(txResult); err != nil { - u.logger.Fatal().Err(err).Msgf("TODO(#327): The transaction can by hydrated but not indexed. Crash the process for now: %v\n", err) - } - - txs = append(txs, txBz) - txIdx++ - } - - if err := u.endBlock(proposer); err != nil { - return "", nil, err - } - - // Compute & return the new state hash - stateHash, err = u.store.ComputeStateHash() - if err != nil { - u.logger.Fatal().Err(err).Msg("Updating the app hash failed. TODO: Look into roll-backing the entire commit...") - } - u.logger.Info().Str("state_hash", stateHash).Msgf("CreateAndApplyProposalBlock finished successfully") - - return stateHash, txs, err -} - -// CLEANUP: code re-use ApplyBlock() for CreateAndApplyBlock() -func (u *utilityContext) ApplyBlock() (string, error) { - lastByzantineValidators, err := u.prevBlockByzantineValidators() - if err != nil { - return "", err - } - - // begin block lifecycle phase - if err := u.beginBlock(lastByzantineValidators); err != nil { - return "", err - } - - mempool := u.GetBus().GetUtilityModule().GetMempool() - - // deliver txs lifecycle phase - for index, txProtoBytes := range u.proposalBlockTxs { - tx, err := coreTypes.TxFromBytes(txProtoBytes) - if err != nil { - return "", err - } - if err := tx.ValidateBasic(); err != nil { - return "", err - } - // TODO(#346): Currently, the pattern is allowing nil err with an error transaction... - // Should we terminate applyBlock immediately if there's an invalid transaction? - // Or wait until the entire lifecycle is over to evaluate an 'invalid' block - - // Validate and apply the transaction to the Postgres database - txResult, err := u.hydrateTxResult(tx, index) - if err != nil { - return "", err - } - - txHash, err := tx.Hash() - if err != nil { - return "", err - } - - // TODO: Need to properly add transactions back on rollbacks - if mempool.Contains(txHash) { - if err := mempool.RemoveTx(txProtoBytes); err != nil { - return "", err - } - u.logger.Info().Str("tx_hash", txHash).Msg("Applying tx that WAS in the local mempool") - } else { - u.logger.Info().Str("tx_hash", txHash).Msg("Applying tx that WAS NOT in the local mempool") - } - - if err := u.store.IndexTransaction(txResult); err != nil { - u.logger.Fatal().Err(err).Msgf("TODO(#327): We can apply the transaction but not index it. Crash the process for now: %v\n", err) - } - } - - // end block lifecycle phase - if err := u.endBlock(u.proposalProposerAddr); err != nil { - return "", err - } - // return the app hash (consensus module will get the validator set directly) - stateHash, err := u.store.ComputeStateHash() - if err != nil { - u.logger.Fatal().Err(err).Msg("Updating the app hash failed. TODO: Look into roll-backing the entire commit...") - return "", typesUtil.ErrAppHash(err) - } - u.logger.Info().Msgf("ApplyBlock - computed state hash: %s", stateHash) - - // return the app hash; consensus module will get the validator set directly - return stateHash, nil -} - -func (u *utilityContext) beginBlock(previousBlockByzantineValidators [][]byte) typesUtil.Error { - if err := u.handleByzantineValidators(previousBlockByzantineValidators); err != nil { - return err - } - // INCOMPLETE: Identify what else needs to be done in the begin block lifecycle phase - return nil -} - -func (u *utilityContext) endBlock(proposer []byte) typesUtil.Error { - // reward the block proposer - if err := u.handleProposerRewards(proposer); err != nil { - return err - } - - // unstake actors that have been 'unstaking' for the UnstakingBlocks - if err := u.unbondUnstakingActors(); err != nil { - return err - } - - // begin unstaking the actors who have been paused for MaxPauseBlocks - if err := u.beginUnstakingMaxPausedActors(); err != nil { - return err - } - - // INCOMPLETE: Identify what else needs to be done in the begin block lifecycle phase - return nil -} - -func (u *utilityContext) handleProposerRewards(proposer []byte) typesUtil.Error { - feePoolName := coreTypes.Pools_POOLS_FEE_COLLECTOR.FriendlyName() - feesAndRewardsCollected, err := u.getPoolAmount(feePoolName) - if err != nil { - return err - } - - // Nullify the rewards pool - if err := u.setPoolAmount(feePoolName, big.NewInt(0)); err != nil { - return err - } - - // - proposerCutPercentage, err := u.getProposerPercentageOfFees() - if err != nil { - return err - } - - daoCutPercentage := 100 - proposerCutPercentage - if daoCutPercentage < 0 || daoCutPercentage > 100 { - return typesUtil.ErrInvalidProposerCutPercentage() - } - - amountToProposerFloat := new(big.Float).SetInt(feesAndRewardsCollected) - amountToProposerFloat.Mul(amountToProposerFloat, big.NewFloat(float64(proposerCutPercentage))) - amountToProposerFloat.Quo(amountToProposerFloat, big.NewFloat(100)) - amountToProposer, _ := amountToProposerFloat.Int(nil) - amountToDAO := feesAndRewardsCollected.Sub(feesAndRewardsCollected, amountToProposer) - if err := u.addAccountAmount(proposer, amountToProposer); err != nil { - return err - } - if err := u.addPoolAmount(coreTypes.Pools_POOLS_DAO.FriendlyName(), amountToDAO); err != nil { - return err - } - return nil -} - -func (u *utilityContext) unbondUnstakingActors() (err typesUtil.Error) { - for actorTypeNum := range coreTypes.ActorType_name { - if actorTypeNum == 0 { // ACTOR_TYPE_UNSPECIFIED - continue - } - actorType := coreTypes.ActorType(actorTypeNum) - - var readyToUnbond []*moduleTypes.UnstakingActor - var poolName string - - var er error - switch actorType { - case coreTypes.ActorType_ACTOR_TYPE_APP: - readyToUnbond, er = u.store.GetAppsReadyToUnstake(u.height, int32(coreTypes.StakeStatus_Unstaking)) - poolName = coreTypes.Pools_POOLS_APP_STAKE.FriendlyName() - case coreTypes.ActorType_ACTOR_TYPE_FISH: - readyToUnbond, er = u.store.GetFishermenReadyToUnstake(u.height, int32(coreTypes.StakeStatus_Unstaking)) - poolName = coreTypes.Pools_POOLS_FISHERMAN_STAKE.FriendlyName() - case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - readyToUnbond, er = u.store.GetServicersReadyToUnstake(u.height, int32(coreTypes.StakeStatus_Unstaking)) - poolName = coreTypes.Pools_POOLS_SERVICER_STAKE.FriendlyName() - case coreTypes.ActorType_ACTOR_TYPE_VAL: - readyToUnbond, er = u.store.GetValidatorsReadyToUnstake(u.height, int32(coreTypes.StakeStatus_Unstaking)) - poolName = coreTypes.Pools_POOLS_VALIDATOR_STAKE.FriendlyName() - case coreTypes.ActorType_ACTOR_TYPE_UNSPECIFIED: - continue - } - if er != nil { - return typesUtil.ErrGetReadyToUnstake(er) - } - - // Loop through all unstaking actors and unbond those that have reached the waiting period, - // move their stake from the pool back to the corresponding account. - for _, actor := range readyToUnbond { - stakeAmount, err := utils.StringToBigInt(actor.StakeAmount) - if err != nil { - return typesUtil.ErrStringToBigInt(err) - } - - outputAddrBz, err := hex.DecodeString(actor.OutputAddress) - if err != nil { - return typesUtil.ErrHexDecodeFromString(err) - } - - if err := u.subPoolAmount(poolName, stakeAmount); err != nil { - return err - } - if err := u.addAccountAmount(outputAddrBz, stakeAmount); err != nil { - return err - } - } - } - - return nil -} - -func (u *utilityContext) beginUnstakingMaxPausedActors() (err typesUtil.Error) { - for actorTypeNum := range coreTypes.ActorType_name { - if actorTypeNum == 0 { // ACTOR_TYPE_UNSPECIFIED - continue - } - actorType := coreTypes.ActorType(actorTypeNum) - - if actorType == coreTypes.ActorType_ACTOR_TYPE_UNSPECIFIED { - continue - } - maxPausedBlocks, err := u.getMaxAllowedPausedBlocks(actorType) - if err != nil { - return err - } - maxPauseHeight := u.height - int64(maxPausedBlocks) - if maxPauseHeight < 0 { // genesis edge case - maxPauseHeight = 0 - } - if err := u.beginUnstakingActorsPausedBefore(maxPauseHeight, actorType); err != nil { - return err - } - } - return nil -} - -func (u *utilityContext) beginUnstakingActorsPausedBefore(pausedBeforeHeight int64, actorType coreTypes.ActorType) (err typesUtil.Error) { - unbondingHeight, err := u.getUnbondingHeight(actorType) - if err != nil { - return err - } - - var er error - switch actorType { - case coreTypes.ActorType_ACTOR_TYPE_APP: - er = u.store.SetAppStatusAndUnstakingHeightIfPausedBefore(pausedBeforeHeight, unbondingHeight, int32(coreTypes.StakeStatus_Unstaking)) - case coreTypes.ActorType_ACTOR_TYPE_FISH: - er = u.store.SetFishermanStatusAndUnstakingHeightIfPausedBefore(pausedBeforeHeight, unbondingHeight, int32(coreTypes.StakeStatus_Unstaking)) - case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - er = u.store.SetServicerStatusAndUnstakingHeightIfPausedBefore(pausedBeforeHeight, unbondingHeight, int32(coreTypes.StakeStatus_Unstaking)) - case coreTypes.ActorType_ACTOR_TYPE_VAL: - er = u.store.SetValidatorsStatusAndUnstakingHeightIfPausedBefore(pausedBeforeHeight, unbondingHeight, int32(coreTypes.StakeStatus_Unstaking)) - } - if er != nil { - return typesUtil.ErrSetStatusPausedBefore(er, pausedBeforeHeight) - } - return nil -} - -// TODO: Need to design & document this business logic. -func (u *utilityContext) prevBlockByzantineValidators() ([][]byte, error) { - return nil, nil -} diff --git a/utility/context.go b/utility/context.go index 94857fd2b..bb8538542 100644 --- a/utility/context.go +++ b/utility/context.go @@ -1,103 +1,103 @@ package utility -import ( - "encoding/hex" +// import ( +// "encoding/hex" - "github.com/pokt-network/pocket/shared/modules" - "github.com/pokt-network/pocket/shared/modules/base_modules" - typesUtil "github.com/pokt-network/pocket/utility/types" -) +// "github.com/pokt-network/pocket/shared/modules" +// "github.com/pokt-network/pocket/shared/modules/base_modules" +// typesUtil "github.com/pokt-network/pocket/utility/types" +// ) -var ( - _ modules.IntegratableModule = &utilityContext{} - _ modules.UtilityContext = &utilityContext{} -) +// var ( +// _ modules.IntegratableModule = &utilityContext{} +// _ modules.UtilityUnitOfWork = &utilityContext{} +// ) -type utilityContext struct { - base_modules.IntegratableModule +// type utilityContext struct { +// base_modules.IntegratableModule - logger *modules.Logger - height int64 +// logger *modules.Logger +// height int64 - store modules.PersistenceRWContext - savePointsSet map[string]struct{} - savePointsList [][]byte +// store modules.PersistenceRWContext +// savePointsSet map[string]struct{} +// savePointsList [][]byte - // TECHDEBT: Consolidate all these types with the shared Protobuf struct and create a `proposalBlock` - proposalStateHash string - proposalProposerAddr []byte - proposalBlockTxs [][]byte -} +// // TECHDEBT: Consolidate all these types with the shared Protobuf struct and create a `proposalBlock` +// proposalStateHash string +// proposalProposerAddr []byte +// proposalBlockTxs [][]byte +// } -func (u *utilityModule) NewContext(height int64) (modules.UtilityContext, error) { - persistenceCtx, err := u.GetBus().GetPersistenceModule().NewRWContext(height) - if err != nil { - return nil, typesUtil.ErrNewPersistenceContext(err) - } - ctx := &utilityContext{ - logger: u.logger, - height: height, +// // func (u *utilityModule) NewContext(height int64) (modules.UtilityContext, error) { +// // persistenceCtx, err := u.GetBus().GetPersistenceModule().NewRWContext(height) +// // if err != nil { +// // return nil, typesUtil.ErrNewPersistenceContext(err) +// // } +// // ctx := &utilityContext{ +// // logger: u.logger, +// // height: height, - // No save points on start - store: persistenceCtx, - savePointsList: make([][]byte, 0), - savePointsSet: make(map[string]struct{}), - } - ctx.SetBus(u.GetBus()) - return ctx, nil -} +// // // No save points on start +// // store: persistenceCtx, +// // savePointsList: make([][]byte, 0), +// // savePointsSet: make(map[string]struct{}), +// // } +// // ctx.SetBus(u.GetBus()) +// // return ctx, nil +// // } -func (p *utilityContext) SetProposalBlock(blockHash string, proposerAddr []byte, txs [][]byte) error { - p.proposalStateHash = blockHash - p.proposalProposerAddr = proposerAddr - p.proposalBlockTxs = txs - return nil -} +// func (p *utilityContext) SetProposalBlock(blockHash string, proposerAddr []byte, txs [][]byte) error { +// p.proposalStateHash = blockHash +// p.proposalProposerAddr = proposerAddr +// p.proposalBlockTxs = txs +// return nil +// } -func (u *utilityContext) Commit(quorumCert []byte) error { - if err := u.store.Commit(u.proposalProposerAddr, quorumCert); err != nil { - return err - } - u.store = nil - return nil -} +// func (u *utilityContext) Commit(quorumCert []byte) error { +// if err := u.store.Commit(u.proposalProposerAddr, quorumCert); err != nil { +// return err +// } +// u.store = nil +// return nil +// } -func (u *utilityContext) Release() error { - if u.store == nil { - return nil - } - if err := u.store.Release(); err != nil { - return err - } - u.store = nil - return nil -} +// func (u *utilityContext) Release() error { +// if u.store == nil { +// return nil +// } +// if err := u.store.Release(); err != nil { +// return err +// } +// u.store = nil +// return nil +// } -// TODO: This has not been tested or investigated in detail -func (u *utilityContext) revertLastSavePoint() typesUtil.Error { - if len(u.savePointsSet) == 0 { - return typesUtil.ErrEmptySavePoints() - } - var key []byte - popIndex := len(u.savePointsList) - 1 - key, u.savePointsList = u.savePointsList[popIndex], u.savePointsList[:popIndex] - delete(u.savePointsSet, hex.EncodeToString(key)) - if err := u.store.RollbackToSavePoint(key); err != nil { - return typesUtil.ErrRollbackSavePoint(err) - } - return nil -} +// // TODO: This has not been tested or investigated in detail +// func (u *utilityContext) revertLastSavePoint() typesUtil.Error { +// if len(u.savePointsSet) == 0 { +// return typesUtil.ErrEmptySavePoints() +// } +// var key []byte +// popIndex := len(u.savePointsList) - 1 +// key, u.savePointsList = u.savePointsList[popIndex], u.savePointsList[:popIndex] +// delete(u.savePointsSet, hex.EncodeToString(key)) +// if err := u.store.RollbackToSavePoint(key); err != nil { +// return typesUtil.ErrRollbackSavePoint(err) +// } +// return nil +// } -//nolint:unused // TODO: This has not been tested or investigated in detail -func (u *utilityContext) newSavePoint(txHashBz []byte) typesUtil.Error { - if err := u.store.NewSavePoint(txHashBz); err != nil { - return typesUtil.ErrNewSavePoint(err) - } - txHash := hex.EncodeToString(txHashBz) - if _, exists := u.savePointsSet[txHash]; exists { - return typesUtil.ErrDuplicateSavePoint() - } - u.savePointsList = append(u.savePointsList, txHashBz) - u.savePointsSet[txHash] = struct{}{} - return nil -} +// //nolint:unused // TODO: This has not been tested or investigated in detail +// func (u *utilityContext) newSavePoint(txHashBz []byte) typesUtil.Error { +// if err := u.store.NewSavePoint(txHashBz); err != nil { +// return typesUtil.ErrNewSavePoint(err) +// } +// txHash := hex.EncodeToString(txHashBz) +// if _, exists := u.savePointsSet[txHash]; exists { +// return typesUtil.ErrDuplicateSavePoint() +// } +// u.savePointsList = append(u.savePointsList, txHashBz) +// u.savePointsSet[txHash] = struct{}{} +// return nil +// } diff --git a/utility/gov.go b/utility/gov.go deleted file mode 100644 index e267c4f91..000000000 --- a/utility/gov.go +++ /dev/null @@ -1,576 +0,0 @@ -package utility - -import ( - "math/big" - - coreTypes "github.com/pokt-network/pocket/shared/core/types" - "github.com/pokt-network/pocket/shared/utils" - typesUtil "github.com/pokt-network/pocket/utility/types" - "google.golang.org/protobuf/types/known/wrapperspb" -) - -func (u *utilityContext) updateParam(paramName string, value any) typesUtil.Error { - switch t := value.(type) { - case *wrapperspb.Int32Value: - if err := u.store.SetParam(paramName, (int(t.Value))); err != nil { - return typesUtil.ErrUpdateParam(err) - } - return nil - case *wrapperspb.StringValue: - if err := u.store.SetParam(paramName, t.Value); err != nil { - return typesUtil.ErrUpdateParam(err) - } - return nil - case *wrapperspb.BytesValue: - if err := u.store.SetParam(paramName, t.Value); err != nil { - return typesUtil.ErrUpdateParam(err) - } - return nil - default: - break - } - u.logger.Fatal().Msgf("unhandled value type %T for %v", value, value) - return typesUtil.ErrUnknownParam(paramName) -} - -func (u *utilityContext) getParameter(paramName string) (any, error) { - return u.store.GetParameter(paramName, u.height) -} - -func (u *utilityContext) getAppMinimumStake() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.AppMinimumStakeParamName) -} - -func (u *utilityContext) getAppMaxChains() (int, typesUtil.Error) { - return u.getIntParam(typesUtil.AppMaxChainsParamName) -} - -func (u *utilityContext) getAppSessionTokensMultiplier() (int, typesUtil.Error) { - return u.getIntParam(typesUtil.AppSessionTokensMultiplierParamName) -} - -func (u *utilityContext) getAppUnstakingBlocks() (int64, typesUtil.Error) { - return u.getInt64Param(typesUtil.AppUnstakingBlocksParamName) -} - -func (u *utilityContext) getAppMinimumPauseBlocks() (int, typesUtil.Error) { - return u.getIntParam(typesUtil.AppMinimumPauseBlocksParamName) -} - -func (u *utilityContext) getAppMaxPausedBlocks() (maxPausedBlocks int, err typesUtil.Error) { - return u.getIntParam(typesUtil.AppMaxPauseBlocksParamName) -} - -func (u *utilityContext) getServicerMinimumStake() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.ServicerMinimumStakeParamName) -} - -func (u *utilityContext) getServicerMaxChains() (int, typesUtil.Error) { - return u.getIntParam(typesUtil.ServicerMaxChainsParamName) -} - -func (u *utilityContext) getServicerUnstakingBlocks() (int64, typesUtil.Error) { - return u.getInt64Param(typesUtil.ServicerUnstakingBlocksParamName) -} - -func (u *utilityContext) getServicerMinimumPauseBlocks() (int, typesUtil.Error) { - return u.getIntParam(typesUtil.ServicerMinimumPauseBlocksParamName) -} - -func (u *utilityContext) getServicerMaxPausedBlocks() (maxPausedBlocks int, err typesUtil.Error) { - return u.getIntParam(typesUtil.ServicerMaxPauseBlocksParamName) -} - -func (u *utilityContext) getValidatorMinimumStake() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.ValidatorMinimumStakeParamName) -} - -func (u *utilityContext) getValidatorUnstakingBlocks() (int64, typesUtil.Error) { - return u.getInt64Param(typesUtil.ValidatorUnstakingBlocksParamName) -} - -func (u *utilityContext) getValidatorMinimumPauseBlocks() (int, typesUtil.Error) { - return u.getIntParam(typesUtil.ValidatorMinimumPauseBlocksParamName) -} - -func (u *utilityContext) getValidatorMaxPausedBlocks() (maxPausedBlocks int, err typesUtil.Error) { - return u.getIntParam(typesUtil.ValidatorMaxPausedBlocksParamName) -} - -func (u *utilityContext) getProposerPercentageOfFees() (proposerPercentage int, err typesUtil.Error) { - return u.getIntParam(typesUtil.ProposerPercentageOfFeesParamName) -} - -func (u *utilityContext) getValidatorMaxMissedBlocks() (maxMissedBlocks int, err typesUtil.Error) { - return u.getIntParam(typesUtil.ValidatorMaximumMissedBlocksParamName) -} - -func (u *utilityContext) getMaxEvidenceAgeInBlocks() (maxMissedBlocks int, err typesUtil.Error) { - return u.getIntParam(typesUtil.ValidatorMaxEvidenceAgeInBlocksParamName) -} - -func (u *utilityContext) getDoubleSignBurnPercentage() (burnPercentage int, err typesUtil.Error) { - return u.getIntParam(typesUtil.DoubleSignBurnPercentageParamName) -} - -func (u *utilityContext) getMissedBlocksBurnPercentage() (burnPercentage int, err typesUtil.Error) { - return u.getIntParam(typesUtil.MissedBlocksBurnPercentageParamName) -} - -func (u *utilityContext) getFishermanMinimumStake() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.FishermanMinimumStakeParamName) -} - -func (u *utilityContext) getFishermanMaxChains() (int, typesUtil.Error) { - return u.getIntParam(typesUtil.FishermanMaxChainsParamName) -} - -func (u *utilityContext) getFishermanUnstakingBlocks() (int64, typesUtil.Error) { - return u.getInt64Param(typesUtil.FishermanUnstakingBlocksParamName) -} - -func (u *utilityContext) getFishermanMinimumPauseBlocks() (int, typesUtil.Error) { - return u.getIntParam(typesUtil.FishermanMinimumPauseBlocksParamName) -} - -func (u *utilityContext) getFishermanMaxPausedBlocks() (maxPausedBlocks int, err typesUtil.Error) { - return u.getIntParam(typesUtil.FishermanMaxPauseBlocksParamName) -} - -func (u *utilityContext) getMessageDoubleSignFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageDoubleSignFee) -} - -func (u *utilityContext) getMessageSendFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageSendFee) -} - -func (u *utilityContext) getMessageStakeFishermanFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageStakeFishermanFee) -} - -func (u *utilityContext) getMessageEditStakeFishermanFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageEditStakeFishermanFee) -} - -func (u *utilityContext) getMessageUnstakeFishermanFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageUnstakeFishermanFee) -} - -func (u *utilityContext) getMessagePauseFishermanFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessagePauseFishermanFee) -} - -func (u *utilityContext) getMessageUnpauseFishermanFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageUnpauseFishermanFee) -} - -func (u *utilityContext) getMessageFishermanPauseServicerFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageFishermanPauseServicerFee) -} - -func (u *utilityContext) getMessageTestScoreFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageTestScoreFee) -} - -func (u *utilityContext) getMessageProveTestScoreFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageProveTestScoreFee) -} - -func (u *utilityContext) getMessageStakeAppFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageStakeAppFee) -} - -func (u *utilityContext) getMessageEditStakeAppFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageEditStakeAppFee) -} - -func (u *utilityContext) getMessageUnstakeAppFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageUnstakeAppFee) -} - -func (u *utilityContext) getMessagePauseAppFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessagePauseAppFee) -} - -func (u *utilityContext) getMessageUnpauseAppFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageUnpauseAppFee) -} - -func (u *utilityContext) getMessageStakeValidatorFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageStakeValidatorFee) -} - -func (u *utilityContext) getMessageEditStakeValidatorFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageEditStakeValidatorFee) -} - -func (u *utilityContext) getMessageUnstakeValidatorFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageUnstakeValidatorFee) -} - -func (u *utilityContext) getMessagePauseValidatorFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessagePauseValidatorFee) -} - -func (u *utilityContext) getMessageUnpauseValidatorFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageUnpauseValidatorFee) -} - -func (u *utilityContext) getMessageStakeServicerFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageStakeServicerFee) -} - -func (u *utilityContext) getMessageEditStakeServicerFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageEditStakeServicerFee) -} - -func (u *utilityContext) getMessageUnstakeServicerFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageUnstakeServicerFee) -} - -func (u *utilityContext) getMessagePauseServicerFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessagePauseServicerFee) -} - -func (u *utilityContext) getMessageUnpauseServicerFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageUnpauseServicerFee) -} - -func (u *utilityContext) getMessageChangeParameterFee() (*big.Int, typesUtil.Error) { - return u.getBigIntParam(typesUtil.MessageChangeParameterFee) -} - -func (u *utilityContext) getDoubleSignFeeOwner() (owner []byte, err typesUtil.Error) { - return u.getByteArrayParam(typesUtil.MessageDoubleSignFeeOwner) -} - -func (u *utilityContext) getParamOwner(paramName string) ([]byte, error) { - // DISCUSS (@deblasis): here we could potentially leverage the struct tags in gov.proto by specifying an `owner` key - // eg: `app_minimum_stake` could have `pokt:"owner=app_minimum_stake_owner"` - // in here we would use that map to point to the owner, removing this switch, centralizing the logic and making it declarative - switch paramName { - case typesUtil.AclOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.BlocksPerSessionParamName: - return u.store.GetBytesParam(typesUtil.BlocksPerSessionOwner, u.height) - case typesUtil.AppMaxChainsParamName: - return u.store.GetBytesParam(typesUtil.AppMaxChainsOwner, u.height) - case typesUtil.AppMinimumStakeParamName: - return u.store.GetBytesParam(typesUtil.AppMinimumStakeOwner, u.height) - case typesUtil.AppSessionTokensMultiplierParamName: - return u.store.GetBytesParam(typesUtil.AppSessionTokensMultiplierOwner, u.height) - case typesUtil.AppUnstakingBlocksParamName: - return u.store.GetBytesParam(typesUtil.AppUnstakingBlocksOwner, u.height) - case typesUtil.AppMinimumPauseBlocksParamName: - return u.store.GetBytesParam(typesUtil.AppMinimumPauseBlocksOwner, u.height) - case typesUtil.AppMaxPauseBlocksParamName: - return u.store.GetBytesParam(typesUtil.AppMaxPausedBlocksOwner, u.height) - case typesUtil.ServicersPerSessionParamName: - return u.store.GetBytesParam(typesUtil.ServicersPerSessionOwner, u.height) - case typesUtil.ServicerMinimumStakeParamName: - return u.store.GetBytesParam(typesUtil.ServicerMinimumStakeOwner, u.height) - case typesUtil.ServicerMaxChainsParamName: - return u.store.GetBytesParam(typesUtil.ServicerMaxChainsOwner, u.height) - case typesUtil.ServicerUnstakingBlocksParamName: - return u.store.GetBytesParam(typesUtil.ServicerUnstakingBlocksOwner, u.height) - case typesUtil.ServicerMinimumPauseBlocksParamName: - return u.store.GetBytesParam(typesUtil.ServicerMinimumPauseBlocksOwner, u.height) - case typesUtil.ServicerMaxPauseBlocksParamName: - return u.store.GetBytesParam(typesUtil.ServicerMaxPausedBlocksOwner, u.height) - case typesUtil.FishermanMinimumStakeParamName: - return u.store.GetBytesParam(typesUtil.FishermanMinimumStakeOwner, u.height) - case typesUtil.FishermanMaxChainsParamName: - return u.store.GetBytesParam(typesUtil.FishermanMaxChainsOwner, u.height) - case typesUtil.FishermanUnstakingBlocksParamName: - return u.store.GetBytesParam(typesUtil.FishermanUnstakingBlocksOwner, u.height) - case typesUtil.FishermanMinimumPauseBlocksParamName: - return u.store.GetBytesParam(typesUtil.FishermanMinimumPauseBlocksOwner, u.height) - case typesUtil.FishermanMaxPauseBlocksParamName: - return u.store.GetBytesParam(typesUtil.FishermanMaxPausedBlocksOwner, u.height) - case typesUtil.ValidatorMinimumStakeParamName: - return u.store.GetBytesParam(typesUtil.ValidatorMinimumStakeOwner, u.height) - case typesUtil.ValidatorUnstakingBlocksParamName: - return u.store.GetBytesParam(typesUtil.ValidatorUnstakingBlocksOwner, u.height) - case typesUtil.ValidatorMinimumPauseBlocksParamName: - return u.store.GetBytesParam(typesUtil.ValidatorMinimumPauseBlocksOwner, u.height) - case typesUtil.ValidatorMaxPausedBlocksParamName: - return u.store.GetBytesParam(typesUtil.ValidatorMaxPausedBlocksOwner, u.height) - case typesUtil.ValidatorMaximumMissedBlocksParamName: - return u.store.GetBytesParam(typesUtil.ValidatorMaximumMissedBlocksOwner, u.height) - case typesUtil.ProposerPercentageOfFeesParamName: - return u.store.GetBytesParam(typesUtil.ProposerPercentageOfFeesOwner, u.height) - case typesUtil.ValidatorMaxEvidenceAgeInBlocksParamName: - return u.store.GetBytesParam(typesUtil.ValidatorMaxEvidenceAgeInBlocksOwner, u.height) - case typesUtil.MissedBlocksBurnPercentageParamName: - return u.store.GetBytesParam(typesUtil.MissedBlocksBurnPercentageOwner, u.height) - case typesUtil.DoubleSignBurnPercentageParamName: - return u.store.GetBytesParam(typesUtil.DoubleSignBurnPercentageOwner, u.height) - case typesUtil.MessageDoubleSignFee: - return u.store.GetBytesParam(typesUtil.MessageDoubleSignFeeOwner, u.height) - case typesUtil.MessageSendFee: - return u.store.GetBytesParam(typesUtil.MessageSendFeeOwner, u.height) - case typesUtil.MessageStakeFishermanFee: - return u.store.GetBytesParam(typesUtil.MessageStakeFishermanFeeOwner, u.height) - case typesUtil.MessageEditStakeFishermanFee: - return u.store.GetBytesParam(typesUtil.MessageEditStakeFishermanFeeOwner, u.height) - case typesUtil.MessageUnstakeFishermanFee: - return u.store.GetBytesParam(typesUtil.MessageUnstakeFishermanFeeOwner, u.height) - case typesUtil.MessagePauseFishermanFee: - return u.store.GetBytesParam(typesUtil.MessagePauseFishermanFeeOwner, u.height) - case typesUtil.MessageUnpauseFishermanFee: - return u.store.GetBytesParam(typesUtil.MessageUnpauseFishermanFeeOwner, u.height) - case typesUtil.MessageFishermanPauseServicerFee: - return u.store.GetBytesParam(typesUtil.MessageFishermanPauseServicerFeeOwner, u.height) - case typesUtil.MessageTestScoreFee: - return u.store.GetBytesParam(typesUtil.MessageTestScoreFeeOwner, u.height) - case typesUtil.MessageProveTestScoreFee: - return u.store.GetBytesParam(typesUtil.MessageProveTestScoreFeeOwner, u.height) - case typesUtil.MessageStakeAppFee: - return u.store.GetBytesParam(typesUtil.MessageStakeAppFeeOwner, u.height) - case typesUtil.MessageEditStakeAppFee: - return u.store.GetBytesParam(typesUtil.MessageEditStakeAppFeeOwner, u.height) - case typesUtil.MessageUnstakeAppFee: - return u.store.GetBytesParam(typesUtil.MessageUnstakeAppFeeOwner, u.height) - case typesUtil.MessagePauseAppFee: - return u.store.GetBytesParam(typesUtil.MessagePauseAppFeeOwner, u.height) - case typesUtil.MessageUnpauseAppFee: - return u.store.GetBytesParam(typesUtil.MessageUnpauseAppFeeOwner, u.height) - case typesUtil.MessageStakeValidatorFee: - return u.store.GetBytesParam(typesUtil.MessageStakeValidatorFeeOwner, u.height) - case typesUtil.MessageEditStakeValidatorFee: - return u.store.GetBytesParam(typesUtil.MessageEditStakeValidatorFeeOwner, u.height) - case typesUtil.MessageUnstakeValidatorFee: - return u.store.GetBytesParam(typesUtil.MessageUnstakeValidatorFeeOwner, u.height) - case typesUtil.MessagePauseValidatorFee: - return u.store.GetBytesParam(typesUtil.MessagePauseValidatorFeeOwner, u.height) - case typesUtil.MessageUnpauseValidatorFee: - return u.store.GetBytesParam(typesUtil.MessageUnpauseValidatorFeeOwner, u.height) - case typesUtil.MessageStakeServicerFee: - return u.store.GetBytesParam(typesUtil.MessageStakeServicerFeeOwner, u.height) - case typesUtil.MessageEditStakeServicerFee: - return u.store.GetBytesParam(typesUtil.MessageEditStakeServicerFeeOwner, u.height) - case typesUtil.MessageUnstakeServicerFee: - return u.store.GetBytesParam(typesUtil.MessageUnstakeServicerFeeOwner, u.height) - case typesUtil.MessagePauseServicerFee: - return u.store.GetBytesParam(typesUtil.MessagePauseServicerFeeOwner, u.height) - case typesUtil.MessageUnpauseServicerFee: - return u.store.GetBytesParam(typesUtil.MessageUnpauseServicerFeeOwner, u.height) - case typesUtil.MessageChangeParameterFee: - return u.store.GetBytesParam(typesUtil.MessageChangeParameterFeeOwner, u.height) - case typesUtil.BlocksPerSessionOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.AppMaxChainsOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.AppMinimumStakeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.AppSessionTokensMultiplierOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.AppUnstakingBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.AppMinimumPauseBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.AppMaxPausedBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ServicerMinimumStakeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ServicerMaxChainsOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ServicerUnstakingBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ServicerMinimumPauseBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ServicerMaxPausedBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ServicersPerSessionOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.FishermanMinimumStakeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.FishermanMaxChainsOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.FishermanUnstakingBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.FishermanMinimumPauseBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.FishermanMaxPausedBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ValidatorMinimumStakeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ValidatorUnstakingBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ValidatorMinimumPauseBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ValidatorMaxPausedBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ValidatorMaximumMissedBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ProposerPercentageOfFeesOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.ValidatorMaxEvidenceAgeInBlocksOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MissedBlocksBurnPercentageOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.DoubleSignBurnPercentageOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageSendFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageStakeFishermanFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageEditStakeFishermanFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageUnstakeFishermanFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessagePauseFishermanFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageUnpauseFishermanFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageFishermanPauseServicerFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageTestScoreFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageProveTestScoreFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageStakeAppFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageEditStakeAppFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageUnstakeAppFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessagePauseAppFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageUnpauseAppFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageStakeValidatorFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageEditStakeValidatorFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageUnstakeValidatorFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessagePauseValidatorFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageUnpauseValidatorFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageStakeServicerFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageEditStakeServicerFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageUnstakeServicerFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessagePauseServicerFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageUnpauseServicerFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - case typesUtil.MessageChangeParameterFeeOwner: - return u.store.GetBytesParam(typesUtil.AclOwner, u.height) - default: - return nil, typesUtil.ErrUnknownParam(paramName) - } -} - -func (u *utilityContext) getFee(msg typesUtil.Message, actorType coreTypes.ActorType) (amount *big.Int, err typesUtil.Error) { - switch x := msg.(type) { - case *typesUtil.MessageSend: - return u.getMessageSendFee() - case *typesUtil.MessageStake: - switch actorType { - case coreTypes.ActorType_ACTOR_TYPE_APP: - return u.getMessageStakeAppFee() - case coreTypes.ActorType_ACTOR_TYPE_FISH: - return u.getMessageStakeFishermanFee() - case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - return u.getMessageStakeServicerFee() - case coreTypes.ActorType_ACTOR_TYPE_VAL: - return u.getMessageStakeValidatorFee() - default: - return nil, typesUtil.ErrUnknownActorType(actorType.String()) - } - case *typesUtil.MessageEditStake: - switch actorType { - case coreTypes.ActorType_ACTOR_TYPE_APP: - return u.getMessageEditStakeAppFee() - case coreTypes.ActorType_ACTOR_TYPE_FISH: - return u.getMessageEditStakeFishermanFee() - case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - return u.getMessageEditStakeServicerFee() - case coreTypes.ActorType_ACTOR_TYPE_VAL: - return u.getMessageEditStakeValidatorFee() - default: - return nil, typesUtil.ErrUnknownActorType(actorType.String()) - } - case *typesUtil.MessageUnstake: - switch actorType { - case coreTypes.ActorType_ACTOR_TYPE_APP: - return u.getMessageUnstakeAppFee() - case coreTypes.ActorType_ACTOR_TYPE_FISH: - return u.getMessageUnstakeFishermanFee() - case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - return u.getMessageUnstakeServicerFee() - case coreTypes.ActorType_ACTOR_TYPE_VAL: - return u.getMessageUnstakeValidatorFee() - default: - return nil, typesUtil.ErrUnknownActorType(actorType.String()) - } - case *typesUtil.MessageUnpause: - switch actorType { - case coreTypes.ActorType_ACTOR_TYPE_APP: - return u.getMessageUnpauseAppFee() - case coreTypes.ActorType_ACTOR_TYPE_FISH: - return u.getMessageUnpauseFishermanFee() - case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - return u.getMessageUnpauseServicerFee() - case coreTypes.ActorType_ACTOR_TYPE_VAL: - return u.getMessageUnpauseValidatorFee() - default: - return nil, typesUtil.ErrUnknownActorType(actorType.String()) - } - case *typesUtil.MessageChangeParameter: - return u.getMessageChangeParameterFee() - default: - return nil, typesUtil.ErrUnknownMessage(x) - } -} - -func (u *utilityContext) getMessageChangeParameterSignerCandidates(msg *typesUtil.MessageChangeParameter) ([][]byte, typesUtil.Error) { - owner, err := u.getParamOwner(msg.ParameterKey) - if err != nil { - return nil, typesUtil.ErrGetParam(msg.ParameterKey, err) - } - return [][]byte{owner}, nil -} - -func (u *utilityContext) getBigIntParam(paramName string) (*big.Int, typesUtil.Error) { - value, err := u.store.GetStringParam(paramName, u.height) - if err != nil { - u.logger.Err(err) - return nil, typesUtil.ErrGetParam(paramName, err) - } - amount, err := utils.StringToBigInt(value) - if err != nil { - return nil, typesUtil.ErrStringToBigInt(err) - } - return amount, nil -} - -func (u *utilityContext) getIntParam(paramName string) (int, typesUtil.Error) { - value, err := u.store.GetIntParam(paramName, u.height) - if err != nil { - return 0, typesUtil.ErrGetParam(paramName, err) - } - return value, nil -} - -func (u *utilityContext) getInt64Param(paramName string) (int64, typesUtil.Error) { - value, err := u.store.GetIntParam(paramName, u.height) - if err != nil { - return 0, typesUtil.ErrGetParam(paramName, err) - } - return int64(value), nil -} - -func (u *utilityContext) getByteArrayParam(paramName string) ([]byte, typesUtil.Error) { - value, err := u.store.GetBytesParam(paramName, u.height) - if err != nil { - return nil, typesUtil.ErrGetParam(paramName, err) - } - return value, nil -} diff --git a/utility/transaction.go b/utility/transaction.go index b553fe6c5..cc0f96241 100644 --- a/utility/transaction.go +++ b/utility/transaction.go @@ -1,13 +1,8 @@ package utility import ( - "bytes" - "fmt" - "github.com/pokt-network/pocket/shared/codec" coreTypes "github.com/pokt-network/pocket/shared/core/types" - "github.com/pokt-network/pocket/shared/crypto" - "github.com/pokt-network/pocket/shared/modules" typesUtil "github.com/pokt-network/pocket/utility/types" ) @@ -41,82 +36,3 @@ func (u *utilityModule) HandleTransaction(txProtoBytes []byte) error { // Store the tx in the mempool return u.mempool.AddTx(txProtoBytes) } - -// hydrateTxResult converts a `Transaction` proto into a `TxResult` struct` after doing basic validation -// and extracting the relevant data from the embedded signed Message. `index` is the intended location -// of its index (i.e. the transaction number) in the block where it is included. -// -// IMPROVE: hydration should accept and return the same type (i.e. TxResult) so there may be opportunity -// to refactor this in the future. -func (u *utilityContext) hydrateTxResult(tx *coreTypes.Transaction, index int) (modules.TxResult, typesUtil.Error) { - msg, err := u.anteHandleMessage(tx) - if err != nil { - return nil, err - } - msgHandlingResult := u.handleMessage(msg) - return typesUtil.TxToTxResult(tx, u.height, index, msg, msgHandlingResult) -} - -// anteHandleMessage handles basic validation of the message in the Transaction before it is processed -// REFACTOR: Splitting this into a `feeValidation`, `signerValidation`, and `messageValidation` etc -// would make it more modular and readable. -func (u *utilityContext) anteHandleMessage(tx *coreTypes.Transaction) (typesUtil.Message, typesUtil.Error) { - // Check if the transaction has a valid message - anyMsg, er := tx.GetMessage() - if er != nil { - return nil, typesUtil.ErrDecodeMessage(er) - } - msg, ok := anyMsg.(typesUtil.Message) - if !ok { - return nil, typesUtil.ErrDecodeMessage(fmt.Errorf("not a supported message type")) - } - - // Get the address of the transaction signer - pubKey, er := crypto.NewPublicKeyFromBytes(tx.Signature.PublicKey) - if er != nil { - return nil, typesUtil.ErrNewPublicKeyFromBytes(er) - } - address := pubKey.Address() - addressHex := address.ToString() - - // Validate that the signer has enough funds to pay the fee of the message signed - fee, err := u.getFee(msg, msg.GetActorType()) - if err != nil { - return nil, err - } - accountAmount, err := u.getAccountAmount(address) - if err != nil { - return nil, typesUtil.ErrGetAccountAmount(err) - } - accountAmount.Sub(accountAmount, fee) - if accountAmount.Sign() == -1 { - return nil, typesUtil.ErrInsufficientAmount(addressHex) - } - - // Validate that the signer has a valid signature - var isValidSigner bool - signerCandidates, err := u.getSignerCandidates(msg) - if err != nil { - return nil, err - } - for _, candidate := range signerCandidates { - if bytes.Equal(candidate, address) { - isValidSigner = true - msg.SetSigner(address) - break - } - } - if !isValidSigner { - return nil, typesUtil.ErrInvalidSigner(addressHex) - } - - // Remove the fee from the signer's account and add it to the fee collector pool - if err := u.setAccountAmount(address, accountAmount); err != nil { - return nil, err - } - if err := u.addPoolAmount(coreTypes.Pools_POOLS_FEE_COLLECTOR.FriendlyName(), fee); err != nil { - return nil, err - } - - return msg, nil -} diff --git a/utility/unit_of_work/account.go b/utility/unit_of_work/account.go new file mode 100644 index 000000000..7af3888e3 --- /dev/null +++ b/utility/unit_of_work/account.go @@ -0,0 +1,92 @@ +package unit_of_work + +// Internal business logic for `Accounts` & `Pools` (i.e. autonomous accounts owned by the protocol) +// +// Accounts are utility module structures that resemble currency holding vehicles; e.g. a bank account. +// Pools are autonomous accounts owned by the protocol; e.g. an account for a fee pool that gets distributed + +import ( + "math/big" + + "github.com/pokt-network/pocket/shared/utils" + "github.com/pokt-network/pocket/utility/types" +) + +// Accounts specific functionality + +func (u *baseUtilityUnitOfWork) getAccountAmount(address []byte) (*big.Int, types.Error) { + amountStr, err := u.persistenceReadContext.GetAccountAmount(address, u.height) + if err != nil { + return nil, types.ErrGetAccountAmount(err) + } + amount, err := utils.StringToBigInt(amountStr) + if err != nil { + return nil, types.ErrStringToBigInt(err) + } + return amount, nil +} + +func (u *baseUtilityUnitOfWork) addAccountAmount(address []byte, amountToAdd *big.Int) types.Error { + if err := u.persistenceRWContext.AddAccountAmount(address, utils.BigIntToString(amountToAdd)); err != nil { + return types.ErrAddAccountAmount(err) + } + return nil +} + +func (u *baseUtilityUnitOfWork) subtractAccountAmount(address []byte, amountToSubtract *big.Int) types.Error { + if err := u.persistenceRWContext.SubtractAccountAmount(address, utils.BigIntToString(amountToSubtract)); err != nil { + return types.ErrSetAccountAmount(err) + } + return nil +} + +func (u *baseUtilityUnitOfWork) setAccountAmount(address []byte, amount *big.Int) types.Error { + if err := u.persistenceRWContext.SetAccountAmount(address, utils.BigIntToString(amount)); err != nil { + return types.ErrSetAccountAmount(err) + } + return nil +} + +// Pools specific functionality + +// IMPROVE: Pool function should accept the actual pool types rather than the `FriendlyName` string + +func (u *baseUtilityUnitOfWork) insertPool(name string, amount *big.Int) types.Error { + if err := u.persistenceRWContext.InsertPool(name, utils.BigIntToString(amount)); err != nil { + return types.ErrSetPool(name, err) + } + return nil +} + +func (u *baseUtilityUnitOfWork) getPoolAmount(name string) (*big.Int, types.Error) { + amountStr, err := u.persistenceReadContext.GetPoolAmount(name, u.height) + if err != nil { + return nil, types.ErrGetPoolAmount(name, err) + } + amount, err := utils.StringToBigInt(amountStr) + if err != nil { + return nil, types.ErrStringToBigInt(err) + } + return amount, nil +} + +func (u *baseUtilityUnitOfWork) addPoolAmount(name string, amountToAdd *big.Int) types.Error { + if err := u.persistenceRWContext.AddPoolAmount(name, utils.BigIntToString(amountToAdd)); err != nil { + return types.ErrAddPoolAmount(name, err) + } + return nil +} + +func (u *baseUtilityUnitOfWork) subPoolAmount(name string, amountToSub *big.Int) types.Error { + if err := u.persistenceRWContext.SubtractPoolAmount(name, utils.BigIntToString(amountToSub)); err != nil { + return types.ErrSubPoolAmount(name, err) + } + return nil +} + +func (u *baseUtilityUnitOfWork) setPoolAmount(name string, amount *big.Int) types.Error { + if err := u.persistenceRWContext.SetPoolAmount(name, utils.BigIntToString(amount)); err != nil { + return types.ErrSetPoolAmount(name, err) + } + return nil +} diff --git a/utility/account_test.go b/utility/unit_of_work/account_test.go similarity index 89% rename from utility/account_test.go rename to utility/unit_of_work/account_test.go index ab17d394a..cf90f50a6 100644 --- a/utility/account_test.go +++ b/utility/unit_of_work/account_test.go @@ -1,4 +1,4 @@ -package utility +package unit_of_work import ( "encoding/hex" @@ -129,8 +129,8 @@ func TestUtilityContext_SubPoolAmount(t *testing.T) { require.Equal(t, expected, amount) } -func getAllTestingAccounts(t *testing.T, ctx *utilityContext) []*coreTypes.Account { - accs, err := ctx.store.GetAllAccounts(0) +func getAllTestingAccounts(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.Account { + accs, err := ctx.persistenceReadContext.GetAllAccounts(0) require.NoError(t, err) sort.Slice(accs, func(i, j int) bool { @@ -139,12 +139,12 @@ func getAllTestingAccounts(t *testing.T, ctx *utilityContext) []*coreTypes.Accou return accs } -func getFirstTestingAccount(t *testing.T, ctx *utilityContext) *coreTypes.Account { +func getFirstTestingAccount(t *testing.T, ctx *baseUtilityUnitOfWork) *coreTypes.Account { return getAllTestingAccounts(t, ctx)[0] } -func getAllTestingPools(t *testing.T, ctx *utilityContext) []*coreTypes.Account { - pools, err := ctx.store.GetAllPools(0) +func getAllTestingPools(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.Account { + pools, err := ctx.persistenceReadContext.GetAllPools(0) require.NoError(t, err) sort.Slice(pools, func(i, j int) bool { @@ -153,6 +153,6 @@ func getAllTestingPools(t *testing.T, ctx *utilityContext) []*coreTypes.Account return pools } -func getFirstTestingPool(t *testing.T, ctx *utilityContext) *coreTypes.Account { +func getFirstTestingPool(t *testing.T, ctx *baseUtilityUnitOfWork) *coreTypes.Account { return getAllTestingPools(t, ctx)[0] } diff --git a/utility/actor.go b/utility/unit_of_work/actor.go similarity index 60% rename from utility/actor.go rename to utility/unit_of_work/actor.go index 9147bd576..32aa47f6e 100644 --- a/utility/actor.go +++ b/utility/unit_of_work/actor.go @@ -1,4 +1,4 @@ -package utility +package unit_of_work // Internal business logic for functionality shared across all `Actors`. // @@ -15,19 +15,19 @@ import ( // Actor setters -func (u *utilityContext) setActorStakeAmount(actorType coreTypes.ActorType, addr []byte, amount *big.Int) typesUtil.Error { +func (u *baseUtilityUnitOfWork) setActorStakeAmount(actorType coreTypes.ActorType, addr []byte, amount *big.Int) typesUtil.Error { amountStr := utils.BigIntToString(amount) var err error switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: - err = u.store.SetAppStakeAmount(addr, amountStr) + err = u.persistenceRWContext.SetAppStakeAmount(addr, amountStr) case coreTypes.ActorType_ACTOR_TYPE_FISH: - err = u.store.SetFishermanStakeAmount(addr, amountStr) + err = u.persistenceRWContext.SetFishermanStakeAmount(addr, amountStr) case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - err = u.store.SetServicerStakeAmount(addr, amountStr) + err = u.persistenceRWContext.SetServicerStakeAmount(addr, amountStr) case coreTypes.ActorType_ACTOR_TYPE_VAL: - err = u.store.SetValidatorStakeAmount(addr, amountStr) + err = u.persistenceRWContext.SetValidatorStakeAmount(addr, amountStr) default: err = typesUtil.ErrUnknownActorType(actorType.String()) } @@ -38,17 +38,17 @@ func (u *utilityContext) setActorStakeAmount(actorType coreTypes.ActorType, addr return nil } -func (u *utilityContext) setActorUnbondingHeight(actorType coreTypes.ActorType, addr []byte, height int64) typesUtil.Error { +func (u *baseUtilityUnitOfWork) setActorUnbondingHeight(actorType coreTypes.ActorType, addr []byte, height int64) typesUtil.Error { var err error switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: - err = u.store.SetAppUnstakingHeightAndStatus(addr, height, int32(coreTypes.StakeStatus_Unstaking)) + err = u.persistenceRWContext.SetAppUnstakingHeightAndStatus(addr, height, int32(coreTypes.StakeStatus_Unstaking)) case coreTypes.ActorType_ACTOR_TYPE_FISH: - err = u.store.SetFishermanUnstakingHeightAndStatus(addr, height, int32(coreTypes.StakeStatus_Unstaking)) + err = u.persistenceRWContext.SetFishermanUnstakingHeightAndStatus(addr, height, int32(coreTypes.StakeStatus_Unstaking)) case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - err = u.store.SetServicerUnstakingHeightAndStatus(addr, height, int32(coreTypes.StakeStatus_Unstaking)) + err = u.persistenceRWContext.SetServicerUnstakingHeightAndStatus(addr, height, int32(coreTypes.StakeStatus_Unstaking)) case coreTypes.ActorType_ACTOR_TYPE_VAL: - err = u.store.SetValidatorUnstakingHeightAndStatus(addr, height, int32(coreTypes.StakeStatus_Unstaking)) + err = u.persistenceRWContext.SetValidatorUnstakingHeightAndStatus(addr, height, int32(coreTypes.StakeStatus_Unstaking)) default: err = typesUtil.ErrUnknownActorType(actorType.String()) } @@ -59,17 +59,17 @@ func (u *utilityContext) setActorUnbondingHeight(actorType coreTypes.ActorType, return nil } -func (u *utilityContext) setActorPausedHeight(actorType coreTypes.ActorType, addr []byte, height int64) typesUtil.Error { +func (u *baseUtilityUnitOfWork) setActorPausedHeight(actorType coreTypes.ActorType, addr []byte, height int64) typesUtil.Error { var err error switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: - err = u.store.SetAppPauseHeight(addr, height) + err = u.persistenceRWContext.SetAppPauseHeight(addr, height) case coreTypes.ActorType_ACTOR_TYPE_FISH: - err = u.store.SetFishermanPauseHeight(addr, height) + err = u.persistenceRWContext.SetFishermanPauseHeight(addr, height) case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - err = u.store.SetServicerPauseHeight(addr, height) + err = u.persistenceRWContext.SetServicerPauseHeight(addr, height) case coreTypes.ActorType_ACTOR_TYPE_VAL: - err = u.store.SetValidatorPauseHeight(addr, height) + err = u.persistenceRWContext.SetValidatorPauseHeight(addr, height) default: err = typesUtil.ErrUnknownActorType(actorType.String()) } @@ -82,19 +82,19 @@ func (u *utilityContext) setActorPausedHeight(actorType coreTypes.ActorType, add // Actor getters -func (u *utilityContext) getActorStakeAmount(actorType coreTypes.ActorType, addr []byte) (*big.Int, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getActorStakeAmount(actorType coreTypes.ActorType, addr []byte) (*big.Int, typesUtil.Error) { var stakeAmount string var err error switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: - stakeAmount, err = u.store.GetAppStakeAmount(u.height, addr) + stakeAmount, err = u.persistenceReadContext.GetAppStakeAmount(u.height, addr) case coreTypes.ActorType_ACTOR_TYPE_FISH: - stakeAmount, err = u.store.GetFishermanStakeAmount(u.height, addr) + stakeAmount, err = u.persistenceReadContext.GetFishermanStakeAmount(u.height, addr) case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - stakeAmount, err = u.store.GetServicerStakeAmount(u.height, addr) + stakeAmount, err = u.persistenceReadContext.GetServicerStakeAmount(u.height, addr) case coreTypes.ActorType_ACTOR_TYPE_VAL: - stakeAmount, err = u.store.GetValidatorStakeAmount(u.height, addr) + stakeAmount, err = u.persistenceReadContext.GetValidatorStakeAmount(u.height, addr) default: err = typesUtil.ErrUnknownActorType(actorType.String()) } @@ -111,7 +111,7 @@ func (u *utilityContext) getActorStakeAmount(actorType coreTypes.ActorType, addr return amount, nil } -func (u *utilityContext) getMaxAllowedPausedBlocks(actorType coreTypes.ActorType) (int, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getMaxAllowedPausedBlocks(actorType coreTypes.ActorType) (int, typesUtil.Error) { var paramName string switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: @@ -126,7 +126,7 @@ func (u *utilityContext) getMaxAllowedPausedBlocks(actorType coreTypes.ActorType return 0, typesUtil.ErrUnknownActorType(actorType.String()) } - maxPausedBlocks, err := u.store.GetIntParam(paramName, u.height) + maxPausedBlocks, err := u.persistenceReadContext.GetIntParam(paramName, u.height) if err != nil { return 0, typesUtil.ErrGetParam(paramName, err) } @@ -134,7 +134,7 @@ func (u *utilityContext) getMaxAllowedPausedBlocks(actorType coreTypes.ActorType return maxPausedBlocks, nil } -func (u *utilityContext) getMinRequiredPausedBlocks(actorType coreTypes.ActorType) (int, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getMinRequiredPausedBlocks(actorType coreTypes.ActorType) (int, typesUtil.Error) { var paramName string switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: @@ -149,26 +149,26 @@ func (u *utilityContext) getMinRequiredPausedBlocks(actorType coreTypes.ActorTyp return 0, typesUtil.ErrUnknownActorType(actorType.String()) } - minPausedBlocks, er := u.store.GetIntParam(paramName, u.height) + minPausedBlocks, er := u.persistenceReadContext.GetIntParam(paramName, u.height) if er != nil { return 0, typesUtil.ErrGetParam(paramName, er) } return minPausedBlocks, nil } -func (u *utilityContext) getPausedHeightIfExists(actorType coreTypes.ActorType, addr []byte) (int64, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getPausedHeightIfExists(actorType coreTypes.ActorType, addr []byte) (int64, typesUtil.Error) { var pauseHeight int64 var err error switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: - pauseHeight, err = u.store.GetAppPauseHeightIfExists(addr, u.height) + pauseHeight, err = u.persistenceReadContext.GetAppPauseHeightIfExists(addr, u.height) case coreTypes.ActorType_ACTOR_TYPE_FISH: - pauseHeight, err = u.store.GetFishermanPauseHeightIfExists(addr, u.height) + pauseHeight, err = u.persistenceReadContext.GetFishermanPauseHeightIfExists(addr, u.height) case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - pauseHeight, err = u.store.GetServicerPauseHeightIfExists(addr, u.height) + pauseHeight, err = u.persistenceReadContext.GetServicerPauseHeightIfExists(addr, u.height) case coreTypes.ActorType_ACTOR_TYPE_VAL: - pauseHeight, err = u.store.GetValidatorPauseHeightIfExists(addr, u.height) + pauseHeight, err = u.persistenceReadContext.GetValidatorPauseHeightIfExists(addr, u.height) default: err = typesUtil.ErrUnknownActorType(actorType.String()) } @@ -180,19 +180,19 @@ func (u *utilityContext) getPausedHeightIfExists(actorType coreTypes.ActorType, return pauseHeight, nil } -func (u *utilityContext) getActorStatus(actorType coreTypes.ActorType, addr []byte) (coreTypes.StakeStatus, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getActorStatus(actorType coreTypes.ActorType, addr []byte) (coreTypes.StakeStatus, typesUtil.Error) { var status int32 var err error switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: - status, err = u.store.GetAppStatus(addr, u.height) + status, err = u.persistenceReadContext.GetAppStatus(addr, u.height) case coreTypes.ActorType_ACTOR_TYPE_FISH: - status, err = u.store.GetFishermanStatus(addr, u.height) + status, err = u.persistenceReadContext.GetFishermanStatus(addr, u.height) case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - status, err = u.store.GetServicerStatus(addr, u.height) + status, err = u.persistenceReadContext.GetServicerStatus(addr, u.height) case coreTypes.ActorType_ACTOR_TYPE_VAL: - status, err = u.store.GetValidatorStatus(addr, u.height) + status, err = u.persistenceReadContext.GetValidatorStatus(addr, u.height) default: err = typesUtil.ErrUnknownActorType(actorType.String()) } @@ -208,7 +208,7 @@ func (u *utilityContext) getActorStatus(actorType coreTypes.ActorType, addr []by return coreTypes.StakeStatus(status), nil } -func (u *utilityContext) getMinRequiredStakeAmount(actorType coreTypes.ActorType) (*big.Int, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getMinRequiredStakeAmount(actorType coreTypes.ActorType) (*big.Int, typesUtil.Error) { var paramName string switch actorType { @@ -224,7 +224,7 @@ func (u *utilityContext) getMinRequiredStakeAmount(actorType coreTypes.ActorType return nil, typesUtil.ErrUnknownActorType(actorType.String()) } - minStake, er := u.store.GetStringParam(paramName, u.height) + minStake, er := u.persistenceReadContext.GetStringParam(paramName, u.height) if er != nil { return nil, typesUtil.ErrGetParam(paramName, er) } @@ -236,7 +236,7 @@ func (u *utilityContext) getMinRequiredStakeAmount(actorType coreTypes.ActorType return amount, nil } -func (u *utilityContext) getUnbondingHeight(actorType coreTypes.ActorType) (int64, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getUnbondingHeight(actorType coreTypes.ActorType) (int64, typesUtil.Error) { var paramName string switch actorType { @@ -252,7 +252,7 @@ func (u *utilityContext) getUnbondingHeight(actorType coreTypes.ActorType) (int6 return 0, typesUtil.ErrUnknownActorType(actorType.String()) } - unstakingBlocksPeriod, err := u.store.GetIntParam(paramName, u.height) + unstakingBlocksPeriod, err := u.persistenceReadContext.GetIntParam(paramName, u.height) if err != nil { return 0, typesUtil.ErrGetParam(paramName, err) } @@ -260,7 +260,7 @@ func (u *utilityContext) getUnbondingHeight(actorType coreTypes.ActorType) (int6 return u.height + int64(unstakingBlocksPeriod), nil } -func (u *utilityContext) getMaxAllowedChains(actorType coreTypes.ActorType) (int, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getMaxAllowedChains(actorType coreTypes.ActorType) (int, typesUtil.Error) { var paramName string switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: @@ -273,7 +273,7 @@ func (u *utilityContext) getMaxAllowedChains(actorType coreTypes.ActorType) (int return 0, typesUtil.ErrUnknownActorType(actorType.String()) } - maxChains, err := u.store.GetIntParam(paramName, u.height) + maxChains, err := u.persistenceReadContext.GetIntParam(paramName, u.height) if err != nil { return 0, typesUtil.ErrGetParam(paramName, err) } @@ -281,19 +281,19 @@ func (u *utilityContext) getMaxAllowedChains(actorType coreTypes.ActorType) (int return maxChains, nil } -func (u *utilityContext) getActorExists(actorType coreTypes.ActorType, addr []byte) (bool, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getActorExists(actorType coreTypes.ActorType, addr []byte) (bool, typesUtil.Error) { var exists bool var err error switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: - exists, err = u.store.GetAppExists(addr, u.height) + exists, err = u.persistenceReadContext.GetAppExists(addr, u.height) case coreTypes.ActorType_ACTOR_TYPE_FISH: - exists, err = u.store.GetFishermanExists(addr, u.height) + exists, err = u.persistenceReadContext.GetFishermanExists(addr, u.height) case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - exists, err = u.store.GetServicerExists(addr, u.height) + exists, err = u.persistenceReadContext.GetServicerExists(addr, u.height) case coreTypes.ActorType_ACTOR_TYPE_VAL: - exists, err = u.store.GetValidatorExists(addr, u.height) + exists, err = u.persistenceReadContext.GetValidatorExists(addr, u.height) default: return false, typesUtil.ErrUnknownActorType(actorType.String()) } @@ -307,19 +307,19 @@ func (u *utilityContext) getActorExists(actorType coreTypes.ActorType, addr []by // IMPROVE: Need to re-evaluate the design of `Output Address` to support things like "rev-share" // and multiple output addresses. -func (u *utilityContext) getActorOutputAddress(actorType coreTypes.ActorType, operator []byte) ([]byte, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getActorOutputAddress(actorType coreTypes.ActorType, operator []byte) ([]byte, typesUtil.Error) { var outputAddr []byte var err error switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: - outputAddr, err = u.store.GetAppOutputAddress(operator, u.height) + outputAddr, err = u.persistenceReadContext.GetAppOutputAddress(operator, u.height) case coreTypes.ActorType_ACTOR_TYPE_FISH: - outputAddr, err = u.store.GetFishermanOutputAddress(operator, u.height) + outputAddr, err = u.persistenceReadContext.GetFishermanOutputAddress(operator, u.height) case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - outputAddr, err = u.store.GetServicerOutputAddress(operator, u.height) + outputAddr, err = u.persistenceReadContext.GetServicerOutputAddress(operator, u.height) case coreTypes.ActorType_ACTOR_TYPE_VAL: - outputAddr, err = u.store.GetValidatorOutputAddress(operator, u.height) + outputAddr, err = u.persistenceReadContext.GetValidatorOutputAddress(operator, u.height) default: err = typesUtil.ErrUnknownActorType(actorType.String()) } diff --git a/utility/actor_test.go b/utility/unit_of_work/actor_test.go similarity index 94% rename from utility/actor_test.go rename to utility/unit_of_work/actor_test.go index 1501bf9c4..b50e8fe97 100644 --- a/utility/actor_test.go +++ b/utility/unit_of_work/actor_test.go @@ -1,4 +1,4 @@ -package utility +package unit_of_work import ( "encoding/hex" @@ -142,7 +142,7 @@ func TestUtilityContext_HandleMessageUnstake(t *testing.T) { default: t.Fatalf("unexpected actor type %s", actorType.String()) } - err := ctx.store.SetParam(paramName, numUnstakingBlocks) + err := ctx.persistenceRWContext.SetParam(paramName, numUnstakingBlocks) require.NoError(t, err, "error setting minimum pause blocks") actor := getFirstActor(t, ctx, actorType) @@ -193,7 +193,7 @@ func TestUtilityContext_HandleMessageUnpause(t *testing.T) { default: t.Fatalf("unexpected actor type %s", actorType.String()) } - err := ctx.store.SetParam(paramName, minPauseBlocksNumber) + err := ctx.persistenceRWContext.SetParam(paramName, minPauseBlocksNumber) require.NoError(t, err, "error setting minimum pause blocks") actor := getFirstActor(t, ctx, actorType) @@ -308,7 +308,7 @@ func TestUtilityContext_BeginUnstakingMaxPausedActors(t *testing.T) { default: t.Fatalf("unexpected actor type %s", actorType.String()) } - err := ctx.store.SetParam(paramName, maxPausedBlocks) + err := ctx.persistenceRWContext.SetParam(paramName, maxPausedBlocks) require.NoError(t, err) actor := getFirstActor(t, ctx, actorType) @@ -400,9 +400,9 @@ func TestUtilityContext_BeginUnstakingActorsPausedBefore_UnbondUnstakingActors(t t.Fatalf("unexpected actor type %s", actorType.String()) } - er := ctx.store.SetParam(paramName1, maxPausedBlocks) + er := ctx.persistenceRWContext.SetParam(paramName1, maxPausedBlocks) require.NoError(t, er, "error setting max paused blocks") - er = ctx.store.SetParam(paramName2, unstakingBlocks) + er = ctx.persistenceRWContext.SetParam(paramName2, unstakingBlocks) require.NoError(t, er, "error setting max paused blocks") er = ctx.setPoolAmount(poolName, poolInitAMount) require.NoError(t, er) @@ -653,7 +653,7 @@ func TestUtilityContext_GetMessageUnstakeSignerCandidates(t *testing.T) { // Helpers -func getAllTestingActors(t *testing.T, ctx *utilityContext, actorType coreTypes.ActorType) (actors []*coreTypes.Actor) { +func getAllTestingActors(t *testing.T, ctx *baseUtilityUnitOfWork, actorType coreTypes.ActorType) (actors []*coreTypes.Actor) { actors = make([]*coreTypes.Actor, 0) switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: @@ -675,24 +675,24 @@ func getAllTestingActors(t *testing.T, ctx *utilityContext, actorType coreTypes. return } -func getFirstActor(t *testing.T, ctx *utilityContext, actorType coreTypes.ActorType) *coreTypes.Actor { +func getFirstActor(t *testing.T, ctx *baseUtilityUnitOfWork, actorType coreTypes.ActorType) *coreTypes.Actor { return getAllTestingActors(t, ctx, actorType)[0] } -func getActorByAddr(t *testing.T, ctx *utilityContext, actorType coreTypes.ActorType, addr string) (actor *coreTypes.Actor) { +func getActorByAddr(t *testing.T, ctx *baseUtilityUnitOfWork, actorType coreTypes.ActorType, addr string) (actor *coreTypes.Actor) { actors := getAllTestingActors(t, ctx, actorType) idx := slices.IndexFunc(actors, func(a *coreTypes.Actor) bool { return a.GetAddress() == addr }) return actors[idx] } -func getAllTestingApps(t *testing.T, ctx *utilityContext) []*coreTypes.Actor { - actors, err := ctx.store.GetAllApps(ctx.height) +func getAllTestingApps(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.Actor { + actors, err := ctx.persistenceReadContext.GetAllApps(ctx.height) require.NoError(t, err) return actors } -func getAllTestingValidators(t *testing.T, ctx *utilityContext) []*coreTypes.Actor { - actors, err := ctx.store.GetAllValidators(ctx.height) +func getAllTestingValidators(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.Actor { + actors, err := ctx.persistenceReadContext.GetAllValidators(ctx.height) require.NoError(t, err) sort.Slice(actors, func(i, j int) bool { return actors[i].GetAddress() < actors[j].GetAddress() @@ -700,14 +700,14 @@ func getAllTestingValidators(t *testing.T, ctx *utilityContext) []*coreTypes.Act return actors } -func getAllTestingFish(t *testing.T, ctx *utilityContext) []*coreTypes.Actor { - actors, err := ctx.store.GetAllFishermen(ctx.height) +func getAllTestingFish(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.Actor { + actors, err := ctx.persistenceReadContext.GetAllFishermen(ctx.height) require.NoError(t, err) return actors } -func getAllTestingServicers(t *testing.T, ctx *utilityContext) []*coreTypes.Actor { - actors, err := ctx.store.GetAllServicers(ctx.height) +func getAllTestingServicers(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.Actor { + actors, err := ctx.persistenceReadContext.GetAllServicers(ctx.height) require.NoError(t, err) return actors } diff --git a/utility/application.go b/utility/unit_of_work/application.go similarity index 88% rename from utility/application.go rename to utility/unit_of_work/application.go index 9fa253dcd..857e85a04 100644 --- a/utility/application.go +++ b/utility/unit_of_work/application.go @@ -1,4 +1,4 @@ -package utility +package unit_of_work // Internal business logic for the `Application` protocol actor. // @@ -15,7 +15,7 @@ import ( // // calculateAppSessionTokens determines the number of "session tokens" an application gets at the beginning // of every session. For example, 1 session token could equate to a quota of 1 relay. -func (u *utilityContext) calculateAppSessionTokens(appStakeStr string) (string, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) calculateAppSessionTokens(appStakeStr string) (string, typesUtil.Error) { appStake, er := utils.StringToBigInt(appStakeStr) if er != nil { return typesUtil.EmptyString, typesUtil.ErrStringToBigInt(er) diff --git a/utility/application_test.go b/utility/unit_of_work/application_test.go similarity index 96% rename from utility/application_test.go rename to utility/unit_of_work/application_test.go index fd5e988cd..2a8ea6529 100644 --- a/utility/application_test.go +++ b/utility/unit_of_work/application_test.go @@ -1,4 +1,4 @@ -package utility +package unit_of_work import ( "testing" diff --git a/utility/unit_of_work/base.go b/utility/unit_of_work/base.go deleted file mode 100644 index f73158255..000000000 --- a/utility/unit_of_work/base.go +++ /dev/null @@ -1,77 +0,0 @@ -package unit_of_work - -import ( - "github.com/pokt-network/pocket/shared/modules" - "github.com/pokt-network/pocket/shared/modules/base_modules" -) - -const ( - leaderUtilityUOWModuleName = "leader_utility_unit_of_work" - replicaUtilityUOWModuleName = "replica_utility_unit_of_work" -) - -var _ modules.UtilityUnitOfWork = &baseUtilityUnitOfWork{} - -type baseUtilityUnitOfWork struct { - base_modules.IntegratableModule - - logger *modules.Logger - - persistenceReadContext modules.PersistenceReadContext - persistenceRWContext modules.PersistenceRWContext - - // TECHDEBT: Consolidate all these types with the shared Protobuf struct and create a `proposalBlock` - proposalStateHash string - proposalProposerAddr []byte - proposalBlockTxs [][]byte -} - -func (uow *baseUtilityUnitOfWork) SetProposalBlock(blockHash string, proposerAddr []byte, txs [][]byte) error { - uow.proposalStateHash = blockHash - uow.proposalProposerAddr = proposerAddr - uow.proposalBlockTxs = txs - return nil -} - -func (uow *baseUtilityUnitOfWork) ApplyBlock(beforeApplyBlock, afterApplyBlock func(modules.UtilityUnitOfWork) error) (stateHash string, txs [][]byte, err error) { - if beforeApplyBlock != nil { - uow.logger.Debug().Msg("running beforeApplyBlock...") - if err := beforeApplyBlock(uow); err != nil { - return "", nil, err - } - } - - if afterApplyBlock != nil { - uow.logger.Debug().Msg("running afterApplyBlock...") - if err := afterApplyBlock(uow); err != nil { - return "", nil, err - } - } - - panic("unimplemented") -} - -func (uow *baseUtilityUnitOfWork) Commit(quorumCert []byte) error { - // TODO: @deblasis - change tracking here - - uow.logger.Debug().Msg("committing the rwPersistenceContext...") - if err := uow.persistenceRWContext.Commit(uow.proposalProposerAddr, quorumCert); err != nil { - return err - } - return uow.Release() -} - -func (uow *baseUtilityUnitOfWork) Release() error { - // TODO: @deblasis - change tracking reset here - - if uow.persistenceRWContext == nil { - return nil - } - if err := uow.persistenceRWContext.Release(); err != nil { - return err - } - if err := uow.persistenceReadContext.Close(); err != nil { - return err - } - return nil -} diff --git a/utility/unit_of_work/block.go b/utility/unit_of_work/block.go new file mode 100644 index 000000000..f30b7464a --- /dev/null +++ b/utility/unit_of_work/block.go @@ -0,0 +1,218 @@ +package unit_of_work + +// Internal business logic containing the lifecycle of Block-related operations + +import ( + "encoding/hex" + "math/big" + + coreTypes "github.com/pokt-network/pocket/shared/core/types" + moduleTypes "github.com/pokt-network/pocket/shared/modules/types" + "github.com/pokt-network/pocket/shared/utils" + typesUtil "github.com/pokt-network/pocket/utility/types" +) + +func (u *baseUtilityUnitOfWork) beginBlock(previousBlockByzantineValidators [][]byte) typesUtil.Error { + if err := u.handleByzantineValidators(previousBlockByzantineValidators); err != nil { + return err + } + // INCOMPLETE: Identify what else needs to be done in the begin block lifecycle phase + return nil +} + +func (u *baseUtilityUnitOfWork) endBlock(proposer []byte) typesUtil.Error { + // reward the block proposer + if err := u.handleProposerRewards(proposer); err != nil { + return err + } + + // unstake actors that have been 'unstaking' for the UnstakingBlocks + if err := u.unbondUnstakingActors(); err != nil { + return err + } + + // begin unstaking the actors who have been paused for MaxPauseBlocks + if err := u.beginUnstakingMaxPausedActors(); err != nil { + return err + } + + // INCOMPLETE: Identify what else needs to be done in the begin block lifecycle phase + return nil +} + +func (u *baseUtilityUnitOfWork) handleProposerRewards(proposer []byte) typesUtil.Error { + feePoolName := coreTypes.Pools_POOLS_FEE_COLLECTOR.FriendlyName() + feesAndRewardsCollected, err := u.getPoolAmount(feePoolName) + if err != nil { + return err + } + + // Nullify the rewards pool + if err := u.setPoolAmount(feePoolName, big.NewInt(0)); err != nil { + return err + } + + // + proposerCutPercentage, err := u.getProposerPercentageOfFees() + if err != nil { + return err + } + + daoCutPercentage := 100 - proposerCutPercentage + if daoCutPercentage < 0 || daoCutPercentage > 100 { + return typesUtil.ErrInvalidProposerCutPercentage() + } + + amountToProposerFloat := new(big.Float).SetInt(feesAndRewardsCollected) + amountToProposerFloat.Mul(amountToProposerFloat, big.NewFloat(float64(proposerCutPercentage))) + amountToProposerFloat.Quo(amountToProposerFloat, big.NewFloat(100)) + amountToProposer, _ := amountToProposerFloat.Int(nil) + amountToDAO := feesAndRewardsCollected.Sub(feesAndRewardsCollected, amountToProposer) + if err := u.addAccountAmount(proposer, amountToProposer); err != nil { + return err + } + if err := u.addPoolAmount(coreTypes.Pools_POOLS_DAO.FriendlyName(), amountToDAO); err != nil { + return err + } + return nil +} + +func (u *baseUtilityUnitOfWork) unbondUnstakingActors() (err typesUtil.Error) { + for actorTypeNum := range coreTypes.ActorType_name { + if actorTypeNum == 0 { // ACTOR_TYPE_UNSPECIFIED + continue + } + actorType := coreTypes.ActorType(actorTypeNum) + + var readyToUnbond []*moduleTypes.UnstakingActor + var poolName string + + var er error + switch actorType { + case coreTypes.ActorType_ACTOR_TYPE_APP: + readyToUnbond, er = u.persistenceReadContext.GetAppsReadyToUnstake(u.height, int32(coreTypes.StakeStatus_Unstaking)) + poolName = coreTypes.Pools_POOLS_APP_STAKE.FriendlyName() + case coreTypes.ActorType_ACTOR_TYPE_FISH: + readyToUnbond, er = u.persistenceReadContext.GetFishermenReadyToUnstake(u.height, int32(coreTypes.StakeStatus_Unstaking)) + poolName = coreTypes.Pools_POOLS_FISHERMAN_STAKE.FriendlyName() + case coreTypes.ActorType_ACTOR_TYPE_SERVICER: + readyToUnbond, er = u.persistenceReadContext.GetServicersReadyToUnstake(u.height, int32(coreTypes.StakeStatus_Unstaking)) + poolName = coreTypes.Pools_POOLS_SERVICER_STAKE.FriendlyName() + case coreTypes.ActorType_ACTOR_TYPE_VAL: + readyToUnbond, er = u.persistenceReadContext.GetValidatorsReadyToUnstake(u.height, int32(coreTypes.StakeStatus_Unstaking)) + poolName = coreTypes.Pools_POOLS_VALIDATOR_STAKE.FriendlyName() + case coreTypes.ActorType_ACTOR_TYPE_UNSPECIFIED: + continue + } + if er != nil { + return typesUtil.ErrGetReadyToUnstake(er) + } + + // Loop through all unstaking actors and unbond those that have reached the waiting period, + // move their stake from the pool back to the corresponding account. + for _, actor := range readyToUnbond { + stakeAmount, err := utils.StringToBigInt(actor.StakeAmount) + if err != nil { + return typesUtil.ErrStringToBigInt(err) + } + + outputAddrBz, err := hex.DecodeString(actor.OutputAddress) + if err != nil { + return typesUtil.ErrHexDecodeFromString(err) + } + + if err := u.subPoolAmount(poolName, stakeAmount); err != nil { + return err + } + if err := u.addAccountAmount(outputAddrBz, stakeAmount); err != nil { + return err + } + } + } + + return nil +} + +func (u *baseUtilityUnitOfWork) beginUnstakingMaxPausedActors() (err typesUtil.Error) { + for actorTypeNum := range coreTypes.ActorType_name { + if actorTypeNum == 0 { // ACTOR_TYPE_UNSPECIFIED + continue + } + actorType := coreTypes.ActorType(actorTypeNum) + + if actorType == coreTypes.ActorType_ACTOR_TYPE_UNSPECIFIED { + continue + } + maxPausedBlocks, err := u.getMaxAllowedPausedBlocks(actorType) + if err != nil { + return err + } + maxPauseHeight := u.height - int64(maxPausedBlocks) + if maxPauseHeight < 0 { // genesis edge case + maxPauseHeight = 0 + } + if err := u.beginUnstakingActorsPausedBefore(maxPauseHeight, actorType); err != nil { + return err + } + } + return nil +} + +func (u *baseUtilityUnitOfWork) beginUnstakingActorsPausedBefore(pausedBeforeHeight int64, actorType coreTypes.ActorType) (err typesUtil.Error) { + unbondingHeight, err := u.getUnbondingHeight(actorType) + if err != nil { + return err + } + + var er error + switch actorType { + case coreTypes.ActorType_ACTOR_TYPE_APP: + er = u.persistenceRWContext.SetAppStatusAndUnstakingHeightIfPausedBefore(pausedBeforeHeight, unbondingHeight, int32(coreTypes.StakeStatus_Unstaking)) + case coreTypes.ActorType_ACTOR_TYPE_FISH: + er = u.persistenceRWContext.SetFishermanStatusAndUnstakingHeightIfPausedBefore(pausedBeforeHeight, unbondingHeight, int32(coreTypes.StakeStatus_Unstaking)) + case coreTypes.ActorType_ACTOR_TYPE_SERVICER: + er = u.persistenceRWContext.SetServicerStatusAndUnstakingHeightIfPausedBefore(pausedBeforeHeight, unbondingHeight, int32(coreTypes.StakeStatus_Unstaking)) + case coreTypes.ActorType_ACTOR_TYPE_VAL: + er = u.persistenceRWContext.SetValidatorsStatusAndUnstakingHeightIfPausedBefore(pausedBeforeHeight, unbondingHeight, int32(coreTypes.StakeStatus_Unstaking)) + } + if er != nil { + return typesUtil.ErrSetStatusPausedBefore(er, pausedBeforeHeight) + } + return nil +} + +// TODO: Need to design & document this business logic. +func (u *baseUtilityUnitOfWork) prevBlockByzantineValidators() ([][]byte, error) { + return nil, nil +} + +// TODO: This has not been tested or investigated in detail +func (u *baseUtilityUnitOfWork) revertLastSavePoint() typesUtil.Error { + // TODO: @deblasis + // if len(u.savePointsSet) == 0 { + // return typesUtil.ErrEmptySavePoints() + // } + // var key []byte + // popIndex := len(u.savePointsList) - 1 + // key, u.savePointsList = u.savePointsList[popIndex], u.savePointsList[:popIndex] + // delete(u.savePointsSet, hex.EncodeToString(key)) + // if err := u.store.RollbackToSavePoint(key); err != nil { + // return typesUtil.ErrRollbackSavePoint(err) + // } + return nil +} + +//nolint:unused // TODO: This has not been tested or investigated in detail +func (u *baseUtilityUnitOfWork) newSavePoint(txHashBz []byte) typesUtil.Error { + // TODO: @deblasis + // if err := u.store.NewSavePoint(txHashBz); err != nil { + // return typesUtil.ErrNewSavePoint(err) + // } + // txHash := hex.EncodeToString(txHashBz) + // if _, exists := u.savePointsSet[txHash]; exists { + // return typesUtil.ErrDuplicateSavePoint() + // } + // u.savePointsList = append(u.savePointsList, txHashBz) + // u.savePointsSet[txHash] = struct{}{} + return nil +} diff --git a/utility/block_test.go b/utility/unit_of_work/block_test.go similarity index 97% rename from utility/block_test.go rename to utility/unit_of_work/block_test.go index 17759ddf9..8436e5a28 100644 --- a/utility/block_test.go +++ b/utility/unit_of_work/block_test.go @@ -1,4 +1,4 @@ -package utility +package unit_of_work import ( "encoding/hex" @@ -27,7 +27,7 @@ func TestUtilityContext_ApplyBlock(t *testing.T) { err = ctx.SetProposalBlock("", addrBz, [][]byte{txBz}) require.NoError(t, err) - appHash, err := ctx.ApplyBlock() + appHash, _, err := ctx.ApplyBlock() require.NoError(t, err) require.NotNil(t, appHash) @@ -77,7 +77,7 @@ func TestUtilityContext_BeginBlock(t *testing.T) { er = ctx.SetProposalBlock("", addrBz, [][]byte{txBz}) require.NoError(t, er) - _, er = ctx.ApplyBlock() + _, _, er = ctx.ApplyBlock() require.NoError(t, er) // // TODO: Uncomment this once `GetValidatorMissedBlocks` is implemented. @@ -106,7 +106,7 @@ func TestUtilityContext_EndBlock(t *testing.T) { er = ctx.SetProposalBlock("", addrBz, [][]byte{txBz}) require.NoError(t, er) - _, er = ctx.ApplyBlock() + _, _, er = ctx.ApplyBlock() require.NoError(t, er) feeBig, err := ctx.getMessageSendFee() diff --git a/utility/unit_of_work/gov.go b/utility/unit_of_work/gov.go new file mode 100644 index 000000000..3f0a7973a --- /dev/null +++ b/utility/unit_of_work/gov.go @@ -0,0 +1,576 @@ +package unit_of_work + +import ( + "math/big" + + coreTypes "github.com/pokt-network/pocket/shared/core/types" + "github.com/pokt-network/pocket/shared/utils" + typesUtil "github.com/pokt-network/pocket/utility/types" + "google.golang.org/protobuf/types/known/wrapperspb" +) + +func (u *baseUtilityUnitOfWork) updateParam(paramName string, value any) typesUtil.Error { + switch t := value.(type) { + case *wrapperspb.Int32Value: + if err := u.persistenceRWContext.SetParam(paramName, (int(t.Value))); err != nil { + return typesUtil.ErrUpdateParam(err) + } + return nil + case *wrapperspb.StringValue: + if err := u.persistenceRWContext.SetParam(paramName, t.Value); err != nil { + return typesUtil.ErrUpdateParam(err) + } + return nil + case *wrapperspb.BytesValue: + if err := u.persistenceRWContext.SetParam(paramName, t.Value); err != nil { + return typesUtil.ErrUpdateParam(err) + } + return nil + default: + break + } + u.logger.Fatal().Msgf("unhandled value type %T for %v", value, value) + return typesUtil.ErrUnknownParam(paramName) +} + +func (u *baseUtilityUnitOfWork) getParameter(paramName string) (any, error) { + return u.persistenceReadContext.GetParameter(paramName, u.height) +} + +func (u *baseUtilityUnitOfWork) getAppMinimumStake() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.AppMinimumStakeParamName) +} + +func (u *baseUtilityUnitOfWork) getAppMaxChains() (int, typesUtil.Error) { + return u.getIntParam(typesUtil.AppMaxChainsParamName) +} + +func (u *baseUtilityUnitOfWork) getAppSessionTokensMultiplier() (int, typesUtil.Error) { + return u.getIntParam(typesUtil.AppSessionTokensMultiplierParamName) +} + +func (u *baseUtilityUnitOfWork) getAppUnstakingBlocks() (int64, typesUtil.Error) { + return u.getInt64Param(typesUtil.AppUnstakingBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getAppMinimumPauseBlocks() (int, typesUtil.Error) { + return u.getIntParam(typesUtil.AppMinimumPauseBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getAppMaxPausedBlocks() (maxPausedBlocks int, err typesUtil.Error) { + return u.getIntParam(typesUtil.AppMaxPauseBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getServicerMinimumStake() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.ServicerMinimumStakeParamName) +} + +func (u *baseUtilityUnitOfWork) getServicerMaxChains() (int, typesUtil.Error) { + return u.getIntParam(typesUtil.ServicerMaxChainsParamName) +} + +func (u *baseUtilityUnitOfWork) getServicerUnstakingBlocks() (int64, typesUtil.Error) { + return u.getInt64Param(typesUtil.ServicerUnstakingBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getServicerMinimumPauseBlocks() (int, typesUtil.Error) { + return u.getIntParam(typesUtil.ServicerMinimumPauseBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getServicerMaxPausedBlocks() (maxPausedBlocks int, err typesUtil.Error) { + return u.getIntParam(typesUtil.ServicerMaxPauseBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getValidatorMinimumStake() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.ValidatorMinimumStakeParamName) +} + +func (u *baseUtilityUnitOfWork) getValidatorUnstakingBlocks() (int64, typesUtil.Error) { + return u.getInt64Param(typesUtil.ValidatorUnstakingBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getValidatorMinimumPauseBlocks() (int, typesUtil.Error) { + return u.getIntParam(typesUtil.ValidatorMinimumPauseBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getValidatorMaxPausedBlocks() (maxPausedBlocks int, err typesUtil.Error) { + return u.getIntParam(typesUtil.ValidatorMaxPausedBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getProposerPercentageOfFees() (proposerPercentage int, err typesUtil.Error) { + return u.getIntParam(typesUtil.ProposerPercentageOfFeesParamName) +} + +func (u *baseUtilityUnitOfWork) getValidatorMaxMissedBlocks() (maxMissedBlocks int, err typesUtil.Error) { + return u.getIntParam(typesUtil.ValidatorMaximumMissedBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getMaxEvidenceAgeInBlocks() (maxMissedBlocks int, err typesUtil.Error) { + return u.getIntParam(typesUtil.ValidatorMaxEvidenceAgeInBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getDoubleSignBurnPercentage() (burnPercentage int, err typesUtil.Error) { + return u.getIntParam(typesUtil.DoubleSignBurnPercentageParamName) +} + +func (u *baseUtilityUnitOfWork) getMissedBlocksBurnPercentage() (burnPercentage int, err typesUtil.Error) { + return u.getIntParam(typesUtil.MissedBlocksBurnPercentageParamName) +} + +func (u *baseUtilityUnitOfWork) getFishermanMinimumStake() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.FishermanMinimumStakeParamName) +} + +func (u *baseUtilityUnitOfWork) getFishermanMaxChains() (int, typesUtil.Error) { + return u.getIntParam(typesUtil.FishermanMaxChainsParamName) +} + +func (u *baseUtilityUnitOfWork) getFishermanUnstakingBlocks() (int64, typesUtil.Error) { + return u.getInt64Param(typesUtil.FishermanUnstakingBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getFishermanMinimumPauseBlocks() (int, typesUtil.Error) { + return u.getIntParam(typesUtil.FishermanMinimumPauseBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getFishermanMaxPausedBlocks() (maxPausedBlocks int, err typesUtil.Error) { + return u.getIntParam(typesUtil.FishermanMaxPauseBlocksParamName) +} + +func (u *baseUtilityUnitOfWork) getMessageDoubleSignFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageDoubleSignFee) +} + +func (u *baseUtilityUnitOfWork) getMessageSendFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageSendFee) +} + +func (u *baseUtilityUnitOfWork) getMessageStakeFishermanFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageStakeFishermanFee) +} + +func (u *baseUtilityUnitOfWork) getMessageEditStakeFishermanFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageEditStakeFishermanFee) +} + +func (u *baseUtilityUnitOfWork) getMessageUnstakeFishermanFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageUnstakeFishermanFee) +} + +func (u *baseUtilityUnitOfWork) getMessagePauseFishermanFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessagePauseFishermanFee) +} + +func (u *baseUtilityUnitOfWork) getMessageUnpauseFishermanFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageUnpauseFishermanFee) +} + +func (u *baseUtilityUnitOfWork) getMessageFishermanPauseServicerFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageFishermanPauseServicerFee) +} + +func (u *baseUtilityUnitOfWork) getMessageTestScoreFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageTestScoreFee) +} + +func (u *baseUtilityUnitOfWork) getMessageProveTestScoreFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageProveTestScoreFee) +} + +func (u *baseUtilityUnitOfWork) getMessageStakeAppFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageStakeAppFee) +} + +func (u *baseUtilityUnitOfWork) getMessageEditStakeAppFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageEditStakeAppFee) +} + +func (u *baseUtilityUnitOfWork) getMessageUnstakeAppFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageUnstakeAppFee) +} + +func (u *baseUtilityUnitOfWork) getMessagePauseAppFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessagePauseAppFee) +} + +func (u *baseUtilityUnitOfWork) getMessageUnpauseAppFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageUnpauseAppFee) +} + +func (u *baseUtilityUnitOfWork) getMessageStakeValidatorFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageStakeValidatorFee) +} + +func (u *baseUtilityUnitOfWork) getMessageEditStakeValidatorFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageEditStakeValidatorFee) +} + +func (u *baseUtilityUnitOfWork) getMessageUnstakeValidatorFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageUnstakeValidatorFee) +} + +func (u *baseUtilityUnitOfWork) getMessagePauseValidatorFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessagePauseValidatorFee) +} + +func (u *baseUtilityUnitOfWork) getMessageUnpauseValidatorFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageUnpauseValidatorFee) +} + +func (u *baseUtilityUnitOfWork) getMessageStakeServicerFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageStakeServicerFee) +} + +func (u *baseUtilityUnitOfWork) getMessageEditStakeServicerFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageEditStakeServicerFee) +} + +func (u *baseUtilityUnitOfWork) getMessageUnstakeServicerFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageUnstakeServicerFee) +} + +func (u *baseUtilityUnitOfWork) getMessagePauseServicerFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessagePauseServicerFee) +} + +func (u *baseUtilityUnitOfWork) getMessageUnpauseServicerFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageUnpauseServicerFee) +} + +func (u *baseUtilityUnitOfWork) getMessageChangeParameterFee() (*big.Int, typesUtil.Error) { + return u.getBigIntParam(typesUtil.MessageChangeParameterFee) +} + +func (u *baseUtilityUnitOfWork) getDoubleSignFeeOwner() (owner []byte, err typesUtil.Error) { + return u.getByteArrayParam(typesUtil.MessageDoubleSignFeeOwner) +} + +func (u *baseUtilityUnitOfWork) getParamOwner(paramName string) ([]byte, error) { + // DISCUSS (@deblasis): here we could potentially leverage the struct tags in gov.proto by specifying an `owner` key + // eg: `app_minimum_stake` could have `pokt:"owner=app_minimum_stake_owner"` + // in here we would use that map to point to the owner, removing this switch, centralizing the logic and making it declarative + switch paramName { + case typesUtil.AclOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.BlocksPerSessionParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.BlocksPerSessionOwner, u.height) + case typesUtil.AppMaxChainsParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.AppMaxChainsOwner, u.height) + case typesUtil.AppMinimumStakeParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.AppMinimumStakeOwner, u.height) + case typesUtil.AppSessionTokensMultiplierParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.AppSessionTokensMultiplierOwner, u.height) + case typesUtil.AppUnstakingBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.AppUnstakingBlocksOwner, u.height) + case typesUtil.AppMinimumPauseBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.AppMinimumPauseBlocksOwner, u.height) + case typesUtil.AppMaxPauseBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.AppMaxPausedBlocksOwner, u.height) + case typesUtil.ServicersPerSessionParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ServicersPerSessionOwner, u.height) + case typesUtil.ServicerMinimumStakeParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ServicerMinimumStakeOwner, u.height) + case typesUtil.ServicerMaxChainsParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ServicerMaxChainsOwner, u.height) + case typesUtil.ServicerUnstakingBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ServicerUnstakingBlocksOwner, u.height) + case typesUtil.ServicerMinimumPauseBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ServicerMinimumPauseBlocksOwner, u.height) + case typesUtil.ServicerMaxPauseBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ServicerMaxPausedBlocksOwner, u.height) + case typesUtil.FishermanMinimumStakeParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.FishermanMinimumStakeOwner, u.height) + case typesUtil.FishermanMaxChainsParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.FishermanMaxChainsOwner, u.height) + case typesUtil.FishermanUnstakingBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.FishermanUnstakingBlocksOwner, u.height) + case typesUtil.FishermanMinimumPauseBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.FishermanMinimumPauseBlocksOwner, u.height) + case typesUtil.FishermanMaxPauseBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.FishermanMaxPausedBlocksOwner, u.height) + case typesUtil.ValidatorMinimumStakeParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ValidatorMinimumStakeOwner, u.height) + case typesUtil.ValidatorUnstakingBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ValidatorUnstakingBlocksOwner, u.height) + case typesUtil.ValidatorMinimumPauseBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ValidatorMinimumPauseBlocksOwner, u.height) + case typesUtil.ValidatorMaxPausedBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ValidatorMaxPausedBlocksOwner, u.height) + case typesUtil.ValidatorMaximumMissedBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ValidatorMaximumMissedBlocksOwner, u.height) + case typesUtil.ProposerPercentageOfFeesParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ProposerPercentageOfFeesOwner, u.height) + case typesUtil.ValidatorMaxEvidenceAgeInBlocksParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.ValidatorMaxEvidenceAgeInBlocksOwner, u.height) + case typesUtil.MissedBlocksBurnPercentageParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.MissedBlocksBurnPercentageOwner, u.height) + case typesUtil.DoubleSignBurnPercentageParamName: + return u.persistenceReadContext.GetBytesParam(typesUtil.DoubleSignBurnPercentageOwner, u.height) + case typesUtil.MessageDoubleSignFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageDoubleSignFeeOwner, u.height) + case typesUtil.MessageSendFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageSendFeeOwner, u.height) + case typesUtil.MessageStakeFishermanFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageStakeFishermanFeeOwner, u.height) + case typesUtil.MessageEditStakeFishermanFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageEditStakeFishermanFeeOwner, u.height) + case typesUtil.MessageUnstakeFishermanFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageUnstakeFishermanFeeOwner, u.height) + case typesUtil.MessagePauseFishermanFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessagePauseFishermanFeeOwner, u.height) + case typesUtil.MessageUnpauseFishermanFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageUnpauseFishermanFeeOwner, u.height) + case typesUtil.MessageFishermanPauseServicerFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageFishermanPauseServicerFeeOwner, u.height) + case typesUtil.MessageTestScoreFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageTestScoreFeeOwner, u.height) + case typesUtil.MessageProveTestScoreFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageProveTestScoreFeeOwner, u.height) + case typesUtil.MessageStakeAppFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageStakeAppFeeOwner, u.height) + case typesUtil.MessageEditStakeAppFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageEditStakeAppFeeOwner, u.height) + case typesUtil.MessageUnstakeAppFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageUnstakeAppFeeOwner, u.height) + case typesUtil.MessagePauseAppFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessagePauseAppFeeOwner, u.height) + case typesUtil.MessageUnpauseAppFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageUnpauseAppFeeOwner, u.height) + case typesUtil.MessageStakeValidatorFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageStakeValidatorFeeOwner, u.height) + case typesUtil.MessageEditStakeValidatorFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageEditStakeValidatorFeeOwner, u.height) + case typesUtil.MessageUnstakeValidatorFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageUnstakeValidatorFeeOwner, u.height) + case typesUtil.MessagePauseValidatorFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessagePauseValidatorFeeOwner, u.height) + case typesUtil.MessageUnpauseValidatorFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageUnpauseValidatorFeeOwner, u.height) + case typesUtil.MessageStakeServicerFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageStakeServicerFeeOwner, u.height) + case typesUtil.MessageEditStakeServicerFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageEditStakeServicerFeeOwner, u.height) + case typesUtil.MessageUnstakeServicerFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageUnstakeServicerFeeOwner, u.height) + case typesUtil.MessagePauseServicerFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessagePauseServicerFeeOwner, u.height) + case typesUtil.MessageUnpauseServicerFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageUnpauseServicerFeeOwner, u.height) + case typesUtil.MessageChangeParameterFee: + return u.persistenceReadContext.GetBytesParam(typesUtil.MessageChangeParameterFeeOwner, u.height) + case typesUtil.BlocksPerSessionOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.AppMaxChainsOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.AppMinimumStakeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.AppSessionTokensMultiplierOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.AppUnstakingBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.AppMinimumPauseBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.AppMaxPausedBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ServicerMinimumStakeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ServicerMaxChainsOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ServicerUnstakingBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ServicerMinimumPauseBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ServicerMaxPausedBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ServicersPerSessionOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.FishermanMinimumStakeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.FishermanMaxChainsOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.FishermanUnstakingBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.FishermanMinimumPauseBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.FishermanMaxPausedBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ValidatorMinimumStakeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ValidatorUnstakingBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ValidatorMinimumPauseBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ValidatorMaxPausedBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ValidatorMaximumMissedBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ProposerPercentageOfFeesOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.ValidatorMaxEvidenceAgeInBlocksOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MissedBlocksBurnPercentageOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.DoubleSignBurnPercentageOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageSendFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageStakeFishermanFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageEditStakeFishermanFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageUnstakeFishermanFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessagePauseFishermanFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageUnpauseFishermanFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageFishermanPauseServicerFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageTestScoreFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageProveTestScoreFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageStakeAppFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageEditStakeAppFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageUnstakeAppFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessagePauseAppFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageUnpauseAppFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageStakeValidatorFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageEditStakeValidatorFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageUnstakeValidatorFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessagePauseValidatorFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageUnpauseValidatorFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageStakeServicerFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageEditStakeServicerFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageUnstakeServicerFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessagePauseServicerFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageUnpauseServicerFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + case typesUtil.MessageChangeParameterFeeOwner: + return u.persistenceReadContext.GetBytesParam(typesUtil.AclOwner, u.height) + default: + return nil, typesUtil.ErrUnknownParam(paramName) + } +} + +func (u *baseUtilityUnitOfWork) getFee(msg typesUtil.Message, actorType coreTypes.ActorType) (amount *big.Int, err typesUtil.Error) { + switch x := msg.(type) { + case *typesUtil.MessageSend: + return u.getMessageSendFee() + case *typesUtil.MessageStake: + switch actorType { + case coreTypes.ActorType_ACTOR_TYPE_APP: + return u.getMessageStakeAppFee() + case coreTypes.ActorType_ACTOR_TYPE_FISH: + return u.getMessageStakeFishermanFee() + case coreTypes.ActorType_ACTOR_TYPE_SERVICER: + return u.getMessageStakeServicerFee() + case coreTypes.ActorType_ACTOR_TYPE_VAL: + return u.getMessageStakeValidatorFee() + default: + return nil, typesUtil.ErrUnknownActorType(actorType.String()) + } + case *typesUtil.MessageEditStake: + switch actorType { + case coreTypes.ActorType_ACTOR_TYPE_APP: + return u.getMessageEditStakeAppFee() + case coreTypes.ActorType_ACTOR_TYPE_FISH: + return u.getMessageEditStakeFishermanFee() + case coreTypes.ActorType_ACTOR_TYPE_SERVICER: + return u.getMessageEditStakeServicerFee() + case coreTypes.ActorType_ACTOR_TYPE_VAL: + return u.getMessageEditStakeValidatorFee() + default: + return nil, typesUtil.ErrUnknownActorType(actorType.String()) + } + case *typesUtil.MessageUnstake: + switch actorType { + case coreTypes.ActorType_ACTOR_TYPE_APP: + return u.getMessageUnstakeAppFee() + case coreTypes.ActorType_ACTOR_TYPE_FISH: + return u.getMessageUnstakeFishermanFee() + case coreTypes.ActorType_ACTOR_TYPE_SERVICER: + return u.getMessageUnstakeServicerFee() + case coreTypes.ActorType_ACTOR_TYPE_VAL: + return u.getMessageUnstakeValidatorFee() + default: + return nil, typesUtil.ErrUnknownActorType(actorType.String()) + } + case *typesUtil.MessageUnpause: + switch actorType { + case coreTypes.ActorType_ACTOR_TYPE_APP: + return u.getMessageUnpauseAppFee() + case coreTypes.ActorType_ACTOR_TYPE_FISH: + return u.getMessageUnpauseFishermanFee() + case coreTypes.ActorType_ACTOR_TYPE_SERVICER: + return u.getMessageUnpauseServicerFee() + case coreTypes.ActorType_ACTOR_TYPE_VAL: + return u.getMessageUnpauseValidatorFee() + default: + return nil, typesUtil.ErrUnknownActorType(actorType.String()) + } + case *typesUtil.MessageChangeParameter: + return u.getMessageChangeParameterFee() + default: + return nil, typesUtil.ErrUnknownMessage(x) + } +} + +func (u *baseUtilityUnitOfWork) getMessageChangeParameterSignerCandidates(msg *typesUtil.MessageChangeParameter) ([][]byte, typesUtil.Error) { + owner, err := u.getParamOwner(msg.ParameterKey) + if err != nil { + return nil, typesUtil.ErrGetParam(msg.ParameterKey, err) + } + return [][]byte{owner}, nil +} + +func (u *baseUtilityUnitOfWork) getBigIntParam(paramName string) (*big.Int, typesUtil.Error) { + value, err := u.persistenceReadContext.GetStringParam(paramName, u.height) + if err != nil { + u.logger.Err(err) + return nil, typesUtil.ErrGetParam(paramName, err) + } + amount, err := utils.StringToBigInt(value) + if err != nil { + return nil, typesUtil.ErrStringToBigInt(err) + } + return amount, nil +} + +func (u *baseUtilityUnitOfWork) getIntParam(paramName string) (int, typesUtil.Error) { + value, err := u.persistenceReadContext.GetIntParam(paramName, u.height) + if err != nil { + return 0, typesUtil.ErrGetParam(paramName, err) + } + return value, nil +} + +func (u *baseUtilityUnitOfWork) getInt64Param(paramName string) (int64, typesUtil.Error) { + value, err := u.persistenceReadContext.GetIntParam(paramName, u.height) + if err != nil { + return 0, typesUtil.ErrGetParam(paramName, err) + } + return int64(value), nil +} + +func (u *baseUtilityUnitOfWork) getByteArrayParam(paramName string) ([]byte, typesUtil.Error) { + value, err := u.persistenceReadContext.GetBytesParam(paramName, u.height) + if err != nil { + return nil, typesUtil.ErrGetParam(paramName, err) + } + return value, nil +} diff --git a/utility/gov_test.go b/utility/unit_of_work/gov_test.go similarity index 99% rename from utility/gov_test.go rename to utility/unit_of_work/gov_test.go index 1be3a8a89..bf764be43 100644 --- a/utility/gov_test.go +++ b/utility/unit_of_work/gov_test.go @@ -1,4 +1,4 @@ -package utility +package unit_of_work import ( "encoding/hex" diff --git a/utility/message_test.go b/utility/unit_of_work/message_test.go similarity index 99% rename from utility/message_test.go rename to utility/unit_of_work/message_test.go index e74f72b09..5a9a50c92 100644 --- a/utility/message_test.go +++ b/utility/unit_of_work/message_test.go @@ -1,4 +1,4 @@ -package utility +package unit_of_work import ( "encoding/hex" diff --git a/utility/unit_of_work/module.go b/utility/unit_of_work/module.go new file mode 100644 index 000000000..f16d2655c --- /dev/null +++ b/utility/unit_of_work/module.go @@ -0,0 +1,209 @@ +package unit_of_work + +import ( + coreTypes "github.com/pokt-network/pocket/shared/core/types" + "github.com/pokt-network/pocket/shared/modules" + "github.com/pokt-network/pocket/shared/modules/base_modules" + utilTypes "github.com/pokt-network/pocket/utility/types" +) + +const ( + leaderUtilityUOWModuleName = "leader_utility_unit_of_work" + replicaUtilityUOWModuleName = "replica_utility_unit_of_work" +) + +var _ modules.UtilityUnitOfWork = &baseUtilityUnitOfWork{} + +type baseUtilityUnitOfWork struct { + base_modules.IntegratableModule + + logger *modules.Logger + + height int64 + + persistenceReadContext modules.PersistenceReadContext + persistenceRWContext modules.PersistenceRWContext + + // TECHDEBT: Consolidate all these types with the shared Protobuf struct and create a `proposalBlock` + proposalStateHash string + proposalProposerAddr []byte + proposalBlockTxs [][]byte +} + +func (uow *baseUtilityUnitOfWork) SetProposalBlock(blockHash string, proposerAddr []byte, txs [][]byte) error { + uow.proposalStateHash = blockHash + uow.proposalProposerAddr = proposerAddr + uow.proposalBlockTxs = txs + return nil +} + +// CreateAndApplyProposalBlock implements the exposed functionality of the shared UtilityContext interface. +func (u *baseUtilityUnitOfWork) CreateAndApplyProposalBlock(proposer []byte, maxTransactionBytes int) (stateHash string, txs [][]byte, err error) { + prevBlockByzantineVals, err := u.prevBlockByzantineValidators() + if err != nil { + return "", nil, err + } + + // begin block lifecycle phase + if err := u.beginBlock(prevBlockByzantineVals); err != nil { + return "", nil, err + } + txs = make([][]byte, 0) + txsTotalBz := 0 + txIdx := 0 + + mempool := u.GetBus().GetUtilityModule().GetMempool() + for !mempool.IsEmpty() { + // NB: In order for transactions to have entered the mempool, `HandleTransaction` must have + // been called which handles basic checks & validation. + txBz, err := mempool.PopTx() + if err != nil { + return "", nil, err + } + + tx, err := coreTypes.TxFromBytes(txBz) + if err != nil { + return "", nil, err + } + + txBzSize := len(txBz) + txsTotalBz += txBzSize + + // Exceeding maximum transaction bytes to be added in this block + if txsTotalBz >= maxTransactionBytes { + // Add back popped tx to be applied in a future block + if err := mempool.AddTx(txBz); err != nil { + return "", nil, err + } + break // we've reached our max + } + + txResult, err := u.hydrateTxResult(tx, txIdx) + if err != nil { + u.logger.Err(err).Msg("Error in ApplyTransaction") + // TODO(#327): Properly implement 'unhappy path' for save points + if err := u.revertLastSavePoint(); err != nil { + return "", nil, err + } + txsTotalBz -= txBzSize + continue + } + + // Index the transaction + if err := u.persistenceRWContext.IndexTransaction(txResult); err != nil { + u.logger.Fatal().Err(err).Msgf("TODO(#327): The transaction can by hydrated but not indexed. Crash the process for now: %v\n", err) + } + + txs = append(txs, txBz) + txIdx++ + } + + if err := u.endBlock(proposer); err != nil { + return "", nil, err + } + + // TODO: @deblasis - this should be from a ReadContext (the ephemeral/staging one) + // Compute & return the new state hash + stateHash, err = u.persistenceRWContext.ComputeStateHash() + if err != nil { + u.logger.Fatal().Err(err).Msg("Updating the app hash failed. TODO: Look into roll-backing the entire commit...") + } + u.logger.Info().Str("state_hash", stateHash).Msgf("CreateAndApplyProposalBlock finished successfully") + + return stateHash, txs, err +} + +// CLEANUP: code re-use ApplyBlock() for CreateAndApplyBlock() +func (u *baseUtilityUnitOfWork) ApplyBlock() (string, [][]byte, error) { + lastByzantineValidators, err := u.prevBlockByzantineValidators() + if err != nil { + return "", nil, err + } + + // begin block lifecycle phase + if err := u.beginBlock(lastByzantineValidators); err != nil { + return "", nil, err + } + + mempool := u.GetBus().GetUtilityModule().GetMempool() + + // deliver txs lifecycle phase + for index, txProtoBytes := range u.proposalBlockTxs { + tx, err := coreTypes.TxFromBytes(txProtoBytes) + if err != nil { + return "", nil, err + } + if err := tx.ValidateBasic(); err != nil { + return "", nil, err + } + // TODO(#346): Currently, the pattern is allowing nil err with an error transaction... + // Should we terminate applyBlock immediately if there's an invalid transaction? + // Or wait until the entire lifecycle is over to evaluate an 'invalid' block + + // Validate and apply the transaction to the Postgres database + txResult, err := u.hydrateTxResult(tx, index) + if err != nil { + return "", nil, err + } + + txHash, err := tx.Hash() + if err != nil { + return "", nil, err + } + + // TODO: Need to properly add transactions back on rollbacks + if mempool.Contains(txHash) { + if err := mempool.RemoveTx(txProtoBytes); err != nil { + return "", nil, err + } + u.logger.Info().Str("tx_hash", txHash).Msg("Applying tx that WAS in the local mempool") + } else { + u.logger.Info().Str("tx_hash", txHash).Msg("Applying tx that WAS NOT in the local mempool") + } + + if err := u.persistenceRWContext.IndexTransaction(txResult); err != nil { + u.logger.Fatal().Err(err).Msgf("TODO(#327): We can apply the transaction but not index it. Crash the process for now: %v\n", err) + } + } + + // end block lifecycle phase + if err := u.endBlock(u.proposalProposerAddr); err != nil { + return "", nil, err + } + // TODO: @deblasis - this should be from a ReadContext (the ephemeral/staging one) + // return the app hash (consensus module will get the validator set directly) + stateHash, err := u.persistenceRWContext.ComputeStateHash() + if err != nil { + u.logger.Fatal().Err(err).Msg("Updating the app hash failed. TODO: Look into roll-backing the entire commit...") + return "", nil, utilTypes.ErrAppHash(err) + } + u.logger.Info().Msgf("ApplyBlock - computed state hash: %s", stateHash) + + // return the app hash; consensus module will get the validator set directly + return stateHash, nil, nil +} + +func (uow *baseUtilityUnitOfWork) Commit(quorumCert []byte) error { + // TODO: @deblasis - change tracking here + + uow.logger.Debug().Msg("committing the rwPersistenceContext...") + if err := uow.persistenceRWContext.Commit(uow.proposalProposerAddr, quorumCert); err != nil { + return err + } + uow.persistenceRWContext = nil + return nil +} + +func (uow *baseUtilityUnitOfWork) Release() error { + // TODO: @deblasis - change tracking reset here + + if uow.persistenceRWContext == nil { + return nil + } + + if err := uow.persistenceRWContext.Release(); err != nil { + return err + } + uow.persistenceRWContext = nil + return nil +} diff --git a/utility/module_test.go b/utility/unit_of_work/module_test.go similarity index 78% rename from utility/module_test.go rename to utility/unit_of_work/module_test.go index 5bc10ff1e..dec28e2a0 100644 --- a/utility/module_test.go +++ b/utility/unit_of_work/module_test.go @@ -1,4 +1,4 @@ -package utility +package unit_of_work import ( "log" @@ -28,7 +28,7 @@ const ( ) var ( - testUtilityMod modules.UtilityModule + // testUtilityMod mock_modules.MockUtilityModule // TODO(#261): Utility module tests should have no dependencies on the persistence module (which instantiates a postgres container) testPersistenceMod modules.PersistenceModule @@ -47,7 +47,7 @@ func TestMain(m *testing.M) { log.Fatalf("Error creating bus: %s", err) } - testUtilityMod = newTestUtilityModule(bus) + //testUtilityMod = newTestUtilityModule(bus) testPersistenceMod = newTestPersistenceModule(bus) exitCode := m.Run() @@ -55,7 +55,7 @@ func TestMain(m *testing.M) { os.Exit(exitCode) } -func newTestingUtilityContext(t *testing.T, height int64) *utilityContext { +func newTestingUtilityContext(t *testing.T, height int64) *baseUtilityUnitOfWork { persistenceContext, err := testPersistenceMod.NewRWContext(height) require.NoError(t, err) @@ -68,28 +68,32 @@ func newTestingUtilityContext(t *testing.T, height int64) *utilityContext { Action: messaging.DebugMessageAction_DEBUG_PERSISTENCE_RESET_TO_GENESIS, Message: nil, })) - testUtilityMod.GetMempool().Clear() + //testUtilityMod.GetMempool().Clear() }) - ctx := &utilityContext{ - logger: logger.Global.CreateLoggerForModule(modules.UtilityModuleName), - height: height, - store: persistenceContext, - savePointsSet: make(map[string]struct{}), - savePointsList: make([][]byte, 0), + ctx := &baseUtilityUnitOfWork{ + logger: logger.Global.CreateLoggerForModule(modules.UtilityModuleName), + height: height, + // TODO: - @deblasis: refactor + persistenceRWContext: persistenceContext, + persistenceReadContext: persistenceContext, } - ctx.SetBus(testUtilityMod.GetBus()) + + utilityMod := mock_modules.NewMockUtilityModule() + + ctx.SetBus(testPersistenceMod.GetBus()) return ctx } -func newTestUtilityModule(bus modules.Bus) modules.UtilityModule { - utilityMod, err := Create(bus) - if err != nil { - log.Fatalf("Error creating persistence module: %s", err) - } - return utilityMod.(modules.UtilityModule) -} +// func newTestUtilityModule(bus modules.Bus) modules.UtilityModule { +// ctrl := gomock.NewController(nil) +// utilityMod = mock_modules.NewMockUtilityModule(bus) +// if err != nil { +// log.Fatalf("Error creating persistence module: %s", err) +// } +// return utilityMod.(modules.UtilityModule) +// } func newTestPersistenceModule(bus modules.Bus) modules.PersistenceModule { persistenceMod, err := persistence.Create(bus) diff --git a/utility/unit_of_work/transaction.go b/utility/unit_of_work/transaction.go new file mode 100644 index 000000000..14b84b59a --- /dev/null +++ b/utility/unit_of_work/transaction.go @@ -0,0 +1,90 @@ +package unit_of_work + +import ( + "bytes" + "fmt" + + coreTypes "github.com/pokt-network/pocket/shared/core/types" + "github.com/pokt-network/pocket/shared/crypto" + "github.com/pokt-network/pocket/shared/modules" + typesUtil "github.com/pokt-network/pocket/utility/types" +) + +// hydrateTxResult converts a `Transaction` proto into a `TxResult` struct` after doing basic validation +// and extracting the relevant data from the embedded signed Message. `index` is the intended location +// of its index (i.e. the transaction number) in the block where it is included. +// +// IMPROVE: hydration should accept and return the same type (i.e. TxResult) so there may be opportunity +// to refactor this in the future. +func (u *baseUtilityUnitOfWork) hydrateTxResult(tx *coreTypes.Transaction, index int) (modules.TxResult, typesUtil.Error) { + msg, err := u.anteHandleMessage(tx) + if err != nil { + return nil, err + } + msgHandlingResult := u.handleMessage(msg) + return typesUtil.TxToTxResult(tx, u.height, index, msg, msgHandlingResult) +} + +// anteHandleMessage handles basic validation of the message in the Transaction before it is processed +// REFACTOR: Splitting this into a `feeValidation`, `signerValidation`, and `messageValidation` etc +// would make it more modular and readable. +func (u *baseUtilityUnitOfWork) anteHandleMessage(tx *coreTypes.Transaction) (typesUtil.Message, typesUtil.Error) { + // Check if the transaction has a valid message + anyMsg, er := tx.GetMessage() + if er != nil { + return nil, typesUtil.ErrDecodeMessage(er) + } + msg, ok := anyMsg.(typesUtil.Message) + if !ok { + return nil, typesUtil.ErrDecodeMessage(fmt.Errorf("not a supported message type")) + } + + // Get the address of the transaction signer + pubKey, er := crypto.NewPublicKeyFromBytes(tx.Signature.PublicKey) + if er != nil { + return nil, typesUtil.ErrNewPublicKeyFromBytes(er) + } + address := pubKey.Address() + addressHex := address.ToString() + + // Validate that the signer has enough funds to pay the fee of the message signed + fee, err := u.getFee(msg, msg.GetActorType()) + if err != nil { + return nil, err + } + accountAmount, err := u.getAccountAmount(address) + if err != nil { + return nil, typesUtil.ErrGetAccountAmount(err) + } + accountAmount.Sub(accountAmount, fee) + if accountAmount.Sign() == -1 { + return nil, typesUtil.ErrInsufficientAmount(addressHex) + } + + // Validate that the signer has a valid signature + var isValidSigner bool + signerCandidates, err := u.getSignerCandidates(msg) + if err != nil { + return nil, err + } + for _, candidate := range signerCandidates { + if bytes.Equal(candidate, address) { + isValidSigner = true + msg.SetSigner(address) + break + } + } + if !isValidSigner { + return nil, typesUtil.ErrInvalidSigner(addressHex) + } + + // Remove the fee from the signer's account and add it to the fee collector pool + if err := u.setAccountAmount(address, accountAmount); err != nil { + return nil, err + } + if err := u.addPoolAmount(coreTypes.Pools_POOLS_FEE_COLLECTOR.FriendlyName(), fee); err != nil { + return nil, err + } + + return msg, nil +} diff --git a/utility/transaction_test.go b/utility/unit_of_work/transaction_test.go similarity index 75% rename from utility/transaction_test.go rename to utility/unit_of_work/transaction_test.go index 4c7cc9244..796654c59 100644 --- a/utility/transaction_test.go +++ b/utility/unit_of_work/transaction_test.go @@ -1,4 +1,4 @@ -package utility +package unit_of_work import ( "encoding/hex" @@ -7,11 +7,9 @@ import ( "github.com/pokt-network/pocket/runtime/test_artifacts" "github.com/pokt-network/pocket/shared/codec" - "github.com/pokt-network/pocket/shared/core/types" coreTypes "github.com/pokt-network/pocket/shared/core/types" "github.com/pokt-network/pocket/shared/crypto" "github.com/pokt-network/pocket/shared/utils" - typesUtil "github.com/pokt-network/pocket/utility/types" "github.com/stretchr/testify/require" ) @@ -53,19 +51,20 @@ func TestUtilityContext_ApplyTransaction(t *testing.T) { require.Equal(t, expectedAfterBalance, amount, "unexpected after balance") } -func TestUtilityContext_HandleTransaction(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - tx, _, _, _ := newTestingTransaction(t, ctx) +//TODO: - @deblasis - refactor this +// func TestUtilityContext_HandleTransaction(t *testing.T) { +// ctx := newTestingUtilityContext(t, 0) +// tx, _, _, _ := newTestingTransaction(t, ctx) - txBz, err := tx.Bytes() - require.NoError(t, err) - require.NoError(t, testUtilityMod.HandleTransaction(txBz)) +// txBz, err := tx.Bytes() +// require.NoError(t, err) +// require.NoError(t, testUtilityMod.HandleTransaction(txBz)) - hash, err := tx.Hash() - require.NoError(t, err) - require.True(t, testUtilityMod.GetMempool().Contains(hash)) - require.Equal(t, testUtilityMod.HandleTransaction(txBz).Error(), typesUtil.ErrDuplicateTransaction().Error()) -} +// hash, err := tx.Hash() +// require.NoError(t, err) +// require.True(t, testUtilityMod.GetMempool().Contains(hash)) +// require.Equal(t, testUtilityMod.HandleTransaction(txBz).Error(), typesUtil.ErrDuplicateTransaction().Error()) +// } func TestUtilityContext_GetSignerCandidates(t *testing.T) { ctx := newTestingUtilityContext(t, 0) @@ -85,21 +84,22 @@ func TestUtilityContext_GetSignerCandidates(t *testing.T) { require.Equal(t, accs[0].GetAddress(), hex.EncodeToString(candidates[0]), "unexpected signer candidate") } -func TestUtilityContext_CreateAndApplyBlock(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - tx, _, _, _ := newTestingTransaction(t, ctx) +//TODO: - @deblasis - refactor this +// func TestUtilityContext_CreateAndApplyBlock(t *testing.T) { +// ctx := newTestingUtilityContext(t, 0) +// tx, _, _, _ := newTestingTransaction(t, ctx) - proposer := getFirstActor(t, ctx, types.ActorType_ACTOR_TYPE_VAL) - txBz, err := tx.Bytes() - require.NoError(t, err) - require.NoError(t, testUtilityMod.HandleTransaction(txBz)) +// proposer := getFirstActor(t, ctx, types.ActorType_ACTOR_TYPE_VAL) +// txBz, err := tx.Bytes() +// require.NoError(t, err) +// require.NoError(t, testUtilityMod.HandleTransaction(txBz)) - appHash, txs, er := ctx.CreateAndApplyProposalBlock([]byte(proposer.GetAddress()), 10000) - require.NoError(t, er) - require.NotEmpty(t, appHash) - require.Equal(t, 1, len(txs)) - require.Equal(t, txs[0], txBz) -} +// appHash, txs, er := ctx.CreateAndApplyProposalBlock([]byte(proposer.GetAddress()), 10000) +// require.NoError(t, er) +// require.NotEmpty(t, appHash) +// require.Equal(t, 1, len(txs)) +// require.Equal(t, txs[0], txBz) +// } func TestUtilityContext_HandleMessage(t *testing.T) { ctx := newTestingUtilityContext(t, 0) @@ -129,7 +129,7 @@ func TestUtilityContext_HandleMessage(t *testing.T) { require.Equal(t, sendAmount, big.NewInt(0).Sub(recipientBalanceAfter, recipientBalanceBefore), "unexpected recipient balance") } -func newTestingTransaction(t *testing.T, ctx *utilityContext) (tx *coreTypes.Transaction, startingBalance, amountSent *big.Int, signer crypto.PrivateKey) { +func newTestingTransaction(t *testing.T, ctx *baseUtilityUnitOfWork) (tx *coreTypes.Transaction, startingBalance, amountSent *big.Int, signer crypto.PrivateKey) { amountSent = new(big.Int).Set(defaultSendAmount) startingBalance = new(big.Int).Set(test_artifacts.DefaultAccountAmount) diff --git a/utility/tx_message_handler.go b/utility/unit_of_work/tx_message_handler.go similarity index 72% rename from utility/tx_message_handler.go rename to utility/unit_of_work/tx_message_handler.go index cbcb03c10..401201f81 100644 --- a/utility/tx_message_handler.go +++ b/utility/unit_of_work/tx_message_handler.go @@ -1,4 +1,4 @@ -package utility +package unit_of_work import ( "encoding/hex" @@ -11,7 +11,7 @@ import ( typesUtil "github.com/pokt-network/pocket/utility/types" ) -func (u *utilityContext) handleMessage(msg typesUtil.Message) (err typesUtil.Error) { +func (u *baseUtilityUnitOfWork) handleMessage(msg typesUtil.Message) (err typesUtil.Error) { switch x := msg.(type) { case *typesUtil.MessageSend: return u.handleMessageSend(x) @@ -30,7 +30,7 @@ func (u *utilityContext) handleMessage(msg typesUtil.Message) (err typesUtil.Err } } -func (u *utilityContext) handleMessageSend(message *typesUtil.MessageSend) typesUtil.Error { +func (u *baseUtilityUnitOfWork) handleMessageSend(message *typesUtil.MessageSend) typesUtil.Error { // convert the amount to big.Int amount, er := utils.StringToBigInt(message.Amount) if er != nil { @@ -59,7 +59,7 @@ func (u *utilityContext) handleMessageSend(message *typesUtil.MessageSend) types return nil } -func (u *utilityContext) handleStakeMessage(message *typesUtil.MessageStake) typesUtil.Error { +func (u *baseUtilityUnitOfWork) handleStakeMessage(message *typesUtil.MessageStake) typesUtil.Error { publicKey, er := crypto.NewPublicKeyFromBytes(message.PublicKey) if er != nil { return typesUtil.ErrNewPublicKeyFromBytes(er) @@ -102,13 +102,13 @@ func (u *utilityContext) handleStakeMessage(message *typesUtil.MessageStake) typ // insert actor switch message.ActorType { case coreTypes.ActorType_ACTOR_TYPE_APP: - er = u.store.InsertApp(publicKey.Address(), publicKey.Bytes(), message.OutputAddress, false, int32(coreTypes.StakeStatus_Staked), message.Amount, message.Chains, typesUtil.HeightNotUsed, typesUtil.HeightNotUsed) + er = u.persistenceRWContext.InsertApp(publicKey.Address(), publicKey.Bytes(), message.OutputAddress, false, int32(coreTypes.StakeStatus_Staked), message.Amount, message.Chains, typesUtil.HeightNotUsed, typesUtil.HeightNotUsed) case coreTypes.ActorType_ACTOR_TYPE_FISH: - er = u.store.InsertFisherman(publicKey.Address(), publicKey.Bytes(), message.OutputAddress, false, int32(coreTypes.StakeStatus_Staked), message.ServiceUrl, message.Amount, message.Chains, typesUtil.HeightNotUsed, typesUtil.HeightNotUsed) + er = u.persistenceRWContext.InsertFisherman(publicKey.Address(), publicKey.Bytes(), message.OutputAddress, false, int32(coreTypes.StakeStatus_Staked), message.ServiceUrl, message.Amount, message.Chains, typesUtil.HeightNotUsed, typesUtil.HeightNotUsed) case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - er = u.store.InsertServicer(publicKey.Address(), publicKey.Bytes(), message.OutputAddress, false, int32(coreTypes.StakeStatus_Staked), message.ServiceUrl, message.Amount, message.Chains, typesUtil.HeightNotUsed, typesUtil.HeightNotUsed) + er = u.persistenceRWContext.InsertServicer(publicKey.Address(), publicKey.Bytes(), message.OutputAddress, false, int32(coreTypes.StakeStatus_Staked), message.ServiceUrl, message.Amount, message.Chains, typesUtil.HeightNotUsed, typesUtil.HeightNotUsed) case coreTypes.ActorType_ACTOR_TYPE_VAL: - er = u.store.InsertValidator(publicKey.Address(), publicKey.Bytes(), message.OutputAddress, false, int32(coreTypes.StakeStatus_Staked), message.ServiceUrl, message.Amount, typesUtil.HeightNotUsed, typesUtil.HeightNotUsed) + er = u.persistenceRWContext.InsertValidator(publicKey.Address(), publicKey.Bytes(), message.OutputAddress, false, int32(coreTypes.StakeStatus_Staked), message.ServiceUrl, message.Amount, typesUtil.HeightNotUsed, typesUtil.HeightNotUsed) } if er != nil { return typesUtil.ErrInsert(er) @@ -116,7 +116,7 @@ func (u *utilityContext) handleStakeMessage(message *typesUtil.MessageStake) typ return nil } -func (u *utilityContext) handleEditStakeMessage(message *typesUtil.MessageEditStake) typesUtil.Error { +func (u *baseUtilityUnitOfWork) handleEditStakeMessage(message *typesUtil.MessageEditStake) typesUtil.Error { // ensure actor exists if exists, err := u.getActorExists(message.ActorType, message.Address); err != nil || !exists { if !exists { @@ -159,13 +159,13 @@ func (u *utilityContext) handleEditStakeMessage(message *typesUtil.MessageEditSt } switch message.ActorType { case coreTypes.ActorType_ACTOR_TYPE_APP: - er = u.store.UpdateApp(message.Address, message.Amount, message.Chains) + er = u.persistenceRWContext.UpdateApp(message.Address, message.Amount, message.Chains) case coreTypes.ActorType_ACTOR_TYPE_FISH: - er = u.store.UpdateFisherman(message.Address, message.ServiceUrl, message.Amount, message.Chains) + er = u.persistenceRWContext.UpdateFisherman(message.Address, message.ServiceUrl, message.Amount, message.Chains) case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - er = u.store.UpdateServicer(message.Address, message.ServiceUrl, message.Amount, message.Chains) + er = u.persistenceRWContext.UpdateServicer(message.Address, message.ServiceUrl, message.Amount, message.Chains) case coreTypes.ActorType_ACTOR_TYPE_VAL: - er = u.store.UpdateValidator(message.Address, message.ServiceUrl, message.Amount) + er = u.persistenceRWContext.UpdateValidator(message.Address, message.ServiceUrl, message.Amount) } if er != nil { return typesUtil.ErrInsert(er) @@ -173,7 +173,7 @@ func (u *utilityContext) handleEditStakeMessage(message *typesUtil.MessageEditSt return nil } -func (u *utilityContext) handleUnstakeMessage(message *typesUtil.MessageUnstake) typesUtil.Error { +func (u *baseUtilityUnitOfWork) handleUnstakeMessage(message *typesUtil.MessageUnstake) typesUtil.Error { if status, err := u.getActorStatus(message.ActorType, message.Address); err != nil || status != coreTypes.StakeStatus_Staked { if status != coreTypes.StakeStatus_Staked { return typesUtil.ErrInvalidStatus(status, coreTypes.StakeStatus_Staked) @@ -190,7 +190,7 @@ func (u *utilityContext) handleUnstakeMessage(message *typesUtil.MessageUnstake) return nil } -func (u *utilityContext) handleUnpauseMessage(message *typesUtil.MessageUnpause) typesUtil.Error { +func (u *baseUtilityUnitOfWork) handleUnpauseMessage(message *typesUtil.MessageUnpause) typesUtil.Error { pausedHeight, err := u.getPausedHeightIfExists(message.ActorType, message.Address) if err != nil { return err @@ -211,7 +211,7 @@ func (u *utilityContext) handleUnpauseMessage(message *typesUtil.MessageUnpause) return nil } -func (u *utilityContext) handleMessageChangeParameter(message *typesUtil.MessageChangeParameter) typesUtil.Error { +func (u *baseUtilityUnitOfWork) handleMessageChangeParameter(message *typesUtil.MessageChangeParameter) typesUtil.Error { v, err := codec.GetCodec().FromAny(message.ParameterValue) if err != nil { return typesUtil.ErrProtoFromAny(err) @@ -220,7 +220,7 @@ func (u *utilityContext) handleMessageChangeParameter(message *typesUtil.Message } // REFACTOR: This can be moved over into utility/types/message.go -func (u *utilityContext) getSignerCandidates(msg typesUtil.Message) ([][]byte, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getSignerCandidates(msg typesUtil.Message) ([][]byte, typesUtil.Error) { switch x := msg.(type) { case *typesUtil.MessageSend: return u.getMessageSendSignerCandidates(x) @@ -237,7 +237,7 @@ func (u *utilityContext) getSignerCandidates(msg typesUtil.Message) ([][]byte, t } } -func (u *utilityContext) getMessageStakeSignerCandidates(msg *typesUtil.MessageStake) ([][]byte, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getMessageStakeSignerCandidates(msg *typesUtil.MessageStake) ([][]byte, typesUtil.Error) { pk, er := crypto.NewPublicKeyFromBytes(msg.PublicKey) if er != nil { return nil, typesUtil.ErrNewPublicKeyFromBytes(er) @@ -247,7 +247,7 @@ func (u *utilityContext) getMessageStakeSignerCandidates(msg *typesUtil.MessageS return candidates, nil } -func (u *utilityContext) getMessageEditStakeSignerCandidates(msg *typesUtil.MessageEditStake) ([][]byte, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getMessageEditStakeSignerCandidates(msg *typesUtil.MessageEditStake) ([][]byte, typesUtil.Error) { output, err := u.getActorOutputAddress(msg.ActorType, msg.Address) if err != nil { return nil, err @@ -257,7 +257,7 @@ func (u *utilityContext) getMessageEditStakeSignerCandidates(msg *typesUtil.Mess return candidates, nil } -func (u *utilityContext) getMessageUnstakeSignerCandidates(msg *typesUtil.MessageUnstake) ([][]byte, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getMessageUnstakeSignerCandidates(msg *typesUtil.MessageUnstake) ([][]byte, typesUtil.Error) { output, err := u.getActorOutputAddress(msg.ActorType, msg.Address) if err != nil { return nil, err @@ -267,7 +267,7 @@ func (u *utilityContext) getMessageUnstakeSignerCandidates(msg *typesUtil.Messag return candidates, nil } -func (u *utilityContext) getMessageUnpauseSignerCandidates(msg *typesUtil.MessageUnpause) ([][]byte, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getMessageUnpauseSignerCandidates(msg *typesUtil.MessageUnpause) ([][]byte, typesUtil.Error) { output, err := u.getActorOutputAddress(msg.ActorType, msg.Address) if err != nil { return nil, err @@ -277,11 +277,11 @@ func (u *utilityContext) getMessageUnpauseSignerCandidates(msg *typesUtil.Messag return candidates, nil } -func (u *utilityContext) getMessageSendSignerCandidates(msg *typesUtil.MessageSend) ([][]byte, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) getMessageSendSignerCandidates(msg *typesUtil.MessageSend) ([][]byte, typesUtil.Error) { return [][]byte{msg.FromAddress}, nil } -func (u *utilityContext) checkBelowMaxChains(actorType coreTypes.ActorType, chains []string) typesUtil.Error { +func (u *baseUtilityUnitOfWork) checkBelowMaxChains(actorType coreTypes.ActorType, chains []string) typesUtil.Error { // validators don't have chains field if actorType == coreTypes.ActorType_ACTOR_TYPE_VAL { return nil @@ -297,7 +297,7 @@ func (u *utilityContext) checkBelowMaxChains(actorType coreTypes.ActorType, chai return nil } -func (u *utilityContext) checkAboveMinStake(actorType coreTypes.ActorType, amountStr string) (*big.Int, typesUtil.Error) { +func (u *baseUtilityUnitOfWork) checkAboveMinStake(actorType coreTypes.ActorType, amountStr string) (*big.Int, typesUtil.Error) { minStake, err := u.getMinRequiredStakeAmount(actorType) if err != nil { return nil, err diff --git a/utility/unit_of_work/leader.go b/utility/unit_of_work/uow_leader.go similarity index 64% rename from utility/unit_of_work/leader.go rename to utility/unit_of_work/uow_leader.go index dab0e1fd7..13a92d9ec 100644 --- a/utility/unit_of_work/leader.go +++ b/utility/unit_of_work/uow_leader.go @@ -17,6 +17,7 @@ type leaderUtilityUnitOfWork struct { func NewForLeader(height int64, readContext modules.PersistenceReadContext, rwPersistenceContext modules.PersistenceRWContext) *leaderUtilityUnitOfWork { return &leaderUtilityUnitOfWork{ baseUtilityUnitOfWork: baseUtilityUnitOfWork{ + height: height, persistenceReadContext: readContext, persistenceRWContext: rwPersistenceContext, logger: logger.Global.CreateLoggerForModule(leaderUtilityUOWModuleName), @@ -24,19 +25,19 @@ func NewForLeader(height int64, readContext modules.PersistenceReadContext, rwPe } } -func (uow *leaderUtilityUnitOfWork) CreateProposalBlock(proposer []byte, maxTxBytes uint64, beforeApplyBlock, afterApplyBlock func(modules.UtilityUnitOfWork) error) (stateHash string, txs [][]byte, err error) { - if beforeApplyBlock != nil { - uow.logger.Debug().Msg("running beforeApplyBlock...") - if err := beforeApplyBlock(uow); err != nil { - return "", nil, err - } - } +func (uow *leaderUtilityUnitOfWork) CreateProposalBlock(proposer []byte, maxTxBytes uint64) (stateHash string, txs [][]byte, err error) { + // if burnFn != nil { + // uow.logger.Debug().Msg("running burnFn...") + // if err := burnFn(uow); err != nil { + // return "", nil, err + // } + // } - if afterApplyBlock != nil { - uow.logger.Debug().Msg("running afterApplyBlock...") - if err := afterApplyBlock(uow); err != nil { - return "", nil, err - } - } + // if rewardFn != nil { + // uow.logger.Debug().Msg("running rewardFn...") + // if err := rewardFn(uow); err != nil { + // return "", nil, err + // } + // } panic("unimplemented") } diff --git a/utility/unit_of_work/replica.go b/utility/unit_of_work/uow_replica.go similarity index 95% rename from utility/unit_of_work/replica.go rename to utility/unit_of_work/uow_replica.go index d9e44f2bb..004bc2895 100644 --- a/utility/unit_of_work/replica.go +++ b/utility/unit_of_work/uow_replica.go @@ -17,6 +17,7 @@ type replicaUtilityUnitOfWork struct { func NewForReplica(height int64, readContext modules.PersistenceReadContext, rwPersistenceContext modules.PersistenceRWContext) *replicaUtilityUnitOfWork { return &replicaUtilityUnitOfWork{ baseUtilityUnitOfWork: baseUtilityUnitOfWork{ + height: height, persistenceReadContext: readContext, persistenceRWContext: rwPersistenceContext, logger: logger.Global.CreateLoggerForModule(replicaUtilityUOWModuleName), diff --git a/utility/validator.go b/utility/unit_of_work/validator.go similarity index 84% rename from utility/validator.go rename to utility/unit_of_work/validator.go index 774114644..3fad00811 100644 --- a/utility/validator.go +++ b/utility/unit_of_work/validator.go @@ -1,4 +1,4 @@ -package utility +package unit_of_work // Internal business logic specific to validator behaviour and interactions. @@ -13,7 +13,7 @@ import ( // This includes validators who double signed, didn't sign at all or disagree with 2/3+ majority. // IMPROVE: Need to add more logging to this function. // INCOMPLETE: handleByzantineValidators is a WIP and needs to be fully designed, implemented, tested and documented -func (u *utilityContext) handleByzantineValidators(prevBlockByzantineValidators [][]byte) typesUtil.Error { +func (u *baseUtilityUnitOfWork) handleByzantineValidators(prevBlockByzantineValidators [][]byte) typesUtil.Error { maxMissedBlocks, err := u.getValidatorMaxMissedBlocks() if err != nil { return err @@ -21,7 +21,7 @@ func (u *utilityContext) handleByzantineValidators(prevBlockByzantineValidators for _, address := range prevBlockByzantineValidators { // Get the latest number of missed blocks by the validator - numMissedBlocks, err := u.store.GetValidatorMissedBlocks(address, u.height) + numMissedBlocks, err := u.persistenceReadContext.GetValidatorMissedBlocks(address, u.height) if err != nil { return typesUtil.ErrGetMissedBlocks(err) } @@ -31,18 +31,18 @@ func (u *utilityContext) handleByzantineValidators(prevBlockByzantineValidators // handle if under the threshold of max missed blocks if numMissedBlocks < maxMissedBlocks { - if err := u.store.SetValidatorMissedBlocks(address, numMissedBlocks); err != nil { + if err := u.persistenceRWContext.SetValidatorMissedBlocks(address, numMissedBlocks); err != nil { return typesUtil.ErrSetMissedBlocks(err) } continue } // pause the validator for exceeding the threshold: numMissedBlocks >= maxMissedBlocks - if err := u.store.SetValidatorPauseHeight(address, u.height); err != nil { + if err := u.persistenceRWContext.SetValidatorPauseHeight(address, u.height); err != nil { return typesUtil.ErrSetPauseHeight(err) } // update the number of blocks it missed - if err := u.store.SetValidatorMissedBlocks(address, numMissedBlocks); err != nil { + if err := u.persistenceRWContext.SetValidatorMissedBlocks(address, numMissedBlocks); err != nil { return typesUtil.ErrSetMissedBlocks(err) } // burn validator for missing blocks @@ -57,7 +57,7 @@ func (u *utilityContext) handleByzantineValidators(prevBlockByzantineValidators // and begins unstaking if the stake falls below the necessary threshold // REFACTOR: Extend this to support burning other actors types & pools once the logic is implemented // ADDTEST: There are no good tests for this functionality, which MUST be added. -func (u *utilityContext) burnValidator(addr []byte) typesUtil.Error { +func (u *baseUtilityUnitOfWork) burnValidator(addr []byte) typesUtil.Error { actorType := coreTypes.ActorType_ACTOR_TYPE_VAL actorPool := coreTypes.Pools_POOLS_VALIDATOR_STAKE From eb63737673f640c35a748ec1eb632b6252dbf47f Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sat, 11 Mar 2023 01:32:41 +0000 Subject: [PATCH 31/59] =?UTF-8?q?refactor(utility):=20ctx=20->=20uow=20+?= =?UTF-8?q?=20=F0=9F=A7=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utility/unit_of_work/account_test.go | 70 ++-- utility/unit_of_work/actor_test.go | 224 ++++++------ utility/unit_of_work/application_test.go | 6 +- utility/unit_of_work/block_test.go | 61 ++-- utility/unit_of_work/gov_test.go | 434 +++++++++++------------ utility/unit_of_work/message_test.go | 4 +- utility/unit_of_work/module_test.go | 24 +- utility/unit_of_work/transaction_test.go | 42 +-- 8 files changed, 434 insertions(+), 431 deletions(-) diff --git a/utility/unit_of_work/account_test.go b/utility/unit_of_work/account_test.go index cf90f50a6..090315bcb 100644 --- a/utility/unit_of_work/account_test.go +++ b/utility/unit_of_work/account_test.go @@ -13,8 +13,8 @@ import ( ) func TestUtilityContext_AddAccountAmount(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - acc := getFirstTestingAccount(t, ctx) + uow := newTestingUtilityUnitOfWork(t, 0) + acc := getFirstTestingAccount(t, uow) initialAmount, err := utils.StringToBigInt(acc.GetAmount()) require.NoError(t, err) @@ -22,9 +22,9 @@ func TestUtilityContext_AddAccountAmount(t *testing.T) { addAmount := big.NewInt(1) addrBz, err := hex.DecodeString(acc.GetAddress()) require.NoError(t, err) - require.NoError(t, ctx.addAccountAmount(addrBz, addAmount), "add account amount") + require.NoError(t, uow.addAccountAmount(addrBz, addAmount), "add account amount") - afterAmount, err := ctx.getAccountAmount(addrBz) + afterAmount, err := uow.getAccountAmount(addrBz) require.NoError(t, err) expected := initialAmount.Add(initialAmount, addAmount) @@ -32,8 +32,8 @@ func TestUtilityContext_AddAccountAmount(t *testing.T) { } func TestUtilityContext_SubtractAccountAmount(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - acc := getFirstTestingAccount(t, ctx) + uow := newTestingUtilityUnitOfWork(t, 0) + acc := getFirstTestingAccount(t, uow) beforeAmount, err := utils.StringToBigInt(acc.GetAmount()) require.NoError(t, err) @@ -41,9 +41,9 @@ func TestUtilityContext_SubtractAccountAmount(t *testing.T) { subAmount := big.NewInt(100) addrBz, er := hex.DecodeString(acc.GetAddress()) require.NoError(t, er) - require.NoError(t, ctx.subtractAccountAmount(addrBz, subAmount), "sub account amount") + require.NoError(t, uow.subtractAccountAmount(addrBz, subAmount), "sub account amount") - amount, err := ctx.getAccountAmount(addrBz) + amount, err := uow.getAccountAmount(addrBz) require.NoError(t, err) require.NotEqual(t, beforeAmount, amount) @@ -52,30 +52,30 @@ func TestUtilityContext_SubtractAccountAmount(t *testing.T) { } func TestUtilityContext_SetAccountAmount(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) addr, err := crypto.GenerateAddress() require.NoError(t, err) amount := big.NewInt(100) - require.NoError(t, ctx.setAccountAmount(addr, amount), "set account amount") + require.NoError(t, uow.setAccountAmount(addr, amount), "set account amount") - gotAmount, err := ctx.getAccountAmount(addr) + gotAmount, err := uow.getAccountAmount(addr) require.NoError(t, err) require.Equal(t, amount, gotAmount) } func TestUtilityContext_AddPoolAmount(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - pool := getFirstTestingPool(t, ctx) + uow := newTestingUtilityUnitOfWork(t, 0) + pool := getFirstTestingPool(t, uow) initialAmount, err := utils.StringToBigInt(pool.GetAmount()) require.NoError(t, err) addAmount := big.NewInt(1) - require.NoError(t, ctx.addPoolAmount(pool.GetAddress(), addAmount), "add pool amount") + require.NoError(t, uow.addPoolAmount(pool.GetAddress(), addAmount), "add pool amount") - afterAmount, err := ctx.getPoolAmount(pool.GetAddress()) + afterAmount, err := uow.getPoolAmount(pool.GetAddress()) require.NoError(t, err) expected := initialAmount.Add(initialAmount, addAmount) @@ -83,45 +83,45 @@ func TestUtilityContext_AddPoolAmount(t *testing.T) { } func TestUtilityContext_InsertPool(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) testPoolName := "TEST_POOL" amount := big.NewInt(1000) - err := ctx.insertPool(testPoolName, amount) + err := uow.insertPool(testPoolName, amount) require.NoError(t, err, "insert pool") - poolAmount, err := ctx.getPoolAmount(testPoolName) + poolAmount, err := uow.getPoolAmount(testPoolName) require.NoError(t, err) require.Equal(t, amount, poolAmount) } func TestUtilityContext_SetPoolAmount(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - pool := getFirstTestingPool(t, ctx) + uow := newTestingUtilityUnitOfWork(t, 0) + pool := getFirstTestingPool(t, uow) beforeAmount, err := utils.StringToBigInt(pool.GetAmount()) require.NoError(t, err) expectedAfterAmount := big.NewInt(100) - require.NoError(t, ctx.setPoolAmount(pool.GetAddress(), expectedAfterAmount), "set pool amount") + require.NoError(t, uow.setPoolAmount(pool.GetAddress(), expectedAfterAmount), "set pool amount") - amount, err := ctx.getPoolAmount(pool.GetAddress()) + amount, err := uow.getPoolAmount(pool.GetAddress()) require.NoError(t, err) require.NotEqual(t, beforeAmount, amount) require.Equal(t, amount, expectedAfterAmount) } func TestUtilityContext_SubPoolAmount(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - pool := getFirstTestingPool(t, ctx) + uow := newTestingUtilityUnitOfWork(t, 0) + pool := getFirstTestingPool(t, uow) beforeAmountBig := big.NewInt(1000000000000000) - require.NoError(t, ctx.setPoolAmount(pool.GetAddress(), beforeAmountBig)) + require.NoError(t, uow.setPoolAmount(pool.GetAddress(), beforeAmountBig)) subAmount := big.NewInt(100) - require.NoError(t, ctx.subPoolAmount(pool.GetAddress(), subAmount), "sub pool amount") + require.NoError(t, uow.subPoolAmount(pool.GetAddress(), subAmount), "sub pool amount") - amount, err := ctx.getPoolAmount(pool.GetAddress()) + amount, err := uow.getPoolAmount(pool.GetAddress()) require.NoError(t, err) require.NotEqual(t, beforeAmountBig, amount) @@ -129,8 +129,8 @@ func TestUtilityContext_SubPoolAmount(t *testing.T) { require.Equal(t, expected, amount) } -func getAllTestingAccounts(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.Account { - accs, err := ctx.persistenceReadContext.GetAllAccounts(0) +func getAllTestingAccounts(t *testing.T, uow *baseUtilityUnitOfWork) []*coreTypes.Account { + accs, err := uow.persistenceReadContext.GetAllAccounts(0) require.NoError(t, err) sort.Slice(accs, func(i, j int) bool { @@ -139,12 +139,12 @@ func getAllTestingAccounts(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreType return accs } -func getFirstTestingAccount(t *testing.T, ctx *baseUtilityUnitOfWork) *coreTypes.Account { - return getAllTestingAccounts(t, ctx)[0] +func getFirstTestingAccount(t *testing.T, uow *baseUtilityUnitOfWork) *coreTypes.Account { + return getAllTestingAccounts(t, uow)[0] } -func getAllTestingPools(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.Account { - pools, err := ctx.persistenceReadContext.GetAllPools(0) +func getAllTestingPools(t *testing.T, uow *baseUtilityUnitOfWork) []*coreTypes.Account { + pools, err := uow.persistenceReadContext.GetAllPools(0) require.NoError(t, err) sort.Slice(pools, func(i, j int) bool { @@ -153,6 +153,6 @@ func getAllTestingPools(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.A return pools } -func getFirstTestingPool(t *testing.T, ctx *baseUtilityUnitOfWork) *coreTypes.Account { - return getAllTestingPools(t, ctx)[0] +func getFirstTestingPool(t *testing.T, uow *baseUtilityUnitOfWork) *coreTypes.Account { + return getAllTestingPools(t, uow)[0] } diff --git a/utility/unit_of_work/actor_test.go b/utility/unit_of_work/actor_test.go index b50e8fe97..de33676a2 100644 --- a/utility/unit_of_work/actor_test.go +++ b/utility/unit_of_work/actor_test.go @@ -25,7 +25,7 @@ func TestUtilityContext_HandleMessageStake(t *testing.T) { actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.HandleMessageStake", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) pubKey, err := crypto.GeneratePublicKey() require.NoError(t, err) @@ -33,7 +33,7 @@ func TestUtilityContext_HandleMessageStake(t *testing.T) { outputAddress, err := crypto.GenerateAddress() require.NoError(t, err) - err = ctx.setAccountAmount(outputAddress, test_artifacts.DefaultAccountAmount) + err = uow.setAccountAmount(outputAddress, test_artifacts.DefaultAccountAmount) require.NoError(t, err, "error setting account amount error") msg := &typesUtil.MessageStake{ @@ -46,10 +46,10 @@ func TestUtilityContext_HandleMessageStake(t *testing.T) { ActorType: actorType, } - err = ctx.handleStakeMessage(msg) + err = uow.handleStakeMessage(msg) require.NoError(t, err) - actor := getActorByAddr(t, ctx, actorType, pubKey.Address().String()) + actor := getActorByAddr(t, uow, actorType, pubKey.Address().String()) require.Equal(t, actor.GetAddress(), pubKey.Address().String(), "incorrect actor address") require.Equal(t, typesUtil.HeightNotUsed, actor.GetPausedHeight(), "incorrect actorpaused height") require.Equal(t, test_artifacts.DefaultStakeAmountString, actor.GetStakedAmount(), "incorrect actor stake amount") @@ -69,8 +69,8 @@ func TestUtilityContext_HandleMessageEditStake(t *testing.T) { actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.HandleMessageEditStake", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - actor := getFirstActor(t, ctx, actorType) + uow := newTestingUtilityUnitOfWork(t, 0) + actor := getFirstActor(t, uow, actorType) addr := actor.GetAddress() addrBz, err := hex.DecodeString(addr) @@ -88,11 +88,11 @@ func TestUtilityContext_HandleMessageEditStake(t *testing.T) { msgChainsEdited.Chains = []string{"0002"} require.NotEqual(t, msgChainsEdited.Chains, test_artifacts.DefaultChains) // sanity check to make sure the test makes sense - err = ctx.handleEditStakeMessage(msgChainsEdited) + err = uow.handleEditStakeMessage(msgChainsEdited) require.NoError(t, err) // Verify the chains were edited - actor = getActorByAddr(t, ctx, actorType, addr) + actor = getActorByAddr(t, uow, actorType, addr) if actorType != coreTypes.ActorType_ACTOR_TYPE_VAL { require.NotEqual(t, test_artifacts.DefaultChains, actor.GetChains(), "incorrect edited chains") require.Equal(t, msgChainsEdited.Chains, actor.GetChains(), "incorrect edited chains") @@ -106,10 +106,10 @@ func TestUtilityContext_HandleMessageEditStake(t *testing.T) { msgAmountEdited.Amount = amountEditedString // Verify the staked amount was edited - err = ctx.handleEditStakeMessage(msgAmountEdited) + err = uow.handleEditStakeMessage(msgAmountEdited) require.NoError(t, err, "handle edit stake message") - actor = getActorByAddr(t, ctx, actorType, addr) + actor = getActorByAddr(t, uow, actorType, addr) require.NotEqual(t, test_artifacts.DefaultStakeAmountString, actor.GetStakedAmount(), "incorrect edited amount staked") require.Equal(t, amountEditedString, actor.StakedAmount, "incorrect edited amount staked") }) @@ -127,7 +127,7 @@ func TestUtilityContext_HandleMessageUnstake(t *testing.T) { actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.HandleMessageUnstake", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 1) + uow := newTestingUtilityUnitOfWork(t, 1) var paramName string switch actorType { @@ -142,10 +142,10 @@ func TestUtilityContext_HandleMessageUnstake(t *testing.T) { default: t.Fatalf("unexpected actor type %s", actorType.String()) } - err := ctx.persistenceRWContext.SetParam(paramName, numUnstakingBlocks) + err := uow.persistenceRWContext.SetParam(paramName, numUnstakingBlocks) require.NoError(t, err, "error setting minimum pause blocks") - actor := getFirstActor(t, ctx, actorType) + actor := getFirstActor(t, uow, actorType) addr := actor.GetAddress() addrBz, err := hex.DecodeString(addr) require.NoError(t, err) @@ -157,11 +157,11 @@ func TestUtilityContext_HandleMessageUnstake(t *testing.T) { } // Unstake the actor - err = ctx.handleUnstakeMessage(msg) + err = uow.handleUnstakeMessage(msg) require.NoError(t, err, "handle unstake message") // Verify the unstaking height is correct - actor = getActorByAddr(t, ctx, actorType, addr) + actor = getActorByAddr(t, uow, actorType, addr) require.Equal(t, int64(numUnstakingBlocks)+1, actor.GetUnstakingHeight(), "actor should be unstaking") }) } @@ -178,7 +178,7 @@ func TestUtilityContext_HandleMessageUnpause(t *testing.T) { actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.HandleMessageUnpause", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 1) + uow := newTestingUtilityUnitOfWork(t, 1) var paramName string switch actorType { @@ -193,20 +193,20 @@ func TestUtilityContext_HandleMessageUnpause(t *testing.T) { default: t.Fatalf("unexpected actor type %s", actorType.String()) } - err := ctx.persistenceRWContext.SetParam(paramName, minPauseBlocksNumber) + err := uow.persistenceRWContext.SetParam(paramName, minPauseBlocksNumber) require.NoError(t, err, "error setting minimum pause blocks") - actor := getFirstActor(t, ctx, actorType) + actor := getFirstActor(t, uow, actorType) addr := actor.GetAddress() addrBz, err := hex.DecodeString(addr) require.NoError(t, err) // Pause the actor - err = ctx.setActorPausedHeight(actorType, addrBz, 1) + err = uow.setActorPausedHeight(actorType, addrBz, 1) require.NoError(t, err, "error setting pause height") // Verify the actor is paused - actor = getActorByAddr(t, ctx, actorType, addr) + actor = getActorByAddr(t, uow, actorType, addr) require.Equal(t, int64(1), actor.GetPausedHeight()) // Try to unpause the actor and verify that it fails @@ -215,35 +215,35 @@ func TestUtilityContext_HandleMessageUnpause(t *testing.T) { Signer: addrBz, ActorType: actorType, } - err = ctx.handleUnpauseMessage(msgUnpauseActor) + err = uow.handleUnpauseMessage(msgUnpauseActor) require.Error(t, err) require.ErrorContains(t, err, "minimum number of blocks hasn't passed since pausing") // Start a new context when the actor still cannot be unpaused - require.NoError(t, ctx.Commit([]byte("empty qc"))) - require.NoError(t, ctx.Release()) - ctx = newTestingUtilityContext(t, int64(minPauseBlocksNumber)-1) + require.NoError(t, uow.Commit([]byte("empty qc"))) + require.NoError(t, uow.Release()) + uow = newTestingUtilityUnitOfWork(t, int64(minPauseBlocksNumber)-1) // Try to unpause the actor - err = ctx.handleUnpauseMessage(msgUnpauseActor) + err = uow.handleUnpauseMessage(msgUnpauseActor) require.Error(t, err) require.ErrorContains(t, err, "minimum number of blocks hasn't passed since pausing") // Verify the actor is still paused - actor = getActorByAddr(t, ctx, actorType, addr) + actor = getActorByAddr(t, uow, actorType, addr) require.NotEqual(t, typesUtil.HeightNotUsed, actor.GetPausedHeight()) // Start a new context when the actor can be unpaused - require.Error(t, ctx.Commit([]byte("empty qc"))) // Nothing to commit so we expect an error - require.NoError(t, ctx.Release()) - ctx = newTestingUtilityContext(t, int64(minPauseBlocksNumber)+1) + require.Error(t, uow.Commit([]byte("empty qc"))) // Nothing to commit so we expect an error + require.NoError(t, uow.Release()) + uow = newTestingUtilityUnitOfWork(t, int64(minPauseBlocksNumber)+1) // Try to unpause the actor - err = ctx.handleUnpauseMessage(msgUnpauseActor) + err = uow.handleUnpauseMessage(msgUnpauseActor) require.NoError(t, err) // Verify the actor is still paused - actor = getActorByAddr(t, ctx, actorType, addr) + actor = getActorByAddr(t, uow, actorType, addr) require.Equal(t, typesUtil.HeightNotUsed, actor.GetPausedHeight()) }) } @@ -257,25 +257,25 @@ func TestUtilityContext_GetUnbondingHeight(t *testing.T) { actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.CalculateUnstakingHeight", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) var unstakingBlocks int64 var err error switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: - unstakingBlocks, err = ctx.getAppUnstakingBlocks() + unstakingBlocks, err = uow.getAppUnstakingBlocks() case coreTypes.ActorType_ACTOR_TYPE_FISH: - unstakingBlocks, err = ctx.getFishermanUnstakingBlocks() + unstakingBlocks, err = uow.getFishermanUnstakingBlocks() case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - unstakingBlocks, err = ctx.getServicerUnstakingBlocks() + unstakingBlocks, err = uow.getServicerUnstakingBlocks() case coreTypes.ActorType_ACTOR_TYPE_VAL: - unstakingBlocks, err = ctx.getValidatorUnstakingBlocks() + unstakingBlocks, err = uow.getValidatorUnstakingBlocks() default: t.Fatalf("unexpected actor type %s", actorType.String()) } require.NoError(t, err, "error getting unstaking blocks") - unbondingHeight, err := ctx.getUnbondingHeight(actorType) + unbondingHeight, err := uow.getUnbondingHeight(actorType) require.NoError(t, err) require.Equal(t, unstakingBlocks, unbondingHeight, "unexpected unstaking height") }) @@ -293,7 +293,7 @@ func TestUtilityContext_BeginUnstakingMaxPausedActors(t *testing.T) { actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.BeginUnstakingMaxPausedActors", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 1) + uow := newTestingUtilityUnitOfWork(t, 1) var paramName string switch actorType { @@ -308,51 +308,51 @@ func TestUtilityContext_BeginUnstakingMaxPausedActors(t *testing.T) { default: t.Fatalf("unexpected actor type %s", actorType.String()) } - err := ctx.persistenceRWContext.SetParam(paramName, maxPausedBlocks) + err := uow.persistenceRWContext.SetParam(paramName, maxPausedBlocks) require.NoError(t, err) - actor := getFirstActor(t, ctx, actorType) + actor := getFirstActor(t, uow, actorType) addr := actor.GetAddress() addrBz, err := hex.DecodeString(addr) require.NoError(t, err) // Pause the actor at height 0 - err = ctx.setActorPausedHeight(actorType, addrBz, 1) + err = uow.setActorPausedHeight(actorType, addrBz, 1) require.NoError(t, err, "error setting actor pause height") // Start unstaking paused actors at the current height - err = ctx.beginUnstakingMaxPausedActors() + err = uow.beginUnstakingMaxPausedActors() require.NoError(t, err, "error beginning unstaking max paused actors") // Verify that the actor is still staked - status, err := ctx.getActorStatus(actorType, addrBz) + status, err := uow.getActorStatus(actorType, addrBz) require.NoError(t, err) require.Equal(t, coreTypes.StakeStatus_Staked, status, "actor should be staked") // Start a new context when the actor still shouldn't be unstaked - require.NoError(t, ctx.Commit([]byte("empty qc"))) - require.NoError(t, ctx.Release()) - ctx = newTestingUtilityContext(t, int64(maxPausedBlocks)-1) + require.NoError(t, uow.Commit([]byte("empty qc"))) + require.NoError(t, uow.Release()) + uow = newTestingUtilityUnitOfWork(t, int64(maxPausedBlocks)-1) // Start unstaking paused actors at the current height - err = ctx.beginUnstakingMaxPausedActors() + err = uow.beginUnstakingMaxPausedActors() require.NoError(t, err, "error beginning unstaking max paused actors") // Verify that the actor is still staked - status, err = ctx.getActorStatus(actorType, addrBz) + status, err = uow.getActorStatus(actorType, addrBz) require.NoError(t, err) require.Equal(t, coreTypes.StakeStatus_Staked, status, "actor should be staked") // Start a new context when the actor should be unstaked - require.NoError(t, ctx.Release()) - ctx = newTestingUtilityContext(t, int64(maxPausedBlocks)+2) + require.NoError(t, uow.Release()) + uow = newTestingUtilityUnitOfWork(t, int64(maxPausedBlocks)+2) // Start unstaking paused actors at the current height - err = ctx.beginUnstakingMaxPausedActors() + err = uow.beginUnstakingMaxPausedActors() require.NoError(t, err, "error beginning unstaking max paused actors") // Verify that the actor is still staked - status, err = ctx.getActorStatus(actorType, addrBz) + status, err = uow.getActorStatus(actorType, addrBz) require.NoError(t, err) require.Equal(t, coreTypes.StakeStatus_Unstaking, status, "actor should be staked") }) @@ -374,7 +374,7 @@ func TestUtilityContext_BeginUnstakingActorsPausedBefore_UnbondUnstakingActors(t actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.BeginUnstakingActorsPausedBefore", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 1) + uow := newTestingUtilityUnitOfWork(t, 1) var poolName string var paramName1 string @@ -400,15 +400,15 @@ func TestUtilityContext_BeginUnstakingActorsPausedBefore_UnbondUnstakingActors(t t.Fatalf("unexpected actor type %s", actorType.String()) } - er := ctx.persistenceRWContext.SetParam(paramName1, maxPausedBlocks) + er := uow.persistenceRWContext.SetParam(paramName1, maxPausedBlocks) require.NoError(t, er, "error setting max paused blocks") - er = ctx.persistenceRWContext.SetParam(paramName2, unstakingBlocks) + er = uow.persistenceRWContext.SetParam(paramName2, unstakingBlocks) require.NoError(t, er, "error setting max paused blocks") - er = ctx.setPoolAmount(poolName, poolInitAMount) + er = uow.setPoolAmount(poolName, poolInitAMount) require.NoError(t, er) // Validate the actor is not unstaking - actor := getFirstActor(t, ctx, actorType) + actor := getFirstActor(t, uow, actorType) addr := actor.GetAddress() require.Equal(t, typesUtil.HeightNotUsed, actor.GetUnstakingHeight(), "wrong starting status") @@ -416,53 +416,53 @@ func TestUtilityContext_BeginUnstakingActorsPausedBefore_UnbondUnstakingActors(t require.NoError(t, err) // Set the actor to be paused at height 1 - err = ctx.setActorPausedHeight(actorType, addrBz, pauseHeight) + err = uow.setActorPausedHeight(actorType, addrBz, pauseHeight) require.NoError(t, err, "error setting actor pause height") // Check that the actor is still not unstaking - actor = getActorByAddr(t, ctx, actorType, addr) + actor = getActorByAddr(t, uow, actorType, addr) require.Equal(t, typesUtil.HeightNotUsed, actor.GetUnstakingHeight(), "incorrect unstaking height") // Verify that the actor is still not unstaking - err = ctx.beginUnstakingActorsPausedBefore(pauseHeight-1, actorType) + err = uow.beginUnstakingActorsPausedBefore(pauseHeight-1, actorType) require.NoError(t, err, "error unstaking actor pause before height 0") - actor = getActorByAddr(t, ctx, actorType, addr) + actor = getActorByAddr(t, uow, actorType, addr) require.Equal(t, typesUtil.HeightNotUsed, actor.GetUnstakingHeight(), "incorrect unstaking height") unbondingHeight := pauseHeight - 1 + int64(unstakingBlocks) // Verify that the actor is now unstaking - err = ctx.beginUnstakingActorsPausedBefore(pauseHeight+1, actorType) + err = uow.beginUnstakingActorsPausedBefore(pauseHeight+1, actorType) require.NoError(t, err, "error unstaking actor pause before height 1") - actor = getActorByAddr(t, ctx, actorType, addr) + actor = getActorByAddr(t, uow, actorType, addr) require.Equal(t, unbondingHeight, actor.GetUnstakingHeight(), "incorrect unstaking height") - status, err := ctx.getActorStatus(actorType, addrBz) + status, err := uow.getActorStatus(actorType, addrBz) require.NoError(t, err) require.Equal(t, coreTypes.StakeStatus_Unstaking, status, "actor should be unstaking") // Commit the context and start a new one while the actor is still unstaking - require.NoError(t, ctx.Commit([]byte("empty QC"))) - ctx = newTestingUtilityContext(t, unbondingHeight-1) + require.NoError(t, uow.Commit([]byte("empty QC"))) + uow = newTestingUtilityUnitOfWork(t, unbondingHeight-1) - status, err = ctx.getActorStatus(actorType, addrBz) + status, err = uow.getActorStatus(actorType, addrBz) require.NoError(t, err) require.Equal(t, coreTypes.StakeStatus_Unstaking, status, "actor should be unstaking") // Release the context since there's nothing to commit and start a new one where the actors can be unbound - require.NoError(t, ctx.Release()) - ctx = newTestingUtilityContext(t, unbondingHeight) + require.NoError(t, uow.Release()) + uow = newTestingUtilityUnitOfWork(t, unbondingHeight) // Before unbonding, the pool amount should be unchanged - amount, err := ctx.getPoolAmount(poolName) + amount, err := uow.getPoolAmount(poolName) require.NoError(t, err) require.Equal(t, poolInitAMount, amount, "pool amount should be unchanged") - err = ctx.unbondUnstakingActors() + err = uow.unbondUnstakingActors() require.NoError(t, err) // Before unbonding, the money from the staked actor should go to the pool - amount, err = ctx.getPoolAmount(poolName) + amount, err = uow.getPoolAmount(poolName) require.NoError(t, err) stakedAmount, err := utils.StringToBigInt(actor.StakedAmount) @@ -471,7 +471,7 @@ func TestUtilityContext_BeginUnstakingActorsPausedBefore_UnbondUnstakingActors(t require.Equalf(t, expectedAmount, amount, "pool amount should be unchanged for %s", poolName) // Status should be changed from Unstaking to Unstaked - status, err = ctx.getActorStatus(actorType, addrBz) + status, err = uow.getActorStatus(actorType, addrBz) require.NoError(t, err) require.Equal(t, coreTypes.StakeStatus_Unstaked, status, "actor should be unstaking") }) @@ -486,20 +486,20 @@ func TestUtilityContext_GetActorExists(t *testing.T) { actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.GetExists", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) - actor := getFirstActor(t, ctx, actorType) + actor := getFirstActor(t, uow, actorType) randAddr, err := crypto.GenerateAddress() require.NoError(t, err) addrBz, err := hex.DecodeString(actor.GetAddress()) require.NoError(t, err) - exists, err := ctx.getActorExists(actorType, addrBz) + exists, err := uow.getActorExists(actorType, addrBz) require.NoError(t, err) require.True(t, exists, "actor that should exist does not") - exists, err = ctx.getActorExists(actorType, randAddr) + exists, err = uow.getActorExists(actorType, randAddr) require.NoError(t, err) require.False(t, exists, "actor that shouldn't exist does") }) @@ -514,13 +514,13 @@ func TestUtilityContext_GetOutputAddress(t *testing.T) { actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.GetOutputAddress", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) - actor := getFirstActor(t, ctx, actorType) + actor := getFirstActor(t, uow, actorType) addrBz, err := hex.DecodeString(actor.GetAddress()) require.NoError(t, err) - outputAddress, err := ctx.getActorOutputAddress(actorType, addrBz) + outputAddress, err := uow.getActorOutputAddress(actorType, addrBz) require.NoError(t, err) require.Equal(t, actor.GetOutput(), hex.EncodeToString(outputAddress), "unexpected output address") }) @@ -537,22 +537,22 @@ func TestUtilityContext_GetPauseHeightIfExists(t *testing.T) { actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.GetPauseHeightIfExists", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - actor := getFirstActor(t, ctx, actorType) + uow := newTestingUtilityUnitOfWork(t, 0) + actor := getFirstActor(t, uow, actorType) addrBz, err := hex.DecodeString(actor.GetAddress()) require.NoError(t, err) // Paused height should not exist here - gotPauseHeight, err := ctx.getPausedHeightIfExists(actorType, addrBz) + gotPauseHeight, err := uow.getPausedHeightIfExists(actorType, addrBz) require.NoError(t, err) require.Equal(t, typesUtil.HeightNotUsed, gotPauseHeight) // Paused height should be set after this - err = ctx.setActorPausedHeight(actorType, addrBz, pauseHeight) + err = uow.setActorPausedHeight(actorType, addrBz, pauseHeight) require.NoError(t, err, "error setting actor pause height") - gotPauseHeight, err = ctx.getPausedHeightIfExists(actorType, addrBz) + gotPauseHeight, err = uow.getPausedHeightIfExists(actorType, addrBz) require.NoError(t, err) require.Equal(t, pauseHeight, gotPauseHeight, "unable to get pause height from the actor") @@ -560,7 +560,7 @@ func TestUtilityContext_GetPauseHeightIfExists(t *testing.T) { randAddr, er := crypto.GenerateAddress() require.NoError(t, er) - _, err = ctx.getPausedHeightIfExists(actorType, randAddr) + _, err = uow.getPausedHeightIfExists(actorType, randAddr) require.Error(t, err, "non existent actor should error") }) } @@ -573,8 +573,8 @@ func TestUtilityContext_GetMessageEditStakeSignerCandidates(t *testing.T) { actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.GetMessageEditStakeSignerCandidates", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - actor := getFirstActor(t, ctx, actorType) + uow := newTestingUtilityUnitOfWork(t, 0) + actor := getFirstActor(t, uow, actorType) addrBz, err := hex.DecodeString(actor.GetAddress()) require.NoError(t, err) @@ -585,7 +585,7 @@ func TestUtilityContext_GetMessageEditStakeSignerCandidates(t *testing.T) { Amount: test_artifacts.DefaultStakeAmountString, ActorType: actorType, } - candidates, err := ctx.getMessageEditStakeSignerCandidates(msgEditStake) + candidates, err := uow.getMessageEditStakeSignerCandidates(msgEditStake) require.NoError(t, err) require.Equal(t, 2, len(candidates), "unexpected number of candidates") @@ -603,8 +603,8 @@ func TestUtilityContext_GetMessageUnpauseSignerCandidates(t *testing.T) { actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.GetMessageUnpauseSignerCandidates", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - actor := getFirstActor(t, ctx, actorType) + uow := newTestingUtilityUnitOfWork(t, 0) + actor := getFirstActor(t, uow, actorType) addrBz, err := hex.DecodeString(actor.GetAddress()) require.NoError(t, err) @@ -613,7 +613,7 @@ func TestUtilityContext_GetMessageUnpauseSignerCandidates(t *testing.T) { Address: addrBz, ActorType: actorType, } - candidates, err := ctx.getMessageUnpauseSignerCandidates(msg) + candidates, err := uow.getMessageUnpauseSignerCandidates(msg) require.NoError(t, err) require.Equal(t, 2, len(candidates), "unexpected number of candidates") @@ -631,8 +631,8 @@ func TestUtilityContext_GetMessageUnstakeSignerCandidates(t *testing.T) { actorType := coreTypes.ActorType(actorTypeNum) t.Run(fmt.Sprintf("%s.GetMessageUnstakeSignerCandidates", actorType.String()), func(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - actor := getFirstActor(t, ctx, actorType) + uow := newTestingUtilityUnitOfWork(t, 0) + actor := getFirstActor(t, uow, actorType) addrBz, err := hex.DecodeString(actor.GetAddress()) require.NoError(t, err) @@ -641,7 +641,7 @@ func TestUtilityContext_GetMessageUnstakeSignerCandidates(t *testing.T) { Address: addrBz, ActorType: actorType, } - candidates, err := ctx.getMessageUnstakeSignerCandidates(msg) + candidates, err := uow.getMessageUnstakeSignerCandidates(msg) require.NoError(t, err) require.Equal(t, 2, len(candidates), "unexpected number of candidates") @@ -653,20 +653,20 @@ func TestUtilityContext_GetMessageUnstakeSignerCandidates(t *testing.T) { // Helpers -func getAllTestingActors(t *testing.T, ctx *baseUtilityUnitOfWork, actorType coreTypes.ActorType) (actors []*coreTypes.Actor) { +func getAllTestingActors(t *testing.T, uow *baseUtilityUnitOfWork, actorType coreTypes.ActorType) (actors []*coreTypes.Actor) { actors = make([]*coreTypes.Actor, 0) switch actorType { case coreTypes.ActorType_ACTOR_TYPE_APP: - apps := getAllTestingApps(t, ctx) + apps := getAllTestingApps(t, uow) actors = append(actors, apps...) case coreTypes.ActorType_ACTOR_TYPE_FISH: - fish := getAllTestingFish(t, ctx) + fish := getAllTestingFish(t, uow) actors = append(actors, fish...) case coreTypes.ActorType_ACTOR_TYPE_SERVICER: - nodes := getAllTestingServicers(t, ctx) + nodes := getAllTestingServicers(t, uow) actors = append(actors, nodes...) case coreTypes.ActorType_ACTOR_TYPE_VAL: - vals := getAllTestingValidators(t, ctx) + vals := getAllTestingValidators(t, uow) actors = append(actors, vals...) default: t.Fatalf("unexpected actor type %s", actorType.String()) @@ -675,24 +675,24 @@ func getAllTestingActors(t *testing.T, ctx *baseUtilityUnitOfWork, actorType cor return } -func getFirstActor(t *testing.T, ctx *baseUtilityUnitOfWork, actorType coreTypes.ActorType) *coreTypes.Actor { - return getAllTestingActors(t, ctx, actorType)[0] +func getFirstActor(t *testing.T, uow *baseUtilityUnitOfWork, actorType coreTypes.ActorType) *coreTypes.Actor { + return getAllTestingActors(t, uow, actorType)[0] } -func getActorByAddr(t *testing.T, ctx *baseUtilityUnitOfWork, actorType coreTypes.ActorType, addr string) (actor *coreTypes.Actor) { - actors := getAllTestingActors(t, ctx, actorType) +func getActorByAddr(t *testing.T, uow *baseUtilityUnitOfWork, actorType coreTypes.ActorType, addr string) (actor *coreTypes.Actor) { + actors := getAllTestingActors(t, uow, actorType) idx := slices.IndexFunc(actors, func(a *coreTypes.Actor) bool { return a.GetAddress() == addr }) return actors[idx] } -func getAllTestingApps(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.Actor { - actors, err := ctx.persistenceReadContext.GetAllApps(ctx.height) +func getAllTestingApps(t *testing.T, uow *baseUtilityUnitOfWork) []*coreTypes.Actor { + actors, err := uow.persistenceReadContext.GetAllApps(uow.height) require.NoError(t, err) return actors } -func getAllTestingValidators(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.Actor { - actors, err := ctx.persistenceReadContext.GetAllValidators(ctx.height) +func getAllTestingValidators(t *testing.T, uow *baseUtilityUnitOfWork) []*coreTypes.Actor { + actors, err := uow.persistenceReadContext.GetAllValidators(uow.height) require.NoError(t, err) sort.Slice(actors, func(i, j int) bool { return actors[i].GetAddress() < actors[j].GetAddress() @@ -700,14 +700,14 @@ func getAllTestingValidators(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTy return actors } -func getAllTestingFish(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.Actor { - actors, err := ctx.persistenceReadContext.GetAllFishermen(ctx.height) +func getAllTestingFish(t *testing.T, uow *baseUtilityUnitOfWork) []*coreTypes.Actor { + actors, err := uow.persistenceReadContext.GetAllFishermen(uow.height) require.NoError(t, err) return actors } -func getAllTestingServicers(t *testing.T, ctx *baseUtilityUnitOfWork) []*coreTypes.Actor { - actors, err := ctx.persistenceReadContext.GetAllServicers(ctx.height) +func getAllTestingServicers(t *testing.T, uow *baseUtilityUnitOfWork) []*coreTypes.Actor { + actors, err := uow.persistenceReadContext.GetAllServicers(uow.height) require.NoError(t, err) return actors } diff --git a/utility/unit_of_work/application_test.go b/utility/unit_of_work/application_test.go index 2a8ea6529..e6b9512de 100644 --- a/utility/unit_of_work/application_test.go +++ b/utility/unit_of_work/application_test.go @@ -12,9 +12,9 @@ const ( ) func TestUtilityContext_CalculateMaxAppRelays(t *testing.T) { - ctx := newTestingUtilityContext(t, 1) - actor := getFirstActor(t, ctx, coreTypes.ActorType_ACTOR_TYPE_APP) - appSessionTokens, err := ctx.calculateAppSessionTokens(actor.StakedAmount) + uow := newTestingUtilityUnitOfWork(t, 1) + actor := getFirstActor(t, uow, coreTypes.ActorType_ACTOR_TYPE_APP) + appSessionTokens, err := uow.calculateAppSessionTokens(actor.StakedAmount) require.NoError(t, err) // TODO: These are hardcoded values based on params from the genesis file. Expand on tests // when implementing the Application protocol. diff --git a/utility/unit_of_work/block_test.go b/utility/unit_of_work/block_test.go index 8436e5a28..32745c65f 100644 --- a/utility/unit_of_work/block_test.go +++ b/utility/unit_of_work/block_test.go @@ -5,29 +5,42 @@ import ( "math/big" "testing" + "github.com/golang/mock/gomock" coreTypes "github.com/pokt-network/pocket/shared/core/types" + "github.com/pokt-network/pocket/shared/modules" + mockModules "github.com/pokt-network/pocket/shared/modules/mocks" + "github.com/pokt-network/pocket/utility/types" "github.com/stretchr/testify/require" ) func TestUtilityContext_ApplyBlock(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - tx, startingBalance, amountSent, signer := newTestingTransaction(t, ctx) + ctrl := gomock.NewController(t) + mockUtilityMod := mockModules.NewMockUtilityModule(ctrl) + mockUtilityMod.EXPECT().GetModuleName().Return(modules.UtilityModuleName).AnyTimes() + mockUtilityMod.EXPECT().SetBus(gomock.Any()).Return().AnyTimes() + + uow := newTestingUtilityUnitOfWork(t, 0, func(uow *baseUtilityUnitOfWork) { + uow.GetBus().RegisterModule(mockUtilityMod) + utilityCfg := uow.GetBus().GetRuntimeMgr().GetConfig().Utility + mockUtilityMod.EXPECT().GetMempool().Return(types.NewTxFIFOMempool(utilityCfg.MaxMempoolTransactionBytes, utilityCfg.MaxMempoolTransactions)).AnyTimes() + }) + tx, startingBalance, amountSent, signer := newTestingTransaction(t, uow) txBz, er := tx.Bytes() require.NoError(t, er) - proposer := getFirstActor(t, ctx, coreTypes.ActorType_ACTOR_TYPE_VAL) + proposer := getFirstActor(t, uow, coreTypes.ActorType_ACTOR_TYPE_VAL) addrBz, err := hex.DecodeString(proposer.GetAddress()) require.NoError(t, err) - proposerBeforeBalance, err := ctx.getAccountAmount(addrBz) + proposerBeforeBalance, err := uow.getAccountAmount(addrBz) require.NoError(t, err) - err = ctx.SetProposalBlock("", addrBz, [][]byte{txBz}) + err = uow.SetProposalBlock("", addrBz, [][]byte{txBz}) require.NoError(t, err) - appHash, _, err := ctx.ApplyBlock() + appHash, _, err := uow.ApplyBlock() require.NoError(t, err) require.NotNil(t, appHash) @@ -37,16 +50,16 @@ func TestUtilityContext_ApplyBlock(t *testing.T) { // require.NoError(t, err) // require.Equal(t, missed, 1) - feeBig, err := ctx.getMessageSendFee() + feeBig, err := uow.getMessageSendFee() require.NoError(t, err) expectedAmountSubtracted := big.NewInt(0).Add(amountSent, feeBig) expectedAfterBalance := big.NewInt(0).Sub(startingBalance, expectedAmountSubtracted) - amountAfter, err := ctx.getAccountAmount(signer.Address()) + amountAfter, err := uow.getAccountAmount(signer.Address()) require.NoError(t, err) require.Equal(t, expectedAfterBalance, amountAfter, "unexpected after balance; expected %v got %v", expectedAfterBalance, amountAfter) - proposerCutPercentage, err := ctx.getProposerPercentageOfFees() + proposerCutPercentage, err := uow.getProposerPercentageOfFees() require.NoError(t, err) feesAndRewardsCollectedFloat := new(big.Float).SetInt(feeBig) @@ -54,7 +67,7 @@ func TestUtilityContext_ApplyBlock(t *testing.T) { feesAndRewardsCollectedFloat.Quo(feesAndRewardsCollectedFloat, big.NewFloat(100)) expectedProposerBalanceDifference, _ := feesAndRewardsCollectedFloat.Int(nil) - proposerAfterBalance, err := ctx.getAccountAmount(addrBz) + proposerAfterBalance, err := uow.getAccountAmount(addrBz) require.NoError(t, err) proposerBalanceDifference := big.NewInt(0).Sub(proposerAfterBalance, proposerBeforeBalance) @@ -63,10 +76,10 @@ func TestUtilityContext_ApplyBlock(t *testing.T) { } func TestUtilityContext_BeginBlock(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - tx, _, _, _ := newTestingTransaction(t, ctx) + uow := newTestingUtilityUnitOfWork(t, 0) + tx, _, _, _ := newTestingTransaction(t, uow) - proposer := getFirstActor(t, ctx, coreTypes.ActorType_ACTOR_TYPE_VAL) + proposer := getFirstActor(t, uow, coreTypes.ActorType_ACTOR_TYPE_VAL) txBz, err := tx.Bytes() require.NoError(t, err) @@ -74,10 +87,10 @@ func TestUtilityContext_BeginBlock(t *testing.T) { addrBz, er := hex.DecodeString(proposer.GetAddress()) require.NoError(t, er) - er = ctx.SetProposalBlock("", addrBz, [][]byte{txBz}) + er = uow.SetProposalBlock("", addrBz, [][]byte{txBz}) require.NoError(t, er) - _, _, er = ctx.ApplyBlock() + _, _, er = uow.ApplyBlock() require.NoError(t, er) // // TODO: Uncomment this once `GetValidatorMissedBlocks` is implemented. @@ -89,10 +102,10 @@ func TestUtilityContext_BeginBlock(t *testing.T) { } func TestUtilityContext_EndBlock(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - tx, _, _, _ := newTestingTransaction(t, ctx) + uow := newTestingUtilityUnitOfWork(t, 0) + tx, _, _, _ := newTestingTransaction(t, uow) - proposer := getFirstActor(t, ctx, coreTypes.ActorType_ACTOR_TYPE_VAL) + proposer := getFirstActor(t, uow, coreTypes.ActorType_ACTOR_TYPE_VAL) txBz, err := tx.Bytes() require.NoError(t, err) @@ -100,26 +113,26 @@ func TestUtilityContext_EndBlock(t *testing.T) { addrBz, er := hex.DecodeString(proposer.GetAddress()) require.NoError(t, er) - proposerBeforeBalance, err := ctx.getAccountAmount(addrBz) + proposerBeforeBalance, err := uow.getAccountAmount(addrBz) require.NoError(t, err) - er = ctx.SetProposalBlock("", addrBz, [][]byte{txBz}) + er = uow.SetProposalBlock("", addrBz, [][]byte{txBz}) require.NoError(t, er) - _, _, er = ctx.ApplyBlock() + _, _, er = uow.ApplyBlock() require.NoError(t, er) - feeBig, err := ctx.getMessageSendFee() + feeBig, err := uow.getMessageSendFee() require.NoError(t, err) - proposerCutPercentage, err := ctx.getProposerPercentageOfFees() + proposerCutPercentage, err := uow.getProposerPercentageOfFees() require.NoError(t, err) feesAndRewardsCollectedFloat := new(big.Float).SetInt(feeBig) feesAndRewardsCollectedFloat.Mul(feesAndRewardsCollectedFloat, big.NewFloat(float64(proposerCutPercentage))) feesAndRewardsCollectedFloat.Quo(feesAndRewardsCollectedFloat, big.NewFloat(100)) expectedProposerBalanceDifference, _ := feesAndRewardsCollectedFloat.Int(nil) - proposerAfterBalance, err := ctx.getAccountAmount(addrBz) + proposerAfterBalance, err := uow.getAccountAmount(addrBz) require.NoError(t, err) proposerBalanceDifference := big.NewInt(0).Sub(proposerAfterBalance, proposerBeforeBalance) diff --git a/utility/unit_of_work/gov_test.go b/utility/unit_of_work/gov_test.go index bf764be43..221dcd84d 100644 --- a/utility/unit_of_work/gov_test.go +++ b/utility/unit_of_work/gov_test.go @@ -21,71 +21,71 @@ func DefaultTestingParams(_ *testing.T) *genesis.Params { } func TestUtilityContext_GetAppMaxChains(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) - maxChains, err := ctx.getAppMaxChains() + maxChains, err := uow.getAppMaxChains() require.NoError(t, err) require.Equal(t, int(defaultParams.GetAppMaxChains()), maxChains) } func TestUtilityContext_GetAppMaxPausedBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) - gotParam, err := ctx.getAppMaxPausedBlocks() + gotParam, err := uow.getAppMaxPausedBlocks() require.NoError(t, err) require.Equal(t, int(defaultParams.GetAppMaxPauseBlocks()), gotParam) } func TestUtilityContext_GetAppMinimumPauseBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetAppMinimumPauseBlocks()) - gotParam, err := ctx.getAppMinimumPauseBlocks() + gotParam, err := uow.getAppMinimumPauseBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetAppMinimumStake(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetAppMinimumStake() - gotParam, err := ctx.getAppMinimumStake() + gotParam, err := uow.getAppMinimumStake() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetAppUnstakingBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int64(defaultParams.GetAppUnstakingBlocks()) - gotParam, err := ctx.getAppUnstakingBlocks() + gotParam, err := uow.getAppUnstakingBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetBlocksPerSession(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetBlocksPerSession()) - gotParam, err := ctx.getParameter(typesUtil.BlocksPerSessionParamName) + gotParam, err := uow.getParameter(typesUtil.BlocksPerSessionParamName) require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetDoubleSignBurnPercentage(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetDoubleSignBurnPercentage()) - gotParam, err := ctx.getDoubleSignBurnPercentage() + gotParam, err := uow.getDoubleSignBurnPercentage() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetDoubleSignFeeOwner(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageDoubleSignFeeOwner() - gotParam, err := ctx.getDoubleSignFeeOwner() + gotParam, err := uow.getDoubleSignFeeOwner() require.NoError(t, err) defaultParamTx, er := hex.DecodeString(defaultParam) @@ -95,416 +95,416 @@ func TestUtilityContext_GetDoubleSignFeeOwner(t *testing.T) { } func TestUtilityContext_GetFishermanMaxChains(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetFishermanMaxChains()) - gotParam, err := ctx.getFishermanMaxChains() + gotParam, err := uow.getFishermanMaxChains() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetFishermanMaxPausedBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetFishermanMaxPauseBlocks()) - gotParam, err := ctx.getFishermanMaxPausedBlocks() + gotParam, err := uow.getFishermanMaxPausedBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetFishermanMinimumPauseBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetFishermanMinimumPauseBlocks()) - gotParam, err := ctx.getFishermanMinimumPauseBlocks() + gotParam, err := uow.getFishermanMinimumPauseBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetFishermanMinimumStake(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetFishermanMinimumStake() - gotParam, err := ctx.getFishermanMinimumStake() + gotParam, err := uow.getFishermanMinimumStake() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetFishermanUnstakingBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int64(defaultParams.GetFishermanUnstakingBlocks()) - gotParam, err := ctx.getFishermanUnstakingBlocks() + gotParam, err := uow.getFishermanUnstakingBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetMaxEvidenceAgeInBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetValidatorMaxEvidenceAgeInBlocks()) - gotParam, err := ctx.getMaxEvidenceAgeInBlocks() + gotParam, err := uow.getMaxEvidenceAgeInBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetMessageChangeParameterFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageChangeParameterFee() - gotParam, err := ctx.getMessageChangeParameterFee() + gotParam, err := uow.getMessageChangeParameterFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageDoubleSignFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageDoubleSignFee() - gotParam, err := ctx.getMessageDoubleSignFee() + gotParam, err := uow.getMessageDoubleSignFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageEditStakeAppFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageEditStakeAppFee() - gotParam, err := ctx.getMessageEditStakeAppFee() + gotParam, err := uow.getMessageEditStakeAppFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageEditStakeFishermanFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageEditStakeFishermanFee() - gotParam, err := ctx.getMessageEditStakeFishermanFee() + gotParam, err := uow.getMessageEditStakeFishermanFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageEditStakeServicerFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageEditStakeServicerFee() - gotParam, err := ctx.getMessageEditStakeServicerFee() + gotParam, err := uow.getMessageEditStakeServicerFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageEditStakeValidatorFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageEditStakeValidatorFee() - gotParam, err := ctx.getMessageEditStakeValidatorFee() + gotParam, err := uow.getMessageEditStakeValidatorFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageFishermanPauseServicerFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageFishermanPauseServicerFee() - gotParam, err := ctx.getMessageFishermanPauseServicerFee() + gotParam, err := uow.getMessageFishermanPauseServicerFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessagePauseAppFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessagePauseAppFee() - gotParam, err := ctx.getMessagePauseAppFee() + gotParam, err := uow.getMessagePauseAppFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessagePauseFishermanFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessagePauseFishermanFee() - gotParam, err := ctx.getMessagePauseFishermanFee() + gotParam, err := uow.getMessagePauseFishermanFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessagePauseServicerFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessagePauseServicerFee() - gotParam, err := ctx.getMessagePauseServicerFee() + gotParam, err := uow.getMessagePauseServicerFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessagePauseValidatorFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessagePauseValidatorFee() - gotParam, err := ctx.getMessagePauseValidatorFee() + gotParam, err := uow.getMessagePauseValidatorFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageProveTestScoreFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageProveTestScoreFee() - gotParam, err := ctx.getMessageProveTestScoreFee() + gotParam, err := uow.getMessageProveTestScoreFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageSendFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageSendFee() - gotParam, err := ctx.getMessageSendFee() + gotParam, err := uow.getMessageSendFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageStakeAppFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageStakeAppFee() - gotParam, err := ctx.getMessageStakeAppFee() + gotParam, err := uow.getMessageStakeAppFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageStakeFishermanFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageStakeFishermanFee() - gotParam, err := ctx.getMessageStakeFishermanFee() + gotParam, err := uow.getMessageStakeFishermanFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageStakeServicerFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageStakeServicerFee() - gotParam, err := ctx.getMessageStakeServicerFee() + gotParam, err := uow.getMessageStakeServicerFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageStakeValidatorFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageStakeValidatorFee() - gotParam, err := ctx.getMessageStakeValidatorFee() + gotParam, err := uow.getMessageStakeValidatorFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageTestScoreFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageTestScoreFee() - gotParam, err := ctx.getMessageTestScoreFee() + gotParam, err := uow.getMessageTestScoreFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageUnpauseAppFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnpauseAppFee() - gotParam, err := ctx.getMessageUnpauseAppFee() + gotParam, err := uow.getMessageUnpauseAppFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageUnpauseFishermanFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnpauseFishermanFee() - gotParam, err := ctx.getMessageUnpauseFishermanFee() + gotParam, err := uow.getMessageUnpauseFishermanFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageUnpauseServicerFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnpauseServicerFee() - gotParam, err := ctx.getMessageUnpauseServicerFee() + gotParam, err := uow.getMessageUnpauseServicerFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageUnpauseValidatorFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnpauseValidatorFee() - gotParam, err := ctx.getMessageUnpauseValidatorFee() + gotParam, err := uow.getMessageUnpauseValidatorFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageUnstakeAppFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnstakeAppFee() - gotParam, err := ctx.getMessageUnstakeAppFee() + gotParam, err := uow.getMessageUnstakeAppFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageUnstakeFishermanFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnstakeFishermanFee() - gotParam, err := ctx.getMessageUnstakeFishermanFee() + gotParam, err := uow.getMessageUnstakeFishermanFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageUnstakeServicerFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnstakeServicerFee() - gotParam, err := ctx.getMessageUnstakeServicerFee() + gotParam, err := uow.getMessageUnstakeServicerFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMessageUnstakeValidatorFee(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnstakeValidatorFee() - gotParam, err := ctx.getMessageUnstakeValidatorFee() + gotParam, err := uow.getMessageUnstakeValidatorFee() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetMissedBlocksBurnPercentage(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetMissedBlocksBurnPercentage()) - gotParam, err := ctx.getMissedBlocksBurnPercentage() + gotParam, err := uow.getMissedBlocksBurnPercentage() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetProposerPercentageOfFees(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetProposerPercentageOfFees()) - gotParam, err := ctx.getProposerPercentageOfFees() + gotParam, err := uow.getProposerPercentageOfFees() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetServicerMaxChains(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetServicerMaxChains()) - gotParam, err := ctx.getServicerMaxChains() + gotParam, err := uow.getServicerMaxChains() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetServicerMaxPausedBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetServicerMaxPauseBlocks()) - gotParam, err := ctx.getServicerMaxPausedBlocks() + gotParam, err := uow.getServicerMaxPausedBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetServicerMinimumPauseBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetServicerMinimumPauseBlocks()) - gotParam, err := ctx.getServicerMinimumPauseBlocks() + gotParam, err := uow.getServicerMinimumPauseBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetServicerMinimumStake(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetServicerMinimumStake() - gotParam, err := ctx.getServicerMinimumStake() + gotParam, err := uow.getServicerMinimumStake() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetServicerUnstakingBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int64(defaultParams.GetServicerUnstakingBlocks()) - gotParam, err := ctx.getServicerUnstakingBlocks() + gotParam, err := uow.getServicerUnstakingBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetSessionTokensMultiplier(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetAppSessionTokensMultiplier()) - gotParam, err := ctx.getAppSessionTokensMultiplier() + gotParam, err := uow.getAppSessionTokensMultiplier() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetValidatorMaxMissedBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetValidatorMaximumMissedBlocks()) - gotParam, err := ctx.getValidatorMaxMissedBlocks() + gotParam, err := uow.getValidatorMaxMissedBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetValidatorMaxPausedBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetValidatorMaxPauseBlocks()) - gotParam, err := ctx.getValidatorMaxPausedBlocks() + gotParam, err := uow.getValidatorMaxPausedBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetValidatorMinimumPauseBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetValidatorMinimumPauseBlocks()) - gotParam, err := ctx.getValidatorMinimumPauseBlocks() + gotParam, err := uow.getValidatorMinimumPauseBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_GetValidatorMinimumStake(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetValidatorMinimumStake() - gotParam, err := ctx.getValidatorMinimumStake() + gotParam, err := uow.getValidatorMinimumStake() require.NoError(t, err) require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } func TestUtilityContext_GetValidatorUnstakingBlocks(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int64(defaultParams.GetValidatorUnstakingBlocks()) - gotParam, err := ctx.getValidatorUnstakingBlocks() + gotParam, err := uow.getValidatorUnstakingBlocks() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) } func TestUtilityContext_HandleMessageChangeParameter(t *testing.T) { cdc := codec.GetCodec() - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetMissedBlocksBurnPercentage()) - gotParam, err := ctx.getMissedBlocksBurnPercentage() + gotParam, err := uow.getMissedBlocksBurnPercentage() require.NoError(t, err) require.Equal(t, defaultParam, gotParam) newParamValue := int32(2) @@ -518,441 +518,441 @@ func TestUtilityContext_HandleMessageChangeParameter(t *testing.T) { ParameterKey: typesUtil.MissedBlocksBurnPercentageParamName, ParameterValue: any, } - require.NoError(t, ctx.handleMessageChangeParameter(msg), "handle message change param") - gotParam, err = ctx.getMissedBlocksBurnPercentage() + require.NoError(t, uow.handleMessageChangeParameter(msg), "handle message change param") + gotParam, err = uow.getMissedBlocksBurnPercentage() require.NoError(t, err) require.Equal(t, int(newParamValue), gotParam) } func TestUtilityContext_GetParamOwner(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetAclOwner() - gotParam, err := ctx.getParamOwner(typesUtil.AclOwner) + gotParam, err := uow.getParamOwner(typesUtil.AclOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetBlocksPerSessionOwner() - gotParam, err = ctx.getParamOwner(typesUtil.BlocksPerSessionParamName) + gotParam, err = uow.getParamOwner(typesUtil.BlocksPerSessionParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAppMaxChainsOwner() - gotParam, err = ctx.getParamOwner(typesUtil.AppMaxChainsParamName) + gotParam, err = uow.getParamOwner(typesUtil.AppMaxChainsParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAppMinimumStakeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.AppMinimumStakeParamName) + gotParam, err = uow.getParamOwner(typesUtil.AppMinimumStakeParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAppSessionTokensMultiplierOwner() - gotParam, err = ctx.getParamOwner(typesUtil.AppSessionTokensMultiplierOwner) + gotParam, err = uow.getParamOwner(typesUtil.AppSessionTokensMultiplierOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAppUnstakingBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.AppUnstakingBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.AppUnstakingBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAppMinimumPauseBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.AppMinimumPauseBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.AppMinimumPauseBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAppMaxPausedBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.AppMaxPauseBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.AppMaxPauseBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetServicersPerSessionOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicersPerSessionParamName) + gotParam, err = uow.getParamOwner(typesUtil.ServicersPerSessionParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetServicerMinimumStakeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicerMinimumStakeParamName) + gotParam, err = uow.getParamOwner(typesUtil.ServicerMinimumStakeParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetServicerMaxChainsOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicerMaxChainsParamName) + gotParam, err = uow.getParamOwner(typesUtil.ServicerMaxChainsParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetServicerUnstakingBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicerUnstakingBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.ServicerUnstakingBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetServicerMinimumPauseBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicerMinimumPauseBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.ServicerMinimumPauseBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetServicerMaxPausedBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicerMaxPauseBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.ServicerMaxPauseBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetFishermanMinimumStakeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.FishermanMinimumStakeParamName) + gotParam, err = uow.getParamOwner(typesUtil.FishermanMinimumStakeParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetServicerMaxChainsOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicerMaxPauseBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.ServicerMaxPauseBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetFishermanUnstakingBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.FishermanUnstakingBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.FishermanUnstakingBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetFishermanMinimumPauseBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.FishermanMinimumPauseBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.FishermanMinimumPauseBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetFishermanMaxPausedBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.FishermanMaxPauseBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.FishermanMaxPauseBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetValidatorMinimumStakeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ValidatorMinimumStakeParamName) + gotParam, err = uow.getParamOwner(typesUtil.ValidatorMinimumStakeParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetValidatorUnstakingBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ValidatorUnstakingBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.ValidatorUnstakingBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetValidatorMinimumPauseBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ValidatorMinimumPauseBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.ValidatorMinimumPauseBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetValidatorMaxPausedBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ValidatorMaxPausedBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.ValidatorMaxPausedBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetValidatorMaximumMissedBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ValidatorMaximumMissedBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.ValidatorMaximumMissedBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetProposerPercentageOfFeesOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ProposerPercentageOfFeesParamName) + gotParam, err = uow.getParamOwner(typesUtil.ProposerPercentageOfFeesParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetValidatorMaxEvidenceAgeInBlocksOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ValidatorMaxEvidenceAgeInBlocksParamName) + gotParam, err = uow.getParamOwner(typesUtil.ValidatorMaxEvidenceAgeInBlocksParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMissedBlocksBurnPercentageOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MissedBlocksBurnPercentageParamName) + gotParam, err = uow.getParamOwner(typesUtil.MissedBlocksBurnPercentageParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetDoubleSignBurnPercentageOwner() - gotParam, err = ctx.getParamOwner(typesUtil.DoubleSignBurnPercentageParamName) + gotParam, err = uow.getParamOwner(typesUtil.DoubleSignBurnPercentageParamName) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageDoubleSignFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageDoubleSignFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageDoubleSignFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageSendFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageSendFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageSendFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageStakeFishermanFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageStakeFishermanFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageStakeFishermanFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageEditStakeFishermanFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageEditStakeFishermanFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageEditStakeFishermanFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageUnstakeFishermanFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnstakeFishermanFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnstakeFishermanFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessagePauseFishermanFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessagePauseFishermanFee) + gotParam, err = uow.getParamOwner(typesUtil.MessagePauseFishermanFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageUnpauseFishermanFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnpauseFishermanFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnpauseFishermanFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageTestScoreFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageTestScoreFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageTestScoreFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageFishermanPauseServicerFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageFishermanPauseServicerFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageFishermanPauseServicerFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageProveTestScoreFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageProveTestScoreFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageProveTestScoreFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageStakeAppFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageStakeAppFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageStakeAppFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageEditStakeAppFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageEditStakeAppFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageEditStakeAppFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageUnstakeAppFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnstakeAppFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnstakeAppFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessagePauseAppFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessagePauseAppFee) + gotParam, err = uow.getParamOwner(typesUtil.MessagePauseAppFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageUnpauseAppFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnpauseAppFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnpauseAppFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageStakeValidatorFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageStakeValidatorFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageStakeValidatorFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageEditStakeValidatorFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageEditStakeValidatorFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageEditStakeValidatorFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageUnstakeValidatorFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnstakeValidatorFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnstakeValidatorFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessagePauseValidatorFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessagePauseValidatorFee) + gotParam, err = uow.getParamOwner(typesUtil.MessagePauseValidatorFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageUnpauseValidatorFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnpauseValidatorFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnpauseValidatorFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageStakeServicerFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageStakeServicerFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageStakeServicerFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageEditStakeServicerFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageEditStakeServicerFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageEditStakeServicerFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageUnstakeServicerFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnstakeServicerFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnstakeServicerFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessagePauseServicerFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessagePauseServicerFee) + gotParam, err = uow.getParamOwner(typesUtil.MessagePauseServicerFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageUnpauseServicerFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnpauseServicerFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnpauseServicerFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetMessageChangeParameterFeeOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageChangeParameterFee) + gotParam, err = uow.getParamOwner(typesUtil.MessageChangeParameterFee) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) // owners defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.BlocksPerSessionOwner) + gotParam, err = uow.getParamOwner(typesUtil.BlocksPerSessionOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.AppMaxChainsOwner) + gotParam, err = uow.getParamOwner(typesUtil.AppMaxChainsOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.AppMinimumStakeOwner) + gotParam, err = uow.getParamOwner(typesUtil.AppMinimumStakeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.AppSessionTokensMultiplierOwner) + gotParam, err = uow.getParamOwner(typesUtil.AppSessionTokensMultiplierOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.AppUnstakingBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.AppUnstakingBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.AppMinimumPauseBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.AppMinimumPauseBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.AppMaxPausedBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.AppMaxPausedBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicerMinimumPauseBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.ServicerMinimumPauseBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicerMaxChainsOwner) + gotParam, err = uow.getParamOwner(typesUtil.ServicerMaxChainsOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicerUnstakingBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.ServicerUnstakingBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicerMinimumStakeOwner) + gotParam, err = uow.getParamOwner(typesUtil.ServicerMinimumStakeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicerMaxPausedBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.ServicerMaxPausedBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ServicersPerSessionOwner) + gotParam, err = uow.getParamOwner(typesUtil.ServicersPerSessionOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.FishermanMinimumStakeOwner) + gotParam, err = uow.getParamOwner(typesUtil.FishermanMinimumStakeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.FishermanMaxChainsOwner) + gotParam, err = uow.getParamOwner(typesUtil.FishermanMaxChainsOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.FishermanUnstakingBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.FishermanUnstakingBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.FishermanMinimumPauseBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.FishermanMinimumPauseBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.FishermanMaxPausedBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.FishermanMaxPausedBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ValidatorMinimumStakeOwner) + gotParam, err = uow.getParamOwner(typesUtil.ValidatorMinimumStakeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ValidatorUnstakingBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.ValidatorUnstakingBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ValidatorMinimumPauseBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.ValidatorMinimumPauseBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ValidatorMaxPausedBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.ValidatorMaxPausedBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ValidatorMaxPausedBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.ValidatorMaxPausedBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ProposerPercentageOfFeesOwner) + gotParam, err = uow.getParamOwner(typesUtil.ProposerPercentageOfFeesOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.ValidatorMaxEvidenceAgeInBlocksOwner) + gotParam, err = uow.getParamOwner(typesUtil.ValidatorMaxEvidenceAgeInBlocksOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MissedBlocksBurnPercentageOwner) + gotParam, err = uow.getParamOwner(typesUtil.MissedBlocksBurnPercentageOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.DoubleSignBurnPercentageOwner) + gotParam, err = uow.getParamOwner(typesUtil.DoubleSignBurnPercentageOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageSendFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageSendFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageStakeFishermanFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageStakeFishermanFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageEditStakeFishermanFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageEditStakeFishermanFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnstakeFishermanFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnstakeFishermanFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessagePauseFishermanFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessagePauseFishermanFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnpauseFishermanFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnpauseFishermanFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageFishermanPauseServicerFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageFishermanPauseServicerFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageTestScoreFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageTestScoreFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageProveTestScoreFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageProveTestScoreFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageStakeAppFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageStakeAppFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageEditStakeAppFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageEditStakeAppFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnstakeAppFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnstakeAppFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessagePauseAppFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessagePauseAppFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnpauseAppFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnpauseAppFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageStakeValidatorFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageStakeValidatorFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageEditStakeValidatorFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageEditStakeValidatorFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnstakeValidatorFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnstakeValidatorFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessagePauseValidatorFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessagePauseValidatorFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnpauseValidatorFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnpauseValidatorFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageStakeServicerFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageStakeServicerFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageEditStakeServicerFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageEditStakeServicerFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnstakeServicerFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnstakeServicerFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessagePauseServicerFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessagePauseServicerFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageUnpauseServicerFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageUnpauseServicerFeeOwner) require.NoError(t, err) require.Equal(t, defaultParam, hex.EncodeToString(gotParam)) defaultParam = defaultParams.GetAclOwner() - gotParam, err = ctx.getParamOwner(typesUtil.MessageChangeParameterFeeOwner) + gotParam, err = uow.getParamOwner(typesUtil.MessageChangeParameterFeeOwner) require.NoError(t, err) defaultParamBz, err := hex.DecodeString(defaultParam) require.NoError(t, err) diff --git a/utility/unit_of_work/message_test.go b/utility/unit_of_work/message_test.go index 5a9a50c92..b522cc619 100644 --- a/utility/unit_of_work/message_test.go +++ b/utility/unit_of_work/message_test.go @@ -11,7 +11,7 @@ import ( ) func TestUtilityContext_HandleMessageSend(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + ctx := newTestingUtilityUnitOfWork(t, 0) accs := getAllTestingAccounts(t, ctx) sendAmount := big.NewInt(1000000) @@ -43,7 +43,7 @@ func TestUtilityContext_HandleMessageSend(t *testing.T) { } func TestUtilityContext_GetMessageSendSignerCandidates(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + ctx := newTestingUtilityUnitOfWork(t, 0) accs := getAllTestingAccounts(t, ctx) sendAmount := big.NewInt(1000000) diff --git a/utility/unit_of_work/module_test.go b/utility/unit_of_work/module_test.go index dec28e2a0..bdde64423 100644 --- a/utility/unit_of_work/module_test.go +++ b/utility/unit_of_work/module_test.go @@ -28,8 +28,6 @@ const ( ) var ( - // testUtilityMod mock_modules.MockUtilityModule - // TODO(#261): Utility module tests should have no dependencies on the persistence module (which instantiates a postgres container) testPersistenceMod modules.PersistenceModule ) @@ -47,7 +45,6 @@ func TestMain(m *testing.M) { log.Fatalf("Error creating bus: %s", err) } - //testUtilityMod = newTestUtilityModule(bus) testPersistenceMod = newTestPersistenceModule(bus) exitCode := m.Run() @@ -55,7 +52,7 @@ func TestMain(m *testing.M) { os.Exit(exitCode) } -func newTestingUtilityContext(t *testing.T, height int64) *baseUtilityUnitOfWork { +func newTestingUtilityUnitOfWork(t *testing.T, height int64, options ...func(*baseUtilityUnitOfWork)) *baseUtilityUnitOfWork { persistenceContext, err := testPersistenceMod.NewRWContext(height) require.NoError(t, err) @@ -71,7 +68,7 @@ func newTestingUtilityContext(t *testing.T, height int64) *baseUtilityUnitOfWork //testUtilityMod.GetMempool().Clear() }) - ctx := &baseUtilityUnitOfWork{ + uow := &baseUtilityUnitOfWork{ logger: logger.Global.CreateLoggerForModule(modules.UtilityModuleName), height: height, // TODO: - @deblasis: refactor @@ -79,22 +76,15 @@ func newTestingUtilityContext(t *testing.T, height int64) *baseUtilityUnitOfWork persistenceReadContext: persistenceContext, } - utilityMod := mock_modules.NewMockUtilityModule() + uow.SetBus(testPersistenceMod.GetBus()) - ctx.SetBus(testPersistenceMod.GetBus()) + for _, option := range options { + option(uow) + } - return ctx + return uow } -// func newTestUtilityModule(bus modules.Bus) modules.UtilityModule { -// ctrl := gomock.NewController(nil) -// utilityMod = mock_modules.NewMockUtilityModule(bus) -// if err != nil { -// log.Fatalf("Error creating persistence module: %s", err) -// } -// return utilityMod.(modules.UtilityModule) -// } - func newTestPersistenceModule(bus modules.Bus) modules.PersistenceModule { persistenceMod, err := persistence.Create(bus) if err != nil { diff --git a/utility/unit_of_work/transaction_test.go b/utility/unit_of_work/transaction_test.go index 796654c59..30c4d3d89 100644 --- a/utility/unit_of_work/transaction_test.go +++ b/utility/unit_of_work/transaction_test.go @@ -18,40 +18,40 @@ var ( ) func TestUtilityContext_AnteHandleMessage(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) - tx, startingBalance, _, signer := newTestingTransaction(t, ctx) - msg, err := ctx.anteHandleMessage(tx) + tx, startingBalance, _, signer := newTestingTransaction(t, uow) + msg, err := uow.anteHandleMessage(tx) require.NoError(t, err) require.Equal(t, signer.Address().Bytes(), msg.GetSigner()) - feeBig, err := ctx.getMessageSendFee() + feeBig, err := uow.getMessageSendFee() require.NoError(t, err) expectedAfterBalance := big.NewInt(0).Sub(startingBalance, feeBig) - amount, err := ctx.getAccountAmount(signer.Address()) + amount, err := uow.getAccountAmount(signer.Address()) require.NoError(t, err) require.Equal(t, expectedAfterBalance, amount, "unexpected after balance") } func TestUtilityContext_ApplyTransaction(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) + uow := newTestingUtilityUnitOfWork(t, 0) - tx, startingBalance, amount, signer := newTestingTransaction(t, ctx) - txResult, err := ctx.hydrateTxResult(tx, 0) + tx, startingBalance, amount, signer := newTestingTransaction(t, uow) + txResult, err := uow.hydrateTxResult(tx, 0) require.NoError(t, err) require.Equal(t, int32(0), txResult.GetResultCode()) require.Equal(t, "", txResult.GetError()) - feeBig, err := ctx.getMessageSendFee() + feeBig, err := uow.getMessageSendFee() require.NoError(t, err) expectedAmountSubtracted := amount.Add(amount, feeBig) expectedAfterBalance := big.NewInt(0).Sub(startingBalance, expectedAmountSubtracted) - amount, err = ctx.getAccountAmount(signer.Address()) + amount, err = uow.getAccountAmount(signer.Address()) require.NoError(t, err) require.Equal(t, expectedAfterBalance, amount, "unexpected after balance") } -//TODO: - @deblasis - refactor this +// TODO: - @deblasis - refactor this to test HandleTransaction specifically in the utility package // func TestUtilityContext_HandleTransaction(t *testing.T) { // ctx := newTestingUtilityContext(t, 0) // tx, _, _, _ := newTestingTransaction(t, ctx) @@ -67,8 +67,8 @@ func TestUtilityContext_ApplyTransaction(t *testing.T) { // } func TestUtilityContext_GetSignerCandidates(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - accs := getAllTestingAccounts(t, ctx) + uow := newTestingUtilityUnitOfWork(t, 0) + accs := getAllTestingAccounts(t, uow) sendAmount := big.NewInt(1000000) sendAmountString := utils.BigIntToString(sendAmount) @@ -77,14 +77,14 @@ func TestUtilityContext_GetSignerCandidates(t *testing.T) { addrBz2, er := hex.DecodeString(accs[1].GetAddress()) require.NoError(t, er) msg := NewTestingSendMessage(t, addrBz, addrBz2, sendAmountString) - candidates, err := ctx.getSignerCandidates(&msg) + candidates, err := uow.getSignerCandidates(&msg) require.NoError(t, err) require.Equal(t, 1, len(candidates), "wrong number of candidates") require.Equal(t, accs[0].GetAddress(), hex.EncodeToString(candidates[0]), "unexpected signer candidate") } -//TODO: - @deblasis - refactor this +// TODO: - @deblasis - refactor this to test HandleTransaction specifically in the utility package // func TestUtilityContext_CreateAndApplyBlock(t *testing.T) { // ctx := newTestingUtilityContext(t, 0) // tx, _, _, _ := newTestingTransaction(t, ctx) @@ -102,8 +102,8 @@ func TestUtilityContext_GetSignerCandidates(t *testing.T) { // } func TestUtilityContext_HandleMessage(t *testing.T) { - ctx := newTestingUtilityContext(t, 0) - accs := getAllTestingAccounts(t, ctx) + uow := newTestingUtilityUnitOfWork(t, 0) + accs := getAllTestingAccounts(t, uow) sendAmount := big.NewInt(1000000) sendAmountString := utils.BigIntToString(sendAmount) @@ -117,8 +117,8 @@ func TestUtilityContext_HandleMessage(t *testing.T) { addrBz2, er := hex.DecodeString(accs[1].GetAddress()) require.NoError(t, er) msg := NewTestingSendMessage(t, addrBz, addrBz2, sendAmountString) - require.NoError(t, ctx.handleMessageSend(&msg)) - accs = getAllTestingAccounts(t, ctx) + require.NoError(t, uow.handleMessageSend(&msg)) + accs = getAllTestingAccounts(t, uow) senderBalanceAfter, err := utils.StringToBigInt(accs[0].GetAmount()) require.NoError(t, err) @@ -129,7 +129,7 @@ func TestUtilityContext_HandleMessage(t *testing.T) { require.Equal(t, sendAmount, big.NewInt(0).Sub(recipientBalanceAfter, recipientBalanceBefore), "unexpected recipient balance") } -func newTestingTransaction(t *testing.T, ctx *baseUtilityUnitOfWork) (tx *coreTypes.Transaction, startingBalance, amountSent *big.Int, signer crypto.PrivateKey) { +func newTestingTransaction(t *testing.T, uow *baseUtilityUnitOfWork) (tx *coreTypes.Transaction, startingBalance, amountSent *big.Int, signer crypto.PrivateKey) { amountSent = new(big.Int).Set(defaultSendAmount) startingBalance = new(big.Int).Set(test_artifacts.DefaultAccountAmount) @@ -140,7 +140,7 @@ func newTestingTransaction(t *testing.T, ctx *baseUtilityUnitOfWork) (tx *coreTy require.NoError(t, err) signerAddr := signer.Address() - require.NoError(t, ctx.setAccountAmount(signerAddr, startingBalance)) + require.NoError(t, uow.setAccountAmount(signerAddr, startingBalance)) msg := NewTestingSendMessage(t, signerAddr, recipientAddr.Bytes(), utils.BigIntToString(amountSent)) any, err := codec.GetCodec().ToAny(&msg) From 75c0f183594145c88cadc4d2fae1c5fd3df90bb8 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 12 Mar 2023 21:17:16 +0000 Subject: [PATCH 32/59] refactor(utility): simplified TestUtilityContext_ApplyBlock --- utility/unit_of_work/block_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/utility/unit_of_work/block_test.go b/utility/unit_of_work/block_test.go index 32745c65f..a0a535e14 100644 --- a/utility/unit_of_work/block_test.go +++ b/utility/unit_of_work/block_test.go @@ -9,7 +9,6 @@ import ( coreTypes "github.com/pokt-network/pocket/shared/core/types" "github.com/pokt-network/pocket/shared/modules" mockModules "github.com/pokt-network/pocket/shared/modules/mocks" - "github.com/pokt-network/pocket/utility/types" "github.com/stretchr/testify/require" ) @@ -21,8 +20,7 @@ func TestUtilityContext_ApplyBlock(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0, func(uow *baseUtilityUnitOfWork) { uow.GetBus().RegisterModule(mockUtilityMod) - utilityCfg := uow.GetBus().GetRuntimeMgr().GetConfig().Utility - mockUtilityMod.EXPECT().GetMempool().Return(types.NewTxFIFOMempool(utilityCfg.MaxMempoolTransactionBytes, utilityCfg.MaxMempoolTransactions)).AnyTimes() + mockUtilityMod.EXPECT().GetMempool().Return(NewTestingMempool(t)).AnyTimes() }) tx, startingBalance, amountSent, signer := newTestingTransaction(t, uow) From 7228de87ff3a8177b75454ca21a8e008403729ad Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 12 Mar 2023 21:22:32 +0000 Subject: [PATCH 33/59] chore(utility): renamed tests --- utility/unit_of_work/account_test.go | 14 +-- utility/unit_of_work/actor_test.go | 26 +++--- utility/unit_of_work/application_test.go | 2 +- utility/unit_of_work/block_test.go | 6 +- utility/unit_of_work/gov_test.go | 110 +++++++++++------------ utility/unit_of_work/message_test.go | 18 ++-- utility/unit_of_work/transaction_test.go | 12 +-- 7 files changed, 94 insertions(+), 94 deletions(-) diff --git a/utility/unit_of_work/account_test.go b/utility/unit_of_work/account_test.go index 090315bcb..5763f502f 100644 --- a/utility/unit_of_work/account_test.go +++ b/utility/unit_of_work/account_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestUtilityContext_AddAccountAmount(t *testing.T) { +func TestUtilityUnitOfWork_AddAccountAmount(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) acc := getFirstTestingAccount(t, uow) @@ -31,7 +31,7 @@ func TestUtilityContext_AddAccountAmount(t *testing.T) { require.Equal(t, expected, afterAmount) } -func TestUtilityContext_SubtractAccountAmount(t *testing.T) { +func TestUtilityUnitOfWork_SubtractAccountAmount(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) acc := getFirstTestingAccount(t, uow) @@ -51,7 +51,7 @@ func TestUtilityContext_SubtractAccountAmount(t *testing.T) { require.Equal(t, expected, amount) } -func TestUtilityContext_SetAccountAmount(t *testing.T) { +func TestUtilityUnitOfWork_SetAccountAmount(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) addr, err := crypto.GenerateAddress() @@ -65,7 +65,7 @@ func TestUtilityContext_SetAccountAmount(t *testing.T) { require.Equal(t, amount, gotAmount) } -func TestUtilityContext_AddPoolAmount(t *testing.T) { +func TestUtilityUnitOfWork_AddPoolAmount(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) pool := getFirstTestingPool(t, uow) @@ -82,7 +82,7 @@ func TestUtilityContext_AddPoolAmount(t *testing.T) { require.Equal(t, expected, afterAmount) } -func TestUtilityContext_InsertPool(t *testing.T) { +func TestUtilityUnitOfWork_InsertPool(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) testPoolName := "TEST_POOL" @@ -95,7 +95,7 @@ func TestUtilityContext_InsertPool(t *testing.T) { require.Equal(t, amount, poolAmount) } -func TestUtilityContext_SetPoolAmount(t *testing.T) { +func TestUtilityUnitOfWork_SetPoolAmount(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) pool := getFirstTestingPool(t, uow) @@ -111,7 +111,7 @@ func TestUtilityContext_SetPoolAmount(t *testing.T) { require.Equal(t, amount, expectedAfterAmount) } -func TestUtilityContext_SubPoolAmount(t *testing.T) { +func TestUtilityUnitOfWork_SubPoolAmount(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) pool := getFirstTestingPool(t, uow) diff --git a/utility/unit_of_work/actor_test.go b/utility/unit_of_work/actor_test.go index de33676a2..d58e6f9f4 100644 --- a/utility/unit_of_work/actor_test.go +++ b/utility/unit_of_work/actor_test.go @@ -17,7 +17,7 @@ import ( "golang.org/x/exp/slices" ) -func TestUtilityContext_HandleMessageStake(t *testing.T) { +func TestUtilityUnitOfWork_HandleMessageStake(t *testing.T) { for actorTypeNum := range coreTypes.ActorType_name { if actorTypeNum == 0 { // ACTOR_TYPE_UNSPECIFIED continue @@ -61,7 +61,7 @@ func TestUtilityContext_HandleMessageStake(t *testing.T) { } } -func TestUtilityContext_HandleMessageEditStake(t *testing.T) { +func TestUtilityUnitOfWork_HandleMessageEditStake(t *testing.T) { for actorTypeNum := range coreTypes.ActorType_name { if actorTypeNum == 0 { // ACTOR_TYPE_UNSPECIFIED continue @@ -116,7 +116,7 @@ func TestUtilityContext_HandleMessageEditStake(t *testing.T) { } } -func TestUtilityContext_HandleMessageUnstake(t *testing.T) { +func TestUtilityUnitOfWork_HandleMessageUnstake(t *testing.T) { // The gov param for each actor will be set to this value numUnstakingBlocks := 5 @@ -167,7 +167,7 @@ func TestUtilityContext_HandleMessageUnstake(t *testing.T) { } } -func TestUtilityContext_HandleMessageUnpause(t *testing.T) { +func TestUtilityUnitOfWork_HandleMessageUnpause(t *testing.T) { // The gov param for each actor will be set to this value minPauseBlocksNumber := 5 @@ -249,7 +249,7 @@ func TestUtilityContext_HandleMessageUnpause(t *testing.T) { } } -func TestUtilityContext_GetUnbondingHeight(t *testing.T) { +func TestUtilityUnitOfWork_GetUnbondingHeight(t *testing.T) { for actorTypeNum := range coreTypes.ActorType_name { if actorTypeNum == 0 { // ACTOR_TYPE_UNSPECIFIED continue @@ -282,7 +282,7 @@ func TestUtilityContext_GetUnbondingHeight(t *testing.T) { } } -func TestUtilityContext_BeginUnstakingMaxPausedActors(t *testing.T) { +func TestUtilityUnitOfWork_BeginUnstakingMaxPausedActors(t *testing.T) { // The gov param for each actor will be set to this value maxPausedBlocks := 5 @@ -359,7 +359,7 @@ func TestUtilityContext_BeginUnstakingMaxPausedActors(t *testing.T) { } } -func TestUtilityContext_BeginUnstakingActorsPausedBefore_UnbondUnstakingActors(t *testing.T) { +func TestUtilityUnitOfWork_BeginUnstakingActorsPausedBefore_UnbondUnstakingActors(t *testing.T) { // The gov param for each actor will be set to this value maxPausedBlocks := 5 unstakingBlocks := 10 @@ -478,7 +478,7 @@ func TestUtilityContext_BeginUnstakingActorsPausedBefore_UnbondUnstakingActors(t } } -func TestUtilityContext_GetActorExists(t *testing.T) { +func TestUtilityUnitOfWork_GetActorExists(t *testing.T) { for actorTypeNum := range coreTypes.ActorType_name { if actorTypeNum == 0 { // ACTOR_TYPE_UNSPECIFIED continue @@ -506,7 +506,7 @@ func TestUtilityContext_GetActorExists(t *testing.T) { } } -func TestUtilityContext_GetOutputAddress(t *testing.T) { +func TestUtilityUnitOfWork_GetOutputAddress(t *testing.T) { for actorTypeNum := range coreTypes.ActorType_name { if actorTypeNum == 0 { // ACTOR_TYPE_UNSPECIFIED continue @@ -527,7 +527,7 @@ func TestUtilityContext_GetOutputAddress(t *testing.T) { } } -func TestUtilityContext_GetPauseHeightIfExists(t *testing.T) { +func TestUtilityUnitOfWork_GetPauseHeightIfExists(t *testing.T) { pauseHeight := int64(100) for actorTypeNum := range coreTypes.ActorType_name { @@ -565,7 +565,7 @@ func TestUtilityContext_GetPauseHeightIfExists(t *testing.T) { }) } } -func TestUtilityContext_GetMessageEditStakeSignerCandidates(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageEditStakeSignerCandidates(t *testing.T) { for actorTypeNum := range coreTypes.ActorType_name { if actorTypeNum == 0 { // ACTOR_TYPE_UNSPECIFIED continue @@ -595,7 +595,7 @@ func TestUtilityContext_GetMessageEditStakeSignerCandidates(t *testing.T) { } } -func TestUtilityContext_GetMessageUnpauseSignerCandidates(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageUnpauseSignerCandidates(t *testing.T) { for actorTypeNum := range coreTypes.ActorType_name { if actorTypeNum == 0 { // ACTOR_TYPE_UNSPECIFIED continue @@ -623,7 +623,7 @@ func TestUtilityContext_GetMessageUnpauseSignerCandidates(t *testing.T) { } } -func TestUtilityContext_GetMessageUnstakeSignerCandidates(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageUnstakeSignerCandidates(t *testing.T) { for actorTypeNum := range coreTypes.ActorType_name { if actorTypeNum == 0 { // ACTOR_TYPE_UNSPECIFIED continue diff --git a/utility/unit_of_work/application_test.go b/utility/unit_of_work/application_test.go index e6b9512de..6bf0afd5c 100644 --- a/utility/unit_of_work/application_test.go +++ b/utility/unit_of_work/application_test.go @@ -11,7 +11,7 @@ const ( DefaultAppSessionTokens = "100000000000000" ) -func TestUtilityContext_CalculateMaxAppRelays(t *testing.T) { +func TestUtilityUnitOfWork_CalculateMaxAppRelays(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 1) actor := getFirstActor(t, uow, coreTypes.ActorType_ACTOR_TYPE_APP) appSessionTokens, err := uow.calculateAppSessionTokens(actor.StakedAmount) diff --git a/utility/unit_of_work/block_test.go b/utility/unit_of_work/block_test.go index a0a535e14..5cf17551c 100644 --- a/utility/unit_of_work/block_test.go +++ b/utility/unit_of_work/block_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestUtilityContext_ApplyBlock(t *testing.T) { +func TestUtilityUnitOfWork_ApplyBlock(t *testing.T) { ctrl := gomock.NewController(t) mockUtilityMod := mockModules.NewMockUtilityModule(ctrl) mockUtilityMod.EXPECT().GetModuleName().Return(modules.UtilityModuleName).AnyTimes() @@ -73,7 +73,7 @@ func TestUtilityContext_ApplyBlock(t *testing.T) { } -func TestUtilityContext_BeginBlock(t *testing.T) { +func TestUtilityUnitOfWork_BeginBlock(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) tx, _, _, _ := newTestingTransaction(t, uow) @@ -99,7 +99,7 @@ func TestUtilityContext_BeginBlock(t *testing.T) { } -func TestUtilityContext_EndBlock(t *testing.T) { +func TestUtilityUnitOfWork_EndBlock(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) tx, _, _, _ := newTestingTransaction(t, uow) diff --git a/utility/unit_of_work/gov_test.go b/utility/unit_of_work/gov_test.go index 221dcd84d..c9b842a45 100644 --- a/utility/unit_of_work/gov_test.go +++ b/utility/unit_of_work/gov_test.go @@ -20,7 +20,7 @@ func DefaultTestingParams(_ *testing.T) *genesis.Params { return test_artifacts.DefaultParams() } -func TestUtilityContext_GetAppMaxChains(t *testing.T) { +func TestUtilityUnitOfWork_GetAppMaxChains(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) maxChains, err := uow.getAppMaxChains() @@ -28,7 +28,7 @@ func TestUtilityContext_GetAppMaxChains(t *testing.T) { require.Equal(t, int(defaultParams.GetAppMaxChains()), maxChains) } -func TestUtilityContext_GetAppMaxPausedBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetAppMaxPausedBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) gotParam, err := uow.getAppMaxPausedBlocks() @@ -36,7 +36,7 @@ func TestUtilityContext_GetAppMaxPausedBlocks(t *testing.T) { require.Equal(t, int(defaultParams.GetAppMaxPauseBlocks()), gotParam) } -func TestUtilityContext_GetAppMinimumPauseBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetAppMinimumPauseBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetAppMinimumPauseBlocks()) @@ -45,7 +45,7 @@ func TestUtilityContext_GetAppMinimumPauseBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetAppMinimumStake(t *testing.T) { +func TestUtilityUnitOfWork_GetAppMinimumStake(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetAppMinimumStake() @@ -54,7 +54,7 @@ func TestUtilityContext_GetAppMinimumStake(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetAppUnstakingBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetAppUnstakingBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int64(defaultParams.GetAppUnstakingBlocks()) @@ -63,7 +63,7 @@ func TestUtilityContext_GetAppUnstakingBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetBlocksPerSession(t *testing.T) { +func TestUtilityUnitOfWork_GetBlocksPerSession(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetBlocksPerSession()) @@ -72,7 +72,7 @@ func TestUtilityContext_GetBlocksPerSession(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetDoubleSignBurnPercentage(t *testing.T) { +func TestUtilityUnitOfWork_GetDoubleSignBurnPercentage(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetDoubleSignBurnPercentage()) @@ -81,7 +81,7 @@ func TestUtilityContext_GetDoubleSignBurnPercentage(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetDoubleSignFeeOwner(t *testing.T) { +func TestUtilityUnitOfWork_GetDoubleSignFeeOwner(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageDoubleSignFeeOwner() @@ -94,7 +94,7 @@ func TestUtilityContext_GetDoubleSignFeeOwner(t *testing.T) { require.Equal(t, defaultParamTx, gotParam) } -func TestUtilityContext_GetFishermanMaxChains(t *testing.T) { +func TestUtilityUnitOfWork_GetFishermanMaxChains(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetFishermanMaxChains()) @@ -103,7 +103,7 @@ func TestUtilityContext_GetFishermanMaxChains(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetFishermanMaxPausedBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetFishermanMaxPausedBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetFishermanMaxPauseBlocks()) @@ -112,7 +112,7 @@ func TestUtilityContext_GetFishermanMaxPausedBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetFishermanMinimumPauseBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetFishermanMinimumPauseBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetFishermanMinimumPauseBlocks()) @@ -121,7 +121,7 @@ func TestUtilityContext_GetFishermanMinimumPauseBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetFishermanMinimumStake(t *testing.T) { +func TestUtilityUnitOfWork_GetFishermanMinimumStake(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetFishermanMinimumStake() @@ -130,7 +130,7 @@ func TestUtilityContext_GetFishermanMinimumStake(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetFishermanUnstakingBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetFishermanUnstakingBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int64(defaultParams.GetFishermanUnstakingBlocks()) @@ -139,7 +139,7 @@ func TestUtilityContext_GetFishermanUnstakingBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetMaxEvidenceAgeInBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetMaxEvidenceAgeInBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetValidatorMaxEvidenceAgeInBlocks()) @@ -148,7 +148,7 @@ func TestUtilityContext_GetMaxEvidenceAgeInBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetMessageChangeParameterFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageChangeParameterFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageChangeParameterFee() @@ -157,7 +157,7 @@ func TestUtilityContext_GetMessageChangeParameterFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageDoubleSignFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageDoubleSignFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageDoubleSignFee() @@ -166,7 +166,7 @@ func TestUtilityContext_GetMessageDoubleSignFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageEditStakeAppFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageEditStakeAppFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageEditStakeAppFee() @@ -175,7 +175,7 @@ func TestUtilityContext_GetMessageEditStakeAppFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageEditStakeFishermanFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageEditStakeFishermanFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageEditStakeFishermanFee() @@ -184,7 +184,7 @@ func TestUtilityContext_GetMessageEditStakeFishermanFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageEditStakeServicerFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageEditStakeServicerFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageEditStakeServicerFee() @@ -193,7 +193,7 @@ func TestUtilityContext_GetMessageEditStakeServicerFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageEditStakeValidatorFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageEditStakeValidatorFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageEditStakeValidatorFee() @@ -202,7 +202,7 @@ func TestUtilityContext_GetMessageEditStakeValidatorFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageFishermanPauseServicerFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageFishermanPauseServicerFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageFishermanPauseServicerFee() @@ -211,7 +211,7 @@ func TestUtilityContext_GetMessageFishermanPauseServicerFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessagePauseAppFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessagePauseAppFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessagePauseAppFee() @@ -220,7 +220,7 @@ func TestUtilityContext_GetMessagePauseAppFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessagePauseFishermanFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessagePauseFishermanFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessagePauseFishermanFee() @@ -229,7 +229,7 @@ func TestUtilityContext_GetMessagePauseFishermanFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessagePauseServicerFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessagePauseServicerFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessagePauseServicerFee() @@ -238,7 +238,7 @@ func TestUtilityContext_GetMessagePauseServicerFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessagePauseValidatorFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessagePauseValidatorFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessagePauseValidatorFee() @@ -247,7 +247,7 @@ func TestUtilityContext_GetMessagePauseValidatorFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageProveTestScoreFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageProveTestScoreFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageProveTestScoreFee() @@ -256,7 +256,7 @@ func TestUtilityContext_GetMessageProveTestScoreFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageSendFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageSendFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageSendFee() @@ -265,7 +265,7 @@ func TestUtilityContext_GetMessageSendFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageStakeAppFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageStakeAppFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageStakeAppFee() @@ -274,7 +274,7 @@ func TestUtilityContext_GetMessageStakeAppFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageStakeFishermanFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageStakeFishermanFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageStakeFishermanFee() @@ -283,7 +283,7 @@ func TestUtilityContext_GetMessageStakeFishermanFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageStakeServicerFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageStakeServicerFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageStakeServicerFee() @@ -292,7 +292,7 @@ func TestUtilityContext_GetMessageStakeServicerFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageStakeValidatorFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageStakeValidatorFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageStakeValidatorFee() @@ -301,7 +301,7 @@ func TestUtilityContext_GetMessageStakeValidatorFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageTestScoreFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageTestScoreFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageTestScoreFee() @@ -310,7 +310,7 @@ func TestUtilityContext_GetMessageTestScoreFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageUnpauseAppFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageUnpauseAppFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnpauseAppFee() @@ -319,7 +319,7 @@ func TestUtilityContext_GetMessageUnpauseAppFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageUnpauseFishermanFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageUnpauseFishermanFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnpauseFishermanFee() @@ -328,7 +328,7 @@ func TestUtilityContext_GetMessageUnpauseFishermanFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageUnpauseServicerFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageUnpauseServicerFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnpauseServicerFee() @@ -337,7 +337,7 @@ func TestUtilityContext_GetMessageUnpauseServicerFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageUnpauseValidatorFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageUnpauseValidatorFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnpauseValidatorFee() @@ -346,7 +346,7 @@ func TestUtilityContext_GetMessageUnpauseValidatorFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageUnstakeAppFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageUnstakeAppFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnstakeAppFee() @@ -355,7 +355,7 @@ func TestUtilityContext_GetMessageUnstakeAppFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageUnstakeFishermanFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageUnstakeFishermanFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnstakeFishermanFee() @@ -364,7 +364,7 @@ func TestUtilityContext_GetMessageUnstakeFishermanFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageUnstakeServicerFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageUnstakeServicerFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnstakeServicerFee() @@ -373,7 +373,7 @@ func TestUtilityContext_GetMessageUnstakeServicerFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMessageUnstakeValidatorFee(t *testing.T) { +func TestUtilityUnitOfWork_GetMessageUnstakeValidatorFee(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetMessageUnstakeValidatorFee() @@ -382,7 +382,7 @@ func TestUtilityContext_GetMessageUnstakeValidatorFee(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetMissedBlocksBurnPercentage(t *testing.T) { +func TestUtilityUnitOfWork_GetMissedBlocksBurnPercentage(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetMissedBlocksBurnPercentage()) @@ -391,7 +391,7 @@ func TestUtilityContext_GetMissedBlocksBurnPercentage(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetProposerPercentageOfFees(t *testing.T) { +func TestUtilityUnitOfWork_GetProposerPercentageOfFees(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetProposerPercentageOfFees()) @@ -400,7 +400,7 @@ func TestUtilityContext_GetProposerPercentageOfFees(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetServicerMaxChains(t *testing.T) { +func TestUtilityUnitOfWork_GetServicerMaxChains(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetServicerMaxChains()) @@ -409,7 +409,7 @@ func TestUtilityContext_GetServicerMaxChains(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetServicerMaxPausedBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetServicerMaxPausedBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetServicerMaxPauseBlocks()) @@ -418,7 +418,7 @@ func TestUtilityContext_GetServicerMaxPausedBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetServicerMinimumPauseBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetServicerMinimumPauseBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetServicerMinimumPauseBlocks()) @@ -427,7 +427,7 @@ func TestUtilityContext_GetServicerMinimumPauseBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetServicerMinimumStake(t *testing.T) { +func TestUtilityUnitOfWork_GetServicerMinimumStake(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetServicerMinimumStake() @@ -436,7 +436,7 @@ func TestUtilityContext_GetServicerMinimumStake(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetServicerUnstakingBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetServicerUnstakingBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int64(defaultParams.GetServicerUnstakingBlocks()) @@ -445,7 +445,7 @@ func TestUtilityContext_GetServicerUnstakingBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetSessionTokensMultiplier(t *testing.T) { +func TestUtilityUnitOfWork_GetSessionTokensMultiplier(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetAppSessionTokensMultiplier()) @@ -454,7 +454,7 @@ func TestUtilityContext_GetSessionTokensMultiplier(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetValidatorMaxMissedBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetValidatorMaxMissedBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetValidatorMaximumMissedBlocks()) @@ -463,7 +463,7 @@ func TestUtilityContext_GetValidatorMaxMissedBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetValidatorMaxPausedBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetValidatorMaxPausedBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetValidatorMaxPauseBlocks()) @@ -472,7 +472,7 @@ func TestUtilityContext_GetValidatorMaxPausedBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetValidatorMinimumPauseBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetValidatorMinimumPauseBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int(defaultParams.GetValidatorMinimumPauseBlocks()) @@ -481,7 +481,7 @@ func TestUtilityContext_GetValidatorMinimumPauseBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_GetValidatorMinimumStake(t *testing.T) { +func TestUtilityUnitOfWork_GetValidatorMinimumStake(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetValidatorMinimumStake() @@ -490,7 +490,7 @@ func TestUtilityContext_GetValidatorMinimumStake(t *testing.T) { require.Equal(t, defaultParam, utils.BigIntToString(gotParam)) } -func TestUtilityContext_GetValidatorUnstakingBlocks(t *testing.T) { +func TestUtilityUnitOfWork_GetValidatorUnstakingBlocks(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := int64(defaultParams.GetValidatorUnstakingBlocks()) @@ -499,7 +499,7 @@ func TestUtilityContext_GetValidatorUnstakingBlocks(t *testing.T) { require.Equal(t, defaultParam, gotParam) } -func TestUtilityContext_HandleMessageChangeParameter(t *testing.T) { +func TestUtilityUnitOfWork_HandleMessageChangeParameter(t *testing.T) { cdc := codec.GetCodec() uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) @@ -524,7 +524,7 @@ func TestUtilityContext_HandleMessageChangeParameter(t *testing.T) { require.Equal(t, int(newParamValue), gotParam) } -func TestUtilityContext_GetParamOwner(t *testing.T) { +func TestUtilityUnitOfWork_GetParamOwner(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) defaultParams := DefaultTestingParams(t) defaultParam := defaultParams.GetAclOwner() diff --git a/utility/unit_of_work/message_test.go b/utility/unit_of_work/message_test.go index b522cc619..39c2531e7 100644 --- a/utility/unit_of_work/message_test.go +++ b/utility/unit_of_work/message_test.go @@ -10,9 +10,9 @@ import ( "github.com/stretchr/testify/require" ) -func TestUtilityContext_HandleMessageSend(t *testing.T) { - ctx := newTestingUtilityUnitOfWork(t, 0) - accs := getAllTestingAccounts(t, ctx) +func TestUtilityUnitOfWork_HandleMessageSend(t *testing.T) { + uow := newTestingUtilityUnitOfWork(t, 0) + accs := getAllTestingAccounts(t, uow) sendAmount := big.NewInt(1000000) sendAmountString := utils.BigIntToString(sendAmount) @@ -29,10 +29,10 @@ func TestUtilityContext_HandleMessageSend(t *testing.T) { require.NoError(t, er) msg := NewTestingSendMessage(t, addrBz, addrBz2, sendAmountString) - err = ctx.handleMessageSend(&msg) + err = uow.handleMessageSend(&msg) require.NoError(t, err, "handle message send") - accs = getAllTestingAccounts(t, ctx) + accs = getAllTestingAccounts(t, uow) senderBalanceAfter, err := utils.StringToBigInt(accs[0].GetAmount()) require.NoError(t, err) @@ -42,9 +42,9 @@ func TestUtilityContext_HandleMessageSend(t *testing.T) { require.Equal(t, sendAmount, big.NewInt(0).Sub(recipientBalanceAfter, recipientBalanceBefore)) } -func TestUtilityContext_GetMessageSendSignerCandidates(t *testing.T) { - ctx := newTestingUtilityUnitOfWork(t, 0) - accs := getAllTestingAccounts(t, ctx) +func TestUtilityUnitOfWork_GetMessageSendSignerCandidates(t *testing.T) { + uow := newTestingUtilityUnitOfWork(t, 0) + accs := getAllTestingAccounts(t, uow) sendAmount := big.NewInt(1000000) sendAmountString := utils.BigIntToString(sendAmount) @@ -56,7 +56,7 @@ func TestUtilityContext_GetMessageSendSignerCandidates(t *testing.T) { require.NoError(t, er) msg := NewTestingSendMessage(t, addrBz, addrBz2, sendAmountString) - candidates, err := ctx.getMessageSendSignerCandidates(&msg) + candidates, err := uow.getMessageSendSignerCandidates(&msg) require.NoError(t, err) require.Equal(t, 1, len(candidates)) require.Equal(t, addrBz, candidates[0]) diff --git a/utility/unit_of_work/transaction_test.go b/utility/unit_of_work/transaction_test.go index 30c4d3d89..11ed620db 100644 --- a/utility/unit_of_work/transaction_test.go +++ b/utility/unit_of_work/transaction_test.go @@ -17,7 +17,7 @@ var ( defaultSendAmount = big.NewInt(10000) ) -func TestUtilityContext_AnteHandleMessage(t *testing.T) { +func TestUtilityUnitOfWork_AnteHandleMessage(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) tx, startingBalance, _, signer := newTestingTransaction(t, uow) @@ -33,7 +33,7 @@ func TestUtilityContext_AnteHandleMessage(t *testing.T) { require.Equal(t, expectedAfterBalance, amount, "unexpected after balance") } -func TestUtilityContext_ApplyTransaction(t *testing.T) { +func TestUtilityUnitOfWork_ApplyTransaction(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) tx, startingBalance, amount, signer := newTestingTransaction(t, uow) @@ -52,7 +52,7 @@ func TestUtilityContext_ApplyTransaction(t *testing.T) { } // TODO: - @deblasis - refactor this to test HandleTransaction specifically in the utility package -// func TestUtilityContext_HandleTransaction(t *testing.T) { +// func TestUtilityUnitOfWork_HandleTransaction(t *testing.T) { // ctx := newTestingUtilityContext(t, 0) // tx, _, _, _ := newTestingTransaction(t, ctx) @@ -66,7 +66,7 @@ func TestUtilityContext_ApplyTransaction(t *testing.T) { // require.Equal(t, testUtilityMod.HandleTransaction(txBz).Error(), typesUtil.ErrDuplicateTransaction().Error()) // } -func TestUtilityContext_GetSignerCandidates(t *testing.T) { +func TestUtilityUnitOfWork_GetSignerCandidates(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) accs := getAllTestingAccounts(t, uow) @@ -85,7 +85,7 @@ func TestUtilityContext_GetSignerCandidates(t *testing.T) { } // TODO: - @deblasis - refactor this to test HandleTransaction specifically in the utility package -// func TestUtilityContext_CreateAndApplyBlock(t *testing.T) { +// func TestUtilityUnitOfWork_CreateAndApplyBlock(t *testing.T) { // ctx := newTestingUtilityContext(t, 0) // tx, _, _, _ := newTestingTransaction(t, ctx) @@ -101,7 +101,7 @@ func TestUtilityContext_GetSignerCandidates(t *testing.T) { // require.Equal(t, txs[0], txBz) // } -func TestUtilityContext_HandleMessage(t *testing.T) { +func TestUtilityUnitOfWork_HandleMessage(t *testing.T) { uow := newTestingUtilityUnitOfWork(t, 0) accs := getAllTestingAccounts(t, uow) From ae32b35af1e92f6c73e542f7157f0fcfea54ca15 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 12 Mar 2023 21:23:00 +0000 Subject: [PATCH 34/59] chore(consensus): updated utilityContext references --- consensus/block.go | 2 +- consensus/state_sync/interfaces.go | 6 +++--- consensus/types/messages.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/consensus/block.go b/consensus/block.go index 7b35f68e3..4a4933337 100644 --- a/consensus/block.go +++ b/consensus/block.go @@ -71,7 +71,7 @@ func (m *consensusModule) refreshUtilityUnitOfWork() error { // Catch-all structure to release the previous utility context if it wasn't properly cleaned up. // Ideally, this should not be called. if m.utilityUnitOfWork != nil { - m.logger.Warn().Msg(typesCons.NilUtilityContextWarning) + m.logger.Warn().Msg(typesCons.NilUtilityUOWWarning) if err := m.utilityUnitOfWork.Release(); err != nil { m.logger.Warn().Err(err).Msg("Error releasing utility context") } diff --git a/consensus/state_sync/interfaces.go b/consensus/state_sync/interfaces.go index 726175f80..12a12cf82 100644 --- a/consensus/state_sync/interfaces.go +++ b/consensus/state_sync/interfaces.go @@ -48,9 +48,9 @@ type StateSyncModuleLEGACY interface { // - Create a Utility Context // - Block.ValidateBasic() // - Consensus Module Replica Path - // - Prepare Block: utilityContext.SetProposalBlock(block) - // - Apply Block: utilityContext.ApplyBlock(block) - // - Validate Block: utilityContext.AppHash == Block.AppHash + // - Prepare Block: utilityUnitOfWork.SetProposalBlock(block) + // - Apply Block: utilityUnitOfWork.ApplyBlock(block) + // - Validate Block: utilityUnitOfWork.AppHash == Block.AppHash // - Store Block: consensusModule.CommitBlock() HandleStateSyncMessage(msg BlockResponseMessage) diff --git a/consensus/types/messages.go b/consensus/types/messages.go index ec0ee174c..7d81c1e04 100644 --- a/consensus/types/messages.go +++ b/consensus/types/messages.go @@ -20,7 +20,7 @@ const ( ProposalBlockExtends = "the ProposalQC block is the same as the LockedQC block" // WARN - NilUtilityContextWarning = "⚠️ [WARN] utilityContext expected to be nil but is not. TODO: Investigate why this is and fix it" + NilUtilityUOWWarning = "⚠️ [WARN] utilityUnitOfWork expected to be nil but is not. TODO: Investigate why this is and fix it" InvalidPartialSigInQCWarning = "⚠️ [WARN] QC contains an invalid partial signature" // DEBUG From 781af57ab2b245892e7c8ff86dead81ce703344e Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 12 Mar 2023 21:23:22 +0000 Subject: [PATCH 35/59] docs(shared): updated utilityContext references --- shared/docs/PROTOCOL_STATE_HASH.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/shared/docs/PROTOCOL_STATE_HASH.md b/shared/docs/PROTOCOL_STATE_HASH.md index a495ed6ed..b7533547a 100644 --- a/shared/docs/PROTOCOL_STATE_HASH.md +++ b/shared/docs/PROTOCOL_STATE_HASH.md @@ -16,7 +16,7 @@ The `Utility` and `Persistence` modules maintain a context (i.e. an ephemeral st On every round of every height: 1. The `Consensus` module handles a `NEWROUND` message -2. A new `UtilityContext` is initialized at the current height +2. A new `UtilityUnitOfWork` is initialized at the current height 3. A new `PersistenceRWContext` is initialized at the current height 4. The [Block Application](#block-application) flow commences @@ -41,7 +41,7 @@ sequenceDiagram U->>U: store context
locally activate U deactivate U - U->>-C: UtilityContext + U->>-C: UtilityUnitOfWork C->>C: store context
locally deactivate C @@ -56,9 +56,9 @@ _The **Proposer** drives the **Validators** to agreement via the **Consensus Lif --- 5. The `Consensus` module handles the `DECIDE` message -6. The `commitQC` is propagated to the `UtilityContext` & `PersistenceContext` on `Commit` +6. The `commitQC` is propagated to the `UtilityUnitOfWork` & `PersistenceContext` on `Commit` 7. The persistence module's internal implementation for ['Store Block'](../../persistence/docs/PROTOCOL_STORE_BLOCK.md) must execute. -8. Both the `UtilityContext` and `PersistenceContext` are released +8. Both the `UtilityUnitOfWork` and `PersistenceContext` are released ```mermaid sequenceDiagram @@ -121,7 +121,7 @@ graph TD As either the _leader_ or _replica_, the following steps are followed to apply the proposal transactions in the block. -1. Update the `UtilityContext` with the proposed block +1. Update the `UtilityUnitOfWork` with the proposed block 2. Call either `ApplyBlock` or `CreateAndApplyProposalBlock` based on the flow above ```mermaid From 809005c39cf4762448556bddbb9209ed57b0727e Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 12 Mar 2023 21:29:47 +0000 Subject: [PATCH 36/59] chore(consensus): updated utility context references --- consensus/block.go | 6 +++--- consensus/e2e_tests/pacemaker_test.go | 2 +- consensus/pacemaker/module.go | 2 +- consensus/state_sync/interfaces.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/consensus/block.go b/consensus/block.go index 4a4933337..c42166c17 100644 --- a/consensus/block.go +++ b/consensus/block.go @@ -68,17 +68,17 @@ func (m *consensusModule) isValidMessageBlock(msg *typesCons.HotstuffMessage) (b // Creates a new Utility Unit Of Work and clears/nullifies any previous U.O.W. if they exist func (m *consensusModule) refreshUtilityUnitOfWork() error { - // Catch-all structure to release the previous utility context if it wasn't properly cleaned up. + // Catch-all structure to release the previous utility unit of work if it wasn't properly cleaned up. // Ideally, this should not be called. if m.utilityUnitOfWork != nil { m.logger.Warn().Msg(typesCons.NilUtilityUOWWarning) if err := m.utilityUnitOfWork.Release(); err != nil { - m.logger.Warn().Err(err).Msg("Error releasing utility context") + m.logger.Warn().Err(err).Msg("Error releasing utility unit of work") } m.utilityUnitOfWork = nil } - // Only one write context can exist at a time, and the utility context needs to instantiate + // Only one write context can exist at a time, and the utility unit of work needs to instantiate // a new one to modify the state. if err := m.GetBus().GetPersistenceModule().ReleaseWriteContext(); err != nil { m.logger.Warn().Err(err).Msg("Error releasing persistence write context") diff --git a/consensus/e2e_tests/pacemaker_test.go b/consensus/e2e_tests/pacemaker_test.go index 8176fd979..0c3fb13be 100644 --- a/consensus/e2e_tests/pacemaker_test.go +++ b/consensus/e2e_tests/pacemaker_test.go @@ -177,7 +177,7 @@ func TestPacemakerCatchupSameStepDifferentRounds(t *testing.T) { // Set all nodes to the same STEP and HEIGHT BUT different ROUNDS for _, pocketNode := range pocketNodes { - // Update height, step, leaderId, and utility context via setters exposed with the debug interface + // Update height, step, leaderId, and utility unit of work via setters exposed with the debug interface consensusModImpl := GetConsensusModImpl(pocketNode) consensusModImpl.MethodByName("SetHeight").Call([]reflect.Value{reflect.ValueOf(testHeight)}) consensusModImpl.MethodByName("SetStep").Call([]reflect.Value{reflect.ValueOf(testStep)}) diff --git a/consensus/pacemaker/module.go b/consensus/pacemaker/module.go index a5f42e3aa..2d89dfbe7 100644 --- a/consensus/pacemaker/module.go +++ b/consensus/pacemaker/module.go @@ -244,7 +244,7 @@ func (m *pacemaker) startNextView(qc *typesCons.QuorumCertificate, forceNextView consensusMod.SetStep(uint8(newRound)) consensusMod.ResetRound() if err := consensusMod.ReleaseUtilityUnitOfWork(); err != nil { - log.Println("[WARN] NewHeight: Failed to release utility context: ", err) + log.Println("[WARN] NewHeight: Failed to release utility unit of work: ", err) return } diff --git a/consensus/state_sync/interfaces.go b/consensus/state_sync/interfaces.go index 12a12cf82..fed117fbc 100644 --- a/consensus/state_sync/interfaces.go +++ b/consensus/state_sync/interfaces.go @@ -45,7 +45,7 @@ type StateSyncModuleLEGACY interface { // -- Constructor Setter Functions -- // `HandleStateSync` function: - // - Create a Utility Context + // - Create a Utility Unit Of Work // - Block.ValidateBasic() // - Consensus Module Replica Path // - Prepare Block: utilityUnitOfWork.SetProposalBlock(block) From f3bbea3bc15fd208da714096a4212912ab49658a Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 12 Mar 2023 21:30:03 +0000 Subject: [PATCH 37/59] chore(shared): updated utility context references --- shared/modules/utility_module.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shared/modules/utility_module.go b/shared/modules/utility_module.go index d43066ad0..77b4ca58d 100644 --- a/shared/modules/utility_module.go +++ b/shared/modules/utility_module.go @@ -46,7 +46,7 @@ type UnstakingActor interface { type UtilityUnitOfWork interface { IntegratableModule - // SetProposalBlock updates the utility context with the proposed state transition. + // SetProposalBlock updates the utility unit of work with the proposed state transition. // It does not apply, validate or commit the changes. // For example, it can be use during state sync to set a proposed state transition before validation. // TODO: Investigate a way to potentially simplify the interface by removing this function. @@ -58,10 +58,10 @@ type UtilityUnitOfWork interface { // NOTE: this is called by the replica OR by the leader when `prepareQc` is not `nil` ApplyBlock() (stateHash string, txs [][]byte, err error) - // Release releases this utility context and any underlying contexts it references + // Release releases this utility unit of work and any underlying contexts it references Release() error - // Commit commits this utility context along with any underlying contexts (e.g. persistenceContext) it references + // Commit commits this utility unit of work along with any underlying contexts (e.g. persistenceContext) it references Commit(quorumCert []byte) error } From 9fa0a008ee24eeebec193f6d0bccd32a64e18b05 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 12 Mar 2023 22:21:23 +0000 Subject: [PATCH 38/59] refactor(consensus): restored CreateAndApplyProposalBlock --- consensus/hotstuff_leader.go | 2 +- shared/modules/utility_module.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/consensus/hotstuff_leader.go b/consensus/hotstuff_leader.go index 6e8faa23d..0c9d776be 100644 --- a/consensus/hotstuff_leader.go +++ b/consensus/hotstuff_leader.go @@ -389,7 +389,7 @@ func (m *consensusModule) prepareBlock(qc *typesCons.QuorumCertificate) (*coreTy } // Reap the mempool for transactions to be applied in this block - stateHash, txs, err := leaderUow.CreateProposalBlock(m.privateKey.Address(), maxTxBytes) + stateHash, txs, err := leaderUow.CreateAndApplyProposalBlock(m.privateKey.Address(), maxTxBytes) if err != nil { return nil, err } diff --git a/shared/modules/utility_module.go b/shared/modules/utility_module.go index 77b4ca58d..10a6b525b 100644 --- a/shared/modules/utility_module.go +++ b/shared/modules/utility_module.go @@ -70,8 +70,8 @@ type LeaderUtilityUnitOfWork interface { // CreateAndApplyProposalBlock reaps the mempool for txs to be proposed in a new block, and // applies them to this context after validation. - // TODO: @deblasis: new signature? - CreateProposalBlock(proposer []byte, maxTxBytes uint64) (stateHash string, txs [][]byte, err error) + // TODO: #508 new signature + CreateAndApplyProposalBlock(proposer []byte, maxTxBytes uint64) (stateHash string, txs [][]byte, err error) } type ReplicaUtilityUnitOfWork interface { From a225d37448b199de510514eb5e7cbcfe1b46907a Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 12 Mar 2023 22:37:36 +0000 Subject: [PATCH 39/59] refactor(utility): CreateAndApplyProposalBlock in leaderUow --- utility/unit_of_work/uow_leader.go | 89 +++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 15 deletions(-) diff --git a/utility/unit_of_work/uow_leader.go b/utility/unit_of_work/uow_leader.go index 13a92d9ec..f57b8f252 100644 --- a/utility/unit_of_work/uow_leader.go +++ b/utility/unit_of_work/uow_leader.go @@ -2,6 +2,7 @@ package unit_of_work import ( "github.com/pokt-network/pocket/logger" + coreTypes "github.com/pokt-network/pocket/shared/core/types" "github.com/pokt-network/pocket/shared/modules" ) @@ -25,19 +26,77 @@ func NewForLeader(height int64, readContext modules.PersistenceReadContext, rwPe } } -func (uow *leaderUtilityUnitOfWork) CreateProposalBlock(proposer []byte, maxTxBytes uint64) (stateHash string, txs [][]byte, err error) { - // if burnFn != nil { - // uow.logger.Debug().Msg("running burnFn...") - // if err := burnFn(uow); err != nil { - // return "", nil, err - // } - // } - - // if rewardFn != nil { - // uow.logger.Debug().Msg("running rewardFn...") - // if err := rewardFn(uow); err != nil { - // return "", nil, err - // } - // } - panic("unimplemented") +func (uow *leaderUtilityUnitOfWork) CreateAndApplyProposalBlock(proposer []byte, maxTxBytes uint64) (stateHash string, txs [][]byte, err error) { + prevBlockByzantineVals, err := uow.prevBlockByzantineValidators() + if err != nil { + return "", nil, err + } + + // begin block lifecycle phase + if err := uow.beginBlock(prevBlockByzantineVals); err != nil { + return "", nil, err + } + txs = make([][]byte, 0) + txsTotalBz := uint64(0) + txIdx := 0 + + mempool := uow.GetBus().GetUtilityModule().GetMempool() + for !mempool.IsEmpty() { + // NB: In order for transactions to have entered the mempool, `HandleTransaction` must have + // been called which handles basic checks & validation. + txBz, err := mempool.PopTx() + if err != nil { + return "", nil, err + } + + tx, err := coreTypes.TxFromBytes(txBz) + if err != nil { + return "", nil, err + } + + txBzSize := uint64(len(txBz)) + txsTotalBz += txBzSize + + // Exceeding maximum transaction bytes to be added in this block + if txsTotalBz >= maxTxBytes { + // Add back popped tx to be applied in a future block + if err := mempool.AddTx(txBz); err != nil { + return "", nil, err + } + break // we've reached our max + } + + txResult, err := uow.hydrateTxResult(tx, txIdx) + if err != nil { + uow.logger.Err(err).Msg("Error in ApplyTransaction") + // TODO(#327): Properly implement 'unhappy path' for save points + if err := uow.revertLastSavePoint(); err != nil { + return "", nil, err + } + txsTotalBz -= txBzSize + continue + } + + // Index the transaction + if err := uow.persistenceRWContext.IndexTransaction(txResult); err != nil { + uow.logger.Fatal().Err(err).Msgf("TODO(#327): The transaction can by hydrated but not indexed. Crash the process for now: %v\n", err) + } + + txs = append(txs, txBz) + txIdx++ + } + + if err := uow.endBlock(proposer); err != nil { + return "", nil, err + } + + // TODO: @deblasis - this should be from a ReadContext (the ephemeral/staging one) + // Compute & return the new state hash + stateHash, err = uow.persistenceRWContext.ComputeStateHash() + if err != nil { + uow.logger.Fatal().Err(err).Msg("Updating the app hash failed. TODO: Look into roll-backing the entire commit...") + } + uow.logger.Info().Str("state_hash", stateHash).Msgf("CreateAndApplyProposalBlock finished successfully") + + return stateHash, txs, err } From 1be71bd280d37a5301bf33894be2bed8080773ed Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 12 Mar 2023 22:38:14 +0000 Subject: [PATCH 40/59] =?UTF-8?q?chore(utility):=20context=20=F0=9F=A7=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utility/context.go | 103 --------------------------------------------- 1 file changed, 103 deletions(-) delete mode 100644 utility/context.go diff --git a/utility/context.go b/utility/context.go deleted file mode 100644 index bb8538542..000000000 --- a/utility/context.go +++ /dev/null @@ -1,103 +0,0 @@ -package utility - -// import ( -// "encoding/hex" - -// "github.com/pokt-network/pocket/shared/modules" -// "github.com/pokt-network/pocket/shared/modules/base_modules" -// typesUtil "github.com/pokt-network/pocket/utility/types" -// ) - -// var ( -// _ modules.IntegratableModule = &utilityContext{} -// _ modules.UtilityUnitOfWork = &utilityContext{} -// ) - -// type utilityContext struct { -// base_modules.IntegratableModule - -// logger *modules.Logger -// height int64 - -// store modules.PersistenceRWContext -// savePointsSet map[string]struct{} -// savePointsList [][]byte - -// // TECHDEBT: Consolidate all these types with the shared Protobuf struct and create a `proposalBlock` -// proposalStateHash string -// proposalProposerAddr []byte -// proposalBlockTxs [][]byte -// } - -// // func (u *utilityModule) NewContext(height int64) (modules.UtilityContext, error) { -// // persistenceCtx, err := u.GetBus().GetPersistenceModule().NewRWContext(height) -// // if err != nil { -// // return nil, typesUtil.ErrNewPersistenceContext(err) -// // } -// // ctx := &utilityContext{ -// // logger: u.logger, -// // height: height, - -// // // No save points on start -// // store: persistenceCtx, -// // savePointsList: make([][]byte, 0), -// // savePointsSet: make(map[string]struct{}), -// // } -// // ctx.SetBus(u.GetBus()) -// // return ctx, nil -// // } - -// func (p *utilityContext) SetProposalBlock(blockHash string, proposerAddr []byte, txs [][]byte) error { -// p.proposalStateHash = blockHash -// p.proposalProposerAddr = proposerAddr -// p.proposalBlockTxs = txs -// return nil -// } - -// func (u *utilityContext) Commit(quorumCert []byte) error { -// if err := u.store.Commit(u.proposalProposerAddr, quorumCert); err != nil { -// return err -// } -// u.store = nil -// return nil -// } - -// func (u *utilityContext) Release() error { -// if u.store == nil { -// return nil -// } -// if err := u.store.Release(); err != nil { -// return err -// } -// u.store = nil -// return nil -// } - -// // TODO: This has not been tested or investigated in detail -// func (u *utilityContext) revertLastSavePoint() typesUtil.Error { -// if len(u.savePointsSet) == 0 { -// return typesUtil.ErrEmptySavePoints() -// } -// var key []byte -// popIndex := len(u.savePointsList) - 1 -// key, u.savePointsList = u.savePointsList[popIndex], u.savePointsList[:popIndex] -// delete(u.savePointsSet, hex.EncodeToString(key)) -// if err := u.store.RollbackToSavePoint(key); err != nil { -// return typesUtil.ErrRollbackSavePoint(err) -// } -// return nil -// } - -// //nolint:unused // TODO: This has not been tested or investigated in detail -// func (u *utilityContext) newSavePoint(txHashBz []byte) typesUtil.Error { -// if err := u.store.NewSavePoint(txHashBz); err != nil { -// return typesUtil.ErrNewSavePoint(err) -// } -// txHash := hex.EncodeToString(txHashBz) -// if _, exists := u.savePointsSet[txHash]; exists { -// return typesUtil.ErrDuplicateSavePoint() -// } -// u.savePointsList = append(u.savePointsList, txHashBz) -// u.savePointsSet[txHash] = struct{}{} -// return nil -// } From dcccee8021cb13572d47e3d2ec39e854670895e5 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 12 Mar 2023 23:27:23 +0000 Subject: [PATCH 41/59] fix(consensus): fix tests --- consensus/e2e_tests/utils_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/e2e_tests/utils_test.go b/consensus/e2e_tests/utils_test.go index 593bb5b3d..4b752d726 100644 --- a/consensus/e2e_tests/utils_test.go +++ b/consensus/e2e_tests/utils_test.go @@ -461,7 +461,7 @@ func baseLeaderUtilityUnitOfWorkMock(t *testing.T, genesisState *genesis.Genesis persistenceContextMock.EXPECT().GetBlockHash(gomock.Any()).Return("", nil).AnyTimes() utilityLeaderUnitOfWorkMock.EXPECT(). - CreateProposalBlock(gomock.Any(), maxTxBytes). + CreateAndApplyProposalBlock(gomock.Any(), maxTxBytes). Return(stateHash, make([][]byte, 0), nil). AnyTimes() utilityLeaderUnitOfWorkMock.EXPECT(). From d7ee6888a09a11724842b52b73f2fca6640e7db3 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 12 Mar 2023 23:27:56 +0000 Subject: [PATCH 42/59] chore(utility): lint --- utility/unit_of_work/module.go | 4 ++-- utility/unit_of_work/module_test.go | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/utility/unit_of_work/module.go b/utility/unit_of_work/module.go index f16d2655c..cf77130f7 100644 --- a/utility/unit_of_work/module.go +++ b/utility/unit_of_work/module.go @@ -114,7 +114,7 @@ func (u *baseUtilityUnitOfWork) CreateAndApplyProposalBlock(proposer []byte, max } // CLEANUP: code re-use ApplyBlock() for CreateAndApplyBlock() -func (u *baseUtilityUnitOfWork) ApplyBlock() (string, [][]byte, error) { +func (u *baseUtilityUnitOfWork) ApplyBlock() (stateHash string, txs [][]byte, err error) { lastByzantineValidators, err := u.prevBlockByzantineValidators() if err != nil { return "", nil, err @@ -172,7 +172,7 @@ func (u *baseUtilityUnitOfWork) ApplyBlock() (string, [][]byte, error) { } // TODO: @deblasis - this should be from a ReadContext (the ephemeral/staging one) // return the app hash (consensus module will get the validator set directly) - stateHash, err := u.persistenceRWContext.ComputeStateHash() + stateHash, err = u.persistenceRWContext.ComputeStateHash() if err != nil { u.logger.Fatal().Err(err).Msg("Updating the app hash failed. TODO: Look into roll-backing the entire commit...") return "", nil, utilTypes.ErrAppHash(err) diff --git a/utility/unit_of_work/module_test.go b/utility/unit_of_work/module_test.go index bdde64423..689a0d783 100644 --- a/utility/unit_of_work/module_test.go +++ b/utility/unit_of_work/module_test.go @@ -65,7 +65,6 @@ func newTestingUtilityUnitOfWork(t *testing.T, height int64, options ...func(*ba Action: messaging.DebugMessageAction_DEBUG_PERSISTENCE_RESET_TO_GENESIS, Message: nil, })) - //testUtilityMod.GetMempool().Clear() }) uow := &baseUtilityUnitOfWork{ From 62f3622799a6b2b0d322a444247ecc1cf76eca19 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 14 Mar 2023 22:13:43 +0000 Subject: [PATCH 43/59] refactor(persistence): removed NewSavePoint([]byte) from interface --- shared/modules/persistence_module.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/shared/modules/persistence_module.go b/shared/modules/persistence_module.go index 1da8ad40c..d46689c1e 100644 --- a/shared/modules/persistence_module.go +++ b/shared/modules/persistence_module.go @@ -58,7 +58,6 @@ type PersistenceRWContext interface { // PersistenceWriteContext has no use-case independent of `PersistenceRWContext`, but is a useful abstraction type PersistenceWriteContext interface { // Context Operations - NewSavePoint([]byte) error RollbackToSavePoint([]byte) error Release() error @@ -203,3 +202,12 @@ type PersistenceReadContext interface { GetStringFlag(paramName string, height int64) (string, bool, error) GetBytesFlag(paramName string, height int64) ([]byte, bool, error) } + +// SavepointManager is an interface that allows the creation of savepoints. +// +// Savepoints are a way to create an in-memory snapshot of the state of the blockchain at a given height. +// This is useful for the utility module, which needs to be able to query the state of the blockchain at +// a given height, but does not have access to the persistence module. +type SavepointManager interface { + CreateSavepoint(height int64) PersistenceReadContext +} From a8f1306578f7f5cf19ec068bbba541ba2681662d Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 14 Mar 2023 22:16:20 +0000 Subject: [PATCH 44/59] refactor(persistence): removed NewSavePoint from PostgresContext --- persistence/context.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/persistence/context.go b/persistence/context.go index ddd307f8e..483da08f5 100644 --- a/persistence/context.go +++ b/persistence/context.go @@ -30,11 +30,6 @@ type PostgresContext struct { stateTrees *stateTrees } -func (p *PostgresContext) NewSavePoint(bytes []byte) error { - p.logger.Info().Bool("TODO", true).Msg("NewSavePoint not implemented") - return nil -} - // TECHDEBT(#327): Guarantee atomicity betweens `prepareBlock`, `insertBlock` and `storeBlock` for save points & rollbacks. func (p *PostgresContext) RollbackToSavePoint(bytes []byte) error { p.logger.Info().Bool("TODO", true).Msg("RollbackToSavePoint not fully implemented") From 48d071ab7a848384b56f20776341ebdc12c4b31c Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 14 Mar 2023 22:24:12 +0000 Subject: [PATCH 45/59] feat(persistence): savepoints prototype --- persistence/savepoints/savepoint.go | 246 +++++++++++++++++++ persistence/savepoints/savepoints_manager.go | 20 ++ 2 files changed, 266 insertions(+) create mode 100644 persistence/savepoints/savepoint.go create mode 100644 persistence/savepoints/savepoints_manager.go diff --git a/persistence/savepoints/savepoint.go b/persistence/savepoints/savepoint.go new file mode 100644 index 000000000..f22747b09 --- /dev/null +++ b/persistence/savepoints/savepoint.go @@ -0,0 +1,246 @@ +package savepoints + +import ( + coreTypes "github.com/pokt-network/pocket/shared/core/types" + "github.com/pokt-network/pocket/shared/modules" + moduleTypes "github.com/pokt-network/pocket/shared/modules/types" +) + +var _ modules.PersistenceReadContext = &Savepoint{} + +type Savepoint struct{} + +// Close implements modules.PersistenceReadContext +func (*Savepoint) Close() error { + panic("unimplemented") +} + +// GetAccountAmount implements modules.PersistenceReadContext +func (*Savepoint) GetAccountAmount(address []byte, height int64) (string, error) { + panic("unimplemented") +} + +// GetAllAccounts implements modules.PersistenceReadContext +func (*Savepoint) GetAllAccounts(height int64) ([]*coreTypes.Account, error) { + panic("unimplemented") +} + +// GetAllApps implements modules.PersistenceReadContext +func (*Savepoint) GetAllApps(height int64) ([]*coreTypes.Actor, error) { + panic("unimplemented") +} + +// GetAllFishermen implements modules.PersistenceReadContext +func (*Savepoint) GetAllFishermen(height int64) ([]*coreTypes.Actor, error) { + panic("unimplemented") +} + +// GetAllPools implements modules.PersistenceReadContext +func (*Savepoint) GetAllPools(height int64) ([]*coreTypes.Account, error) { + panic("unimplemented") +} + +// GetAllServicers implements modules.PersistenceReadContext +func (*Savepoint) GetAllServicers(height int64) ([]*coreTypes.Actor, error) { + panic("unimplemented") +} + +// GetAllStakedActors implements modules.PersistenceReadContext +func (*Savepoint) GetAllStakedActors(height int64) ([]*coreTypes.Actor, error) { + panic("unimplemented") +} + +// GetAllValidators implements modules.PersistenceReadContext +func (*Savepoint) GetAllValidators(height int64) ([]*coreTypes.Actor, error) { + panic("unimplemented") +} + +// GetAppExists implements modules.PersistenceReadContext +func (*Savepoint) GetAppExists(address []byte, height int64) (exists bool, err error) { + panic("unimplemented") +} + +// GetAppOutputAddress implements modules.PersistenceReadContext +func (*Savepoint) GetAppOutputAddress(operator []byte, height int64) (output []byte, err error) { + panic("unimplemented") +} + +// GetAppPauseHeightIfExists implements modules.PersistenceReadContext +func (*Savepoint) GetAppPauseHeightIfExists(address []byte, height int64) (int64, error) { + panic("unimplemented") +} + +// GetAppStakeAmount implements modules.PersistenceReadContext +func (*Savepoint) GetAppStakeAmount(height int64, address []byte) (string, error) { + panic("unimplemented") +} + +// GetAppStatus implements modules.PersistenceReadContext +func (*Savepoint) GetAppStatus(address []byte, height int64) (status int32, err error) { + panic("unimplemented") +} + +// GetAppsReadyToUnstake implements modules.PersistenceReadContext +func (*Savepoint) GetAppsReadyToUnstake(height int64, status int32) (apps []*moduleTypes.UnstakingActor, err error) { + panic("unimplemented") +} + +// GetBlockHash implements modules.PersistenceReadContext +func (*Savepoint) GetBlockHash(height int64) (string, error) { + panic("unimplemented") +} + +// GetBytesFlag implements modules.PersistenceReadContext +func (*Savepoint) GetBytesFlag(paramName string, height int64) ([]byte, bool, error) { + panic("unimplemented") +} + +// GetBytesParam implements modules.PersistenceReadContext +func (*Savepoint) GetBytesParam(paramName string, height int64) ([]byte, error) { + panic("unimplemented") +} + +// GetFishermanExists implements modules.PersistenceReadContext +func (*Savepoint) GetFishermanExists(address []byte, height int64) (exists bool, err error) { + panic("unimplemented") +} + +// GetFishermanOutputAddress implements modules.PersistenceReadContext +func (*Savepoint) GetFishermanOutputAddress(operator []byte, height int64) (output []byte, err error) { + panic("unimplemented") +} + +// GetFishermanPauseHeightIfExists implements modules.PersistenceReadContext +func (*Savepoint) GetFishermanPauseHeightIfExists(address []byte, height int64) (int64, error) { + panic("unimplemented") +} + +// GetFishermanStakeAmount implements modules.PersistenceReadContext +func (*Savepoint) GetFishermanStakeAmount(height int64, address []byte) (string, error) { + panic("unimplemented") +} + +// GetFishermanStatus implements modules.PersistenceReadContext +func (*Savepoint) GetFishermanStatus(address []byte, height int64) (status int32, err error) { + panic("unimplemented") +} + +// GetFishermenReadyToUnstake implements modules.PersistenceReadContext +func (*Savepoint) GetFishermenReadyToUnstake(height int64, status int32) (fishermen []*moduleTypes.UnstakingActor, err error) { + panic("unimplemented") +} + +// GetHeight implements modules.PersistenceReadContext +func (*Savepoint) GetHeight() (int64, error) { + panic("unimplemented") +} + +// GetIntFlag implements modules.PersistenceReadContext +func (*Savepoint) GetIntFlag(paramName string, height int64) (int, bool, error) { + panic("unimplemented") +} + +// GetIntParam implements modules.PersistenceReadContext +func (*Savepoint) GetIntParam(paramName string, height int64) (int, error) { + panic("unimplemented") +} + +// GetMaximumBlockHeight implements modules.PersistenceReadContext +func (*Savepoint) GetMaximumBlockHeight() (uint64, error) { + panic("unimplemented") +} + +// GetMinimumBlockHeight implements modules.PersistenceReadContext +func (*Savepoint) GetMinimumBlockHeight() (uint64, error) { + panic("unimplemented") +} + +// GetParameter implements modules.PersistenceReadContext +func (*Savepoint) GetParameter(paramName string, height int64) (any, error) { + panic("unimplemented") +} + +// GetPoolAmount implements modules.PersistenceReadContext +func (*Savepoint) GetPoolAmount(name string, height int64) (amount string, err error) { + panic("unimplemented") +} + +// GetServicerCount implements modules.PersistenceReadContext +func (*Savepoint) GetServicerCount(chain string, height int64) (int, error) { + panic("unimplemented") +} + +// GetServicerExists implements modules.PersistenceReadContext +func (*Savepoint) GetServicerExists(address []byte, height int64) (exists bool, err error) { + panic("unimplemented") +} + +// GetServicerOutputAddress implements modules.PersistenceReadContext +func (*Savepoint) GetServicerOutputAddress(operator []byte, height int64) (output []byte, err error) { + panic("unimplemented") +} + +// GetServicerPauseHeightIfExists implements modules.PersistenceReadContext +func (*Savepoint) GetServicerPauseHeightIfExists(address []byte, height int64) (int64, error) { + panic("unimplemented") +} + +// GetServicerStakeAmount implements modules.PersistenceReadContext +func (*Savepoint) GetServicerStakeAmount(height int64, address []byte) (string, error) { + panic("unimplemented") +} + +// GetServicerStatus implements modules.PersistenceReadContext +func (*Savepoint) GetServicerStatus(address []byte, height int64) (status int32, err error) { + panic("unimplemented") +} + +// GetServicersReadyToUnstake implements modules.PersistenceReadContext +func (*Savepoint) GetServicersReadyToUnstake(height int64, status int32) (servicers []*moduleTypes.UnstakingActor, err error) { + panic("unimplemented") +} + +// GetStringFlag implements modules.PersistenceReadContext +func (*Savepoint) GetStringFlag(paramName string, height int64) (string, bool, error) { + panic("unimplemented") +} + +// GetStringParam implements modules.PersistenceReadContext +func (*Savepoint) GetStringParam(paramName string, height int64) (string, error) { + panic("unimplemented") +} + +// GetValidatorExists implements modules.PersistenceReadContext +func (*Savepoint) GetValidatorExists(address []byte, height int64) (exists bool, err error) { + panic("unimplemented") +} + +// GetValidatorMissedBlocks implements modules.PersistenceReadContext +func (*Savepoint) GetValidatorMissedBlocks(address []byte, height int64) (int, error) { + panic("unimplemented") +} + +// GetValidatorOutputAddress implements modules.PersistenceReadContext +func (*Savepoint) GetValidatorOutputAddress(operator []byte, height int64) (output []byte, err error) { + panic("unimplemented") +} + +// GetValidatorPauseHeightIfExists implements modules.PersistenceReadContext +func (*Savepoint) GetValidatorPauseHeightIfExists(address []byte, height int64) (int64, error) { + panic("unimplemented") +} + +// GetValidatorStakeAmount implements modules.PersistenceReadContext +func (*Savepoint) GetValidatorStakeAmount(height int64, address []byte) (string, error) { + panic("unimplemented") +} + +// GetValidatorStatus implements modules.PersistenceReadContext +func (*Savepoint) GetValidatorStatus(address []byte, height int64) (status int32, err error) { + panic("unimplemented") +} + +// GetValidatorsReadyToUnstake implements modules.PersistenceReadContext +func (*Savepoint) GetValidatorsReadyToUnstake(height int64, status int32) (validators []*moduleTypes.UnstakingActor, err error) { + panic("unimplemented") +} diff --git a/persistence/savepoints/savepoints_manager.go b/persistence/savepoints/savepoints_manager.go new file mode 100644 index 000000000..1a66dc3bf --- /dev/null +++ b/persistence/savepoints/savepoints_manager.go @@ -0,0 +1,20 @@ +package savepoints + +import "github.com/pokt-network/pocket/shared/modules" + +var _ modules.SavepointManager = &savepointManager{} + +type savepointManager struct { + readContext modules.PersistenceReadContext +} + +func NewSavepointManager(readContext modules.PersistenceReadContext) modules.SavepointManager { + return &savepointManager{ + readContext: readContext, + } +} + +// CreateSavepoint implements SavepointManager +func (*savepointManager) CreateSavepoint(height int64) modules.PersistenceReadContext { + return &Savepoint{} +} From bd528bb822bebddbbb99ef6d09b9bc8c6bdc4eba Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 14 Mar 2023 23:08:49 +0000 Subject: [PATCH 46/59] feat(persistence): select * in JSON format for all tables --- persistence/types/base_account.go | 4 ++++ persistence/types/base_actor.go | 4 ++++ persistence/types/gov.go | 8 ++++++++ persistence/types/protocol_account.go | 2 ++ persistence/types/protocol_actor.go | 2 ++ persistence/types/shared_sql.go | 11 +++++++++++ 6 files changed, 31 insertions(+) create mode 100644 persistence/types/shared_sql.go diff --git a/persistence/types/base_account.go b/persistence/types/base_account.go index c12a442c7..1a22a7142 100644 --- a/persistence/types/base_account.go +++ b/persistence/types/base_account.go @@ -43,6 +43,10 @@ func (account baseProtocolAccountSchema) GetAllQuery(height int64) string { return SelectAccounts(height, account.accountSpecificColName, account.tableName) } +func (account baseProtocolAccountSchema) GetAllJSONQuery(height int64) string { + return SelectJSON(SelectAccounts(height, account.accountSpecificColName, account.tableName)) +} + func (account baseProtocolAccountSchema) InsertAccountQuery(identifier, amount string, height int64) string { return InsertAccount(account.accountSpecificColName, identifier, amount, height, account.tableName, account.heightConstraintName) } diff --git a/persistence/types/base_actor.go b/persistence/types/base_actor.go index 01bbb6096..d90038d9f 100644 --- a/persistence/types/base_actor.go +++ b/persistence/types/base_actor.go @@ -65,6 +65,10 @@ func (actor *BaseProtocolActorSchema) GetAllQuery(height int64) string { return SelectActors(actor.GetActorSpecificColName(), height, actor.tableName) } +func (actor *BaseProtocolActorSchema) GetAllJSONQuery(height int64) string { + return SelectJSON(SelectActors(actor.GetActorSpecificColName(), height, actor.tableName)) +} + func (actor *BaseProtocolActorSchema) GetExistsQuery(address string, height int64) string { return Exists(address, height, actor.tableName) } diff --git a/persistence/types/gov.go b/persistence/types/gov.go index c728ec3ed..4809253ba 100644 --- a/persistence/types/gov.go +++ b/persistence/types/gov.go @@ -108,6 +108,14 @@ func GetParamOrFlagQuery(tableName, flagName string, height int64) string { return fmt.Sprintf(`SELECT %s FROM %s WHERE name='%s' AND height<=%d ORDER BY height DESC LIMIT 1`, fields, tableName, flagName, height) } +func GetAllParamsOrFlagsJSONQuery(tableName string, height int64) string { + fields := "value" + if tableName == FlagsTableName { + fields += ",enabled" + } + return SelectJSON(fmt.Sprintf(`SELECT %s FROM %s WHERE height<=%d ORDER BY height DESC LIMIT 1`, fields, tableName, height)) +} + // SupportedParamTypes represents the types currently supported for the `value` property in params and flags type SupportedParamTypes interface { int | int32 | int64 | []byte | string diff --git a/persistence/types/protocol_account.go b/persistence/types/protocol_account.go index 623457b65..311579547 100644 --- a/persistence/types/protocol_account.go +++ b/persistence/types/protocol_account.go @@ -16,6 +16,8 @@ type ProtocolAccountSchema interface { // Returns a query to get all accounts GetAllQuery(height int64) string + // Returns a query to get all accounts in JSON format + GetAllJSONQuery(height int64) string // Returns a query to get the balance of an account at a specified height GetAccountAmountQuery(identifier string, height int64) string // Identifier can either be address (cryptographic ID) (Account) or semantic name (Pool) // Returns a query to select all accounts updated at a specified height diff --git a/persistence/types/protocol_actor.go b/persistence/types/protocol_actor.go index f8069eb21..1a39efe49 100644 --- a/persistence/types/protocol_actor.go +++ b/persistence/types/protocol_actor.go @@ -27,6 +27,8 @@ type ProtocolActorSchema interface { GetQuery(address string, height int64) string // Returns all actors at that height GetAllQuery(height int64) string + // Returns all actors at that height in JSON format + GetAllJSONQuery(height int64) string // Returns a query for the existence of an Actor given its address. GetExistsQuery(address string, height int64) string // Returns a query to retrieve data associated with all the apps ready to unstake. diff --git a/persistence/types/shared_sql.go b/persistence/types/shared_sql.go new file mode 100644 index 000000000..48eaf8001 --- /dev/null +++ b/persistence/types/shared_sql.go @@ -0,0 +1,11 @@ +package types + +import "fmt" + +func SelectJSON(selectStatement string) string { + return fmt.Sprintf(` + SELECT json_agg(row_to_json(subquery.*)) + FROM ( + %s + ) AS subquery;`, selectStatement) +} From 528b0dc4ef51a84a045aedda56eca6434aae6941 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 14 Mar 2023 23:42:11 +0000 Subject: [PATCH 47/59] feat(persistence): JSON getters --- shared/modules/persistence_module.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/shared/modules/persistence_module.go b/shared/modules/persistence_module.go index d46689c1e..ffc2108e5 100644 --- a/shared/modules/persistence_module.go +++ b/shared/modules/persistence_module.go @@ -143,15 +143,18 @@ type PersistenceReadContext interface { // Returns "0" if the account does not exist GetPoolAmount(name string, height int64) (amount string, err error) GetAllPools(height int64) ([]*coreTypes.Account, error) + GetAllPoolsJSON(height int64) (string, error) // Account Queries // Returns "0" if the account does not exist GetAccountAmount(address []byte, height int64) (string, error) GetAllAccounts(height int64) ([]*coreTypes.Account, error) + GetAllAccountsJSON(height int64) (string, error) // App Queries GetAllApps(height int64) ([]*coreTypes.Actor, error) + GetAllAppsJSON(height int64) (string, error) GetAppExists(address []byte, height int64) (exists bool, err error) GetAppStakeAmount(height int64, address []byte) (string, error) GetAppsReadyToUnstake(height int64, status int32) (apps []*moduleTypes.UnstakingActor, err error) @@ -161,6 +164,7 @@ type PersistenceReadContext interface { // Servicer Queries GetAllServicers(height int64) ([]*coreTypes.Actor, error) + GetAllServicersJSON(height int64) (string, error) GetServicerExists(address []byte, height int64) (exists bool, err error) GetServicerStakeAmount(height int64, address []byte) (string, error) GetServicersReadyToUnstake(height int64, status int32) (servicers []*moduleTypes.UnstakingActor, err error) @@ -171,6 +175,7 @@ type PersistenceReadContext interface { // Fisherman Queries GetAllFishermen(height int64) ([]*coreTypes.Actor, error) + GetAllFishermenJSON(height int64) (string, error) GetFishermanExists(address []byte, height int64) (exists bool, err error) GetFishermanStakeAmount(height int64, address []byte) (string, error) GetFishermenReadyToUnstake(height int64, status int32) (fishermen []*moduleTypes.UnstakingActor, err error) @@ -180,6 +185,7 @@ type PersistenceReadContext interface { // Validator Queries GetAllValidators(height int64) ([]*coreTypes.Actor, error) + GetAllValidatorsJSON(height int64) (string, error) GetValidatorExists(address []byte, height int64) (exists bool, err error) GetValidatorStakeAmount(height int64, address []byte) (string, error) GetValidatorsReadyToUnstake(height int64, status int32) (validators []*moduleTypes.UnstakingActor, err error) @@ -196,11 +202,13 @@ type PersistenceReadContext interface { GetStringParam(paramName string, height int64) (string, error) GetBytesParam(paramName string, height int64) ([]byte, error) GetParameter(paramName string, height int64) (any, error) + GetAllParamsJSON(height int64) (string, error) // Flags GetIntFlag(paramName string, height int64) (int, bool, error) GetStringFlag(paramName string, height int64) (string, bool, error) GetBytesFlag(paramName string, height int64) ([]byte, bool, error) + GetAllFlagsJSON(height int64) (string, error) } // SavepointManager is an interface that allows the creation of savepoints. From 656a815bd8a8f068eba024fb3fb19ab5e3cf1fb3 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 14 Mar 2023 23:42:44 +0000 Subject: [PATCH 48/59] feat(persistence): JSON getters implementations --- persistence/actor.go | 24 ++++++++++++++++++++++++ persistence/genesis.go | 11 +++++++++++ persistence/gov.go | 14 ++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/persistence/actor.go b/persistence/actor.go index 092314797..3dce4bc38 100644 --- a/persistence/actor.go +++ b/persistence/actor.go @@ -34,6 +34,12 @@ func (p *PostgresContext) GetAllApps(height int64) (apps []*coreTypes.Actor, err return } +func (p *PostgresContext) GetAllAppsJSON(height int64) (json string, err error) { + ctx, tx := p.getCtxAndTx() + err = tx.QueryRow(ctx, types.SelectJSON(types.ApplicationActor.GetAllQuery(height))).Scan(&json) + return +} + func (p *PostgresContext) GetAllValidators(height int64) (vals []*coreTypes.Actor, err error) { ctx, tx := p.getCtxAndTx() rows, err := tx.Query(ctx, types.ValidatorActor.GetAllQuery(height)) @@ -61,6 +67,12 @@ func (p *PostgresContext) GetAllValidators(height int64) (vals []*coreTypes.Acto return } +func (p *PostgresContext) GetAllValidatorsJSON(height int64) (json string, err error) { + ctx, tx := p.getCtxAndTx() + err = tx.QueryRow(ctx, types.SelectJSON(types.ValidatorActor.GetAllQuery(height))).Scan(&json) + return +} + func (p *PostgresContext) GetAllServicers(height int64) (sn []*coreTypes.Actor, err error) { ctx, tx := p.getCtxAndTx() rows, err := tx.Query(ctx, types.ServicerActor.GetAllQuery(height)) @@ -87,6 +99,12 @@ func (p *PostgresContext) GetAllServicers(height int64) (sn []*coreTypes.Actor, return } +func (p *PostgresContext) GetAllServicersJSON(height int64) (json string, err error) { + ctx, tx := p.getCtxAndTx() + err = tx.QueryRow(ctx, types.SelectJSON(types.ServicerActor.GetAllQuery(height))).Scan(&json) + return +} + func (p *PostgresContext) GetAllFishermen(height int64) (f []*coreTypes.Actor, err error) { ctx, tx := p.getCtxAndTx() rows, err := tx.Query(ctx, types.FishermanActor.GetAllQuery(height)) @@ -113,6 +131,12 @@ func (p *PostgresContext) GetAllFishermen(height int64) (f []*coreTypes.Actor, e return } +func (p *PostgresContext) GetAllFishermenJSON(height int64) (json string, err error) { + ctx, tx := p.getCtxAndTx() + err = tx.QueryRow(ctx, types.SelectJSON(types.FishermanActor.GetAllQuery(height))).Scan(&json) + return +} + // IMPROVE: This is a proof of concept. Ideally we should have a single query that returns all actors. func (p *PostgresContext) GetAllStakedActors(height int64) (allActors []*coreTypes.Actor, err error) { type actorGetter func(height int64) ([]*coreTypes.Actor, error) diff --git a/persistence/genesis.go b/persistence/genesis.go index cd4e2d588..46e3a31b9 100644 --- a/persistence/genesis.go +++ b/persistence/genesis.go @@ -151,6 +151,11 @@ func (p *PostgresContext) GetAllAccounts(height int64) (accs []*coreTypes.Accoun } return } +func (p *PostgresContext) GetAllAccountsJSON(height int64) (json string, err error) { + ctx, tx := p.getCtxAndTx() + err = tx.QueryRow(ctx, types.SelectJSON(types.Account.GetAllQuery(height))).Scan(&json) + return +} // CLEANUP: Consolidate with GetAllAccounts. func (p *PostgresContext) GetAllPools(height int64) (accs []*coreTypes.Account, err error) { @@ -168,3 +173,9 @@ func (p *PostgresContext) GetAllPools(height int64) (accs []*coreTypes.Account, } return } + +func (p *PostgresContext) GetAllPoolsJSON(height int64) (json string, err error) { + ctx, tx := p.getCtxAndTx() + err = tx.QueryRow(ctx, types.SelectJSON(types.Pool.GetAllQuery(height))).Scan(&json) + return +} diff --git a/persistence/gov.go b/persistence/gov.go index 80f05e936..4c773d6a2 100644 --- a/persistence/gov.go +++ b/persistence/gov.go @@ -80,6 +80,13 @@ func (p *PostgresContext) GetBytesParam(paramName string, height int64) (param [ return v, err } +func (p *PostgresContext) GetAllParamsJSON(height int64) (string, error) { + ctx, tx := p.getCtxAndTx() + var paramsJSON string + err := tx.QueryRow(ctx, types.GetAllParamsOrFlagsJSONQuery(types.ParamsTableName, height)).Scan(¶msJSON) + return paramsJSON, err +} + func (p *PostgresContext) SetParam(paramName string, value any) error { return p.setParamOrFlag(paramName, value, nil) } @@ -101,6 +108,13 @@ func (p *PostgresContext) GetBytesFlag(flagName string, height int64) (value []b return getParamOrFlag[[]byte](p, types.FlagsTableName, flagName, height) } +func (p *PostgresContext) GetAllFlagsJSON(height int64) (string, error) { + ctx, tx := p.getCtxAndTx() + var paramsJSON string + err := tx.QueryRow(ctx, types.GetAllParamsOrFlagsJSONQuery(types.FlagsTableName, height)).Scan(¶msJSON) + return paramsJSON, err +} + func (p *PostgresContext) SetFlag(flagName string, value any, enabled bool) error { return p.setParamOrFlag(flagName, value, &enabled) } From 81f08beabffe0b6b5661e0f19259f45910de0bb0 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 16 Mar 2023 21:26:21 +0000 Subject: [PATCH 49/59] feat(persistence): savepoint WIP --- persistence/savepoints/savepoint.go | 220 ++++++++++++++----- persistence/savepoints/savepoints_factory.go | 107 +++++++++ persistence/savepoints/savepoints_manager.go | 20 -- 3 files changed, 272 insertions(+), 75 deletions(-) create mode 100644 persistence/savepoints/savepoints_factory.go delete mode 100644 persistence/savepoints/savepoints_manager.go diff --git a/persistence/savepoints/savepoint.go b/persistence/savepoints/savepoint.go index f22747b09..797cd5dcf 100644 --- a/persistence/savepoints/savepoint.go +++ b/persistence/savepoints/savepoint.go @@ -1,246 +1,356 @@ package savepoints import ( + "encoding/hex" + "encoding/json" + "log" + + "github.com/itchyny/gojq" coreTypes "github.com/pokt-network/pocket/shared/core/types" "github.com/pokt-network/pocket/shared/modules" moduleTypes "github.com/pokt-network/pocket/shared/modules/types" ) -var _ modules.PersistenceReadContext = &Savepoint{} +var _ modules.PersistenceReadContext = &savepoint{} -type Savepoint struct{} +type savepoint struct { + appsJson string + validatorsJson string + fishermenJson string + servicersJson string + accountsJson string + poolsJson string + paramsJson string + flagsJson string -// Close implements modules.PersistenceReadContext -func (*Savepoint) Close() error { - panic("unimplemented") + height int64 + + //TODO: @deblasis - add chains } -// GetAccountAmount implements modules.PersistenceReadContext -func (*Savepoint) GetAccountAmount(address []byte, height int64) (string, error) { - panic("unimplemented") +func (s *savepoint) GetAllAccountsJSON(height int64) (string, error) { + return s.accountsJson, nil +} + +func (s *savepoint) GetAllAppsJSON(height int64) (string, error) { + return s.appsJson, nil } -// GetAllAccounts implements modules.PersistenceReadContext -func (*Savepoint) GetAllAccounts(height int64) ([]*coreTypes.Account, error) { - panic("unimplemented") +func (s *savepoint) GetAllFishermenJSON(height int64) (string, error) { + return s.fishermenJson, nil +} + +func (s *savepoint) GetAllFlagsJSON(height int64) (string, error) { + return s.flagsJson, nil +} + +func (s *savepoint) GetAllParamsJSON(height int64) (string, error) { + return s.paramsJson, nil +} + +func (s *savepoint) GetAllPoolsJSON(height int64) (string, error) { + return s.poolsJson, nil +} + +func (s *savepoint) GetAllServicersJSON(height int64) (string, error) { + return s.servicersJson, nil +} + +func (s *savepoint) GetAllValidatorsJSON(height int64) (string, error) { + return s.validatorsJson, nil +} + +func (s *savepoint) Close() error { + // no-op + return nil +} + +func (s *savepoint) GetAccountAmount(address []byte, height int64) (string, error) { + // Parse JSON data into an interface{} + var data interface{} + if err := json.Unmarshal([]byte(s.accountsJson), &data); err != nil { + log.Fatalf("Error unmarshalling JSON: %v", err) + } + + query, err := gojq.Parse(".[] | select(.address == $address) | .balance") + if err != nil { + return "", err + } + code, err := gojq.Compile( + query, + gojq.WithVariables([]string{ + "$address", + }), + ) + if err != nil { + return "", err + } + var balance string + iter := code.Run(data, any(hex.EncodeToString(address))) + for { + v, ok := iter.Next() + if !ok { + break + } + if err, ok := v.(error); ok { + log.Fatalln(err) + } + balance = v.(string) + break + } + return balance, nil +} + +func (s *savepoint) GetAllAccounts(height int64) (accs []*coreTypes.Account, err error) { + // Parse JSON data into an interface{} + var data interface{} + if err := json.Unmarshal([]byte(s.accountsJson), &data); err != nil { + log.Fatalf("Error unmarshalling JSON: %v", err) + } + + query, err := gojq.Parse(".[]") + if err != nil { + return + } + code, err := gojq.Compile(query) + if err != nil { + return + } + iter := code.Run(data) + for { + v, ok := iter.Next() + if !ok { + break + } + if err, ok := v.(error); ok { + log.Fatalln(err) + } + + row := v.(map[string]any) + acc := new(coreTypes.Account) + acc.Address = row["address"].(string) + acc.Amount = row["balance"].(string) + accs = append(accs, acc) + } + return } // GetAllApps implements modules.PersistenceReadContext -func (*Savepoint) GetAllApps(height int64) ([]*coreTypes.Actor, error) { +func (*savepoint) GetAllApps(height int64) ([]*coreTypes.Actor, error) { panic("unimplemented") } // GetAllFishermen implements modules.PersistenceReadContext -func (*Savepoint) GetAllFishermen(height int64) ([]*coreTypes.Actor, error) { +func (*savepoint) GetAllFishermen(height int64) ([]*coreTypes.Actor, error) { panic("unimplemented") } // GetAllPools implements modules.PersistenceReadContext -func (*Savepoint) GetAllPools(height int64) ([]*coreTypes.Account, error) { +func (*savepoint) GetAllPools(height int64) ([]*coreTypes.Account, error) { panic("unimplemented") } // GetAllServicers implements modules.PersistenceReadContext -func (*Savepoint) GetAllServicers(height int64) ([]*coreTypes.Actor, error) { +func (*savepoint) GetAllServicers(height int64) ([]*coreTypes.Actor, error) { panic("unimplemented") } // GetAllStakedActors implements modules.PersistenceReadContext -func (*Savepoint) GetAllStakedActors(height int64) ([]*coreTypes.Actor, error) { +func (*savepoint) GetAllStakedActors(height int64) ([]*coreTypes.Actor, error) { panic("unimplemented") } // GetAllValidators implements modules.PersistenceReadContext -func (*Savepoint) GetAllValidators(height int64) ([]*coreTypes.Actor, error) { +func (*savepoint) GetAllValidators(height int64) ([]*coreTypes.Actor, error) { panic("unimplemented") } // GetAppExists implements modules.PersistenceReadContext -func (*Savepoint) GetAppExists(address []byte, height int64) (exists bool, err error) { +func (*savepoint) GetAppExists(address []byte, height int64) (exists bool, err error) { panic("unimplemented") } // GetAppOutputAddress implements modules.PersistenceReadContext -func (*Savepoint) GetAppOutputAddress(operator []byte, height int64) (output []byte, err error) { +func (*savepoint) GetAppOutputAddress(operator []byte, height int64) (output []byte, err error) { panic("unimplemented") } // GetAppPauseHeightIfExists implements modules.PersistenceReadContext -func (*Savepoint) GetAppPauseHeightIfExists(address []byte, height int64) (int64, error) { +func (*savepoint) GetAppPauseHeightIfExists(address []byte, height int64) (int64, error) { panic("unimplemented") } // GetAppStakeAmount implements modules.PersistenceReadContext -func (*Savepoint) GetAppStakeAmount(height int64, address []byte) (string, error) { +func (*savepoint) GetAppStakeAmount(height int64, address []byte) (string, error) { panic("unimplemented") } // GetAppStatus implements modules.PersistenceReadContext -func (*Savepoint) GetAppStatus(address []byte, height int64) (status int32, err error) { +func (*savepoint) GetAppStatus(address []byte, height int64) (status int32, err error) { panic("unimplemented") } // GetAppsReadyToUnstake implements modules.PersistenceReadContext -func (*Savepoint) GetAppsReadyToUnstake(height int64, status int32) (apps []*moduleTypes.UnstakingActor, err error) { +func (*savepoint) GetAppsReadyToUnstake(height int64, status int32) (apps []*moduleTypes.UnstakingActor, err error) { panic("unimplemented") } // GetBlockHash implements modules.PersistenceReadContext -func (*Savepoint) GetBlockHash(height int64) (string, error) { +func (*savepoint) GetBlockHash(height int64) (string, error) { panic("unimplemented") } // GetBytesFlag implements modules.PersistenceReadContext -func (*Savepoint) GetBytesFlag(paramName string, height int64) ([]byte, bool, error) { +func (*savepoint) GetBytesFlag(paramName string, height int64) ([]byte, bool, error) { panic("unimplemented") } // GetBytesParam implements modules.PersistenceReadContext -func (*Savepoint) GetBytesParam(paramName string, height int64) ([]byte, error) { +func (*savepoint) GetBytesParam(paramName string, height int64) ([]byte, error) { panic("unimplemented") } // GetFishermanExists implements modules.PersistenceReadContext -func (*Savepoint) GetFishermanExists(address []byte, height int64) (exists bool, err error) { +func (*savepoint) GetFishermanExists(address []byte, height int64) (exists bool, err error) { panic("unimplemented") } // GetFishermanOutputAddress implements modules.PersistenceReadContext -func (*Savepoint) GetFishermanOutputAddress(operator []byte, height int64) (output []byte, err error) { +func (*savepoint) GetFishermanOutputAddress(operator []byte, height int64) (output []byte, err error) { panic("unimplemented") } // GetFishermanPauseHeightIfExists implements modules.PersistenceReadContext -func (*Savepoint) GetFishermanPauseHeightIfExists(address []byte, height int64) (int64, error) { +func (*savepoint) GetFishermanPauseHeightIfExists(address []byte, height int64) (int64, error) { panic("unimplemented") } // GetFishermanStakeAmount implements modules.PersistenceReadContext -func (*Savepoint) GetFishermanStakeAmount(height int64, address []byte) (string, error) { +func (*savepoint) GetFishermanStakeAmount(height int64, address []byte) (string, error) { panic("unimplemented") } // GetFishermanStatus implements modules.PersistenceReadContext -func (*Savepoint) GetFishermanStatus(address []byte, height int64) (status int32, err error) { +func (*savepoint) GetFishermanStatus(address []byte, height int64) (status int32, err error) { panic("unimplemented") } // GetFishermenReadyToUnstake implements modules.PersistenceReadContext -func (*Savepoint) GetFishermenReadyToUnstake(height int64, status int32) (fishermen []*moduleTypes.UnstakingActor, err error) { +func (*savepoint) GetFishermenReadyToUnstake(height int64, status int32) (fishermen []*moduleTypes.UnstakingActor, err error) { panic("unimplemented") } // GetHeight implements modules.PersistenceReadContext -func (*Savepoint) GetHeight() (int64, error) { +func (*savepoint) GetHeight() (int64, error) { panic("unimplemented") } // GetIntFlag implements modules.PersistenceReadContext -func (*Savepoint) GetIntFlag(paramName string, height int64) (int, bool, error) { +func (*savepoint) GetIntFlag(paramName string, height int64) (int, bool, error) { panic("unimplemented") } // GetIntParam implements modules.PersistenceReadContext -func (*Savepoint) GetIntParam(paramName string, height int64) (int, error) { +func (*savepoint) GetIntParam(paramName string, height int64) (int, error) { panic("unimplemented") } // GetMaximumBlockHeight implements modules.PersistenceReadContext -func (*Savepoint) GetMaximumBlockHeight() (uint64, error) { +func (*savepoint) GetMaximumBlockHeight() (uint64, error) { panic("unimplemented") } // GetMinimumBlockHeight implements modules.PersistenceReadContext -func (*Savepoint) GetMinimumBlockHeight() (uint64, error) { +func (*savepoint) GetMinimumBlockHeight() (uint64, error) { panic("unimplemented") } // GetParameter implements modules.PersistenceReadContext -func (*Savepoint) GetParameter(paramName string, height int64) (any, error) { +func (*savepoint) GetParameter(paramName string, height int64) (any, error) { panic("unimplemented") } // GetPoolAmount implements modules.PersistenceReadContext -func (*Savepoint) GetPoolAmount(name string, height int64) (amount string, err error) { +func (*savepoint) GetPoolAmount(name string, height int64) (amount string, err error) { panic("unimplemented") } // GetServicerCount implements modules.PersistenceReadContext -func (*Savepoint) GetServicerCount(chain string, height int64) (int, error) { +func (*savepoint) GetServicerCount(chain string, height int64) (int, error) { panic("unimplemented") } // GetServicerExists implements modules.PersistenceReadContext -func (*Savepoint) GetServicerExists(address []byte, height int64) (exists bool, err error) { +func (*savepoint) GetServicerExists(address []byte, height int64) (exists bool, err error) { panic("unimplemented") } // GetServicerOutputAddress implements modules.PersistenceReadContext -func (*Savepoint) GetServicerOutputAddress(operator []byte, height int64) (output []byte, err error) { +func (*savepoint) GetServicerOutputAddress(operator []byte, height int64) (output []byte, err error) { panic("unimplemented") } // GetServicerPauseHeightIfExists implements modules.PersistenceReadContext -func (*Savepoint) GetServicerPauseHeightIfExists(address []byte, height int64) (int64, error) { +func (*savepoint) GetServicerPauseHeightIfExists(address []byte, height int64) (int64, error) { panic("unimplemented") } // GetServicerStakeAmount implements modules.PersistenceReadContext -func (*Savepoint) GetServicerStakeAmount(height int64, address []byte) (string, error) { +func (*savepoint) GetServicerStakeAmount(height int64, address []byte) (string, error) { panic("unimplemented") } // GetServicerStatus implements modules.PersistenceReadContext -func (*Savepoint) GetServicerStatus(address []byte, height int64) (status int32, err error) { +func (*savepoint) GetServicerStatus(address []byte, height int64) (status int32, err error) { panic("unimplemented") } // GetServicersReadyToUnstake implements modules.PersistenceReadContext -func (*Savepoint) GetServicersReadyToUnstake(height int64, status int32) (servicers []*moduleTypes.UnstakingActor, err error) { +func (*savepoint) GetServicersReadyToUnstake(height int64, status int32) (servicers []*moduleTypes.UnstakingActor, err error) { panic("unimplemented") } // GetStringFlag implements modules.PersistenceReadContext -func (*Savepoint) GetStringFlag(paramName string, height int64) (string, bool, error) { +func (*savepoint) GetStringFlag(paramName string, height int64) (string, bool, error) { panic("unimplemented") } // GetStringParam implements modules.PersistenceReadContext -func (*Savepoint) GetStringParam(paramName string, height int64) (string, error) { +func (*savepoint) GetStringParam(paramName string, height int64) (string, error) { panic("unimplemented") } // GetValidatorExists implements modules.PersistenceReadContext -func (*Savepoint) GetValidatorExists(address []byte, height int64) (exists bool, err error) { +func (*savepoint) GetValidatorExists(address []byte, height int64) (exists bool, err error) { panic("unimplemented") } // GetValidatorMissedBlocks implements modules.PersistenceReadContext -func (*Savepoint) GetValidatorMissedBlocks(address []byte, height int64) (int, error) { +func (*savepoint) GetValidatorMissedBlocks(address []byte, height int64) (int, error) { panic("unimplemented") } // GetValidatorOutputAddress implements modules.PersistenceReadContext -func (*Savepoint) GetValidatorOutputAddress(operator []byte, height int64) (output []byte, err error) { +func (*savepoint) GetValidatorOutputAddress(operator []byte, height int64) (output []byte, err error) { panic("unimplemented") } // GetValidatorPauseHeightIfExists implements modules.PersistenceReadContext -func (*Savepoint) GetValidatorPauseHeightIfExists(address []byte, height int64) (int64, error) { +func (*savepoint) GetValidatorPauseHeightIfExists(address []byte, height int64) (int64, error) { panic("unimplemented") } // GetValidatorStakeAmount implements modules.PersistenceReadContext -func (*Savepoint) GetValidatorStakeAmount(height int64, address []byte) (string, error) { +func (*savepoint) GetValidatorStakeAmount(height int64, address []byte) (string, error) { panic("unimplemented") } // GetValidatorStatus implements modules.PersistenceReadContext -func (*Savepoint) GetValidatorStatus(address []byte, height int64) (status int32, err error) { +func (*savepoint) GetValidatorStatus(address []byte, height int64) (status int32, err error) { panic("unimplemented") } // GetValidatorsReadyToUnstake implements modules.PersistenceReadContext -func (*Savepoint) GetValidatorsReadyToUnstake(height int64, status int32) (validators []*moduleTypes.UnstakingActor, err error) { +func (*savepoint) GetValidatorsReadyToUnstake(height int64, status int32) (validators []*moduleTypes.UnstakingActor, err error) { panic("unimplemented") } diff --git a/persistence/savepoints/savepoints_factory.go b/persistence/savepoints/savepoints_factory.go new file mode 100644 index 000000000..20949ab84 --- /dev/null +++ b/persistence/savepoints/savepoints_factory.go @@ -0,0 +1,107 @@ +package savepoints + +import ( + "sync" + + "github.com/pokt-network/pocket/logger" + "github.com/pokt-network/pocket/shared/modules" +) + +const ( + moduleName = "savepoint_factory" + + appsKey = "apps" + validatorsKey = "validators" + fishermenKey = "fishermen" + servicersKey = "servicers" + accountsKey = "accounts" + poolsKey = "pools" + paramsKey = "params" + flagsKey = "flags" +) + +var _ modules.SavepointFactory = &savepointFactory{} + +type savepointFactory struct { + readContext modules.PersistenceReadContext + logger *modules.Logger +} + +func NewSavepointFactory(readContext modules.PersistenceReadContext) modules.SavepointFactory { + return &savepointFactory{ + readContext: readContext, + logger: logger.Global.CreateLoggerForModule(moduleName), + } +} + +func (sm *savepointFactory) CreateSavepoint(height int64) (modules.PersistenceReadContext, error) { + getters := []struct { + name string + getterFunc func(height int64) (string, error) + }{ + {appsKey, sm.readContext.GetAllAppsJSON}, + {validatorsKey, sm.readContext.GetAllValidatorsJSON}, + {fishermenKey, sm.readContext.GetAllFishermenJSON}, + {servicersKey, sm.readContext.GetAllServicersJSON}, + {accountsKey, sm.readContext.GetAllAccountsJSON}, + {poolsKey, sm.readContext.GetAllPoolsJSON}, + {paramsKey, sm.readContext.GetAllParamsJSON}, + {flagsKey, sm.readContext.GetAllFlagsJSON}, + } + + var mutex sync.Mutex + var wg sync.WaitGroup + wg.Add(len(getters)) + errChan := make(chan error, 1) + cancelChan := make(chan struct{}) + + dataDumps := make(map[string]string, len(getters)) + + for _, getter := range getters { + // INVESTIGATE: the code below is structured so that it could run in parallel just by adding `go` to start goroutines below but for some reason the pg.tx is nil + // when the goroutines are started. This is a temporary fix to get the code working but it should be investigated further. + func(getter struct { + name string + getterFunc func(height int64) (string, error) + }) { + defer wg.Done() + + select { + case <-cancelChan: + // exit the goroutine if cancellation is signaled + return + default: + // continue execution if no cancellation signal is received + } + + data, err := getter.getterFunc(height) + if err != nil { + select { + case errChan <- err: + sm.logger.Error().Err(err).Str("getter_name", getter.name).Msg("error getting data for savepoint") + // signal cancellation to all goroutines + close(cancelChan) + default: + } + return + } + + mutex.Lock() + dataDumps[getter.name] = data + mutex.Unlock() + }(getter) + } + + return &savepoint{ + height: height, + + appsJson: dataDumps[appsKey], + validatorsJson: dataDumps[validatorsKey], + fishermenJson: dataDumps[fishermenKey], + servicersJson: dataDumps[servicersKey], + accountsJson: dataDumps[accountsKey], + poolsJson: dataDumps[poolsKey], + paramsJson: dataDumps[paramsKey], + flagsJson: dataDumps[flagsKey], + }, nil +} diff --git a/persistence/savepoints/savepoints_manager.go b/persistence/savepoints/savepoints_manager.go deleted file mode 100644 index 1a66dc3bf..000000000 --- a/persistence/savepoints/savepoints_manager.go +++ /dev/null @@ -1,20 +0,0 @@ -package savepoints - -import "github.com/pokt-network/pocket/shared/modules" - -var _ modules.SavepointManager = &savepointManager{} - -type savepointManager struct { - readContext modules.PersistenceReadContext -} - -func NewSavepointManager(readContext modules.PersistenceReadContext) modules.SavepointManager { - return &savepointManager{ - readContext: readContext, - } -} - -// CreateSavepoint implements SavepointManager -func (*savepointManager) CreateSavepoint(height int64) modules.PersistenceReadContext { - return &Savepoint{} -} From b9c5e6e878bf7cefdb05e27f3376e16229c83b75 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 16 Mar 2023 21:26:49 +0000 Subject: [PATCH 50/59] feat(persistence): savepointFactory WIP --- shared/modules/persistence_module.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shared/modules/persistence_module.go b/shared/modules/persistence_module.go index ffc2108e5..475870c05 100644 --- a/shared/modules/persistence_module.go +++ b/shared/modules/persistence_module.go @@ -211,11 +211,11 @@ type PersistenceReadContext interface { GetAllFlagsJSON(height int64) (string, error) } -// SavepointManager is an interface that allows the creation of savepoints. +// SavepointFactory is an interface that allows the creation of savepoints. // // Savepoints are a way to create an in-memory snapshot of the state of the blockchain at a given height. // This is useful for the utility module, which needs to be able to query the state of the blockchain at // a given height, but does not have access to the persistence module. -type SavepointManager interface { - CreateSavepoint(height int64) PersistenceReadContext +type SavepointFactory interface { + CreateSavepoint(height int64) (PersistenceReadContext, error) } From eef51b09b253ce3ee66881829947c3daf7e1ecb5 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 16 Mar 2023 21:27:46 +0000 Subject: [PATCH 51/59] feat(persistence): POC test --- persistence/test/savepoints_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 persistence/test/savepoints_test.go diff --git a/persistence/test/savepoints_test.go b/persistence/test/savepoints_test.go new file mode 100644 index 000000000..de5a3295e --- /dev/null +++ b/persistence/test/savepoints_test.go @@ -0,0 +1,28 @@ +package test + +import ( + "encoding/hex" + "testing" + + "github.com/pokt-network/pocket/persistence/savepoints" + "github.com/stretchr/testify/require" +) + +func TestSavepoint_GetAccountAmount(t *testing.T) { + db := NewTestPostgresContext(t, 0) + + sF := savepoints.NewSavepointFactory(db) + savepoint, err := sF.CreateSavepoint(0) + require.NoError(t, err) + + accounts, err := savepoint.GetAllAccounts(0) + require.NoError(t, err) + require.Equal(t, 8, len(accounts)) + + addrBz, err := hex.DecodeString(accounts[0].Address) + require.NoError(t, err) + + accountAmount, err := savepoint.GetAccountAmount(addrBz, 0) + require.NoError(t, err) + require.Equal(t, accounts[0].Amount, accountAmount) +} From 7aa75ac9ce84c5c3497c350697a98fe57ee221ba Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 16 Mar 2023 21:31:34 +0000 Subject: [PATCH 52/59] chore(shared): go mod tidy --- go.mod | 6 ++++-- go.sum | 11 ++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 9a5e79937..c016bc956 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/deepmap/oapi-codegen v1.12.4 github.com/dgraph-io/badger/v3 v3.2103.2 github.com/getkin/kin-openapi v0.107.0 + github.com/itchyny/gojq v0.12.12 github.com/jackc/pgconn v1.13.0 github.com/jordanorelli/lexnum v0.0.0-20141216151731-460eeb125754 github.com/labstack/echo/v4 v4.9.1 @@ -99,6 +100,7 @@ require ( github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/ipfs/go-cid v0.3.2 // indirect github.com/ipfs/go-log/v2 v2.5.1 // indirect + github.com/itchyny/timefmt-go v0.1.5 // indirect github.com/jackc/puddle/v2 v2.1.2 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect @@ -192,7 +194,7 @@ require ( github.com/magiconair/properties v1.8.6 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.16 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect github.com/pelletier/go-toml v1.9.5 // indirect github.com/pelletier/go-toml/v2 v2.0.5 // indirect @@ -205,7 +207,7 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect golang.org/x/mod v0.7.0 // indirect - golang.org/x/sys v0.3.0 // indirect + golang.org/x/sys v0.5.0 // indirect golang.org/x/text v0.5.0 // indirect golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect golang.org/x/tools v0.3.0 // indirect diff --git a/go.sum b/go.sum index b11f654a0..90ce3760e 100644 --- a/go.sum +++ b/go.sum @@ -356,6 +356,10 @@ github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps= github.com/ipfs/go-log/v2 v2.5.1 h1:1XdUzF7048prq4aBjDQQ4SL5RxftpRGdXhNRwKSAlcY= github.com/ipfs/go-log/v2 v2.5.1/go.mod h1:prSpmC1Gpllc9UYWxDiZDreBYw7zp4Iqp1kOLU9U5UI= +github.com/itchyny/gojq v0.12.12 h1:x+xGI9BXqKoJQZkr95ibpe3cdrTbY8D9lonrK433rcA= +github.com/itchyny/gojq v0.12.12/go.mod h1:j+3sVkjxwd7A7Z5jrbKibgOLn0ZfLWkV+Awxr/pyzJE= +github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE= +github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8= github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= @@ -509,8 +513,9 @@ github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= -github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= @@ -1016,8 +1021,8 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= -golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= From 023c42612b585c45c863192e6a452da637869084 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 2 Apr 2023 06:06:32 +0100 Subject: [PATCH 53/59] refactor(persistence): savepoint Release() --- persistence/savepoints/savepoint.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/persistence/savepoints/savepoint.go b/persistence/savepoints/savepoint.go index 797cd5dcf..08135b9c4 100644 --- a/persistence/savepoints/savepoint.go +++ b/persistence/savepoints/savepoint.go @@ -354,3 +354,7 @@ func (*savepoint) GetValidatorStatus(address []byte, height int64) (status int32 func (*savepoint) GetValidatorsReadyToUnstake(height int64, status int32) (validators []*moduleTypes.UnstakingActor, err error) { panic("unimplemented") } + +func (*savepoint) Release() { + // no-op +} From ac167a7eb2de23ab76c1ea309c4004bc6ec08750 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 2 Apr 2023 06:12:11 +0100 Subject: [PATCH 54/59] chore(shared): TODOes --- persistence/savepoints/savepoint.go | 2 +- shared/modules/utility_module.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/persistence/savepoints/savepoint.go b/persistence/savepoints/savepoint.go index 08135b9c4..3425bc0dd 100644 --- a/persistence/savepoints/savepoint.go +++ b/persistence/savepoints/savepoint.go @@ -25,7 +25,7 @@ type savepoint struct { height int64 - //TODO: @deblasis - add chains + //TODO(@deblasis): add chains } func (s *savepoint) GetAllAccountsJSON(height int64) (string, error) { diff --git a/shared/modules/utility_module.go b/shared/modules/utility_module.go index 7edc6f48d..5c83073d9 100644 --- a/shared/modules/utility_module.go +++ b/shared/modules/utility_module.go @@ -53,7 +53,7 @@ type UtilityUnitOfWork interface { // It does not apply, validate or commit the changes. // For example, it can be use during state sync to set a proposed state transition before validation. // TODO: Investigate a way to potentially simplify the interface by removing this function. - // TODO: @deblasis: there's still some mix and match between blockHash and stateHash + // TODO(@deblasis): there's still some mix and match between blockHash and stateHash SetProposalBlock(blockHash string, proposerAddr []byte, txs [][]byte) error // ApplyBlock applies the context's in-memory proposed state (i.e. the txs in this context). From 52063d94afe8ac6861ca1ebb1d182746fedb7a9e Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Sun, 2 Apr 2023 16:55:07 +0100 Subject: [PATCH 55/59] refactor(persistence): improved savepoint --- persistence/savepoints/savepoint.go | 44 +++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/persistence/savepoints/savepoint.go b/persistence/savepoints/savepoint.go index 3425bc0dd..4793bc58a 100644 --- a/persistence/savepoints/savepoint.go +++ b/persistence/savepoints/savepoint.go @@ -3,15 +3,19 @@ package savepoints import ( "encoding/hex" "encoding/json" - "log" + "fmt" "github.com/itchyny/gojq" + "github.com/pokt-network/pocket/logger" coreTypes "github.com/pokt-network/pocket/shared/core/types" "github.com/pokt-network/pocket/shared/modules" moduleTypes "github.com/pokt-network/pocket/shared/modules/types" ) -var _ modules.PersistenceReadContext = &savepoint{} +var ( + _ modules.PersistenceReadContext = &savepoint{} + log = logger.Global.CreateLoggerForModule("savepoint") +) type savepoint struct { appsJson string @@ -25,7 +29,7 @@ type savepoint struct { height int64 - //TODO(@deblasis): add chains + // TODO(@deblasis): add chains } func (s *savepoint) GetAllAccountsJSON(height int64) (string, error) { @@ -66,10 +70,21 @@ func (s *savepoint) Close() error { } func (s *savepoint) GetAccountAmount(address []byte, height int64) (string, error) { + log := log.With().Fields(map[string]interface{}{ + "address": hex.EncodeToString(address), + "height": height, + "source": "GetAccountAmount", + }).Logger() + // Parse JSON data into an interface{} var data interface{} + if s.accountsJson == "" { + return "", fmt.Errorf("no accounts found in savepoint with address %s", hex.EncodeToString(address)) + } if err := json.Unmarshal([]byte(s.accountsJson), &data); err != nil { - log.Fatalf("Error unmarshalling JSON: %v", err) + log.Fatal(). + Err(err). + Msg("Error unmarshalling JSON") } query, err := gojq.Parse(".[] | select(.address == $address) | .balance") @@ -93,19 +108,30 @@ func (s *savepoint) GetAccountAmount(address []byte, height int64) (string, erro break } if err, ok := v.(error); ok { - log.Fatalln(err) + log.Fatal(). + Err(err). + Msg("Error while rehydrating account") } balance = v.(string) - break } return balance, nil } func (s *savepoint) GetAllAccounts(height int64) (accs []*coreTypes.Account, err error) { + log := log.With().Fields(map[string]interface{}{ + "height": height, + "source": "GetAllAccounts", + }).Logger() + // Parse JSON data into an interface{} var data interface{} + if s.accountsJson == "" { + return + } if err := json.Unmarshal([]byte(s.accountsJson), &data); err != nil { - log.Fatalf("Error unmarshalling JSON: %v", err) + log.Fatal(). + Err(err). + Msg("Error unmarshalling JSON") } query, err := gojq.Parse(".[]") @@ -123,7 +149,9 @@ func (s *savepoint) GetAllAccounts(height int64) (accs []*coreTypes.Account, err break } if err, ok := v.(error); ok { - log.Fatalln(err) + log.Fatal(). + Err(err). + Msg("Error while rehydrating account") } row := v.(map[string]any) From 7ce96f9b7c3534fc199cc122cca3a0188432bf31 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 3 Apr 2023 05:36:19 +0100 Subject: [PATCH 56/59] fix(persistence): handling "no results" in JSON db queries --- persistence/actor.go | 14 ++++++++++++++ persistence/db.go | 3 +++ persistence/genesis.go | 7 +++++++ persistence/gov.go | 6 ++++++ 4 files changed, 30 insertions(+) diff --git a/persistence/actor.go b/persistence/actor.go index 3dce4bc38..ce8a884e8 100644 --- a/persistence/actor.go +++ b/persistence/actor.go @@ -1,6 +1,8 @@ package persistence import ( + "strings" + "github.com/pokt-network/pocket/persistence/types" coreTypes "github.com/pokt-network/pocket/shared/core/types" ) @@ -37,6 +39,9 @@ func (p *PostgresContext) GetAllApps(height int64) (apps []*coreTypes.Actor, err func (p *PostgresContext) GetAllAppsJSON(height int64) (json string, err error) { ctx, tx := p.getCtxAndTx() err = tx.QueryRow(ctx, types.SelectJSON(types.ApplicationActor.GetAllQuery(height))).Scan(&json) + if err != nil && strings.Contains(err.Error(), errCannotScanNULL) { + err = nil + } return } @@ -70,6 +75,9 @@ func (p *PostgresContext) GetAllValidators(height int64) (vals []*coreTypes.Acto func (p *PostgresContext) GetAllValidatorsJSON(height int64) (json string, err error) { ctx, tx := p.getCtxAndTx() err = tx.QueryRow(ctx, types.SelectJSON(types.ValidatorActor.GetAllQuery(height))).Scan(&json) + if err != nil && strings.Contains(err.Error(), errCannotScanNULL) { + err = nil + } return } @@ -102,6 +110,9 @@ func (p *PostgresContext) GetAllServicers(height int64) (sn []*coreTypes.Actor, func (p *PostgresContext) GetAllServicersJSON(height int64) (json string, err error) { ctx, tx := p.getCtxAndTx() err = tx.QueryRow(ctx, types.SelectJSON(types.ServicerActor.GetAllQuery(height))).Scan(&json) + if err != nil && strings.Contains(err.Error(), errCannotScanNULL) { + err = nil + } return } @@ -134,6 +145,9 @@ func (p *PostgresContext) GetAllFishermen(height int64) (f []*coreTypes.Actor, e func (p *PostgresContext) GetAllFishermenJSON(height int64) (json string, err error) { ctx, tx := p.getCtxAndTx() err = tx.QueryRow(ctx, types.SelectJSON(types.FishermanActor.GetAllQuery(height))).Scan(&json) + if err != nil && strings.Contains(err.Error(), errCannotScanNULL) { + err = nil + } return } diff --git a/persistence/db.go b/persistence/db.go index 43bf14e91..e0621b0c0 100644 --- a/persistence/db.go +++ b/persistence/db.go @@ -27,6 +27,9 @@ const ( // TODO: Make this a node configuration connTimeout = 5 * time.Second + + // errCannotScanNULL is the error returned when a NULL value is scanned via #pgx.Row.Scan(). For reference: https://pkg.go.dev/github.com/jackc/pgx/v5@v5.2.0#Row.Scan + errCannotScanNULL = "cannot scan NULL" ) // TODO: Move schema related functionality into its own package diff --git a/persistence/genesis.go b/persistence/genesis.go index 07fb70ca0..1c8fb1f88 100644 --- a/persistence/genesis.go +++ b/persistence/genesis.go @@ -4,6 +4,7 @@ import ( "encoding/hex" "log" "math/big" + "strings" "github.com/pokt-network/pocket/persistence/types" "github.com/pokt-network/pocket/runtime/genesis" @@ -155,6 +156,9 @@ func (p *PostgresContext) GetAllAccounts(height int64) (accs []*coreTypes.Accoun func (p *PostgresContext) GetAllAccountsJSON(height int64) (json string, err error) { ctx, tx := p.getCtxAndTx() err = tx.QueryRow(ctx, types.SelectJSON(types.Account.GetAllQuery(height))).Scan(&json) + if err != nil && strings.Contains(err.Error(), errCannotScanNULL) { + err = nil + } return } @@ -178,5 +182,8 @@ func (p *PostgresContext) GetAllPools(height int64) (accs []*coreTypes.Account, func (p *PostgresContext) GetAllPoolsJSON(height int64) (json string, err error) { ctx, tx := p.getCtxAndTx() err = tx.QueryRow(ctx, types.SelectJSON(types.Pool.GetAllQuery(height))).Scan(&json) + if err != nil && strings.Contains(err.Error(), errCannotScanNULL) { + err = nil + } return } diff --git a/persistence/gov.go b/persistence/gov.go index 4c773d6a2..1222d2ea6 100644 --- a/persistence/gov.go +++ b/persistence/gov.go @@ -84,6 +84,9 @@ func (p *PostgresContext) GetAllParamsJSON(height int64) (string, error) { ctx, tx := p.getCtxAndTx() var paramsJSON string err := tx.QueryRow(ctx, types.GetAllParamsOrFlagsJSONQuery(types.ParamsTableName, height)).Scan(¶msJSON) + if err != nil && strings.Contains(err.Error(), errCannotScanNULL) { + err = nil + } return paramsJSON, err } @@ -112,6 +115,9 @@ func (p *PostgresContext) GetAllFlagsJSON(height int64) (string, error) { ctx, tx := p.getCtxAndTx() var paramsJSON string err := tx.QueryRow(ctx, types.GetAllParamsOrFlagsJSONQuery(types.FlagsTableName, height)).Scan(¶msJSON) + if err != nil && strings.Contains(err.Error(), errCannotScanNULL) { + err = nil + } return paramsJSON, err } From 6249db3208803e998075ec03a440ea41aaa4d432 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 3 Apr 2023 06:44:01 +0100 Subject: [PATCH 57/59] fix(consensus): fix merge --- consensus/hotstuff_leader.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/consensus/hotstuff_leader.go b/consensus/hotstuff_leader.go index 5fd35b4af..9d82b138a 100644 --- a/consensus/hotstuff_leader.go +++ b/consensus/hotstuff_leader.go @@ -74,15 +74,14 @@ func (handler *HotstuffLeaderMessageHandler) HandleNewRoundMessage(m *consensusM } else { // Leader acts like a replica if `prepareQC` is not `nil` // TODO: Do we need to call `validateProposal` here similar to how replicas does it + if err := m.applyBlock(m.block); err != nil { + m.logger.Error().Err(err).Msg(typesCons.ErrApplyBlock.Error()) + m.paceMaker.InterruptRound("failed to apply block") + return + } m.block = highPrepareQC.Block } - if err := m.applyBlock(m.block); err != nil { - m.logger.Error().Err(err).Msg(typesCons.ErrApplyBlock.Error()) - m.paceMaker.InterruptRound("failed to apply block") - return - } - m.step = Prepare m.hotstuffMempool[NewRound].Clear() From c3c4100fe823eae441fbea640d05b8b460fc3110 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 3 Apr 2023 06:44:49 +0100 Subject: [PATCH 58/59] fix(consensus): fix merge --- consensus/types/messages.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/consensus/types/messages.go b/consensus/types/messages.go index 432cb5993..09bb97292 100644 --- a/consensus/types/messages.go +++ b/consensus/types/messages.go @@ -21,8 +21,7 @@ const ( DisregardBlock = "Discarding block" // WARN - NilUtilityUOWWarning = "⚠️ utilityUnitOfWork expected to be nil but is not." - InvalidPartialSigInQCWarning = "⚠️ [WARN] QC contains an invalid partial signature" + NilUtilityUOWWarning = "⚠️ utilityUnitOfWork expected to be nil but is not." // DEBUG DebugResetToGenesis = "🧑‍💻 Resetting to genesis..." From b94016f5a8eeaf7d10d64ce5c76e77b2bcf1f890 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 3 Apr 2023 07:05:49 +0100 Subject: [PATCH 59/59] fix(consensus): merge fix --- consensus/hotstuff_leader.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/hotstuff_leader.go b/consensus/hotstuff_leader.go index 9d82b138a..e5b0491f1 100644 --- a/consensus/hotstuff_leader.go +++ b/consensus/hotstuff_leader.go @@ -74,7 +74,7 @@ func (handler *HotstuffLeaderMessageHandler) HandleNewRoundMessage(m *consensusM } else { // Leader acts like a replica if `prepareQC` is not `nil` // TODO: Do we need to call `validateProposal` here similar to how replicas does it - if err := m.applyBlock(m.block); err != nil { + if err := m.applyBlock(highPrepareQC.Block); err != nil { m.logger.Error().Err(err).Msg(typesCons.ErrApplyBlock.Error()) m.paceMaker.InterruptRound("failed to apply block") return