Reorganize files into separate packages
This commit is contained in:
parent
70c34e86ea
commit
0a04df72d3
@ -8,6 +8,6 @@ before_script:
|
||||
- createdb vulcanize_private
|
||||
- psql vulcanize_private < migrations/schema.sql
|
||||
script:
|
||||
- go test -v ./core/...
|
||||
- go test -v ./blockchain_listener ./core ./geth ./observers
|
||||
notifications:
|
||||
email: false
|
||||
|
@ -5,8 +5,11 @@ import (
|
||||
|
||||
"fmt"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/blockchain_listener"
|
||||
cfg "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"
|
||||
)
|
||||
@ -20,15 +23,15 @@ func parseIpcPath(context *do.Context) string {
|
||||
}
|
||||
|
||||
func startBlockchainListener(config cfg.Config, ipcPath string) {
|
||||
blockchain := core.NewGethBlockchain(ipcPath)
|
||||
loggingObserver := core.BlockchainLoggingObserver{}
|
||||
blockchain := geth.NewGethBlockchain(ipcPath)
|
||||
loggingObserver := observers.BlockchainLoggingObserver{}
|
||||
connectString := cfg.DbConnectionString(cfg.Public().Database)
|
||||
db, err := sqlx.Connect("postgres", connectString)
|
||||
if err != nil {
|
||||
log.Fatalf("Error connecting to DB: %v\n", err)
|
||||
}
|
||||
dbObserver := (core.BlockchainDBObserver{Db: db})
|
||||
listener := core.NewBlockchainListener(blockchain, []core.BlockchainObserver{
|
||||
dbObserver := (observers.BlockchainDBObserver{Db: db})
|
||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{
|
||||
loggingObserver,
|
||||
dbObserver,
|
||||
})
|
||||
|
@ -1,13 +1,15 @@
|
||||
package core
|
||||
package blockchain_listener
|
||||
|
||||
import "github.com/8thlight/vulcanizedb/core"
|
||||
|
||||
type BlockchainListener struct {
|
||||
inputBlocks chan Block
|
||||
blockchain Blockchain
|
||||
observers []BlockchainObserver
|
||||
inputBlocks chan core.Block
|
||||
blockchain core.Blockchain
|
||||
observers []core.BlockchainObserver
|
||||
}
|
||||
|
||||
func NewBlockchainListener(blockchain Blockchain, observers []BlockchainObserver) BlockchainListener {
|
||||
inputBlocks := make(chan Block, 10)
|
||||
func NewBlockchainListener(blockchain core.Blockchain, observers []core.BlockchainObserver) BlockchainListener {
|
||||
inputBlocks := make(chan core.Block, 10)
|
||||
blockchain.SubscribeToBlocks(inputBlocks)
|
||||
listener := BlockchainListener{
|
||||
inputBlocks: inputBlocks,
|
||||
@ -24,7 +26,7 @@ func (listener BlockchainListener) Start() {
|
||||
}
|
||||
}
|
||||
|
||||
func (listener BlockchainListener) notifyObservers(block Block) {
|
||||
func (listener BlockchainListener) notifyObservers(block core.Block) {
|
||||
for _, observer := range listener.observers {
|
||||
observer.NotifyBlockAdded(block)
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package core_test
|
||||
package blockchain_listener_test
|
||||
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/blockchain_listener"
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/8thlight/vulcanizedb/fakes"
|
||||
. "github.com/onsi/ginkgo"
|
||||
@ -13,7 +14,7 @@ var _ = Describe("Blockchain listeners", func() {
|
||||
observer := fakes.NewFakeBlockchainObserverTwo()
|
||||
blockchain := &fakes.Blockchain{}
|
||||
|
||||
core.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
|
||||
Expect(len(observer.CurrentBlocks)).To(Equal(0))
|
||||
close(done)
|
||||
@ -22,7 +23,7 @@ var _ = Describe("Blockchain listeners", func() {
|
||||
It("sees when one block was added", func(done Done) {
|
||||
observer := fakes.NewFakeBlockchainObserverTwo()
|
||||
blockchain := &fakes.Blockchain{}
|
||||
listener := core.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
go listener.Start()
|
||||
|
||||
go blockchain.AddBlock(core.Block{Number: 123})
|
||||
@ -38,7 +39,7 @@ var _ = Describe("Blockchain listeners", func() {
|
||||
It("sees a second block", func(done Done) {
|
||||
observer := fakes.NewFakeBlockchainObserverTwo()
|
||||
blockchain := &fakes.Blockchain{}
|
||||
listener := core.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
go listener.Start()
|
||||
|
||||
go blockchain.AddBlock(core.Block{Number: 123})
|
13
blockchain_listener/listener_suite_test.go
Normal file
13
blockchain_listener/listener_suite_test.go
Normal file
@ -0,0 +1,13 @@
|
||||
package blockchain_listener_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestListener(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Listener Suite")
|
||||
}
|
@ -1,9 +1,5 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
)
|
||||
|
||||
type Block struct {
|
||||
Number int64
|
||||
GasLimit int64
|
||||
@ -11,17 +7,3 @@ type Block struct {
|
||||
Time int64
|
||||
Transactions []Transaction
|
||||
}
|
||||
|
||||
func GethBlockToCoreBlock(gethBlock *types.Block) Block {
|
||||
transactions := []Transaction{}
|
||||
for _, gethTransaction := range gethBlock.Transactions() {
|
||||
transactions = append(transactions, gethTransToCoreTrans(gethTransaction))
|
||||
}
|
||||
return Block{
|
||||
Number: gethBlock.Number().Int64(),
|
||||
GasLimit: gethBlock.GasLimit().Int64(),
|
||||
GasUsed: gethBlock.GasUsed().Int64(),
|
||||
Time: gethBlock.Time().Int64(),
|
||||
Transactions: transactions,
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,5 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
)
|
||||
|
||||
type Transaction struct {
|
||||
Hash string
|
||||
Data []byte
|
||||
@ -14,25 +9,3 @@ type Transaction struct {
|
||||
GasPrice int64
|
||||
Value int64
|
||||
}
|
||||
|
||||
func gethTransToCoreTrans(transaction *types.Transaction) Transaction {
|
||||
to := transaction.To()
|
||||
toHex := convertTo(to)
|
||||
return Transaction{
|
||||
Hash: transaction.Hash().Hex(),
|
||||
Data: transaction.Data(),
|
||||
Nonce: transaction.Nonce(),
|
||||
To: toHex,
|
||||
GasLimit: transaction.Gas().Int64(),
|
||||
GasPrice: transaction.GasPrice().Int64(),
|
||||
Value: transaction.Value().Int64(),
|
||||
}
|
||||
}
|
||||
|
||||
func convertTo(to *common.Address) string {
|
||||
if to == nil {
|
||||
return ""
|
||||
} else {
|
||||
return to.Hex()
|
||||
}
|
||||
}
|
||||
|
43
geth/geth_block_to_core_block.go
Normal file
43
geth/geth_block_to_core_block.go
Normal file
@ -0,0 +1,43 @@
|
||||
package geth
|
||||
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
)
|
||||
|
||||
func gethTransToCoreTrans(transaction *types.Transaction) core.Transaction {
|
||||
to := transaction.To()
|
||||
toHex := convertTo(to)
|
||||
return core.Transaction{
|
||||
Hash: transaction.Hash().Hex(),
|
||||
Data: transaction.Data(),
|
||||
Nonce: transaction.Nonce(),
|
||||
To: toHex,
|
||||
GasLimit: transaction.Gas().Int64(),
|
||||
GasPrice: transaction.GasPrice().Int64(),
|
||||
Value: transaction.Value().Int64(),
|
||||
}
|
||||
}
|
||||
|
||||
func GethBlockToCoreBlock(gethBlock *types.Block) core.Block {
|
||||
transactions := []core.Transaction{}
|
||||
for _, gethTransaction := range gethBlock.Transactions() {
|
||||
transactions = append(transactions, gethTransToCoreTrans(gethTransaction))
|
||||
}
|
||||
return core.Block{
|
||||
Number: gethBlock.Number().Int64(),
|
||||
GasLimit: gethBlock.GasLimit().Int64(),
|
||||
GasUsed: gethBlock.GasUsed().Int64(),
|
||||
Time: gethBlock.Time().Int64(),
|
||||
Transactions: transactions,
|
||||
}
|
||||
}
|
||||
|
||||
func convertTo(to *common.Address) string {
|
||||
if to == nil {
|
||||
return ""
|
||||
} else {
|
||||
return to.Hex()
|
||||
}
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
package core_test
|
||||
package geth_test
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/8thlight/vulcanizedb/geth"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
. "github.com/onsi/ginkgo"
|
||||
@ -25,7 +25,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
GasLimit: big.NewInt(gasLimit),
|
||||
}
|
||||
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{})
|
||||
gethBlock := core.GethBlockToCoreBlock(block)
|
||||
gethBlock := geth.GethBlockToCoreBlock(block)
|
||||
|
||||
Expect(gethBlock.Number).To(Equal(blockNumber))
|
||||
Expect(gethBlock.GasUsed).To(Equal(gasUsed))
|
||||
@ -38,7 +38,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
header := types.Header{}
|
||||
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{})
|
||||
|
||||
coreBlock := core.GethBlockToCoreBlock(block)
|
||||
coreBlock := geth.GethBlockToCoreBlock(block)
|
||||
|
||||
Expect(len(coreBlock.Transactions)).To(Equal(0))
|
||||
})
|
||||
@ -54,7 +54,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
|
||||
gethTransaction := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, payload)
|
||||
gethBlock := types.NewBlock(&header, []*types.Transaction{gethTransaction}, []*types.Header{}, []*types.Receipt{})
|
||||
coreBlock := core.GethBlockToCoreBlock(gethBlock)
|
||||
coreBlock := geth.GethBlockToCoreBlock(gethBlock)
|
||||
|
||||
Expect(len(coreBlock.Transactions)).To(Equal(1))
|
||||
coreTransaction := coreBlock.Transactions[0]
|
||||
@ -70,7 +70,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
gethTransaction := types.NewContractCreation(uint64(10000), big.NewInt(10), big.NewInt(5000), big.NewInt(3), []byte("1234"))
|
||||
gethBlock := types.NewBlock(&types.Header{}, []*types.Transaction{gethTransaction}, []*types.Header{}, []*types.Receipt{})
|
||||
|
||||
coreBlock := core.GethBlockToCoreBlock(gethBlock)
|
||||
coreBlock := geth.GethBlockToCoreBlock(gethBlock)
|
||||
|
||||
coreTransaction := coreBlock.Transactions[0]
|
||||
Expect(coreTransaction.To).To(Equal(""))
|
@ -1,8 +1,9 @@
|
||||
package core
|
||||
package geth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"golang.org/x/net/context"
|
||||
@ -11,7 +12,7 @@ import (
|
||||
type GethBlockchain struct {
|
||||
client *ethclient.Client
|
||||
readGethHeaders chan *types.Header
|
||||
outputBlocks chan Block
|
||||
outputBlocks chan core.Block
|
||||
}
|
||||
|
||||
func NewGethBlockchain(ipcPath string) *GethBlockchain {
|
||||
@ -22,7 +23,7 @@ func NewGethBlockchain(ipcPath string) *GethBlockchain {
|
||||
return &blockchain
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) SubscribeToBlocks(blocks chan Block) {
|
||||
func (blockchain *GethBlockchain) SubscribeToBlocks(blocks chan core.Block) {
|
||||
blockchain.outputBlocks = blocks
|
||||
fmt.Println("SubscribeToBlocks")
|
||||
inputHeaders := make(chan *types.Header, 10)
|
13
geth/geth_suite_test.go
Normal file
13
geth/geth_suite_test.go
Normal file
@ -0,0 +1,13 @@
|
||||
package geth_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGeth(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Geth Suite")
|
||||
}
|
@ -4,8 +4,10 @@ import (
|
||||
"path"
|
||||
"runtime"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/blockchain_listener"
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/8thlight/vulcanizedb/fakes"
|
||||
"github.com/8thlight/vulcanizedb/geth"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
@ -20,11 +22,11 @@ func RunTimePath() string {
|
||||
|
||||
var _ = Describe("Reading from the Geth blockchain", func() {
|
||||
|
||||
It("reads two block with listener", func(done Done) {
|
||||
It("reads two blocks", func(done Done) {
|
||||
observer := fakes.NewFakeBlockchainObserverTwo()
|
||||
blockchain := core.NewGethBlockchain(RunTimePath() + "/test_data_dir/geth.ipc")
|
||||
blockchain := geth.NewGethBlockchain(RunTimePath() + "/test_data_dir/geth.ipc")
|
||||
observers := []core.BlockchainObserver{observer}
|
||||
listener := core.NewBlockchainListener(blockchain, observers)
|
||||
listener := blockchain_listener.NewBlockchainListener(blockchain, observers)
|
||||
go listener.Start()
|
||||
|
||||
<-observer.WasNotified
|
||||
|
@ -1,6 +1,7 @@
|
||||
package core
|
||||
package observers
|
||||
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
@ -9,12 +10,12 @@ type BlockchainDBObserver struct {
|
||||
Db *sqlx.DB
|
||||
}
|
||||
|
||||
func (observer BlockchainDBObserver) NotifyBlockAdded(block Block) {
|
||||
func (observer BlockchainDBObserver) NotifyBlockAdded(block core.Block) {
|
||||
insertedBlockId := saveBlock(observer, block)
|
||||
saveTransactions(insertedBlockId, block.Transactions, observer)
|
||||
}
|
||||
|
||||
func saveBlock(observer BlockchainDBObserver, block Block) int64 {
|
||||
func saveBlock(observer BlockchainDBObserver, block core.Block) int64 {
|
||||
insertedBlock := observer.Db.QueryRow("Insert INTO blocks "+
|
||||
"(block_number, block_gaslimit, block_gasused, block_time) "+
|
||||
"VALUES ($1, $2, $3, $4) RETURNING id",
|
||||
@ -24,7 +25,7 @@ func saveBlock(observer BlockchainDBObserver, block Block) int64 {
|
||||
return blockId
|
||||
}
|
||||
|
||||
func saveTransactions(blockId int64, transactions []Transaction, observer BlockchainDBObserver) {
|
||||
func saveTransactions(blockId int64, transactions []core.Transaction, observer BlockchainDBObserver) {
|
||||
for _, transaction := range transactions {
|
||||
observer.Db.MustExec("Insert INTO transactions "+
|
||||
"(block_id, tx_hash, tx_nonce, tx_to, tx_gaslimit, tx_gasprice, tx_value) VALUES ($1, $2, $3, $4, $5, $6, $7)",
|
@ -1,8 +1,9 @@
|
||||
package core_test
|
||||
package observers_test
|
||||
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/config"
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/8thlight/vulcanizedb/observers"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
. "github.com/onsi/ginkgo"
|
||||
@ -26,7 +27,7 @@ var _ = Describe("Saving blocks to the database", func() {
|
||||
})
|
||||
|
||||
It("implements the observer interface", func() {
|
||||
var observer core.BlockchainObserver = core.BlockchainDBObserver{Db: db}
|
||||
var observer core.BlockchainObserver = observers.BlockchainDBObserver{Db: db}
|
||||
Expect(observer).NotTo(BeNil())
|
||||
})
|
||||
|
||||
@ -51,7 +52,7 @@ var _ = Describe("Saving blocks to the database", func() {
|
||||
block := core.Block{Number: blockNumber, GasLimit: gasLimit, GasUsed: gasUsed, Time: blockTime}
|
||||
|
||||
// save the block to the database
|
||||
observer := core.BlockchainDBObserver{Db: db}
|
||||
observer := observers.BlockchainDBObserver{Db: db}
|
||||
observer.NotifyBlockAdded(block)
|
||||
|
||||
// find the saved block
|
||||
@ -99,7 +100,7 @@ var _ = Describe("Saving blocks to the database", func() {
|
||||
}
|
||||
block := core.Block{Transactions: []core.Transaction{txRecord}}
|
||||
|
||||
observer := core.BlockchainDBObserver{Db: db}
|
||||
observer := observers.BlockchainDBObserver{Db: db}
|
||||
observer.NotifyBlockAdded(block)
|
||||
|
||||
rows, err := db.Query("SELECT tx_hash, tx_nonce, tx_to, tx_gaslimit, tx_gasprice, tx_value FROM transactions")
|
||||
@ -141,7 +142,7 @@ var _ = Describe("Saving blocks to the database", func() {
|
||||
Transactions: []core.Transaction{txRecord},
|
||||
}
|
||||
|
||||
observer := core.BlockchainDBObserver{Db: db}
|
||||
observer := observers.BlockchainDBObserver{Db: db}
|
||||
observer.NotifyBlockAdded(block)
|
||||
|
||||
blockRows, err := db.Query("SELECT id FROM blocks")
|
@ -1,13 +1,15 @@
|
||||
package core
|
||||
package observers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
)
|
||||
|
||||
type BlockchainLoggingObserver struct{}
|
||||
|
||||
func (blockchainObserver BlockchainLoggingObserver) NotifyBlockAdded(block Block) {
|
||||
func (blockchainObserver BlockchainLoggingObserver) NotifyBlockAdded(block core.Block) {
|
||||
fmt.Printf("New block was added: %d\n"+
|
||||
"\tTime: %v\n"+
|
||||
"\tGas Limit: %d\n"+
|
13
observers/observers_suite_test.go
Normal file
13
observers/observers_suite_test.go
Normal file
@ -0,0 +1,13 @@
|
||||
package observers_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestObservers(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Observers Suite")
|
||||
}
|
Loading…
Reference in New Issue
Block a user