2017-10-23 15:56:29 +00:00
|
|
|
package fakes
|
|
|
|
|
2017-11-06 18:53:43 +00:00
|
|
|
import "github.com/8thlight/vulcanizedb/pkg/core"
|
2017-10-23 15:56:29 +00:00
|
|
|
|
|
|
|
type Blockchain struct {
|
2017-11-06 20:36:12 +00:00
|
|
|
blocks map[int64]core.Block
|
|
|
|
blocksChannel chan core.Block
|
2017-11-03 13:54:32 +00:00
|
|
|
WasToldToStop bool
|
2017-10-23 15:56:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 20:36:12 +00:00
|
|
|
func NewBlockchain() *Blockchain {
|
|
|
|
return &Blockchain{blocks: make(map[int64]core.Block)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockchainWithBlocks(blocks []core.Block) *Blockchain {
|
|
|
|
blockNumberToBlocks := make(map[int64]core.Block)
|
|
|
|
for _, block := range blocks {
|
|
|
|
blockNumberToBlocks[block.Number] = block
|
|
|
|
}
|
|
|
|
return &Blockchain{
|
|
|
|
blocks: blockNumberToBlocks,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blockchain *Blockchain) GetBlockByNumber(blockNumber int64) core.Block {
|
|
|
|
return blockchain.blocks[blockNumber]
|
|
|
|
}
|
|
|
|
|
2017-11-02 11:41:24 +00:00
|
|
|
func (blockchain *Blockchain) SubscribeToBlocks(outputBlocks chan core.Block) {
|
2017-11-06 20:36:12 +00:00
|
|
|
blockchain.blocksChannel = outputBlocks
|
2017-10-23 15:56:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 20:36:12 +00:00
|
|
|
func (blockchain *Blockchain) AddBlock(block core.Block) {
|
|
|
|
blockchain.blocks[block.Number] = block
|
|
|
|
blockchain.blocksChannel <- block
|
2017-10-23 15:56:29 +00:00
|
|
|
}
|
2017-10-23 18:58:33 +00:00
|
|
|
|
2017-11-02 11:41:24 +00:00
|
|
|
func (*Blockchain) StartListening() {}
|
2017-11-03 13:54:32 +00:00
|
|
|
|
|
|
|
func (blockchain *Blockchain) StopListening() {
|
|
|
|
blockchain.WasToldToStop = true
|
|
|
|
}
|