Filled in ContainsBlock for Blockchain Service (#512)

* Revert "Add Skip Coverage Condition in Coverage.sh (#416)"

This reverts commit 72a5dd1cf4.

* add coverage.sh back

* filled in contains block to not always return false

* fixed lint

* lint
This commit is contained in:
terence tsao 2018-09-13 08:24:18 -07:00 committed by Nishant Das
parent 15a6545f64
commit 593c1ec9fc
5 changed files with 31 additions and 15 deletions

View File

@ -123,8 +123,8 @@ func (c *ChainService) SaveBlock(block *types.Block) error {
// ContainsBlock checks if a block for the hash exists in the chain.
// This method must be safe to call from a goroutine.
func (c *ChainService) ContainsBlock(h [32]byte) bool {
return false
func (c *ChainService) ContainsBlock(h [32]byte) (bool, error) {
return c.chain.hasBlock(h)
}
// CurrentCrystallizedState of the canonical chain.

View File

@ -97,7 +97,11 @@ func TestStartStop(t *testing.T) {
if len(chainService.CurrentCrystallizedState().Validators()) != params.BootstrappedValidatorsCount {
t.Errorf("incorrect default validator size")
}
if chainService.ContainsBlock([32]byte{}) {
blockExists, err := chainService.ContainsBlock([32]byte{})
if err != nil {
t.Fatalf("unable to check if block exists: %v", err)
}
if blockExists {
t.Errorf("chain is not empty")
}
hasState, err := chainService.HasStoredState()

View File

@ -15,7 +15,7 @@ import (
var log = logrus.WithField("prefix", "sync")
type chainService interface {
ContainsBlock(h [32]byte) bool
ContainsBlock(h [32]byte) (bool, error)
HasStoredState() (bool, error)
IncomingBlockFeed() *event.Feed
CheckForCanonicalBlockBySlot(slotnumber uint64) (bool, error)
@ -110,15 +110,20 @@ func (ss *Service) BlockAnnouncementFeed() *event.Feed {
// ReceiveBlockHash accepts a block hash.
// New hashes are forwarded to other peers in the network (unimplemented), and
// the contents of the block are requested if the local chain doesn't have the block.
func (ss *Service) ReceiveBlockHash(data *pb.BeaconBlockHashAnnounce, peer p2p.Peer) {
func (ss *Service) ReceiveBlockHash(data *pb.BeaconBlockHashAnnounce, peer p2p.Peer) error {
var h [32]byte
copy(h[:], data.Hash[:32])
if ss.chainService.ContainsBlock(h) {
return
blockExists, err := ss.chainService.ContainsBlock(h)
if err != nil {
return err
}
if blockExists {
return nil
}
log.WithField("blockHash", fmt.Sprintf("0x%x", h)).Debug("Received incoming block hash, requesting full block data from sender")
// Request the full block data from peer that sent the block hash.
ss.p2p.Send(&pb.BeaconBlockRequest{Hash: h[:]}, peer)
return nil
}
// run handles incoming block sync.
@ -143,12 +148,14 @@ func (ss *Service) run() {
log.Error("Received malformed beacon block hash announcement p2p message")
continue
}
ss.ReceiveBlockHash(data, msg.Peer)
if err := ss.ReceiveBlockHash(data, msg.Peer); err != nil {
log.Errorf("Received block hash failed: %v", err)
}
case msg := <-ss.blockBuf:
response, ok := msg.Data.(*pb.BeaconBlockResponse)
// TODO: Handle this at p2p layer.
if !ok {
log.Errorf("Received malformed beacon block p2p message")
log.Error("Received malformed beacon block p2p message")
continue
}
block := types.NewBlock(response.Block)
@ -156,7 +163,12 @@ func (ss *Service) run() {
if err != nil {
log.Errorf("Could not hash received block: %v", err)
}
if ss.chainService.ContainsBlock(h) {
blockExists, err := ss.chainService.ContainsBlock(h)
if err != nil {
log.Errorf("Can not check for block in DB: %v", err)
continue
}
if blockExists {
continue
}
log.WithField("blockHash", fmt.Sprintf("0x%x", h)).Debug("Sending newly received block to subscribers")

View File

@ -40,8 +40,8 @@ type mockChainService struct {
getError bool
}
func (ms *mockChainService) ContainsBlock(h [32]byte) bool {
return false
func (ms *mockChainService) ContainsBlock(h [32]byte) (bool, error) {
return false, nil
}
func (ms *mockChainService) HasStoredState() (bool, error) {
@ -304,8 +304,8 @@ type mockEmptyChainService struct {
hasStoredState bool
}
func (ms *mockEmptyChainService) ContainsBlock(h [32]byte) bool {
return false
func (ms *mockEmptyChainService) ContainsBlock(h [32]byte) (bool, error) {
return false, nil
}
func (ms *mockEmptyChainService) HasStoredState() (bool, error) {

View File

@ -36,7 +36,7 @@ type BlockChainService interface {
ProcessedBlockHashes() [][32]byte
ProcessBlock(b *Block)
HasStoredState() (bool, error)
ContainsBlock(h [32]byte) bool
ContainsBlock(h [32]byte) (bool, error)
SaveBlock(b *Block) error
}