From 5f794d14fbc715312a5dbe0635afde06c3a6f472 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Mon, 3 Nov 2014 15:50:23 -0800 Subject: [PATCH] nil bugs --- blocks/part_set.go | 16 ++++++++++++---- consensus/reactor.go | 4 ++-- consensus/state.go | 4 ++-- consensus/vote_set.go | 9 ++++++++- state/state_test.go | 6 +++--- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/blocks/part_set.go b/blocks/part_set.go index c9240243b..fd39a24e7 100644 --- a/blocks/part_set.go +++ b/blocks/part_set.go @@ -101,14 +101,14 @@ func (psh PartSetHeader) WriteTo(w io.Writer) (n int64, err error) { return } -func (psh PartSetHeader) IsZero() bool { - return psh.Total == 0 -} - func (psh PartSetHeader) String() string { return fmt.Sprintf("PartSet{%X/%v}", psh.Hash, psh.Total) } +func (psh PartSetHeader) IsZero() bool { + return psh.Total == 0 +} + func (psh PartSetHeader) Equals(other PartSetHeader) bool { return psh.Total == other.Total && bytes.Equal(psh.Hash, other.Hash) } @@ -179,6 +179,14 @@ func (ps *PartSet) Header() PartSetHeader { } } +func (ps *PartSet) HasHeader(header PartSetHeader) bool { + if ps == nil { + return false + } else { + return ps.Header().Equals(header) + } +} + func (ps *PartSet) BitArray() BitArray { ps.mtx.Lock() defer ps.mtx.Unlock() diff --git a/consensus/reactor.go b/consensus/reactor.go index 70761364a..76d6a3200 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -252,7 +252,7 @@ OUTER_LOOP: // Send proposal Block parts? // NOTE: if we or peer is at RoundStepCommit*, the round // won't necessarily match, but that's OK. - if rs.ProposalBlockParts.Header().Equals(prs.ProposalBlockParts) { + if rs.ProposalBlockParts.HasHeader(prs.ProposalBlockParts) { if index, ok := rs.ProposalBlockParts.BitArray().Sub( prs.ProposalBlockBitArray).PickRandom(); ok { msg := &PartMessage{ @@ -282,7 +282,7 @@ OUTER_LOOP: } // Send proposal POL parts? - if rs.ProposalPOLParts.Header().Equals(prs.ProposalPOLParts) { + if rs.ProposalPOLParts.HasHeader(prs.ProposalPOLParts) { if index, ok := rs.ProposalPOLParts.BitArray().Sub( prs.ProposalPOLBitArray).PickRandom(); ok { msg := &PartMessage{ diff --git a/consensus/state.go b/consensus/state.go index 3bb6618ad..1f51107c1 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -676,7 +676,7 @@ func (cs *ConsensusState) RunActionCommit(height uint32) { // If we don't have the block being committed, set up to get it. if !cs.ProposalBlock.HashesTo(hash) { - if !cs.ProposalBlockParts.Header().Equals(partsHeader) { + if !cs.ProposalBlockParts.HasHeader(partsHeader) { // We're getting the wrong block. // Set up ProposalBlockParts and keep waiting. cs.ProposalBlock = nil @@ -720,7 +720,7 @@ func (cs *ConsensusState) TryFinalizeCommit(height uint32) bool { if !cs.ProposalBlock.HashesTo(hash) { Panicf("Expected ProposalBlock to hash to commit hash") } - if !cs.ProposalBlockParts.Header().Equals(header) { + if !cs.ProposalBlockParts.HasHeader(header) { Panicf("Expected ProposalBlockParts header to be commit header") } diff --git a/consensus/vote_set.go b/consensus/vote_set.go index 845e90c32..d6b94fdf3 100644 --- a/consensus/vote_set.go +++ b/consensus/vote_set.go @@ -55,7 +55,11 @@ func NewVoteSet(height uint32, round uint16, type_ byte, vset *state.ValidatorSe } func (vs *VoteSet) Size() uint { - return vs.vset.Size() + if vs == nil { + return 0 + } else { + return vs.vset.Size() + } } // True if added, false if not. @@ -132,6 +136,9 @@ func (vs *VoteSet) AddFromCommits(commits *VoteSet) { } func (vs *VoteSet) BitArray() BitArray { + if vs == nil { + return BitArray{} + } vs.mtx.Lock() defer vs.mtx.Unlock() return vs.votesBitArray.Copy() diff --git a/state/state_test.go b/state/state_test.go index f6f88378f..85d4bdc5b 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -85,7 +85,7 @@ func TestGenesisSaveLoad(t *testing.T) { s0, _ := randGenesisState(10, 5) // Mutate the state to append one empty block. block := &Block{ - Header: Header{ + Header: &Header{ Network: Config.Network, Height: 1, Time: s0.LastBlockTime.Add(time.Minute), @@ -94,8 +94,8 @@ func TestGenesisSaveLoad(t *testing.T) { LastBlockParts: s0.LastBlockParts, StateHash: nil, }, - Validation: Validation{}, - Data: Data{ + Validation: &Validation{}, + Data: &Data{ Txs: []Tx{}, }, }