Fixed fakeChain

This commit is contained in:
Jakub Matys 2018-10-04 10:43:22 +02:00
parent 37c66e81ba
commit 5fc74eca31
2 changed files with 19 additions and 27 deletions

View File

@ -2,37 +2,27 @@
package sync package sync
import ( import "blockbook/bchain"
"blockbook/bchain"
"errors"
)
type fakeBlockChain struct { type fakeBlockChain struct {
bchain.BlockChain bchain.BlockChain
returnFakes bool returnFakes bool
fakeBlocks map[uint32]BlockID fakeBlocks map[uint32]BlockID
fakeBestHeight uint32 bestHeight uint32
} }
func (c *fakeBlockChain) GetBestBlockHash() (v string, err error) { func (c *fakeBlockChain) GetBestBlockHash() (v string, err error) {
if !c.returnFakes { return c.GetBlockHash(c.bestHeight)
return c.BlockChain.GetBestBlockHash()
}
if b, found := c.fakeBlocks[c.fakeBestHeight]; found {
return b.Hash, nil
} else {
return "", errors.New("Not found")
}
} }
func (c *fakeBlockChain) GetBestBlockHeight() (v uint32, err error) { func (c *fakeBlockChain) GetBestBlockHeight() (v uint32, err error) {
if !c.returnFakes { return c.bestHeight, nil
return c.BlockChain.GetBestBlockHeight()
}
return c.fakeBestHeight, nil
} }
func (c *fakeBlockChain) GetBlockHash(height uint32) (v string, err error) { func (c *fakeBlockChain) GetBlockHash(height uint32) (v string, err error) {
if height > c.bestHeight {
return "", bchain.ErrBlockNotFound
}
if c.returnFakes { if c.returnFakes {
if b, found := c.fakeBlocks[height]; found { if b, found := c.fakeBlocks[height]; found {
return b.Hash, nil 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) { 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 c.returnFakes {
if hash == "" && height > 0 { if hash == "" && height > 0 {
var err error var err error

View File

@ -5,7 +5,6 @@ package sync
import ( import (
"blockbook/bchain" "blockbook/bchain"
"blockbook/db" "blockbook/db"
"errors"
"fmt" "fmt"
"math/big" "math/big"
"os" "os"
@ -154,8 +153,8 @@ func verifyTransactionsXXX(t *testing.T, d *db.RocksDB, rng Range, addr2txs map[
func getFakeBlocks(h *TestHandler, rng Range) []BlockID { func getFakeBlocks(h *TestHandler, rng Range) []BlockID {
blks := make([]BlockID, 0, rng.Upper-rng.Lower+1) blks := make([]BlockID, 0, rng.Upper-rng.Lower+1)
for _, b := range h.TestData.HandleFork.FakeBlocks { for i := rng.Lower; i <= rng.Upper; i++ {
if b.Height >= rng.Lower && b.Height <= rng.Upper { if b, found := h.TestData.HandleFork.FakeBlocks[i]; found {
blks = append(blks, b) 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) { func makeFakeChain(chain bchain.BlockChain, blks []BlockID, upper uint32) (*fakeBlockChain, error) {
if blks[len(blks)-1].Height != upper { 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)) mBlks := make(map[uint32]BlockID, len(blks))
for i := 0; i < len(blks); i++ { for i := 0; i < len(blks); i++ {
mBlks[blks[i].Height] = blks[i] mBlks[blks[i].Height] = blks[i]
} }
return &fakeBlockChain{ return &fakeBlockChain{
BlockChain: chain, BlockChain: chain,
returnFakes: true, returnFakes: true,
fakeBlocks: mBlks, fakeBlocks: mBlks,
fakeBestHeight: upper, bestHeight: upper,
}, nil }, nil
} }