diff --git a/core/block.go b/core/block.go new file mode 100644 index 00000000..3eb57f06 --- /dev/null +++ b/core/block.go @@ -0,0 +1,7 @@ +package core + +import "math/big" + +type Block struct { + Number *big.Int +} diff --git a/core/blockchain.go b/core/blockchain.go new file mode 100644 index 00000000..2f32a59e --- /dev/null +++ b/core/blockchain.go @@ -0,0 +1,5 @@ +package core + +type Blockchain interface { + RegisterObserver(observer BlockchainObserver) +} diff --git a/core/blockchain_logging_observer.go b/core/blockchain_logging_observer.go new file mode 100644 index 00000000..4ea5ae9b --- /dev/null +++ b/core/blockchain_logging_observer.go @@ -0,0 +1,9 @@ +package core + +import "fmt" + +type BlockchainLoggingObserver struct{} + +func (blockchainObserver BlockchainLoggingObserver) NotifyBlockAdded(block Block) { + fmt.Println("Added block: %f", block.Number) +} diff --git a/core/blockchain_observer.go b/core/blockchain_observer.go new file mode 100644 index 00000000..6b8d466d --- /dev/null +++ b/core/blockchain_observer.go @@ -0,0 +1,5 @@ +package core + +type BlockchainObserver interface { + NotifyBlockAdded(Block) +} diff --git a/core/fake_blockchain_test.go b/core/fake_blockchain_test.go new file mode 100644 index 00000000..fbc71bc0 --- /dev/null +++ b/core/fake_blockchain_test.go @@ -0,0 +1,48 @@ +package core_test + +import ( + "math/big" + + "github.com/8thlight/vulcanizedb/core" + "github.com/8thlight/vulcanizedb/fakes" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("The fake blockchain", func() { + + It("lets the only observer know when a block was added", func() { + blockchain := fakes.Blockchain{} + blockchainObserver := &fakes.BlockchainObserver{} + blockchain.RegisterObserver(blockchainObserver) + + blockchain.AddBlock(core.Block{}) + + Expect(blockchainObserver.WasToldBlockAdded()).Should(Equal(true)) + }) + + It("lets the second observer know when a block was added", func() { + blockchain := fakes.Blockchain{} + blockchainObserverOne := &fakes.BlockchainObserver{} + blockchainObserverTwo := &fakes.BlockchainObserver{} + blockchain.RegisterObserver(blockchainObserverOne) + blockchain.RegisterObserver(blockchainObserverTwo) + + blockchain.AddBlock(core.Block{}) + + Expect(blockchainObserverTwo.WasToldBlockAdded()).Should(Equal(true)) + }) + + It("passes the added block to the observer", func() { + blockchain := fakes.Blockchain{} + blockchainObserver := &fakes.BlockchainObserver{} + blockchain.RegisterObserver(blockchainObserver) + + blockchain.AddBlock(core.Block{Number: big.NewInt(123)}) + + Expect(blockchainObserver.LastAddedBlock.Number).ShouldNot(BeNil()) + Expect(blockchainObserver.LastAddedBlock.Number).Should(Equal(big.NewInt(123))) + }) + +}) diff --git a/core/geth_blockchain.go b/core/geth_blockchain.go new file mode 100644 index 00000000..177314f6 --- /dev/null +++ b/core/geth_blockchain.go @@ -0,0 +1,14 @@ +package core + +import "fmt" + +type GethBlockchain struct{} + +func NewGethBlockchain() *GethBlockchain { + fmt.Println("Creating Gethblockchain") + return &GethBlockchain{} +} + +func (blockchain *GethBlockchain) RegisterObserver(_ BlockchainObserver) { + +} diff --git a/fakes/blockchain.go b/fakes/blockchain.go new file mode 100644 index 00000000..3c6c08ac --- /dev/null +++ b/fakes/blockchain.go @@ -0,0 +1,19 @@ +package fakes + +import ( + "github.com/8thlight/vulcanizedb/core" +) + +type Blockchain struct { + observers []core.BlockchainObserver +} + +func (blockchain *Blockchain) RegisterObserver(observer core.BlockchainObserver) { + blockchain.observers = append(blockchain.observers, observer) +} + +func (blockchain *Blockchain) AddBlock(block core.Block) { + for _, observer := range blockchain.observers { + observer.NotifyBlockAdded(block) + } +} diff --git a/fakes/blockchain_observer.go b/fakes/blockchain_observer.go new file mode 100644 index 00000000..386e699d --- /dev/null +++ b/fakes/blockchain_observer.go @@ -0,0 +1,19 @@ +package fakes + +import ( + "github.com/8thlight/vulcanizedb/core" +) + +type BlockchainObserver struct { + wasToldBlockAdded bool + LastAddedBlock core.Block +} + +func (blockchainObserver *BlockchainObserver) WasToldBlockAdded() bool { + return blockchainObserver.wasToldBlockAdded +} + +func (blockchainObserver *BlockchainObserver) NotifyBlockAdded(block core.Block) { + blockchainObserver.LastAddedBlock = block + blockchainObserver.wasToldBlockAdded = true +} diff --git a/main.go b/main.go index d2c4e91e..65411828 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,13 @@ package main -import "fmt" +import ( + "fmt" + + "github.com/8thlight/vulcanizedb/core" +) func main() { - fmt.Println("Hello world") + fmt.Println("Starting connection") + var blockchain core.Blockchain = core.NewGethBlockchain() + blockchain.RegisterObserver(core.BlockchainLoggingObserver{}) }