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
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

View File

@ -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
}