Skip to content

Commit

Permalink
add golden tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sunspirit99 committed Sep 27, 2024
1 parent 01d1276 commit a3fee13
Showing 1 changed file with 103 additions and 27 deletions.
130 changes: 103 additions & 27 deletions tm2/pkg/bft/state/validation_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package state_test

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/gnolang/gno/tm2/pkg/bft/mempool/mock"
Expand All @@ -29,51 +31,125 @@ func TestValidateBlockHeader(t *testing.T) {
blockExec := sm.NewBlockExecutor(stateDB, log.NewTestingLogger(t), proxyApp.Consensus(), mock.Mempool{})
lastCommit := types.NewCommit(types.BlockID{}, nil)

// some bad values
validHash := tmhash.Sum([]byte("this hash is valid"))
wrongHash := tmhash.Sum([]byte("this hash is wrong"))

wrongAddress := ed25519.GenPrivKey().PubKey().Address()
invalidAddress := crypto.Address{}

// Manipulation of any header field causes failure.
testCases := []struct {
name string
malleateBlock func(block *types.Block)
expectedError string
}{
{"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 }},
{"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" },
"wrong Block.Header.Version",
},
{
"AppVersion wrong",
func(block *types.Block) { block.AppVersion += "-wrong" },
"wrong Block.Header.AppVersion",
},
{
"ChainID wrong",
func(block *types.Block) { block.ChainID = "not-the-real-one" },
"wrong Block.Header.ChainID",
},
{
"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 },
"wrong Header.NumTxs",
},
{
"TotalTxs wrong",
func(block *types.Block) { block.TotalTxs += 10 },
"wrong Block.Header.TotalTxs",
},
{
"LastBlockID wrong",
func(block *types.Block) { block.LastBlockID.PartsHeader.Total += 10 },
"wrong Block.Header.LastBlockID",
},
{
"LastCommitHash wrong",
func(block *types.Block) { block.LastCommitHash = wrongHash },
"wrong Header.LastCommitHash",
},
{
"DataHash wrong",
func(block *types.Block) { block.DataHash = wrongHash },
"wrong Header.DataHash",
},
{
"ValidatorsHash wrong",
func(block *types.Block) { block.ValidatorsHash = wrongHash },
"wrong Block.Header.ValidatorsHash",
},
{
"NextValidatorsHash wrong",
func(block *types.Block) { block.NextValidatorsHash = wrongHash },
"wrong Block.Header.NextValidatorsHash",
},
{
"ConsensusHash wrong",
func(block *types.Block) { block.ConsensusHash = wrongHash },
"wrong Block.Header.ConsensusHash",
},
{
"AppHash mismatch",
func(block *types.Block) { block.AppHash = wrongHash },
fmt.Sprintf("wrong Block.Header.AppHash. Expected %X, got %X", validHash, wrongHash),
},
{
"LastResultsHash wrong",
func(block *types.Block) { block.LastResultsHash = wrongHash },
fmt.Sprintf("wrong Block.Header.LastResultsHash. Expected %X, got %X", validHash, wrongHash),
},
{
"Proposer wrong",
func(block *types.Block) { block.ProposerAddress = wrongAddress },
fmt.Sprintf("Block.Header.ProposerAddress, %X, is not a validator", wrongAddress),
},
{
"Proposer invalid",
func(block *types.Block) { block.ProposerAddress = invalidAddress /* zero */ },
fmt.Sprintf("Block.Header.ProposerAddress, %X, is not a validator", invalidAddress),
},
}

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

/*
Invalid blocks don't pass
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)
t.Run(tc.name, func(t *testing.T) {
block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, proposerAddr)
tc.malleateBlock(block)
err := blockExec.ValidateBlock(state, block)
assert.ErrorContains(t, err, tc.expectedError, tc.name)
})
}

/*
A good block passes
A good block passes
*/
var err error
state, _, lastCommit, err = makeAndCommitGoodBlock(state, height, lastCommit, proposerAddr, blockExec, privVals)
Expand Down

0 comments on commit a3fee13

Please sign in to comment.