Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: give the application control to validate the app version #1098

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
421 changes: 231 additions & 190 deletions abci/types/types.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions proto/tendermint/abci/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ message RequestProcessProposal {
bytes next_validators_hash = 7;
// address of the public key of the original proposer of the block.
bytes proposer_address = 8;
uint64 app_version = 9;
}

//----------------------------------------
Expand Down
1 change: 1 addition & 0 deletions state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func (blockExec *BlockExecutor) ProcessProposal(
Misbehavior: block.Evidence.Evidence.ToABCI(),
ProposerAddress: block.ProposerAddress,
NextValidatorsHash: block.NextValidatorsHash,
AppVersion: block.Header.Version.App,
})
if err != nil {
return false, ErrInvalidBlock(err)
Expand Down
10 changes: 5 additions & 5 deletions state/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func validateBlock(state State, block *types.Block) error {
return err
}

// Validate basic info.
if block.Version.App != state.Version.Consensus.App ||
block.Version.Block != state.Version.Consensus.Block {
// Validate block version. App version validation should
// happen on the application side as part of process proposal
if block.Version.Block != state.Version.Consensus.Block {
return fmt.Errorf("wrong Block.Header.Version. Expected %v, got %v",
state.Version.Consensus,
block.Version,
state.Version.Consensus.Block,
block.Version.Block,
)
}
if block.ChainID != state.ChainID {
Expand Down
3 changes: 0 additions & 3 deletions state/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@ func TestValidateBlockHeader(t *testing.T) {
wrongHash := tmhash.Sum([]byte("this hash is wrong"))
wrongVersion1 := state.Version.Consensus
wrongVersion1.Block += 2
wrongVersion2 := state.Version.Consensus
wrongVersion2.App += 2

// Manipulation of any header field causes failure.
testCases := []struct {
name string
malleateBlock func(block *types.Block)
}{
{"Version wrong1", func(block *types.Block) { block.Version = wrongVersion1 }},
{"Version wrong2", func(block *types.Block) { block.Version = wrongVersion2 }},
{"ChainID wrong", func(block *types.Block) { block.ChainID = "not-the-real-one" }},
{"Height wrong", func(block *types.Block) { block.Height += 10 }},
{"Time wrong", func(block *types.Block) { block.Time = block.Time.Add(-time.Second * 1) }},
Expand Down
12 changes: 2 additions & 10 deletions statesync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (s *syncer) Sync(snapshot *snapshot, chunks *chunkQueue) (sm.State, *types.
}

// Verify app and app version
if err := s.verifyApp(snapshot, state.Version.Consensus.App); err != nil {
if err := s.verifyApp(snapshot); err != nil {
return sm.State{}, nil, err
}

Expand Down Expand Up @@ -482,20 +482,12 @@ func (s *syncer) requestChunk(snapshot *snapshot, chunk uint32) {
}

// verifyApp verifies the sync, checking the app hash, last block height and app version
func (s *syncer) verifyApp(snapshot *snapshot, appVersion uint64) error {
func (s *syncer) verifyApp(snapshot *snapshot) error {
resp, err := s.connQuery.InfoSync(proxy.RequestInfo)
if err != nil {
return fmt.Errorf("failed to query ABCI app for appHash: %w", err)
}

// sanity check that the app version in the block matches the application's own record
// of its version
if resp.AppVersion != appVersion {
// An error here most likely means that the app hasn't inplemented state sync
// or the Info call correctly
return fmt.Errorf("app version mismatch. Expected: %d, got: %d",
appVersion, resp.AppVersion)
}
if !bytes.Equal(snapshot.trustedAppHash, resp.LastBlockAppHash) {
s.logger.Error("appHash verification failed",
"expected", snapshot.trustedAppHash,
Expand Down
8 changes: 1 addition & 7 deletions statesync/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,6 @@ func TestSyncer_applyChunks_RejectSenders(t *testing.T) {
func TestSyncer_verifyApp(t *testing.T) {
boom := errors.New("boom")
const appVersion = 9
appVersionMismatchErr := errors.New("app version mismatch. Expected: 9, got: 2")
s := &snapshot{Height: 3, Format: 1, Chunks: 5, Hash: []byte{1, 2, 3}, trustedAppHash: []byte("app_hash")}

testcases := map[string]struct {
Expand All @@ -647,11 +646,6 @@ func TestSyncer_verifyApp(t *testing.T) {
LastBlockAppHash: []byte("app_hash"),
AppVersion: appVersion,
}, nil, nil},
"invalid app version": {&abci.ResponseInfo{
LastBlockHeight: 3,
LastBlockAppHash: []byte("app_hash"),
AppVersion: 2,
}, nil, appVersionMismatchErr},
"invalid height": {&abci.ResponseInfo{
LastBlockHeight: 5,
LastBlockAppHash: []byte("app_hash"),
Expand All @@ -675,7 +669,7 @@ func TestSyncer_verifyApp(t *testing.T) {
syncer := newSyncer(*cfg, log.NewNopLogger(), connSnapshot, connQuery, stateProvider, "")

connQuery.On("InfoSync", proxy.RequestInfo).Return(tc.response, tc.err)
err := syncer.verifyApp(s, appVersion)
err := syncer.verifyApp(s)
unwrapped := errors.Unwrap(err)
if unwrapped != nil {
err = unwrapped
Expand Down
Loading