Merge pull request #50 from 8thlight/reogranize-packages

Reorganize files into separate packages
This commit is contained in:
ericmeyer 2017-11-02 15:10:52 -05:00 committed by GitHub
commit 4733b25f54
16 changed files with 134 additions and 84 deletions

View File

@ -8,6 +8,6 @@ before_script:
- createdb vulcanize_private - createdb vulcanize_private
- psql vulcanize_private < migrations/schema.sql - psql vulcanize_private < migrations/schema.sql
script: script:
- go test -v ./core/... - go test -v ./blockchain_listener ./core ./geth ./observers
notifications: notifications:
email: false email: false

View File

@ -5,8 +5,11 @@ import (
"fmt" "fmt"
"github.com/8thlight/vulcanizedb/blockchain_listener"
cfg "github.com/8thlight/vulcanizedb/config" cfg "github.com/8thlight/vulcanizedb/config"
"github.com/8thlight/vulcanizedb/core" "github.com/8thlight/vulcanizedb/core"
"github.com/8thlight/vulcanizedb/geth"
"github.com/8thlight/vulcanizedb/observers"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
do "gopkg.in/godo.v2" do "gopkg.in/godo.v2"
) )
@ -20,15 +23,15 @@ func parseIpcPath(context *do.Context) string {
} }
func startBlockchainListener(config cfg.Config, ipcPath string) { func startBlockchainListener(config cfg.Config, ipcPath string) {
blockchain := core.NewGethBlockchain(ipcPath) blockchain := geth.NewGethBlockchain(ipcPath)
loggingObserver := core.BlockchainLoggingObserver{} loggingObserver := observers.BlockchainLoggingObserver{}
connectString := cfg.DbConnectionString(cfg.Public().Database) connectString := cfg.DbConnectionString(cfg.Public().Database)
db, err := sqlx.Connect("postgres", connectString) db, err := sqlx.Connect("postgres", connectString)
if err != nil { if err != nil {
log.Fatalf("Error connecting to DB: %v\n", err) log.Fatalf("Error connecting to DB: %v\n", err)
} }
dbObserver := (core.BlockchainDBObserver{Db: db}) dbObserver := (observers.BlockchainDBObserver{Db: db})
listener := core.NewBlockchainListener(blockchain, []core.BlockchainObserver{ listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{
loggingObserver, loggingObserver,
dbObserver, dbObserver,
}) })

View File

@ -1,13 +1,15 @@
package core package blockchain_listener
import "github.com/8thlight/vulcanizedb/core"
type BlockchainListener struct { type BlockchainListener struct {
inputBlocks chan Block inputBlocks chan core.Block
blockchain Blockchain blockchain core.Blockchain
observers []BlockchainObserver observers []core.BlockchainObserver
} }
func NewBlockchainListener(blockchain Blockchain, observers []BlockchainObserver) BlockchainListener { func NewBlockchainListener(blockchain core.Blockchain, observers []core.BlockchainObserver) BlockchainListener {
inputBlocks := make(chan Block, 10) inputBlocks := make(chan core.Block, 10)
blockchain.SubscribeToBlocks(inputBlocks) blockchain.SubscribeToBlocks(inputBlocks)
listener := BlockchainListener{ listener := BlockchainListener{
inputBlocks: inputBlocks, 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 { for _, observer := range listener.observers {
observer.NotifyBlockAdded(block) observer.NotifyBlockAdded(block)
} }

View File

@ -1,6 +1,7 @@
package core_test package blockchain_listener_test
import ( import (
"github.com/8thlight/vulcanizedb/blockchain_listener"
"github.com/8thlight/vulcanizedb/core" "github.com/8thlight/vulcanizedb/core"
"github.com/8thlight/vulcanizedb/fakes" "github.com/8thlight/vulcanizedb/fakes"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
@ -13,7 +14,7 @@ var _ = Describe("Blockchain listeners", func() {
observer := fakes.NewFakeBlockchainObserverTwo() observer := fakes.NewFakeBlockchainObserverTwo()
blockchain := &fakes.Blockchain{} blockchain := &fakes.Blockchain{}
core.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer}) blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
Expect(len(observer.CurrentBlocks)).To(Equal(0)) Expect(len(observer.CurrentBlocks)).To(Equal(0))
close(done) close(done)
@ -22,7 +23,7 @@ var _ = Describe("Blockchain listeners", func() {
It("sees when one block was added", func(done Done) { It("sees when one block was added", func(done Done) {
observer := fakes.NewFakeBlockchainObserverTwo() observer := fakes.NewFakeBlockchainObserverTwo()
blockchain := &fakes.Blockchain{} blockchain := &fakes.Blockchain{}
listener := core.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer}) listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
go listener.Start() go listener.Start()
go blockchain.AddBlock(core.Block{Number: 123}) go blockchain.AddBlock(core.Block{Number: 123})
@ -38,7 +39,7 @@ var _ = Describe("Blockchain listeners", func() {
It("sees a second block", func(done Done) { It("sees a second block", func(done Done) {
observer := fakes.NewFakeBlockchainObserverTwo() observer := fakes.NewFakeBlockchainObserverTwo()
blockchain := &fakes.Blockchain{} blockchain := &fakes.Blockchain{}
listener := core.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer}) listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
go listener.Start() go listener.Start()
go blockchain.AddBlock(core.Block{Number: 123}) go blockchain.AddBlock(core.Block{Number: 123})

View 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")
}

View File

@ -1,9 +1,5 @@
package core package core
import (
"github.com/ethereum/go-ethereum/core/types"
)
type Block struct { type Block struct {
Number int64 Number int64
GasLimit int64 GasLimit int64
@ -11,17 +7,3 @@ type Block struct {
Time int64 Time int64
Transactions []Transaction 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,
}
}

View File

@ -1,10 +1,5 @@
package core package core
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
type Transaction struct { type Transaction struct {
Hash string Hash string
Data []byte Data []byte
@ -14,25 +9,3 @@ type Transaction struct {
GasPrice int64 GasPrice int64
Value 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()
}
}

View 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()
}
}

View File

@ -1,9 +1,9 @@
package core_test package geth_test
import ( import (
"math/big" "math/big"
"github.com/8thlight/vulcanizedb/core" "github.com/8thlight/vulcanizedb/geth"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
@ -25,7 +25,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
GasLimit: big.NewInt(gasLimit), GasLimit: big.NewInt(gasLimit),
} }
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{}) 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.Number).To(Equal(blockNumber))
Expect(gethBlock.GasUsed).To(Equal(gasUsed)) Expect(gethBlock.GasUsed).To(Equal(gasUsed))
@ -38,7 +38,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
header := types.Header{} header := types.Header{}
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{}) 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)) 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) gethTransaction := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, payload)
gethBlock := types.NewBlock(&header, []*types.Transaction{gethTransaction}, []*types.Header{}, []*types.Receipt{}) 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)) Expect(len(coreBlock.Transactions)).To(Equal(1))
coreTransaction := coreBlock.Transactions[0] 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")) 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{}) gethBlock := types.NewBlock(&types.Header{}, []*types.Transaction{gethTransaction}, []*types.Header{}, []*types.Receipt{})
coreBlock := core.GethBlockToCoreBlock(gethBlock) coreBlock := geth.GethBlockToCoreBlock(gethBlock)
coreTransaction := coreBlock.Transactions[0] coreTransaction := coreBlock.Transactions[0]
Expect(coreTransaction.To).To(Equal("")) Expect(coreTransaction.To).To(Equal(""))

View File

@ -1,8 +1,9 @@
package core package geth
import ( import (
"fmt" "fmt"
"github.com/8thlight/vulcanizedb/core"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/ethclient"
"golang.org/x/net/context" "golang.org/x/net/context"
@ -11,7 +12,7 @@ import (
type GethBlockchain struct { type GethBlockchain struct {
client *ethclient.Client client *ethclient.Client
readGethHeaders chan *types.Header readGethHeaders chan *types.Header
outputBlocks chan Block outputBlocks chan core.Block
} }
func NewGethBlockchain(ipcPath string) *GethBlockchain { func NewGethBlockchain(ipcPath string) *GethBlockchain {
@ -22,7 +23,7 @@ func NewGethBlockchain(ipcPath string) *GethBlockchain {
return &blockchain return &blockchain
} }
func (blockchain *GethBlockchain) SubscribeToBlocks(blocks chan Block) { func (blockchain *GethBlockchain) SubscribeToBlocks(blocks chan core.Block) {
blockchain.outputBlocks = blocks blockchain.outputBlocks = blocks
fmt.Println("SubscribeToBlocks") fmt.Println("SubscribeToBlocks")
inputHeaders := make(chan *types.Header, 10) inputHeaders := make(chan *types.Header, 10)

13
geth/geth_suite_test.go Normal file
View 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")
}

View File

@ -4,8 +4,10 @@ import (
"path" "path"
"runtime" "runtime"
"github.com/8thlight/vulcanizedb/blockchain_listener"
"github.com/8thlight/vulcanizedb/core" "github.com/8thlight/vulcanizedb/core"
"github.com/8thlight/vulcanizedb/fakes" "github.com/8thlight/vulcanizedb/fakes"
"github.com/8thlight/vulcanizedb/geth"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@ -20,11 +22,11 @@ func RunTimePath() string {
var _ = Describe("Reading from the Geth blockchain", func() { 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() 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} observers := []core.BlockchainObserver{observer}
listener := core.NewBlockchainListener(blockchain, observers) listener := blockchain_listener.NewBlockchainListener(blockchain, observers)
go listener.Start() go listener.Start()
<-observer.WasNotified <-observer.WasNotified

View File

@ -1,6 +1,7 @@
package core package observers
import ( import (
"github.com/8thlight/vulcanizedb/core"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
@ -9,12 +10,12 @@ type BlockchainDBObserver struct {
Db *sqlx.DB Db *sqlx.DB
} }
func (observer BlockchainDBObserver) NotifyBlockAdded(block Block) { func (observer BlockchainDBObserver) NotifyBlockAdded(block core.Block) {
insertedBlockId := saveBlock(observer, block) insertedBlockId := saveBlock(observer, block)
saveTransactions(insertedBlockId, block.Transactions, observer) 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 "+ insertedBlock := observer.Db.QueryRow("Insert INTO blocks "+
"(block_number, block_gaslimit, block_gasused, block_time) "+ "(block_number, block_gaslimit, block_gasused, block_time) "+
"VALUES ($1, $2, $3, $4) RETURNING id", "VALUES ($1, $2, $3, $4) RETURNING id",
@ -24,7 +25,7 @@ func saveBlock(observer BlockchainDBObserver, block Block) int64 {
return blockId return blockId
} }
func saveTransactions(blockId int64, transactions []Transaction, observer BlockchainDBObserver) { func saveTransactions(blockId int64, transactions []core.Transaction, observer BlockchainDBObserver) {
for _, transaction := range transactions { for _, transaction := range transactions {
observer.Db.MustExec("Insert INTO 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)", "(block_id, tx_hash, tx_nonce, tx_to, tx_gaslimit, tx_gasprice, tx_value) VALUES ($1, $2, $3, $4, $5, $6, $7)",

View File

@ -1,8 +1,9 @@
package core_test package observers_test
import ( import (
"github.com/8thlight/vulcanizedb/config" "github.com/8thlight/vulcanizedb/config"
"github.com/8thlight/vulcanizedb/core" "github.com/8thlight/vulcanizedb/core"
"github.com/8thlight/vulcanizedb/observers"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
_ "github.com/lib/pq" _ "github.com/lib/pq"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
@ -26,7 +27,7 @@ var _ = Describe("Saving blocks to the database", func() {
}) })
It("implements the observer interface", 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()) 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} block := core.Block{Number: blockNumber, GasLimit: gasLimit, GasUsed: gasUsed, Time: blockTime}
// save the block to the database // save the block to the database
observer := core.BlockchainDBObserver{Db: db} observer := observers.BlockchainDBObserver{Db: db}
observer.NotifyBlockAdded(block) observer.NotifyBlockAdded(block)
// find the saved block // find the saved block
@ -99,7 +100,7 @@ var _ = Describe("Saving blocks to the database", func() {
} }
block := core.Block{Transactions: []core.Transaction{txRecord}} block := core.Block{Transactions: []core.Transaction{txRecord}}
observer := core.BlockchainDBObserver{Db: db} observer := observers.BlockchainDBObserver{Db: db}
observer.NotifyBlockAdded(block) observer.NotifyBlockAdded(block)
rows, err := db.Query("SELECT tx_hash, tx_nonce, tx_to, tx_gaslimit, tx_gasprice, tx_value FROM transactions") 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}, Transactions: []core.Transaction{txRecord},
} }
observer := core.BlockchainDBObserver{Db: db} observer := observers.BlockchainDBObserver{Db: db}
observer.NotifyBlockAdded(block) observer.NotifyBlockAdded(block)
blockRows, err := db.Query("SELECT id FROM blocks") blockRows, err := db.Query("SELECT id FROM blocks")

View File

@ -1,13 +1,15 @@
package core package observers
import ( import (
"fmt" "fmt"
"time" "time"
"github.com/8thlight/vulcanizedb/core"
) )
type BlockchainLoggingObserver struct{} type BlockchainLoggingObserver struct{}
func (blockchainObserver BlockchainLoggingObserver) NotifyBlockAdded(block Block) { func (blockchainObserver BlockchainLoggingObserver) NotifyBlockAdded(block core.Block) {
fmt.Printf("New block was added: %d\n"+ fmt.Printf("New block was added: %d\n"+
"\tTime: %v\n"+ "\tTime: %v\n"+
"\tGas Limit: %d\n"+ "\tGas Limit: %d\n"+

View 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")
}