ipld-eth-server/blockchain_listener/blockchain_listener.go
Eric Meyer 0262a99321 Close subscription in integration test
* This was causing issues in future tests,
   since the subscription was causing blocks
   and transactions to be added to the DB
2017-11-03 08:56:58 -05:00

38 lines
955 B
Go

package blockchain_listener
import "github.com/8thlight/vulcanizedb/core"
type BlockchainListener struct {
inputBlocks chan core.Block
blockchain core.Blockchain
observers []core.BlockchainObserver
}
func NewBlockchainListener(blockchain core.Blockchain, observers []core.BlockchainObserver) BlockchainListener {
inputBlocks := make(chan core.Block, 10)
blockchain.SubscribeToBlocks(inputBlocks)
listener := BlockchainListener{
inputBlocks: inputBlocks,
blockchain: blockchain,
observers: observers,
}
return listener
}
func (listener BlockchainListener) Start() {
go listener.blockchain.StartListening()
for block := range listener.inputBlocks {
listener.notifyObservers(block)
}
}
func (listener BlockchainListener) notifyObservers(block core.Block) {
for _, observer := range listener.observers {
observer.NotifyBlockAdded(block)
}
}
func (listener BlockchainListener) Stop() {
listener.blockchain.StopListening()
}