Start introducing domain concepts

* Potential observers include a logger and/or an observer that writes to the DB
This commit is contained in:
Eric Meyer 2017-10-23 10:56:29 -05:00
parent 0ebe0c253f
commit de2cb3b5cc
9 changed files with 134 additions and 2 deletions

7
core/block.go Normal file
View File

@ -0,0 +1,7 @@
package core
import "math/big"
type Block struct {
Number *big.Int
}

5
core/blockchain.go Normal file
View File

@ -0,0 +1,5 @@
package core
type Blockchain interface {
RegisterObserver(observer BlockchainObserver)
}

View File

@ -0,0 +1,9 @@
package core
import "fmt"
type BlockchainLoggingObserver struct{}
func (blockchainObserver BlockchainLoggingObserver) NotifyBlockAdded(block Block) {
fmt.Println("Added block: %f", block.Number)
}

View File

@ -0,0 +1,5 @@
package core
type BlockchainObserver interface {
NotifyBlockAdded(Block)
}

View File

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

14
core/geth_blockchain.go Normal file
View File

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

19
fakes/blockchain.go Normal file
View File

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

View File

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

10
main.go
View File

@ -1,7 +1,13 @@
package main package main
import "fmt" import (
"fmt"
"github.com/8thlight/vulcanizedb/core"
)
func main() { func main() {
fmt.Println("Hello world") fmt.Println("Starting connection")
var blockchain core.Blockchain = core.NewGethBlockchain()
blockchain.RegisterObserver(core.BlockchainLoggingObserver{})
} }