mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-04 15:17:02 +00:00
Merge remote-tracking branch 'origin/develop' into jae/literefactor4
This commit is contained in:
+2
-9
@@ -25,7 +25,7 @@ type Block struct {
|
||||
|
||||
// MakeBlock returns a new block with an empty header, except what can be computed from itself.
|
||||
// It populates the same set of fields validated by ValidateBasic
|
||||
func MakeBlock(height int64, txs []Tx, commit *Commit) *Block {
|
||||
func MakeBlock(height int64, txs []Tx, commit *Commit, evidence []Evidence) *Block {
|
||||
block := &Block{
|
||||
Header: Header{
|
||||
Height: height,
|
||||
@@ -35,20 +35,13 @@ func MakeBlock(height int64, txs []Tx, commit *Commit) *Block {
|
||||
Data: Data{
|
||||
Txs: txs,
|
||||
},
|
||||
Evidence: EvidenceData{Evidence: evidence},
|
||||
LastCommit: commit,
|
||||
}
|
||||
block.fillHeader()
|
||||
return block
|
||||
}
|
||||
|
||||
// AddEvidence appends the given evidence to the block
|
||||
func (b *Block) AddEvidence(evidence []Evidence) {
|
||||
if b == nil {
|
||||
return
|
||||
}
|
||||
b.Evidence.Evidence = append(b.Evidence.Evidence, evidence...)
|
||||
}
|
||||
|
||||
// ValidateBasic performs basic validation that doesn't involve state data.
|
||||
// It checks the internal consistency of the block.
|
||||
func (b *Block) ValidateBasic() error {
|
||||
|
||||
+45
-18
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
crypto "github.com/tendermint/tendermint/crypto"
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
cmn "github.com/tendermint/tendermint/libs/common"
|
||||
)
|
||||
|
||||
@@ -19,11 +19,13 @@ func TestBlockAddEvidence(t *testing.T) {
|
||||
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
|
||||
require.NoError(t, err)
|
||||
|
||||
block := MakeBlock(h, txs, commit)
|
||||
require.NotNil(t, block)
|
||||
|
||||
ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
|
||||
block.AddEvidence([]Evidence{ev})
|
||||
evList := []Evidence{ev}
|
||||
|
||||
block := MakeBlock(h, txs, commit, evList)
|
||||
require.NotNil(t, block)
|
||||
require.Equal(t, 1, len(block.Evidence.Evidence))
|
||||
require.NotNil(t, block.EvidenceHash)
|
||||
}
|
||||
|
||||
func TestBlockValidateBasic(t *testing.T) {
|
||||
@@ -33,11 +35,14 @@ func TestBlockValidateBasic(t *testing.T) {
|
||||
lastID := makeBlockIDRandom()
|
||||
h := int64(3)
|
||||
|
||||
voteSet, _, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
|
||||
voteSet, valSet, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
|
||||
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
|
||||
require.NoError(t, err)
|
||||
|
||||
block := MakeBlock(h, txs, commit)
|
||||
ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
|
||||
evList := []Evidence{ev}
|
||||
|
||||
block := MakeBlock(h, txs, commit, evList)
|
||||
require.NotNil(t, block)
|
||||
|
||||
// proper block must pass
|
||||
@@ -45,39 +50,39 @@ func TestBlockValidateBasic(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// tamper with NumTxs
|
||||
block = MakeBlock(h, txs, commit)
|
||||
block = MakeBlock(h, txs, commit, evList)
|
||||
block.NumTxs++
|
||||
err = block.ValidateBasic()
|
||||
require.Error(t, err)
|
||||
|
||||
// remove 1/2 the commits
|
||||
block = MakeBlock(h, txs, commit)
|
||||
block = MakeBlock(h, txs, commit, evList)
|
||||
block.LastCommit.Precommits = commit.Precommits[:commit.Size()/2]
|
||||
block.LastCommit.hash = nil // clear hash or change wont be noticed
|
||||
err = block.ValidateBasic()
|
||||
require.Error(t, err)
|
||||
|
||||
// tamper with LastCommitHash
|
||||
block = MakeBlock(h, txs, commit)
|
||||
block = MakeBlock(h, txs, commit, evList)
|
||||
block.LastCommitHash = []byte("something else")
|
||||
err = block.ValidateBasic()
|
||||
require.Error(t, err)
|
||||
|
||||
// tamper with data
|
||||
block = MakeBlock(h, txs, commit)
|
||||
block = MakeBlock(h, txs, commit, evList)
|
||||
block.Data.Txs[0] = Tx("something else")
|
||||
block.Data.hash = nil // clear hash or change wont be noticed
|
||||
err = block.ValidateBasic()
|
||||
require.Error(t, err)
|
||||
|
||||
// tamper with DataHash
|
||||
block = MakeBlock(h, txs, commit)
|
||||
block = MakeBlock(h, txs, commit, evList)
|
||||
block.DataHash = cmn.RandBytes(len(block.DataHash))
|
||||
err = block.ValidateBasic()
|
||||
require.Error(t, err)
|
||||
|
||||
// tamper with evidence
|
||||
block = MakeBlock(h, txs, commit)
|
||||
block = MakeBlock(h, txs, commit, evList)
|
||||
block.EvidenceHash = []byte("something else")
|
||||
err = block.ValidateBasic()
|
||||
require.Error(t, err)
|
||||
@@ -85,17 +90,36 @@ func TestBlockValidateBasic(t *testing.T) {
|
||||
|
||||
func TestBlockHash(t *testing.T) {
|
||||
assert.Nil(t, (*Block)(nil).Hash())
|
||||
assert.Nil(t, MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil).Hash())
|
||||
assert.Nil(t, MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).Hash())
|
||||
}
|
||||
|
||||
func TestBlockMakePartSet(t *testing.T) {
|
||||
assert.Nil(t, (*Block)(nil).MakePartSet(2))
|
||||
|
||||
partSet := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil).MakePartSet(1024)
|
||||
partSet := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).MakePartSet(1024)
|
||||
assert.NotNil(t, partSet)
|
||||
assert.Equal(t, 1, partSet.Total())
|
||||
}
|
||||
|
||||
func TestBlockMakePartSetWithEvidence(t *testing.T) {
|
||||
assert.Nil(t, (*Block)(nil).MakePartSet(2))
|
||||
|
||||
txs := []Tx{Tx("foo"), Tx("bar")}
|
||||
lastID := makeBlockIDRandom()
|
||||
h := int64(3)
|
||||
|
||||
voteSet, valSet, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
|
||||
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
|
||||
require.NoError(t, err)
|
||||
|
||||
ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
|
||||
evList := []Evidence{ev}
|
||||
|
||||
partSet := MakeBlock(h, txs, commit, evList).MakePartSet(1024)
|
||||
assert.NotNil(t, partSet)
|
||||
assert.Equal(t, 3, partSet.Total())
|
||||
}
|
||||
|
||||
func TestBlockHashesTo(t *testing.T) {
|
||||
assert.False(t, (*Block)(nil).HashesTo(nil))
|
||||
|
||||
@@ -105,7 +129,10 @@ func TestBlockHashesTo(t *testing.T) {
|
||||
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
|
||||
require.NoError(t, err)
|
||||
|
||||
block := MakeBlock(h, []Tx{Tx("Hello World")}, commit)
|
||||
ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
|
||||
evList := []Evidence{ev}
|
||||
|
||||
block := MakeBlock(h, []Tx{Tx("Hello World")}, commit, evList)
|
||||
block.ValidatorsHash = valSet.Hash()
|
||||
assert.False(t, block.HashesTo([]byte{}))
|
||||
assert.False(t, block.HashesTo([]byte("something else")))
|
||||
@@ -113,7 +140,7 @@ func TestBlockHashesTo(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBlockSize(t *testing.T) {
|
||||
size := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil).Size()
|
||||
size := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).Size()
|
||||
if size <= 0 {
|
||||
t.Fatal("Size of the block is zero or negative")
|
||||
}
|
||||
@@ -124,7 +151,7 @@ func TestBlockString(t *testing.T) {
|
||||
assert.Equal(t, "nil-Block", (*Block)(nil).StringIndented(""))
|
||||
assert.Equal(t, "nil-Block", (*Block)(nil).StringShort())
|
||||
|
||||
block := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil)
|
||||
block := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil)
|
||||
assert.NotEqual(t, "nil-Block", block.String())
|
||||
assert.NotEqual(t, "nil-Block", block.StringIndented(""))
|
||||
assert.NotEqual(t, "nil-Block", block.StringShort())
|
||||
|
||||
@@ -22,7 +22,7 @@ func TestEventBusPublishEventTx(t *testing.T) {
|
||||
defer eventBus.Stop()
|
||||
|
||||
tx := Tx("foo")
|
||||
result := abci.ResponseDeliverTx{Data: []byte("bar"), Tags: []cmn.KVPair{{[]byte("baz"), []byte("1")}}, Fee: cmn.KI64Pair{Key: []uint8{}, Value: 0}}
|
||||
result := abci.ResponseDeliverTx{Data: []byte("bar"), Tags: []cmn.KVPair{{[]byte("baz"), []byte("1")}}}
|
||||
|
||||
txEventsCh := make(chan interface{})
|
||||
|
||||
|
||||
@@ -41,6 +41,10 @@ type Evidence interface {
|
||||
func RegisterEvidences(cdc *amino.Codec) {
|
||||
cdc.RegisterInterface((*Evidence)(nil), nil)
|
||||
cdc.RegisterConcrete(&DuplicateVoteEvidence{}, "tendermint/DuplicateVoteEvidence", nil)
|
||||
|
||||
// mocks
|
||||
cdc.RegisterConcrete(MockGoodEvidence{}, "tendermint/MockGoodEvidence", nil)
|
||||
cdc.RegisterConcrete(MockBadEvidence{}, "tendermint/MockBadEvidence", nil)
|
||||
}
|
||||
|
||||
//-------------------------------------------
|
||||
|
||||
+10
-3
@@ -38,7 +38,7 @@ func (tm2pb) Header(header *Header) abci.Header {
|
||||
ChainID: header.ChainID,
|
||||
Height: header.Height,
|
||||
|
||||
Time: header.Time.Unix(),
|
||||
Time: header.Time,
|
||||
NumTxs: int32(header.NumTxs), // XXX: overflow
|
||||
TotalTxs: header.TotalTxs,
|
||||
|
||||
@@ -50,6 +50,13 @@ func (tm2pb) Header(header *Header) abci.Header {
|
||||
}
|
||||
}
|
||||
|
||||
func (tm2pb) ValidatorWithoutPubKey(val *Validator) abci.Validator {
|
||||
return abci.Validator{
|
||||
Address: val.PubKey.Address(),
|
||||
Power: val.VotingPower,
|
||||
}
|
||||
}
|
||||
|
||||
// XXX: panics on unknown pubkey type
|
||||
func (tm2pb) Validator(val *Validator) abci.Validator {
|
||||
return abci.Validator{
|
||||
@@ -129,9 +136,9 @@ func (tm2pb) Evidence(ev Evidence, valSet *ValidatorSet, evTime time.Time) abci.
|
||||
|
||||
return abci.Evidence{
|
||||
Type: evType,
|
||||
Validator: TM2PB.Validator(val),
|
||||
Validator: TM2PB.ValidatorWithoutPubKey(val),
|
||||
Height: ev.Height(),
|
||||
Time: evTime.Unix(),
|
||||
Time: evTime,
|
||||
TotalVotingPower: valSet.TotalVotingPower(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,6 +102,9 @@ func TestABCIEvidence(t *testing.T) {
|
||||
)
|
||||
|
||||
assert.Equal(t, "duplicate/vote", abciEv.Type)
|
||||
|
||||
// test we do not send pubkeys
|
||||
assert.Empty(t, abciEv.Validator.PubKey)
|
||||
}
|
||||
|
||||
type pubKeyEddie struct{}
|
||||
@@ -120,3 +123,21 @@ func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
|
||||
assert.Panics(t, func() { TM2PB.ValidatorFromPubKeyAndPower(nil, 10) })
|
||||
assert.Panics(t, func() { TM2PB.ValidatorFromPubKeyAndPower(pubKeyEddie{}, 10) })
|
||||
}
|
||||
|
||||
func TestABCIValidatorWithoutPubKey(t *testing.T) {
|
||||
pkEd := ed25519.GenPrivKey().PubKey()
|
||||
|
||||
abciVal := TM2PB.ValidatorWithoutPubKey(&Validator{
|
||||
Address: pkEd.Address(),
|
||||
PubKey: pkEd,
|
||||
VotingPower: 10,
|
||||
})
|
||||
|
||||
// pubkey must be nil
|
||||
tmValExpected := abci.Validator{
|
||||
Address: pkEd.Address(),
|
||||
Power: 10,
|
||||
}
|
||||
|
||||
assert.Equal(t, tmValExpected, abciVal)
|
||||
}
|
||||
|
||||
+6
-1
@@ -2,11 +2,16 @@ package types
|
||||
|
||||
import (
|
||||
"github.com/tendermint/go-amino"
|
||||
cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"
|
||||
"github.com/tendermint/tendermint/crypto/encoding/amino"
|
||||
)
|
||||
|
||||
var cdc = amino.NewCodec()
|
||||
|
||||
func init() {
|
||||
RegisterBlockAmino(cdc)
|
||||
}
|
||||
|
||||
func RegisterBlockAmino(cdc *amino.Codec) {
|
||||
cryptoAmino.RegisterAmino(cdc)
|
||||
RegisterEvidences(cdc)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user