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

fix: consensus invalid block and version test improvement #2789

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion tm2/pkg/bft/state/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func validateBlock(stateDB dbm.DB, state State, block *types.Block) error {

// Validate app info
if !bytes.Equal(block.AppHash, state.AppHash) {
return fmt.Errorf("wrong Block.Header.AppHash. Expected %X, got %v",
return fmt.Errorf("wrong Block.Header.AppHash. Expected %X, got %X",
state.AppHash,
block.AppHash,
)
Expand Down
55 changes: 35 additions & 20 deletions tm2/pkg/bft/state/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,55 @@ func TestValidateBlockHeader(t *testing.T) {
testCases := []struct {
name string
malleateBlock func(block *types.Block)
expectedError string
}{
{"BlockVersion wrong", func(block *types.Block) { block.Version += "-wrong" }},
singhhp1069 marked this conversation as resolved.
Show resolved Hide resolved
{"AppVersion wrong", func(block *types.Block) { block.AppVersion += "-wrong" }},
{"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) }},
{"NumTxs wrong", func(block *types.Block) { block.NumTxs += 10 }},
{"TotalTxs wrong", func(block *types.Block) { block.TotalTxs += 10 }},

{"LastBlockID wrong", func(block *types.Block) { block.LastBlockID.PartsHeader.Total += 10 }},
{"LastCommitHash wrong", func(block *types.Block) { block.LastCommitHash = wrongHash }},
{"DataHash wrong", func(block *types.Block) { block.DataHash = wrongHash }},

{"ValidatorsHash wrong", func(block *types.Block) { block.ValidatorsHash = wrongHash }},
{"NextValidatorsHash wrong", func(block *types.Block) { block.NextValidatorsHash = wrongHash }},
{"ConsensusHash wrong", func(block *types.Block) { block.ConsensusHash = wrongHash }},
{"AppHash wrong", func(block *types.Block) { block.AppHash = wrongHash }},
{"LastResultsHash wrong", func(block *types.Block) { block.LastResultsHash = wrongHash }},

{"Proposer wrong", func(block *types.Block) { block.ProposerAddress = ed25519.GenPrivKey().PubKey().Address() }},
{"Proposer invalid", func(block *types.Block) { block.ProposerAddress = crypto.Address{} /* zero */ }},
{"BlockVersion wrong", func(block *types.Block) { block.Version += "-wrong" }, ""},
{"AppVersion wrong", func(block *types.Block) { block.AppVersion += "-wrong" }, ""},
{"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) }, ""},
{"NumTxs wrong", func(block *types.Block) { block.NumTxs += 10 }, ""},
{"TotalTxs wrong", func(block *types.Block) { block.TotalTxs += 10 }, ""},
{"LastBlockID wrong", func(block *types.Block) { block.LastBlockID.PartsHeader.Total += 10 }, ""},
{"LastCommitHash wrong", func(block *types.Block) { block.LastCommitHash = wrongHash }, ""},
{"DataHash wrong", func(block *types.Block) { block.DataHash = wrongHash }, ""},
{"ValidatorsHash wrong", func(block *types.Block) { block.ValidatorsHash = wrongHash }, ""},
{"NextValidatorsHash wrong", func(block *types.Block) { block.NextValidatorsHash = wrongHash }, ""},
{"ConsensusHash wrong", func(block *types.Block) { block.ConsensusHash = wrongHash }, ""},
{"LastResultsHash wrong", func(block *types.Block) { block.LastResultsHash = wrongHash }, ""},
{"Proposer wrong", func(block *types.Block) { block.ProposerAddress = ed25519.GenPrivKey().PubKey().Address() }, ""},
{"Proposer invalid", func(block *types.Block) { block.ProposerAddress = crypto.Address{} }, ""},

// Specific test for AppHash mismatch
{
name: "AppHash mismatch",
malleateBlock: func(block *types.Block) {
block.AppHash = []byte{142, 79, 194, 197, 189, 178, 218, 227, 75, 55, 185, 135, 160, 172, 150, 4, 93, 216, 145, 92, 158, 85, 255, 177, 105, 103, 123, 124, 90, 166, 212, 226}
},
expectedError: "wrong Block.Header.AppHash. Expected CE9623208B00F0592466D9D020BDBA35CB624DFADEF80E7F150D66FBBBF37321, got 8E4FC2C5BDB2DAE34B37B987A0AC96045DD8915C9E55FFB169677B7C5AA6D4E",
},
}

// Build up state for multiple heights
for height := int64(1); height < validationTestsStopHeight; height++ {
proposerAddr := state.Validators.GetProposer().Address

// Set a specific AppHash for testing
state.AppHash = []byte{206, 150, 35, 32, 139, 0, 240, 89, 36, 102, 217, 208, 32, 189, 186, 53, 203, 98, 77, 250, 222, 248, 14, 127, 21, 13, 102, 251, 187, 243, 115, 33}

/*
Invalid blocks don't pass
*/
for _, tc := range testCases {
block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, proposerAddr)
tc.malleateBlock(block)
err := blockExec.ValidateBlock(state, block)

require.Error(t, err, tc.name)

if tc.expectedError != "" {
require.Equal(t, tc.expectedError, err.Error(), "Error mismatch for test case: %s", tc.name)
}
}

/*
Expand Down