diff --git a/.travis.yml b/.travis.yml index 25d19f0e..2f1c125b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Gododir/main.go b/Gododir/main.go index 1e7a0a55..46a68ad1 100644 --- a/Gododir/main.go +++ b/Gododir/main.go @@ -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, }) diff --git a/core/blockchain_listener.go b/blockchain_listener/blockchain_listener.go similarity index 54% rename from core/blockchain_listener.go rename to blockchain_listener/blockchain_listener.go index a488ab54..a5734565 100644 --- a/core/blockchain_listener.go +++ b/blockchain_listener/blockchain_listener.go @@ -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) } diff --git a/core/blockchain_listener_test.go b/blockchain_listener/blockchain_listener_test.go similarity index 78% rename from core/blockchain_listener_test.go rename to blockchain_listener/blockchain_listener_test.go index 86bf1ad7..787b134f 100644 --- a/core/blockchain_listener_test.go +++ b/blockchain_listener/blockchain_listener_test.go @@ -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}) diff --git a/blockchain_listener/listener_suite_test.go b/blockchain_listener/listener_suite_test.go new file mode 100644 index 00000000..d2a519ad --- /dev/null +++ b/blockchain_listener/listener_suite_test.go @@ -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") +} diff --git a/core/block.go b/core/block.go index 76056d01..e0329ccd 100644 --- a/core/block.go +++ b/core/block.go @@ -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, - } -} diff --git a/core/transaction.go b/core/transaction.go index 84390347..a0dfd1d2 100644 --- a/core/transaction.go +++ b/core/transaction.go @@ -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() - } -} diff --git a/geth/geth_block_to_core_block.go b/geth/geth_block_to_core_block.go new file mode 100644 index 00000000..ff82086a --- /dev/null +++ b/geth/geth_block_to_core_block.go @@ -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() + } +} diff --git a/core/converting_a_geth_block_test.go b/geth/geth_block_to_core_block_test.go similarity index 90% rename from core/converting_a_geth_block_test.go rename to geth/geth_block_to_core_block_test.go index 75b81fba..3fe5de0e 100644 --- a/core/converting_a_geth_block_test.go +++ b/geth/geth_block_to_core_block_test.go @@ -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("")) diff --git a/core/geth_blockchain.go b/geth/geth_blockchain.go similarity index 91% rename from core/geth_blockchain.go rename to geth/geth_blockchain.go index 932ae4ae..e712ee11 100644 --- a/core/geth_blockchain.go +++ b/geth/geth_blockchain.go @@ -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) diff --git a/geth/geth_suite_test.go b/geth/geth_suite_test.go new file mode 100644 index 00000000..2d293f6c --- /dev/null +++ b/geth/geth_suite_test.go @@ -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") +} diff --git a/integration_test/geth_blockchain_test.go b/integration_test/geth_blockchain_test.go index 9c40d7be..cc92710a 100644 --- a/integration_test/geth_blockchain_test.go +++ b/integration_test/geth_blockchain_test.go @@ -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 diff --git a/core/blockchain_db_observer.go b/observers/blockchain_db_observer.go similarity index 73% rename from core/blockchain_db_observer.go rename to observers/blockchain_db_observer.go index 5318d8f9..338c8061 100644 --- a/core/blockchain_db_observer.go +++ b/observers/blockchain_db_observer.go @@ -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)", diff --git a/core/blockchain_db_observer_test.go b/observers/blockchain_db_observer_test.go similarity index 93% rename from core/blockchain_db_observer_test.go rename to observers/blockchain_db_observer_test.go index d781606e..b22eb261 100644 --- a/core/blockchain_db_observer_test.go +++ b/observers/blockchain_db_observer_test.go @@ -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") diff --git a/core/blockchain_logging_observer.go b/observers/blockchain_logging_observer.go similarity index 81% rename from core/blockchain_logging_observer.go rename to observers/blockchain_logging_observer.go index 1de9e160..15397caa 100644 --- a/core/blockchain_logging_observer.go +++ b/observers/blockchain_logging_observer.go @@ -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"+ diff --git a/observers/observers_suite_test.go b/observers/observers_suite_test.go new file mode 100644 index 00000000..4a89e8a0 --- /dev/null +++ b/observers/observers_suite_test.go @@ -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") +}