Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Sep 18, 2024
1 parent 2ab5d03 commit ce65781
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 53 deletions.
24 changes: 1 addition & 23 deletions abci/example/kvstore/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kvstore
import (
"context"
"errors"
"fmt"
"os"
"testing"

Expand All @@ -12,7 +11,6 @@ import (

abci "github.com/dashpay/tenderdash/abci/types"
"github.com/dashpay/tenderdash/config"
selectproposer "github.com/dashpay/tenderdash/internal/consensus/versioned/selectproposer"
sm "github.com/dashpay/tenderdash/internal/state"
"github.com/dashpay/tenderdash/internal/test/factory"
"github.com/dashpay/tenderdash/libs/log"
Expand Down Expand Up @@ -87,7 +85,7 @@ func (e *blockExecutor) createBlock(txs types.Txs, commit *types.Commit) *types.
if commit == nil {
commit = &types.Commit{}
}
proposer := getProposerFromState(e.state, e.state.LastBlockHeight+1, 0)
proposer := e.state.GetProposerFromState(e.state.LastBlockHeight+1, 0)
block := e.state.MakeBlock(
e.state.LastBlockHeight+1,
txs,
Expand Down Expand Up @@ -134,23 +132,3 @@ func (e *blockExecutor) commit(ctx context.Context, block *types.Block) (*types.
}
return vs.MakeCommit(), nil
}

// GetProposerFromState returns the proposer for the given height and round.
//
// This function is a copy of the one in internal/state/test/factory/block.go
// to avoid a circular dependency.
func getProposerFromState(state sm.State, height int64, round int32) *types.Validator {
vs, err := selectproposer.NewProposerSelector(
state.ConsensusParams,
state.Validators.Copy(),
state.LastBlockHeight,
state.LastBlockRound,
nil,
nil,
)
if err != nil {
panic(fmt.Errorf("failed to create validator scoring strategy: %w", err))
}
proposer := vs.MustGetProposer(height, round)
return proposer
}
3 changes: 1 addition & 2 deletions internal/consensus/replayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/dashpay/tenderdash/internal/eventbus"
"github.com/dashpay/tenderdash/internal/proxy"
sm "github.com/dashpay/tenderdash/internal/state"
sf "github.com/dashpay/tenderdash/internal/state/test/factory"
"github.com/dashpay/tenderdash/internal/store"
"github.com/dashpay/tenderdash/internal/test/factory"
tmbytes "github.com/dashpay/tenderdash/libs/bytes"
Expand Down Expand Up @@ -208,7 +207,7 @@ func TestInitChainGenesisTime(t *testing.T) {
require.NoError(t, err)
stateStore := sm.NewStore(dbm.NewMemDB())
blockStore := store.NewBlockStore(dbm.NewMemDB())
proposer := sf.GetProposerFromState(smState, 1, 0)
proposer := smState.GetProposerFromState(1, 0)
proposerProTxHash := proposer.ProTxHash
replayer := newBlockReplayer(stateStore, blockStore, &genDoc, eventBus, proxyApp, proposerProTxHash)

Expand Down
3 changes: 1 addition & 2 deletions internal/evidence/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
tmquery "github.com/dashpay/tenderdash/internal/pubsub/query"
sm "github.com/dashpay/tenderdash/internal/state"
smmocks "github.com/dashpay/tenderdash/internal/state/mocks"
sf "github.com/dashpay/tenderdash/internal/state/test/factory"
"github.com/dashpay/tenderdash/internal/store"
"github.com/dashpay/tenderdash/libs/log"
"github.com/dashpay/tenderdash/types"
Expand Down Expand Up @@ -541,7 +540,7 @@ func initializeBlockStore(db dbm.DB, state sm.State) (*store.BlockStore, error)

for i := int64(1); i <= state.LastBlockHeight; i++ {
lastCommit := makeCommit(i-1, state.Validators.QuorumHash)
prop := sf.GetProposerFromState(state, i, 0)
prop := state.GetProposerFromState(i, 0)
block := state.MakeBlock(i, []types.Tx{}, lastCommit, nil, prop.ProTxHash, 0)

block.Header.Time = defaultEvidenceTime.Add(time.Duration(i) * time.Minute)
Expand Down
4 changes: 2 additions & 2 deletions internal/state/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func TestUpdateConsensusParams(t *testing.T) {
ConsensusParamUpdates: &tmtypes.ConsensusParams{Block: &tmtypes.BlockParams{MaxBytes: 1024 * 1024}},
AppVersion: 1,
}, nil).Once()
prop := sf.GetProposerFromState(state, height, round)
prop := state.GetProposerFromState(height, round)
block1, _, err := blockExec.CreateProposalBlock(
ctx,
height,
Expand Down Expand Up @@ -400,7 +400,7 @@ func TestOverrideAppVersion(t *testing.T) {
AppVersion: appVersion,
}, nil).Once()

proposer := sf.GetProposerFromState(state, height, round)
proposer := state.GetProposerFromState(height, round)
block1, _, err := blockExec.CreateProposalBlock(
ctx,
height,
Expand Down
24 changes: 24 additions & 0 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"errors"
"fmt"
"os"
"testing"
"time"

"github.com/gogo/protobuf/proto"

"github.com/dashpay/tenderdash/dash"
selectproposer "github.com/dashpay/tenderdash/internal/consensus/versioned/selectproposer"
tmbytes "github.com/dashpay/tenderdash/libs/bytes"
tmtime "github.com/dashpay/tenderdash/libs/time"
tmstate "github.com/dashpay/tenderdash/proto/tendermint/state"
Expand Down Expand Up @@ -312,6 +314,28 @@ func (state State) ValidatorsAtHeight(height int64) *types.ValidatorSet {
}
}

// GetProposerFromState is a helper function that returns the proposer for the given height and round,
// based on current state. Only use in tests.
func (state *State) GetProposerFromState(height int64, round int32) *types.Validator {
if !testing.Testing() {
panic("GetProposerFromState should only be used in tests")
}

vs, err := selectproposer.NewProposerSelector(
state.ConsensusParams,
state.Validators.Copy(),
state.LastBlockHeight,
state.LastBlockRound,
nil,
nil,
)
if err != nil {
panic(fmt.Errorf("failed to create validator scoring strategy: %w", err))
}
proposer := vs.MustGetProposer(height, round)
return proposer
}

// NewStateChangeset returns a structure that will hold new changes to the state, that can be applied once the block is finalized
func (state State) NewStateChangeset(ctx context.Context, rp RoundParams) (CurrentRoundState, error) {
proTxHash, _ := dash.ProTxHashFromContext(ctx)
Expand Down
3 changes: 1 addition & 2 deletions internal/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
selectproposer "github.com/dashpay/tenderdash/internal/consensus/versioned/selectproposer"
"github.com/dashpay/tenderdash/internal/evidence/mocks"
sm "github.com/dashpay/tenderdash/internal/state"
sf "github.com/dashpay/tenderdash/internal/state/test/factory"
statefactory "github.com/dashpay/tenderdash/internal/state/test/factory"
"github.com/dashpay/tenderdash/libs/log"
tmstate "github.com/dashpay/tenderdash/proto/tendermint/state"
Expand Down Expand Up @@ -741,7 +740,7 @@ func TestStateMakeBlock(t *testing.T) {
stateVersion.App = kvstore.ProtocolVersion
var height int64 = 2
state.LastBlockHeight = height - 1
proposerProTxHash := sf.GetProposerFromState(state, height, 0).ProTxHash
proposerProTxHash := state.GetProposerFromState(height, 0).ProTxHash
block, err := statefactory.MakeBlock(state, height, new(types.Commit), 0)
require.NoError(t, err)

Expand Down
21 changes: 2 additions & 19 deletions internal/state/test/factory/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/dashpay/tenderdash/abci/example/kvstore"
abci "github.com/dashpay/tenderdash/abci/types"
"github.com/dashpay/tenderdash/crypto"
selectproposer "github.com/dashpay/tenderdash/internal/consensus/versioned/selectproposer"
sm "github.com/dashpay/tenderdash/internal/state"
"github.com/dashpay/tenderdash/internal/test/factory"
"github.com/dashpay/tenderdash/types"
Expand Down Expand Up @@ -56,7 +55,7 @@ func MakeBlock(state sm.State, height int64, c *types.Commit, proposedAppVersion
if state.LastBlockHeight != (height - 1) {
return nil, fmt.Errorf("requested height %d should be 1 more than last block height %d", height, state.LastBlockHeight)
}
proposer := GetProposerFromState(state, height, 0)
proposer := state.GetProposerFromState(height, 0)
block := state.MakeBlock(
height,
factory.MakeNTxs(state.LastBlockHeight, 10),
Expand Down Expand Up @@ -124,26 +123,10 @@ func makeBlockAndPartSet(
},
)
}
proposer := GetProposerFromState(state, height, 0)
proposer := state.GetProposerFromState(height, 0)
block := state.MakeBlock(height, []types.Tx{}, lastCommit, nil, proposer.ProTxHash, proposedAppVersion)
partSet, err := block.MakePartSet(types.BlockPartSizeBytes)
require.NoError(t, err)

return block, partSet
}

func GetProposerFromState(state sm.State, height int64, round int32) *types.Validator {
vs, err := selectproposer.NewProposerSelector(
state.ConsensusParams,
state.Validators.Copy(),
state.LastBlockHeight,
state.LastBlockRound,
nil,
nil,
)
if err != nil {
panic(fmt.Errorf("failed to create validator scoring strategy: %w", err))
}
proposer := vs.MustGetProposer(height, round)
return proposer
}
6 changes: 3 additions & 3 deletions internal/state/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestValidateBlockHeader(t *testing.T) {

// Build up state for multiple heights
for height := int64(1); height < validationTestsStopHeight; height++ {
proposer := statefactory.GetProposerFromState(state, height, 0)
proposer := state.GetProposerFromState(height, 0)
proposerProTxHash := proposer.ProTxHash
/*
Invalid blocks don't pass
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestValidateBlockCommit(t *testing.T) {
badPrivVal := types.NewMockPVForQuorum(badPrivValQuorumHash)

for height := int64(1); height < validationTestsStopHeight; height++ {
proposer := statefactory.GetProposerFromState(state, height, 0)
proposer := state.GetProposerFromState(height, 0)
proTxHash := proposer.ProTxHash
if height > 1 {
/*
Expand Down Expand Up @@ -388,7 +388,7 @@ func TestValidateBlockEvidence(t *testing.T) {
lastCommit := types.NewCommit(0, 0, types.BlockID{}, nil, nil)

for height := int64(1); height < validationTestsStopHeight; height++ {
proposer := statefactory.GetProposerFromState(state, height, 0)
proposer := state.GetProposerFromState(height, 0)
proposerProTxHash := proposer.ProTxHash
maxBytesEvidence := state.ConsensusParams.Evidence.MaxBytes
if height > 1 {
Expand Down

0 comments on commit ce65781

Please sign in to comment.