From 5fc74eca31bff78004d6e3f64eec57b84acb014e Mon Sep 17 00:00:00 2001 From: Jakub Matys Date: Thu, 4 Oct 2018 10:43:22 +0200 Subject: [PATCH] Fixed fakeChain --- tests/sync/fakechain.go | 31 ++++++++++++------------------- tests/sync/handlefork.go | 15 +++++++-------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/tests/sync/fakechain.go b/tests/sync/fakechain.go index 67635cfb..6d399d1f 100644 --- a/tests/sync/fakechain.go +++ b/tests/sync/fakechain.go @@ -2,37 +2,27 @@ package sync -import ( - "blockbook/bchain" - "errors" -) +import "blockbook/bchain" type fakeBlockChain struct { bchain.BlockChain - returnFakes bool - fakeBlocks map[uint32]BlockID - fakeBestHeight uint32 + returnFakes bool + fakeBlocks map[uint32]BlockID + bestHeight uint32 } func (c *fakeBlockChain) GetBestBlockHash() (v string, err error) { - if !c.returnFakes { - return c.BlockChain.GetBestBlockHash() - } - if b, found := c.fakeBlocks[c.fakeBestHeight]; found { - return b.Hash, nil - } else { - return "", errors.New("Not found") - } + return c.GetBlockHash(c.bestHeight) } func (c *fakeBlockChain) GetBestBlockHeight() (v uint32, err error) { - if !c.returnFakes { - return c.BlockChain.GetBestBlockHeight() - } - return c.fakeBestHeight, nil + return c.bestHeight, nil } func (c *fakeBlockChain) GetBlockHash(height uint32) (v string, err error) { + if height > c.bestHeight { + return "", bchain.ErrBlockNotFound + } if c.returnFakes { if b, found := c.fakeBlocks[height]; found { return b.Hash, nil @@ -42,6 +32,9 @@ func (c *fakeBlockChain) GetBlockHash(height uint32) (v string, err error) { } func (c *fakeBlockChain) GetBlock(hash string, height uint32) (*bchain.Block, error) { + if height > 0 && height > c.bestHeight { + return nil, bchain.ErrBlockNotFound + } if c.returnFakes { if hash == "" && height > 0 { var err error diff --git a/tests/sync/handlefork.go b/tests/sync/handlefork.go index f30ede09..07017bbf 100644 --- a/tests/sync/handlefork.go +++ b/tests/sync/handlefork.go @@ -5,7 +5,6 @@ package sync import ( "blockbook/bchain" "blockbook/db" - "errors" "fmt" "math/big" "os" @@ -154,8 +153,8 @@ func verifyTransactionsXXX(t *testing.T, d *db.RocksDB, rng Range, addr2txs map[ func getFakeBlocks(h *TestHandler, rng Range) []BlockID { blks := make([]BlockID, 0, rng.Upper-rng.Lower+1) - for _, b := range h.TestData.HandleFork.FakeBlocks { - if b.Height >= rng.Lower && b.Height <= rng.Upper { + for i := rng.Lower; i <= rng.Upper; i++ { + if b, found := h.TestData.HandleFork.FakeBlocks[i]; found { blks = append(blks, b) } } @@ -174,17 +173,17 @@ func getRealBlocks(h *TestHandler, rng Range) []BlockID { func makeFakeChain(chain bchain.BlockChain, blks []BlockID, upper uint32) (*fakeBlockChain, error) { if blks[len(blks)-1].Height != upper { - return nil, errors.New("Range must end with fake block in order to emulate fork") + return nil, fmt.Errorf("Range must end with fake block in order to emulate fork [%d != %d]", blks[len(blks)-1].Height, upper) } mBlks := make(map[uint32]BlockID, len(blks)) for i := 0; i < len(blks); i++ { mBlks[blks[i].Height] = blks[i] } return &fakeBlockChain{ - BlockChain: chain, - returnFakes: true, - fakeBlocks: mBlks, - fakeBestHeight: upper, + BlockChain: chain, + returnFakes: true, + fakeBlocks: mBlks, + bestHeight: upper, }, nil }