forked from cerc-io/ipld-eth-server
Start introducing domain concepts
* Potential observers include a logger and/or an observer that writes to the DB
This commit is contained in:
parent
0ebe0c253f
commit
de2cb3b5cc
7
core/block.go
Normal file
7
core/block.go
Normal file
@ -0,0 +1,7 @@
|
||||
package core
|
||||
|
||||
import "math/big"
|
||||
|
||||
type Block struct {
|
||||
Number *big.Int
|
||||
}
|
5
core/blockchain.go
Normal file
5
core/blockchain.go
Normal file
@ -0,0 +1,5 @@
|
||||
package core
|
||||
|
||||
type Blockchain interface {
|
||||
RegisterObserver(observer BlockchainObserver)
|
||||
}
|
9
core/blockchain_logging_observer.go
Normal file
9
core/blockchain_logging_observer.go
Normal 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)
|
||||
}
|
5
core/blockchain_observer.go
Normal file
5
core/blockchain_observer.go
Normal file
@ -0,0 +1,5 @@
|
||||
package core
|
||||
|
||||
type BlockchainObserver interface {
|
||||
NotifyBlockAdded(Block)
|
||||
}
|
48
core/fake_blockchain_test.go
Normal file
48
core/fake_blockchain_test.go
Normal 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
14
core/geth_blockchain.go
Normal 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
19
fakes/blockchain.go
Normal 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)
|
||||
}
|
||||
}
|
19
fakes/blockchain_observer.go
Normal file
19
fakes/blockchain_observer.go
Normal 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
10
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{})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user