This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from 8thlight/refactor-to-listener
Refactor to use listener
- Loading branch information
Showing
9 changed files
with
145 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package core | ||
|
||
type Blockchain interface { | ||
RegisterObserver(observer BlockchainObserver) | ||
SubscribeToEvents() | ||
SubscribeToBlocks(blocks chan Block) | ||
StartListening() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package core | ||
|
||
type BlockchainListener struct { | ||
inputBlocks chan Block | ||
blockchain Blockchain | ||
observers []BlockchainObserver | ||
} | ||
|
||
func NewBlockchainListener(blockchain Blockchain, observers []BlockchainObserver) BlockchainListener { | ||
inputBlocks := make(chan 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 Block) { | ||
for _, observer := range listener.observers { | ||
observer.NotifyBlockAdded(block) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package core_test | ||
|
||
import ( | ||
"github.com/8thlight/vulcanizedb/core" | ||
"github.com/8thlight/vulcanizedb/fakes" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Blockchain listeners", func() { | ||
|
||
It("starts with no blocks", func(done Done) { | ||
observer := fakes.NewFakeBlockchainObserverTwo() | ||
blockchain := &fakes.Blockchain{} | ||
|
||
core.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer}) | ||
|
||
Expect(len(observer.CurrentBlocks)).To(Equal(0)) | ||
close(done) | ||
}, 1) | ||
|
||
It("sees when one block was added", func(done Done) { | ||
observer := fakes.NewFakeBlockchainObserverTwo() | ||
blockchain := &fakes.Blockchain{} | ||
listener := core.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer}) | ||
go listener.Start() | ||
|
||
go blockchain.AddBlock(core.Block{Number: 123}) | ||
|
||
wasObserverNotified := <-observer.WasNotified | ||
Expect(wasObserverNotified).To(BeTrue()) | ||
Expect(len(observer.CurrentBlocks)).To(Equal(1)) | ||
addedBlock := observer.CurrentBlocks[0] | ||
Expect(addedBlock.Number).To(Equal(int64(123))) | ||
close(done) | ||
}, 1) | ||
|
||
It("sees a second block", func(done Done) { | ||
observer := fakes.NewFakeBlockchainObserverTwo() | ||
blockchain := &fakes.Blockchain{} | ||
listener := core.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer}) | ||
go listener.Start() | ||
|
||
go blockchain.AddBlock(core.Block{Number: 123}) | ||
<-observer.WasNotified | ||
go blockchain.AddBlock(core.Block{Number: 456}) | ||
wasObserverNotified := <-observer.WasNotified | ||
|
||
Expect(wasObserverNotified).To(BeTrue()) | ||
Expect(len(observer.CurrentBlocks)).To(Equal(2)) | ||
addedBlock := observer.CurrentBlocks[1] | ||
Expect(addedBlock.Number).To(Equal(int64(456))) | ||
close(done) | ||
}, 1) | ||
|
||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,17 @@ | ||
package fakes | ||
|
||
import ( | ||
"github.com/8thlight/vulcanizedb/core" | ||
) | ||
import "github.com/8thlight/vulcanizedb/core" | ||
|
||
type Blockchain struct { | ||
observers []core.BlockchainObserver | ||
outputBlocks chan core.Block | ||
} | ||
|
||
func (blockchain *Blockchain) RegisterObserver(observer core.BlockchainObserver) { | ||
blockchain.observers = append(blockchain.observers, observer) | ||
func (blockchain *Blockchain) SubscribeToBlocks(outputBlocks chan core.Block) { | ||
blockchain.outputBlocks = outputBlocks | ||
} | ||
|
||
func (blockchain *Blockchain) AddBlock(block core.Block) { | ||
for _, observer := range blockchain.observers { | ||
observer.NotifyBlockAdded(block) | ||
} | ||
func (blockchain Blockchain) AddBlock(block core.Block) { | ||
blockchain.outputBlocks <- block | ||
} | ||
|
||
func (_ *Blockchain) SubscribeToEvents() {} | ||
func (*Blockchain) StartListening() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
package fakes | ||
|
||
import ( | ||
"github.com/8thlight/vulcanizedb/core" | ||
) | ||
import "github.com/8thlight/vulcanizedb/core" | ||
|
||
type BlockchainObserver struct { | ||
wasToldBlockAdded bool | ||
blocks []core.Block | ||
CurrentBlocks []core.Block | ||
WasNotified chan bool | ||
} | ||
|
||
func (blockchainObserver *BlockchainObserver) WasToldBlockAdded() bool { | ||
return blockchainObserver.wasToldBlockAdded | ||
func (observer *BlockchainObserver) LastBlock() core.Block { | ||
return observer.CurrentBlocks[len(observer.CurrentBlocks)-1] | ||
} | ||
|
||
func (blockchainObserver *BlockchainObserver) NotifyBlockAdded(block core.Block) { | ||
blockchainObserver.blocks = append(blockchainObserver.blocks, block) | ||
blockchainObserver.wasToldBlockAdded = true | ||
func NewFakeBlockchainObserverTwo() *BlockchainObserver { | ||
return &BlockchainObserver{ | ||
WasNotified: make(chan bool), | ||
} | ||
} | ||
|
||
func (observer *BlockchainObserver) LastAddedBlock() core.Block { | ||
return observer.blocks[len(observer.blocks)-1] | ||
func (observer *BlockchainObserver) NotifyBlockAdded(block core.Block) { | ||
observer.CurrentBlocks = append(observer.CurrentBlocks, block) | ||
observer.WasNotified <- true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters