Skip to content

Commit

Permalink
test(kvstore): enforce app version in block to be equal to current he…
Browse files Browse the repository at this point in the history
…ight
  • Loading branch information
lklimek committed Mar 20, 2024
1 parent cc25fb0 commit 7a5f8fb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
7 changes: 6 additions & 1 deletion abci/example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/dashpay/tenderdash/abci/types"
"github.com/dashpay/tenderdash/libs/log"
tmnet "github.com/dashpay/tenderdash/libs/net"
"github.com/dashpay/tenderdash/proto/tendermint/version"
)

func TestKVStore(t *testing.T) {
Expand Down Expand Up @@ -73,7 +74,11 @@ func testBulk(ctx context.Context, t *testing.T, logger log.Logger, app types.Ap
require.NoError(t, err)

// Construct request
rpp := &types.RequestProcessProposal{Height: 1, Txs: make([][]byte, numDeliverTxs)}
rpp := &types.RequestProcessProposal{
Height: 1,
Txs: make([][]byte, numDeliverTxs),
Version: &version.Consensus{App: 1},
}
for counter := 0; counter < numDeliverTxs; counter++ {
rpp.Txs[counter] = []byte("test")
}
Expand Down
18 changes: 15 additions & 3 deletions abci/example/kvstore/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (
"github.com/dashpay/tenderdash/version"
)

const ProtocolVersion uint64 = 0x12345678
// ProtocolVersion defines initial protocol (app) version.
// App version is incremented on every block, to match current height.
const ProtocolVersion uint64 = 1

//---------------------------------------------------

Expand Down Expand Up @@ -287,7 +289,7 @@ func (app *Application) InitChain(_ context.Context, req *abci.RequestInitChain)
if !ok {
consensusParams = types1.ConsensusParams{
Version: &types1.VersionParams{
AppVersion: ProtocolVersion,
AppVersion: uint64(app.LastCommittedState.GetHeight()) + 1,
},
}
}
Expand Down Expand Up @@ -346,6 +348,7 @@ func (app *Application) PrepareProposal(_ context.Context, req *abci.RequestPrep
ConsensusParamUpdates: app.getConsensusParamsUpdate(req.Height),
CoreChainLockUpdate: coreChainLock,
ValidatorSetUpdate: app.getValidatorSetUpdate(req.Height),
XAppVersion: &abci.ResponsePrepareProposal_AppVersion{AppVersion: uint64(roundState.GetHeight())},
}

if app.cfg.PrepareProposalDelayMS != 0 {
Expand Down Expand Up @@ -373,6 +376,15 @@ func (app *Application) ProcessProposal(_ context.Context, req *abci.RequestProc
Status: abci.ResponseProcessProposal_REJECT,
}, err
}

if req.Version.App != uint64(roundState.GetHeight()) {
app.logger.Error("app version mismatch, kvstore expects it to be equal to the current height",
"version", req.Version, "height", roundState.GetHeight())
return &abci.ResponseProcessProposal{
Status: abci.ResponseProcessProposal_REJECT,
}, nil
}

resp := &abci.ResponseProcessProposal{
Status: abci.ResponseProcessProposal_ACCEPT,
AppHash: roundState.GetAppHash(),
Expand Down Expand Up @@ -584,7 +596,7 @@ func (app *Application) Info(_ context.Context, req *abci.RequestInfo) (*abci.Re
resp := &abci.ResponseInfo{
Data: fmt.Sprintf("{\"appHash\":\"%s\"}", appHash.String()),
Version: version.ABCIVersion,
AppVersion: ProtocolVersion,
AppVersion: uint64(app.LastCommittedState.GetHeight()) + 1, // we set app version to CURRENT height
LastBlockHeight: app.LastCommittedState.GetHeight(),
LastBlockAppHash: app.LastCommittedState.GetAppHash(),
}
Expand Down
24 changes: 14 additions & 10 deletions abci/example/kvstore/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/dashpay/tenderdash/libs/log"
"github.com/dashpay/tenderdash/libs/service"
tmproto "github.com/dashpay/tenderdash/proto/tendermint/types"
pbversion "github.com/dashpay/tenderdash/proto/tendermint/version"
tmtypes "github.com/dashpay/tenderdash/types"
"github.com/dashpay/tenderdash/version"
)
Expand Down Expand Up @@ -48,8 +49,9 @@ func testKVStore(ctx context.Context, t *testing.T, app types.Application, tx []
require.ErrorContains(t, err, "duplicate PrepareProposal call")

reqProcess := &types.RequestProcessProposal{
Txs: [][]byte{tx},
Height: height,
Txs: [][]byte{tx},
Height: height,
Version: &pbversion.Consensus{App: uint64(height)},
}
respProcess, err := app.ProcessProposal(ctx, reqProcess)
require.NoError(t, err)
Expand Down Expand Up @@ -283,9 +285,10 @@ func makeApplyBlock(
}

respProcessProposal, err := kvstore.ProcessProposal(ctx, &types.RequestProcessProposal{
Hash: hash,
Height: height,
Txs: txs,
Hash: hash,
Height: height,
Txs: txs,
Version: &pbversion.Consensus{App: uint64(height)},
})
require.NoError(t, err)
require.NotZero(t, respProcessProposal)
Expand Down Expand Up @@ -406,8 +409,9 @@ func runClientTests(ctx context.Context, t *testing.T, client abciclient.Client)

func testClient(ctx context.Context, t *testing.T, app abciclient.Client, height int64, tx []byte, key, value string) {
rpp, err := app.ProcessProposal(ctx, &types.RequestProcessProposal{
Txs: [][]byte{tx},
Height: height,
Txs: [][]byte{tx},
Height: height,
Version: &pbversion.Consensus{App: uint64(height)},
})
require.NoError(t, err)
require.NotZero(t, rpp)
Expand Down Expand Up @@ -536,17 +540,17 @@ func newKvApp(ctx context.Context, t *testing.T, genesisHeight int64, opts ...Op
return app
}

func assertRespInfo(t *testing.T, expectHeight int64, expectAppHash tmbytes.HexBytes, actual types.ResponseInfo, msgs ...interface{}) {
func assertRespInfo(t *testing.T, expectLastBlockHeight int64, expectAppHash tmbytes.HexBytes, actual types.ResponseInfo, msgs ...interface{}) {
t.Helper()

if expectAppHash == nil {
expectAppHash = make(tmbytes.HexBytes, tmcrypto.DefaultAppHashSize)
}
expected := types.ResponseInfo{
LastBlockHeight: expectHeight,
LastBlockHeight: expectLastBlockHeight,
LastBlockAppHash: expectAppHash,
Version: version.ABCIVersion,
AppVersion: ProtocolVersion,
AppVersion: uint64(expectLastBlockHeight + 1),
Data: fmt.Sprintf(`{"appHash":"%s"}`, expectAppHash.String()),
}

Expand Down
2 changes: 2 additions & 0 deletions internal/state/test/factory/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func MakeBlock(state sm.State, height int64, c *types.Commit, proposedAppVersion
if block.ResultsHash, err = abci.TxResultsHash(factory.ExecTxResults(block.Txs)); err != nil {
return nil, err
}
// kvstore expects app version to be set to height
block.Version.App = uint64(height)

return block, nil
}
Expand Down

0 comments on commit 7a5f8fb

Please sign in to comment.