forked from cerc-io/ipld-eth-server
commit
cf5f2f28db
@ -5,12 +5,7 @@ import (
|
||||
|
||||
"fmt"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/blockchain_listener"
|
||||
"github.com/8thlight/vulcanizedb/config"
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/8thlight/vulcanizedb/geth"
|
||||
"github.com/8thlight/vulcanizedb/observers"
|
||||
"github.com/jmoiron/sqlx"
|
||||
do "gopkg.in/godo.v2"
|
||||
)
|
||||
|
||||
@ -22,29 +17,12 @@ func parseEnvironment(context *do.Context) string {
|
||||
return environment
|
||||
}
|
||||
|
||||
func startBlockchainListener(cfg config.Config) {
|
||||
fmt.Println("Client Path ", cfg.Client.IPCPath)
|
||||
blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath)
|
||||
loggingObserver := observers.BlockchainLoggingObserver{}
|
||||
connectString := config.DbConnectionString(cfg.Database)
|
||||
db, err := sqlx.Connect("postgres", connectString)
|
||||
if err != nil {
|
||||
log.Fatalf("Error connecting to DB: %v\n", err)
|
||||
}
|
||||
dbObserver := (observers.BlockchainDBObserver{Db: db})
|
||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{
|
||||
loggingObserver,
|
||||
dbObserver,
|
||||
})
|
||||
listener.Start()
|
||||
}
|
||||
|
||||
func tasks(p *do.Project) {
|
||||
|
||||
p.Task("run", nil, func(context *do.Context) {
|
||||
environment := parseEnvironment(context)
|
||||
cfg := config.NewConfig(environment)
|
||||
startBlockchainListener(cfg)
|
||||
context.Start(`go run main.go --environment={{.environment}}`,
|
||||
do.M{"environment": environment, "$in": "cmd/run"})
|
||||
})
|
||||
|
||||
p.Task("migrate", nil, func(context *do.Context) {
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
var _ = Describe("Blockchain listeners", func() {
|
||||
|
||||
It("starts with no blocks", func(done Done) {
|
||||
observer := fakes.NewFakeBlockchainObserverTwo()
|
||||
observer := fakes.NewFakeBlockchainObserver()
|
||||
blockchain := &fakes.Blockchain{}
|
||||
|
||||
blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
@ -21,7 +21,7 @@ var _ = Describe("Blockchain listeners", func() {
|
||||
}, 1)
|
||||
|
||||
It("sees when one block was added", func(done Done) {
|
||||
observer := fakes.NewFakeBlockchainObserverTwo()
|
||||
observer := fakes.NewFakeBlockchainObserver()
|
||||
blockchain := &fakes.Blockchain{}
|
||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
go listener.Start()
|
||||
@ -37,7 +37,7 @@ var _ = Describe("Blockchain listeners", func() {
|
||||
}, 1)
|
||||
|
||||
It("sees a second block", func(done Done) {
|
||||
observer := fakes.NewFakeBlockchainObserverTwo()
|
||||
observer := fakes.NewFakeBlockchainObserver()
|
||||
blockchain := &fakes.Blockchain{}
|
||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
go listener.Start()
|
||||
@ -55,7 +55,7 @@ var _ = Describe("Blockchain listeners", func() {
|
||||
}, 1)
|
||||
|
||||
It("stops listening", func(done Done) {
|
||||
observer := fakes.NewFakeBlockchainObserverTwo()
|
||||
observer := fakes.NewFakeBlockchainObserver()
|
||||
blockchain := &fakes.Blockchain{}
|
||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
go listener.Start()
|
||||
|
38
cmd/run/main.go
Normal file
38
cmd/run/main.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"flag"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/blockchain_listener"
|
||||
"github.com/8thlight/vulcanizedb/config"
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/8thlight/vulcanizedb/geth"
|
||||
"github.com/8thlight/vulcanizedb/observers"
|
||||
"github.com/8thlight/vulcanizedb/repositories"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
environment := flag.String("environment", "", "Environment name")
|
||||
flag.Parse()
|
||||
cfg := config.NewConfig(*environment)
|
||||
|
||||
fmt.Println("Client Path ", cfg.Client.IPCPath)
|
||||
blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath)
|
||||
loggingObserver := observers.BlockchainLoggingObserver{}
|
||||
connectString := config.DbConnectionString(cfg.Database)
|
||||
db, err := sqlx.Connect("postgres", connectString)
|
||||
if err != nil {
|
||||
log.Fatalf("Error connecting to DB: %v\n", err)
|
||||
}
|
||||
repository := repositories.NewPostgres(db)
|
||||
dbObserver := observers.NewBlockchainDbObserver(repository)
|
||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{
|
||||
loggingObserver,
|
||||
dbObserver,
|
||||
})
|
||||
listener.Start()
|
||||
}
|
@ -11,7 +11,7 @@ func (observer *BlockchainObserver) LastBlock() core.Block {
|
||||
return observer.CurrentBlocks[len(observer.CurrentBlocks)-1]
|
||||
}
|
||||
|
||||
func NewFakeBlockchainObserverTwo() *BlockchainObserver {
|
||||
func NewFakeBlockchainObserver() *BlockchainObserver {
|
||||
return &BlockchainObserver{
|
||||
WasNotified: make(chan bool),
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ var _ = Describe("Reading from the Geth blockchain", func() {
|
||||
var observer *fakes.BlockchainObserver
|
||||
|
||||
BeforeEach(func() {
|
||||
observer = fakes.NewFakeBlockchainObserverTwo()
|
||||
observer = fakes.NewFakeBlockchainObserver()
|
||||
blockchain := geth.NewGethBlockchain(RunTimePath() + "/test_data_dir/geth.ipc")
|
||||
observers := []core.BlockchainObserver{observer}
|
||||
listener = blockchain_listener.NewBlockchainListener(blockchain, observers)
|
||||
|
@ -3,13 +3,16 @@ package observers
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/8thlight/vulcanizedb/repositories"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type BlockchainDBObserver struct {
|
||||
Db *sqlx.DB
|
||||
type BlockchainDbObserver struct {
|
||||
repository repositories.Repository
|
||||
}
|
||||
|
||||
func (observer BlockchainDBObserver) NotifyBlockAdded(block core.Block) {
|
||||
repositories.NewPostgres(observer.Db).CreateBlock(block)
|
||||
func NewBlockchainDbObserver(repository repositories.Repository) BlockchainDbObserver {
|
||||
return BlockchainDbObserver{repository: repository}
|
||||
}
|
||||
|
||||
func (observer BlockchainDbObserver) NotifyBlockAdded(block core.Block) {
|
||||
observer.repository.CreateBlock(block)
|
||||
}
|
||||
|
@ -1,41 +1,23 @@
|
||||
package observers_test
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/config"
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/8thlight/vulcanizedb/observers"
|
||||
"github.com/8thlight/vulcanizedb/repositories"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var (
|
||||
_, filename, _, _ = runtime.Caller(0)
|
||||
)
|
||||
|
||||
var _ = Describe("Saving blocks to the database", func() {
|
||||
|
||||
var db *sqlx.DB
|
||||
var err error
|
||||
var repository *repositories.InMemory
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg := config.NewConfig("private")
|
||||
pgConfig := config.DbConnectionString(cfg.Database)
|
||||
db, err = sqlx.Connect("postgres", pgConfig)
|
||||
db.MustExec("DELETE FROM transactions")
|
||||
db.MustExec("DELETE FROM blocks")
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
db.Close()
|
||||
repository = repositories.NewInMemory()
|
||||
})
|
||||
|
||||
It("implements the observer interface", func() {
|
||||
var observer core.BlockchainObserver = observers.BlockchainDBObserver{Db: db}
|
||||
var observer core.BlockchainObserver = observers.NewBlockchainDbObserver(repository)
|
||||
Expect(observer).NotTo(BeNil())
|
||||
})
|
||||
|
||||
@ -45,10 +27,9 @@ var _ = Describe("Saving blocks to the database", func() {
|
||||
Transactions: []core.Transaction{{}},
|
||||
}
|
||||
|
||||
observer := observers.BlockchainDBObserver{Db: db}
|
||||
observer := observers.NewBlockchainDbObserver(repository)
|
||||
observer.NotifyBlockAdded(block)
|
||||
|
||||
repository := repositories.NewPostgres(db)
|
||||
savedBlock := repository.FindBlockByNumber(123)
|
||||
Expect(savedBlock).NotTo(BeNil())
|
||||
Expect(len(savedBlock.Transactions)).To(Equal(1))
|
||||
|
27
repositories/in_memory.go
Normal file
27
repositories/in_memory.go
Normal file
@ -0,0 +1,27 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
)
|
||||
|
||||
type InMemory struct {
|
||||
blocks map[int64]*core.Block
|
||||
}
|
||||
|
||||
func NewInMemory() *InMemory {
|
||||
return &InMemory{
|
||||
blocks: make(map[int64]*core.Block),
|
||||
}
|
||||
}
|
||||
|
||||
func (repository *InMemory) CreateBlock(block core.Block) {
|
||||
repository.blocks[block.Number] = &block
|
||||
}
|
||||
|
||||
func (repository *InMemory) BlockCount() int {
|
||||
return len(repository.blocks)
|
||||
}
|
||||
|
||||
func (repository *InMemory) FindBlockByNumber(blockNumber int64) *core.Block {
|
||||
return repository.blocks[blockNumber]
|
||||
}
|
141
repositories/in_memory_test.go
Normal file
141
repositories/in_memory_test.go
Normal file
@ -0,0 +1,141 @@
|
||||
package repositories_test
|
||||
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/8thlight/vulcanizedb/repositories"
|
||||
_ "github.com/lib/pq"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("The In Memory Repository", func() {
|
||||
|
||||
Describe("Saving blocks", func() {
|
||||
It("starts with no blocks", func() {
|
||||
count := buildRepository().BlockCount()
|
||||
Expect(count).Should(Equal(0))
|
||||
})
|
||||
|
||||
It("increments the block count", func() {
|
||||
block := core.Block{Number: 123}
|
||||
repository := buildRepository()
|
||||
|
||||
repository.CreateBlock(block)
|
||||
|
||||
Expect(repository.BlockCount()).To(Equal(1))
|
||||
})
|
||||
|
||||
It("saves the attributes of the block", func() {
|
||||
blockNumber := int64(123)
|
||||
gasLimit := int64(1000000)
|
||||
gasUsed := int64(10)
|
||||
blockHash := "x123"
|
||||
blockParentHash := "x456"
|
||||
blockNonce := "0x881db2ca900682e9a9"
|
||||
blockTime := int64(1508981640)
|
||||
uncleHash := "x789"
|
||||
blockSize := int64(1000)
|
||||
difficulty := int64(10)
|
||||
block := core.Block{
|
||||
Difficulty: difficulty,
|
||||
GasLimit: gasLimit,
|
||||
GasUsed: gasUsed,
|
||||
Hash: blockHash,
|
||||
Nonce: blockNonce,
|
||||
Number: blockNumber,
|
||||
ParentHash: blockParentHash,
|
||||
Size: blockSize,
|
||||
Time: blockTime,
|
||||
UncleHash: uncleHash,
|
||||
}
|
||||
|
||||
repository := buildRepository()
|
||||
repository.CreateBlock(block)
|
||||
|
||||
savedBlock := repository.FindBlockByNumber(blockNumber)
|
||||
Expect(savedBlock.Difficulty).To(Equal(difficulty))
|
||||
Expect(savedBlock.GasLimit).To(Equal(gasLimit))
|
||||
Expect(savedBlock.GasUsed).To(Equal(gasUsed))
|
||||
Expect(savedBlock.Hash).To(Equal(blockHash))
|
||||
Expect(savedBlock.Nonce).To(Equal(blockNonce))
|
||||
Expect(savedBlock.Number).To(Equal(blockNumber))
|
||||
Expect(savedBlock.ParentHash).To(Equal(blockParentHash))
|
||||
Expect(savedBlock.Size).To(Equal(blockSize))
|
||||
Expect(savedBlock.Time).To(Equal(blockTime))
|
||||
Expect(savedBlock.UncleHash).To(Equal(uncleHash))
|
||||
})
|
||||
|
||||
It("does not find a block when searching for a number that does not exist", func() {
|
||||
repository := buildRepository()
|
||||
|
||||
savedBlock := repository.FindBlockByNumber(111)
|
||||
|
||||
Expect(savedBlock).To(BeNil())
|
||||
})
|
||||
|
||||
It("saves one transaction associated to the block", func() {
|
||||
block := core.Block{
|
||||
Number: 123,
|
||||
Transactions: []core.Transaction{{}},
|
||||
}
|
||||
repository := buildRepository()
|
||||
|
||||
repository.CreateBlock(block)
|
||||
|
||||
savedBlock := repository.FindBlockByNumber(123)
|
||||
Expect(len(savedBlock.Transactions)).To(Equal(1))
|
||||
})
|
||||
|
||||
It("saves two transactions associated to the block", func() {
|
||||
block := core.Block{
|
||||
Number: 123,
|
||||
Transactions: []core.Transaction{{}, {}},
|
||||
}
|
||||
repository := buildRepository()
|
||||
|
||||
repository.CreateBlock(block)
|
||||
|
||||
savedBlock := repository.FindBlockByNumber(123)
|
||||
Expect(len(savedBlock.Transactions)).To(Equal(2))
|
||||
})
|
||||
|
||||
It("saves the attributes associated to a transaction", func() {
|
||||
gasLimit := int64(5000)
|
||||
gasPrice := int64(3)
|
||||
nonce := uint64(10000)
|
||||
to := "1234567890"
|
||||
value := int64(10)
|
||||
|
||||
transaction := core.Transaction{
|
||||
Hash: "x1234",
|
||||
GasPrice: gasPrice,
|
||||
GasLimit: gasLimit,
|
||||
Nonce: nonce,
|
||||
To: to,
|
||||
Value: value,
|
||||
}
|
||||
block := core.Block{
|
||||
Number: 123,
|
||||
Transactions: []core.Transaction{transaction},
|
||||
}
|
||||
repository := buildRepository()
|
||||
|
||||
repository.CreateBlock(block)
|
||||
|
||||
savedBlock := repository.FindBlockByNumber(123)
|
||||
Expect(len(savedBlock.Transactions)).To(Equal(1))
|
||||
savedTransaction := savedBlock.Transactions[0]
|
||||
Expect(savedTransaction.Hash).To(Equal(transaction.Hash))
|
||||
Expect(savedTransaction.To).To(Equal(to))
|
||||
Expect(savedTransaction.Nonce).To(Equal(nonce))
|
||||
Expect(savedTransaction.GasLimit).To(Equal(gasLimit))
|
||||
Expect(savedTransaction.GasPrice).To(Equal(gasPrice))
|
||||
Expect(savedTransaction.Value).To(Equal(value))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
func buildRepository() *repositories.InMemory {
|
||||
return repositories.NewInMemory()
|
||||
}
|
7
repositories/repository.go
Normal file
7
repositories/repository.go
Normal file
@ -0,0 +1,7 @@
|
||||
package repositories
|
||||
|
||||
import "github.com/8thlight/vulcanizedb/core"
|
||||
|
||||
type Repository interface {
|
||||
CreateBlock(block core.Block)
|
||||
}
|
Loading…
Reference in New Issue
Block a user