Browse Source

make Block Header and Data non-pointers

make BlockMeta Header a non-pointer

Refs #693
pull/1965/head
Anton Kaliaev 6 years ago
parent
commit
270659f03f
No known key found for this signature in database GPG Key ID: 7B6881D965918214
13 changed files with 59 additions and 72 deletions
  1. +1
    -1
      blockchain/pool_test.go
  2. +9
    -9
      blockchain/store_test.go
  3. +5
    -4
      consensus/types/round_state_test.go
  4. +2
    -2
      lite/proxy/block.go
  5. +16
    -22
      lite/proxy/validate_test.go
  6. +3
    -3
      rpc/core/blocks.go
  7. +2
    -2
      state/execution.go
  8. +6
    -17
      state/state_test.go
  9. +1
    -1
      tools/tm-monitor/monitor/node.go
  10. +3
    -3
      tools/tm-monitor/monitor/node_test.go
  11. +9
    -6
      types/block.go
  12. +1
    -1
      types/block_meta.go
  13. +1
    -1
      types/events.go

+ 1
- 1
blockchain/pool_test.go View File

@ -80,7 +80,7 @@ func TestBasic(t *testing.T) {
}
// Request desired, pretend like we got the block immediately.
go func() {
block := &types.Block{Header: &types.Header{Height: request.Height}}
block := &types.Block{Header: types.Header{Height: request.Height}}
pool.AddBlock(request.PeerID, block, 123)
t.Logf("Added block from peer %v (height: %v)", request.PeerID, request.Height)
}()


+ 9
- 9
blockchain/store_test.go View File

@ -126,7 +126,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) {
eraseSeenCommitInDB bool
}{
{
block: newBlock(&header1, commitAtH10),
block: newBlock(header1, commitAtH10),
parts: validPartSet,
seenCommit: seenCommit1,
},
@ -137,19 +137,19 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) {
},
{
block: newBlock(&header2, commitAtH10),
block: newBlock(header2, commitAtH10),
parts: uncontiguousPartSet,
wantPanic: "only save contiguous blocks", // and incomplete and uncontiguous parts
},
{
block: newBlock(&header1, commitAtH10),
block: newBlock(header1, commitAtH10),
parts: incompletePartSet,
wantPanic: "only save complete block", // incomplete parts
},
{
block: newBlock(&header1, commitAtH10),
block: newBlock(header1, commitAtH10),
parts: validPartSet,
seenCommit: seenCommit1,
corruptCommitInDB: true, // Corrupt the DB's commit entry
@ -157,7 +157,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) {
},
{
block: newBlock(&header1, commitAtH10),
block: newBlock(header1, commitAtH10),
parts: validPartSet,
seenCommit: seenCommit1,
wantPanic: "unmarshal to types.BlockMeta failed",
@ -165,7 +165,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) {
},
{
block: newBlock(&header1, commitAtH10),
block: newBlock(header1, commitAtH10),
parts: validPartSet,
seenCommit: seenCommit1,
@ -174,7 +174,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) {
},
{
block: newBlock(&header1, commitAtH10),
block: newBlock(header1, commitAtH10),
parts: validPartSet,
seenCommit: seenCommit1,
@ -183,7 +183,7 @@ func TestBlockStoreSaveLoadBlock(t *testing.T) {
},
{
block: newBlock(&header1, commitAtH10),
block: newBlock(header1, commitAtH10),
parts: validPartSet,
seenCommit: seenCommit1,
@ -375,7 +375,7 @@ func doFn(fn func() (interface{}, error)) (res interface{}, err error, panicErr
return res, err, panicErr
}
func newBlock(hdr *types.Header, lastCommit *types.Commit) *types.Block {
func newBlock(hdr types.Header, lastCommit *types.Commit) *types.Block {
return &types.Block{
Header: hdr,
LastCommit: lastCommit,


+ 5
- 4
consensus/types/round_state_test.go View File

@ -4,10 +4,11 @@ import (
"testing"
"time"
"github.com/tendermint/go-amino"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/types"
)
func BenchmarkRoundStateDeepCopy(b *testing.B) {
@ -38,7 +39,7 @@ func BenchmarkRoundStateDeepCopy(b *testing.B) {
}
// Random block
block := &types.Block{
Header: &types.Header{
Header: types.Header{
ChainID: cmn.RandStr(12),
Time: time.Now(),
LastBlockID: blockID,
@ -50,7 +51,7 @@ func BenchmarkRoundStateDeepCopy(b *testing.B) {
LastResultsHash: cmn.RandBytes(20),
EvidenceHash: cmn.RandBytes(20),
},
Data: &types.Data{
Data: types.Data{
Txs: txs,
},
Evidence: types.EvidenceData{},


+ 2
- 2
lite/proxy/block.go View File

@ -15,14 +15,14 @@ func ValidateBlockMeta(meta *types.BlockMeta, check lite.Commit) error {
return errors.New("expecting a non-nil BlockMeta")
}
// TODO: check the BlockID??
return ValidateHeader(meta.Header, check)
return ValidateHeader(&meta.Header, check)
}
func ValidateBlock(meta *types.Block, check lite.Commit) error {
if meta == nil {
return errors.New("expecting a non-nil Block")
}
err := ValidateHeader(meta.Header, check)
err := ValidateHeader(&meta.Header, check)
if err != nil {
return err
}


+ 16
- 22
lite/proxy/validate_test.go View File

@ -18,7 +18,7 @@ var (
testTime2 = time.Date(2017, 1, 2, 1, 1, 1, 1, time.UTC)
)
var hdrHeight11 = &types.Header{
var hdrHeight11 = types.Header{
Height: 11,
Time: testTime1,
ValidatorsHash: []byte("Tendermint"),
@ -34,21 +34,18 @@ func TestValidateBlock(t *testing.T) {
block: nil, wantErr: "non-nil Block",
},
{
block: &types.Block{}, wantErr: "nil Header",
},
{
block: &types.Block{Header: new(types.Header)},
block: &types.Block{},
},
// Start Header.Height mismatch test
{
block: &types.Block{Header: &types.Header{Height: 10}},
block: &types.Block{Header: types.Header{Height: 10}},
commit: lite.Commit{Header: &types.Header{Height: 11}},
wantErr: "don't match - 10 vs 11",
},
{
block: &types.Block{Header: &types.Header{Height: 11}},
block: &types.Block{Header: types.Header{Height: 11}},
commit: lite.Commit{Header: &types.Header{Height: 11}},
},
// End Header.Height mismatch test
@ -62,15 +59,15 @@ func TestValidateBlock(t *testing.T) {
{
block: &types.Block{Header: hdrHeight11},
commit: lite.Commit{Header: hdrHeight11},
commit: lite.Commit{Header: &hdrHeight11},
},
// End Header.Hash mismatch test
// Start Header.Data hash mismatch test
{
block: &types.Block{
Header: &types.Header{Height: 11},
Data: &types.Data{Txs: []types.Tx{[]byte("0xDE"), []byte("AD")}},
Header: types.Header{Height: 11},
Data: types.Data{Txs: []types.Tx{[]byte("0xDE"), []byte("AD")}},
},
commit: lite.Commit{
Header: &types.Header{Height: 11},
@ -80,8 +77,8 @@ func TestValidateBlock(t *testing.T) {
},
{
block: &types.Block{
Header: &types.Header{Height: 11, DataHash: deadBeefHash},
Data: &types.Data{Txs: deadBeefTxs},
Header: types.Header{Height: 11, DataHash: deadBeefHash},
Data: types.Data{Txs: deadBeefTxs},
},
commit: lite.Commit{
Header: &types.Header{Height: 11},
@ -116,21 +113,18 @@ func TestValidateBlockMeta(t *testing.T) {
meta: nil, wantErr: "non-nil BlockMeta",
},
{
meta: &types.BlockMeta{}, wantErr: "non-nil Header",
},
{
meta: &types.BlockMeta{Header: new(types.Header)},
meta: &types.BlockMeta{},
},
// Start Header.Height mismatch test
{
meta: &types.BlockMeta{Header: &types.Header{Height: 10}},
meta: &types.BlockMeta{Header: types.Header{Height: 10}},
commit: lite.Commit{Header: &types.Header{Height: 11}},
wantErr: "don't match - 10 vs 11",
},
{
meta: &types.BlockMeta{Header: &types.Header{Height: 11}},
meta: &types.BlockMeta{Header: types.Header{Height: 11}},
commit: lite.Commit{Header: &types.Header{Height: 11}},
},
// End Header.Height mismatch test
@ -144,12 +138,12 @@ func TestValidateBlockMeta(t *testing.T) {
{
meta: &types.BlockMeta{Header: hdrHeight11},
commit: lite.Commit{Header: hdrHeight11},
commit: lite.Commit{Header: &hdrHeight11},
},
{
meta: &types.BlockMeta{
Header: &types.Header{
Header: types.Header{
Height: 11,
ValidatorsHash: []byte("lite-test"),
// TODO: should be able to use empty time after Amino upgrade
@ -164,7 +158,7 @@ func TestValidateBlockMeta(t *testing.T) {
{
meta: &types.BlockMeta{
Header: &types.Header{
Header: types.Header{
Height: 11, DataHash: deadBeefHash,
ValidatorsHash: []byte("Tendermint"),
Time: testTime1,
@ -183,7 +177,7 @@ func TestValidateBlockMeta(t *testing.T) {
{
meta: &types.BlockMeta{
Header: &types.Header{
Header: types.Header{
Height: 11, DataHash: deadBeefHash,
ValidatorsHash: []byte("Tendermint"),
Time: testTime2,


+ 3
- 3
rpc/core/blocks.go View File

@ -3,10 +3,10 @@ package core
import (
"fmt"
cmn "github.com/tendermint/tendermint/libs/common"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tendermint/libs/common"
)
// Get block headers for minHeight <= height <= maxHeight.
@ -288,12 +288,12 @@ func Commit(heightPtr *int64) (*ctypes.ResultCommit, error) {
// use a non-canonical commit
if height == storeHeight {
commit := blockStore.LoadSeenCommit(height)
return ctypes.NewResultCommit(header, commit, false), nil
return ctypes.NewResultCommit(&header, commit, false), nil
}
// Return the canonical commit (comes from the block at height+1)
commit := blockStore.LoadBlockCommit(height)
return ctypes.NewResultCommit(header, commit, true), nil
return ctypes.NewResultCommit(&header, commit, true), nil
}
// BlockResults gets ABCIResults at a given height.


+ 2
- 2
state/execution.go View File

@ -86,7 +86,7 @@ func (blockExec *BlockExecutor) ApplyBlock(state State, blockID types.BlockID, b
fail.Fail() // XXX
// update the state with the block and responses
state, err = updateState(state, blockID, block.Header, abciResponses)
state, err = updateState(state, blockID, &block.Header, abciResponses)
if err != nil {
return state, fmt.Errorf("Commit failed for application: %v", err)
}
@ -189,7 +189,7 @@ func execBlockOnProxyApp(logger log.Logger, proxyAppConn proxy.AppConnConsensus,
// Begin block
_, err := proxyAppConn.BeginBlockSync(abci.RequestBeginBlock{
Hash: block.Hash(),
Header: types.TM2PB.Header(block.Header),
Header: types.TM2PB.Header(&block.Header),
Validators: signVals,
ByzantineValidators: byzVals,
})


+ 6
- 17
state/state_test.go View File

@ -219,7 +219,7 @@ func TestOneValidatorChangesSaveLoad(t *testing.T) {
power++
}
header, blockID, responses := makeHeaderPartsResponsesValPowerChange(state, i, power)
state, err = updateState(state, blockID, header, responses)
state, err = updateState(state, blockID, &header, responses)
assert.Nil(t, err)
nextHeight := state.LastBlockHeight + 1
saveValidatorsInfo(stateDB, nextHeight, state.LastHeightValidatorsChanged, state.Validators)
@ -264,7 +264,7 @@ func TestManyValidatorChangesSaveLoad(t *testing.T) {
// swap the first validator with a new one ^^^ (validator set size stays the same)
header, blockID, responses := makeHeaderPartsResponsesValPubKeyChange(state, height, pubkey)
var err error
state, err = updateState(state, blockID, header, responses)
state, err = updateState(state, blockID, &header, responses)
require.Nil(t, err)
nextHeight := state.LastBlockHeight + 1
saveValidatorsInfo(stateDB, nextHeight, state.LastHeightValidatorsChanged, state.Validators)
@ -321,7 +321,7 @@ func TestConsensusParamsChangesSaveLoad(t *testing.T) {
cp = params[changeIndex]
}
header, blockID, responses := makeHeaderPartsResponsesParams(state, i, cp)
state, err = updateState(state, blockID, header, responses)
state, err = updateState(state, blockID, &header, responses)
require.Nil(t, err)
nextHeight := state.LastBlockHeight + 1
@ -420,7 +420,7 @@ func TestApplyUpdates(t *testing.T) {
}
func makeHeaderPartsResponsesValPubKeyChange(state State, height int64,
pubkey crypto.PubKey) (*types.Header, types.BlockID, *ABCIResponses) {
pubkey crypto.PubKey) (types.Header, types.BlockID, *ABCIResponses) {
block := makeBlock(state, height)
abciResponses := &ABCIResponses{
@ -442,7 +442,7 @@ func makeHeaderPartsResponsesValPubKeyChange(state State, height int64,
}
func makeHeaderPartsResponsesValPowerChange(state State, height int64,
power int64) (*types.Header, types.BlockID, *ABCIResponses) {
power int64) (types.Header, types.BlockID, *ABCIResponses) {
block := makeBlock(state, height)
abciResponses := &ABCIResponses{
@ -463,7 +463,7 @@ func makeHeaderPartsResponsesValPowerChange(state State, height int64,
}
func makeHeaderPartsResponsesParams(state State, height int64,
params types.ConsensusParams) (*types.Header, types.BlockID, *ABCIResponses) {
params types.ConsensusParams) (types.Header, types.BlockID, *ABCIResponses) {
block := makeBlock(state, height)
abciResponses := &ABCIResponses{
@ -476,14 +476,3 @@ type paramsChangeTestCase struct {
height int64
params types.ConsensusParams
}
func makeHeaderPartsResults(state State, height int64,
results []*abci.ResponseDeliverTx) (*types.Header, types.BlockID, *ABCIResponses) {
block := makeBlock(state, height)
abciResponses := &ABCIResponses{
DeliverTx: results,
EndBlock: &abci.ResponseEndBlock{},
}
return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
}

+ 1
- 1
tools/tm-monitor/monitor/node.go View File

@ -134,7 +134,7 @@ func newBlockCallback(n *Node) em.EventCallbackFunc {
n.logger.Info("new block", "height", block.Height, "numTxs", block.NumTxs)
if n.blockCh != nil {
n.blockCh <- *block
n.blockCh <- block
}
}
}


+ 3
- 3
tools/tm-monitor/monitor/node_test.go View File

@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/go-amino"
amino "github.com/tendermint/go-amino"
crypto "github.com/tendermint/tendermint/crypto"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
em "github.com/tendermint/tendermint/tools/tm-monitor/eventmeter"
@ -33,11 +33,11 @@ func TestNodeNewBlockReceived(t *testing.T) {
defer n.Stop()
n.SendBlocksTo(blockCh)
blockHeader := &tmtypes.Header{Height: 5}
blockHeader := tmtypes.Header{Height: 5}
emMock.Call("eventCallback", &em.EventMetric{}, tmtypes.EventDataNewBlockHeader{blockHeader})
assert.Equal(t, int64(5), n.Height)
assert.Equal(t, *blockHeader, <-blockCh)
assert.Equal(t, blockHeader, <-blockCh)
}
func TestNodeNewBlockLatencyReceived(t *testing.T) {


+ 9
- 6
types/block.go View File

@ -17,8 +17,8 @@ import (
// TODO: add Version byte
type Block struct {
mtx sync.Mutex
*Header `json:"header"`
*Data `json:"data"`
Header `json:"header"`
Data `json:"data"`
Evidence EvidenceData `json:"evidence"`
LastCommit *Commit `json:"last_commit"`
}
@ -27,15 +27,15 @@ type Block struct {
// It populates the same set of fields validated by ValidateBasic
func MakeBlock(height int64, txs []Tx, commit *Commit) *Block {
block := &Block{
Header: &Header{
Header: Header{
Height: height,
Time: time.Now(),
NumTxs: int64(len(txs)),
},
LastCommit: commit,
Data: &Data{
Data: Data{
Txs: txs,
},
LastCommit: commit,
}
block.fillHeader()
return block
@ -43,6 +43,9 @@ func MakeBlock(height int64, txs []Tx, commit *Commit) *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...)
}
@ -98,7 +101,7 @@ func (b *Block) Hash() cmn.HexBytes {
b.mtx.Lock()
defer b.mtx.Unlock()
if b == nil || b.Header == nil || b.Data == nil || b.LastCommit == nil {
if b == nil || b.LastCommit == nil {
return nil
}
b.fillHeader()


+ 1
- 1
types/block_meta.go View File

@ -3,7 +3,7 @@ package types
// BlockMeta contains meta information about a block - namely, it's ID and Header.
type BlockMeta struct {
BlockID BlockID `json:"block_id"` // the block hash and partsethash
Header *Header `json:"header"` // The block's Header
Header Header `json:"header"` // The block's Header
}
// NewBlockMeta returns a new BlockMeta from the block and its blockParts.


+ 1
- 1
types/events.go View File

@ -69,7 +69,7 @@ type EventDataNewBlock struct {
// light weight event for benchmarking
type EventDataNewBlockHeader struct {
Header *Header `json:"header"`
Header Header `json:"header"`
}
// All txs fire EventDataTx


Loading…
Cancel
Save