diff --git a/go.sum b/go.sum index 24b0bd52..b72e93a6 100644 --- a/go.sum +++ b/go.sum @@ -69,6 +69,7 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= github.com/Kubuxu/go-os-helper v0.0.1/go.mod h1:N8B+I7vPCT80IcP58r50u4+gEEcsZETFUpAzWW2ep1Y= +github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/OneOfOne/xxhash v1.2.5 h1:zl/OfRA6nftbBK9qTohYBJ5xvw6C/oNKizR7cZGl3cI= @@ -184,7 +185,9 @@ github.com/cloudflare/cloudflare-go v0.14.0/go.mod h1:EnwdgGMaFOruiPZRFSgn+TsQ3h github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= +github.com/cockroachdb/cockroach-go/v2 v2.0.3 h1:ZA346ACHIZctef6trOTwBAEvPVm1k0uLm/bb2Atc+S8= github.com/cockroachdb/cockroach-go/v2 v2.0.3/go.mod h1:hAuDgiVgDVkfirP9JnhXEfcXEPRKBpYdGz+l7mvYSzw= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= @@ -224,7 +227,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davidlazar/go-crypto v0.0.0-20170701192655-dcfb0a7ac018/go.mod h1:rQYf4tfk5sSwFsnDg3qYaBxSjsD9S8+59vW0dKUgme4= github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c h1:pFUpOrbxDR6AkioZ1ySsx5yxlDQZ8stG2b88gTPxgJU= github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c/go.mod h1:6UhI8N9EjYm1c2odKpFpAYeR8dsBeM7PtzQhRgxRr9U= -github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea h1:j4317fAZh7X6GqbFowYdYdI0L9bwxL07jyPZIdepyZ0= github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ= github.com/deckarep/golang-set v1.8.0 h1:sk9/l/KqpunDwP7pSjUg0keiOOLEnOBHzykLrsPppp4= github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo= @@ -346,6 +348,7 @@ github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -460,7 +463,6 @@ github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc= -github.com/graph-gophers/graphql-go v0.0.0-20201113091052-beb923fada29 h1:sezaKhEfPFg8W0Enm61B9Gs911H8iesGY5R8NDPtd1M= github.com/graph-gophers/graphql-go v0.0.0-20201113091052-beb923fada29/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc= github.com/graph-gophers/graphql-go v1.3.0 h1:Eb9x/q6MFpCLz7jBCiP/WTxjSDrYLR1QY41SORZyNJ0= github.com/graph-gophers/graphql-go v1.3.0/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc= @@ -765,6 +767,7 @@ github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c= +github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65 h1:DadwsjnMwFjfWc9y5Wi/+Zz7xoE5ALHsRQlOctkOiHc= github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= @@ -1305,9 +1308,7 @@ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/Qd github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= -github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= -github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA= -github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw= github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= @@ -1591,6 +1592,7 @@ github.com/shirou/gopsutil v3.21.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shopspring/decimal v0.0.0-20200419222939-1884f454f8ea/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= +github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY= github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM= @@ -1717,8 +1719,6 @@ github.com/vulcanize/eth-ipfs-state-validator v0.1.0 h1:ZB54GOUrxQeSYvOmqk8jMgtG github.com/vulcanize/eth-ipfs-state-validator v0.1.0/go.mod h1:QtmfhqDjN29UHHk2Fb+ouzO4j/lbhM7GuiEmxHIKdzk= github.com/vulcanize/gap-filler v0.3.1 h1:N5d+jCJo/VTWFvBSbTD7biRhK/OqDZzi1tgA85SIBKs= github.com/vulcanize/gap-filler v0.3.1/go.mod h1:qowG1cgshVpBqMokiWro/1xhh0uypw7oAu8FQ42JMy4= -github.com/vulcanize/go-ethereum v1.10.15-statediff-2.0.0 h1:/BiYPUHnubh46YVtASGt4MPlFR96Rc+iJuTyOI8KZa4= -github.com/vulcanize/go-ethereum v1.10.15-statediff-2.0.0/go.mod h1:9L+QY31AnWnX2/2HDOySCjQoYUdWNGBRMezFJVfH73E= github.com/vulcanize/go-ethereum v1.10.16-statediff-3.0.2 h1:H3SLHZdvTyKYbFc1CO2b8A9XF3BcakcXtvThKPbgT8k= github.com/vulcanize/go-ethereum v1.10.16-statediff-3.0.2/go.mod h1:NI+tCVeIQBPrMfJUZvTLjhCieb7CZcmNPbJVlXbncxU= github.com/vulcanize/ipfs-ethdb v0.0.6 h1:Jl+YHtee5Zd8jD9wix2aJLYXwX/WpPt37QBzoz8pVwM= diff --git a/pkg/eth/api_test.go b/pkg/eth/api_test.go index 203a7572..38c42161 100644 --- a/pkg/eth/api_test.go +++ b/pkg/eth/api_test.go @@ -19,7 +19,6 @@ package eth_test import ( "context" "math/big" - "os" "strconv" "github.com/ethereum/go-ethereum/common" @@ -31,10 +30,9 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" - "github.com/ethereum/go-ethereum/statediff/indexer" - "github.com/ethereum/go-ethereum/statediff/indexer/node" - "github.com/ethereum/go-ethereum/statediff/indexer/postgres" + "github.com/ethereum/go-ethereum/statediff/indexer/interfaces" sdtypes "github.com/ethereum/go-ethereum/statediff/types" + "github.com/jmoiron/sqlx" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -183,22 +181,9 @@ var ( } ) -// SetupDB is use to setup a db for watcher tests -func SetupDB() (*postgres.DB, error) { - port, _ := strconv.Atoi(os.Getenv("DATABASE_PORT")) - uri := postgres.DbConnectionString(postgres.ConnectionParams{ - User: os.Getenv("DATABASE_USER"), - Password: os.Getenv("DATABASE_PASSWORD"), - Hostname: os.Getenv("DATABASE_HOSTNAME"), - Name: os.Getenv("DATABASE_NAME"), - Port: port, - }) - return postgres.NewDB(uri, postgres.ConnectionConfig{}, node.Info{}) -} - var _ = Describe("API", func() { var ( - db *postgres.DB + db *sqlx.DB api *eth.PublicEthAPI chainConfig = params.TestChainConfig ) @@ -207,14 +192,12 @@ var _ = Describe("API", func() { It("test init", func() { var ( err error - tx *indexer.BlockTx + tx interfaces.Batch ) - db, err = SetupDB() - Expect(err).ToNot(HaveOccurred()) + db = eth.SetupTestDB() + indexAndPublisher := eth.SetupTestStateDiffIndexer(ctx, chainConfig, test_helpers.Genesis.Hash()) - indexAndPublisher, err := indexer.NewStateDiffIndexer(chainConfig, db) - Expect(err).ToNot(HaveOccurred()) backend, err := eth.NewEthBackend(db, ð.Config{ ChainConfig: chainConfig, VMConfig: vm.Config{}, @@ -233,11 +216,6 @@ var _ = Describe("API", func() { tx, err = indexAndPublisher.PushBlock(test_helpers.MockBlock, test_helpers.MockReceipts, test_helpers.MockBlock.Difficulty()) Expect(err).ToNot(HaveOccurred()) - for _, node := range test_helpers.MockStateNodes { - err = indexAndPublisher.PushStateNode(tx, node) - Expect(err).ToNot(HaveOccurred()) - } - ccHash := sdtypes.CodeAndCodeHash{ Hash: test_helpers.ContractCodeHash, Code: test_helpers.ContractCode, @@ -246,7 +224,12 @@ var _ = Describe("API", func() { err = indexAndPublisher.PushCodeAndCodeHash(tx, ccHash) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + for _, node := range test_helpers.MockStateNodes { + err = indexAndPublisher.PushStateNode(tx, node, test_helpers.MockBlock.Hash().String()) + Expect(err).ToNot(HaveOccurred()) + } + + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) uncles := test_helpers.MockBlock.Uncles() @@ -258,18 +241,17 @@ var _ = Describe("API", func() { // setting chain config to for london block chainConfig.LondonBlock = big.NewInt(2) - indexAndPublisher, err = indexer.NewStateDiffIndexer(chainConfig, db) - Expect(err).ToNot(HaveOccurred()) + indexAndPublisher = eth.SetupTestStateDiffIndexer(ctx, chainConfig, test_helpers.Genesis.Hash()) tx, err = indexAndPublisher.PushBlock(test_helpers.MockLondonBlock, test_helpers.MockLondonReceipts, test_helpers.MockLondonBlock.Difficulty()) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) }) // Single test db tear down at end of all tests - defer It("test teardown", func() { eth.TearDownDB(db) }) + defer It("test teardown", func() { eth.TearDownTestDB(db) }) /* Headers and blocks @@ -287,8 +269,6 @@ var _ = Describe("API", func() { Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("sql: no rows in result set")) Expect(header).To(BeNil()) - _, err = api.B.DB.Beginx() - Expect(err).ToNot(HaveOccurred()) }) }) diff --git a/pkg/eth/cid_retriever_test.go b/pkg/eth/cid_retriever_test.go index f6b74b99..ff8d0698 100644 --- a/pkg/eth/cid_retriever_test.go +++ b/pkg/eth/cid_retriever_test.go @@ -19,14 +19,13 @@ package eth_test import ( "math/big" - "github.com/ethereum/go-ethereum/params" - "github.com/ethereum/go-ethereum/statediff/indexer" - "github.com/ethereum/go-ethereum/statediff/indexer/models" - "github.com/ethereum/go-ethereum/statediff/indexer/postgres" - "github.com/ethereum/go-ethereum/trie" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/statediff/indexer/interfaces" + "github.com/ethereum/go-ethereum/statediff/indexer/models" + "github.com/ethereum/go-ethereum/trie" + "github.com/jmoiron/sqlx" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -211,21 +210,18 @@ var ( var _ = Describe("Retriever", func() { var ( - db *postgres.DB - diffIndexer *indexer.StateDiffIndexer + db *sqlx.DB + diffIndexer interfaces.StateDiffIndexer retriever *eth.CIDRetriever ) BeforeEach(func() { - var err error - db, err = SetupDB() - Expect(err).ToNot(HaveOccurred()) - diffIndexer, err = indexer.NewStateDiffIndexer(params.TestChainConfig, db) - Expect(err).ToNot(HaveOccurred()) + db = eth.SetupTestDB() + diffIndexer = eth.SetupTestStateDiffIndexer(ctx, params.TestChainConfig, test_helpers.Genesis.Hash()) retriever = eth.NewCIDRetriever(db) }) AfterEach(func() { - eth.TearDownDB(db) + eth.TearDownTestDB(db) }) Describe("Retrieve", func() { @@ -233,11 +229,11 @@ var _ = Describe("Retriever", func() { tx, err := diffIndexer.PushBlock(test_helpers.MockBlock, test_helpers.MockReceipts, test_helpers.MockBlock.Difficulty()) Expect(err).ToNot(HaveOccurred()) for _, node := range test_helpers.MockStateNodes { - err = diffIndexer.PushStateNode(tx, node) + err = diffIndexer.PushStateNode(tx, node, test_helpers.MockBlock.Hash().String()) Expect(err).ToNot(HaveOccurred()) } - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) }) It("Retrieves all CIDs for the given blocknumber when provided an open filter", func() { @@ -247,8 +243,8 @@ var _ = Describe("Retriever", func() { } expectedRctCIDsAndLeafNodes := make([]rctCIDAndMHKeyResult, 0) pgStr := `SELECT receipt_cids.leaf_cid, receipt_cids.leaf_mh_key FROM eth.receipt_cids, eth.transaction_cids, eth.header_cids - WHERE receipt_cids.tx_id = transaction_cids.id - AND transaction_cids.header_id = header_cids.id + WHERE receipt_cids.tx_id = transaction_cids.tx_hash + AND transaction_cids.header_id = header_cids.block_hash AND header_cids.block_number = $1 ORDER BY transaction_cids.index` err := db.Select(&expectedRctCIDsAndLeafNodes, pgStr, test_helpers.BlockNumber.Uint64()) @@ -259,7 +255,7 @@ var _ = Describe("Retriever", func() { Expect(cids[0].BlockNumber).To(Equal(test_helpers.MockCIDWrapper.BlockNumber)) expectedHeaderCID := test_helpers.MockCIDWrapper.Header - expectedHeaderCID.ID = cids[0].Header.ID + expectedHeaderCID.BlockHash = cids[0].Header.BlockHash expectedHeaderCID.NodeID = cids[0].Header.NodeID Expect(cids[0].Header).To(Equal(expectedHeaderCID)) Expect(len(cids[0].Transactions)).To(Equal(4)) @@ -286,8 +282,8 @@ var _ = Describe("Retriever", func() { } Expect(len(cids[0].StorageNodes)).To(Equal(1)) expectedStorageNodeCIDs := test_helpers.MockCIDWrapper.StorageNodes - expectedStorageNodeCIDs[0].ID = cids[0].StorageNodes[0].ID - expectedStorageNodeCIDs[0].StateID = cids[0].StorageNodes[0].StateID + expectedStorageNodeCIDs[0].HeaderID = cids[0].StorageNodes[0].HeaderID + expectedStorageNodeCIDs[0].StatePath = cids[0].StorageNodes[0].StatePath Expect(cids[0].StorageNodes).To(Equal(expectedStorageNodeCIDs)) }) @@ -298,8 +294,8 @@ var _ = Describe("Retriever", func() { } expectedRctCIDsAndLeafNodes := make([]rctCIDAndMHKeyResult, 0) pgStr := `SELECT receipt_cids.leaf_cid, receipt_cids.leaf_mh_key FROM eth.receipt_cids, eth.transaction_cids, eth.header_cids - WHERE receipt_cids.tx_id = transaction_cids.id - AND transaction_cids.header_id = header_cids.id + WHERE receipt_cids.tx_id = transaction_cids.tx_hash + AND transaction_cids.header_id = header_cids.block_hash AND header_cids.block_number = $1 ORDER BY transaction_cids.index` err := db.Select(&expectedRctCIDsAndLeafNodes, pgStr, test_helpers.BlockNumber.Uint64()) @@ -314,7 +310,6 @@ var _ = Describe("Retriever", func() { Expect(len(cids1[0].StorageNodes)).To(Equal(0)) Expect(len(cids1[0].Receipts)).To(Equal(1)) expectedReceiptCID := test_helpers.MockCIDWrapper.Receipts[0] - expectedReceiptCID.ID = cids1[0].Receipts[0].ID expectedReceiptCID.TxID = cids1[0].Receipts[0].TxID expectedReceiptCID.LeafCID = expectedRctCIDsAndLeafNodes[0].LeafCID expectedReceiptCID.LeafMhKey = expectedRctCIDsAndLeafNodes[0].LeafMhKey @@ -331,7 +326,6 @@ var _ = Describe("Retriever", func() { Expect(len(cids2[0].StorageNodes)).To(Equal(0)) Expect(len(cids2[0].Receipts)).To(Equal(1)) expectedReceiptCID = test_helpers.MockCIDWrapper.Receipts[0] - expectedReceiptCID.ID = cids2[0].Receipts[0].ID expectedReceiptCID.TxID = cids2[0].Receipts[0].TxID expectedReceiptCID.LeafCID = expectedRctCIDsAndLeafNodes[0].LeafCID expectedReceiptCID.LeafMhKey = expectedRctCIDsAndLeafNodes[0].LeafMhKey @@ -348,7 +342,6 @@ var _ = Describe("Retriever", func() { Expect(len(cids3[0].StorageNodes)).To(Equal(0)) Expect(len(cids3[0].Receipts)).To(Equal(1)) expectedReceiptCID = test_helpers.MockCIDWrapper.Receipts[0] - expectedReceiptCID.ID = cids3[0].Receipts[0].ID expectedReceiptCID.TxID = cids3[0].Receipts[0].TxID expectedReceiptCID.LeafCID = expectedRctCIDsAndLeafNodes[0].LeafCID expectedReceiptCID.LeafMhKey = expectedRctCIDsAndLeafNodes[0].LeafMhKey @@ -365,7 +358,6 @@ var _ = Describe("Retriever", func() { Expect(len(cids4[0].StorageNodes)).To(Equal(0)) Expect(len(cids4[0].Receipts)).To(Equal(1)) expectedReceiptCID = test_helpers.MockCIDWrapper.Receipts[1] - expectedReceiptCID.ID = cids4[0].Receipts[0].ID expectedReceiptCID.TxID = cids4[0].Receipts[0].TxID expectedReceiptCID.LeafCID = expectedRctCIDsAndLeafNodes[1].LeafCID expectedReceiptCID.LeafMhKey = expectedRctCIDsAndLeafNodes[1].LeafMhKey @@ -396,14 +388,13 @@ var _ = Describe("Retriever", func() { Expect(cids6[0].Header).To(Equal(models.HeaderModel{})) Expect(len(cids6[0].Transactions)).To(Equal(1)) expectedTxCID := test_helpers.MockCIDWrapper.Transactions[1] - expectedTxCID.ID = cids6[0].Transactions[0].ID + expectedTxCID.TxHash = cids6[0].Transactions[0].TxHash expectedTxCID.HeaderID = cids6[0].Transactions[0].HeaderID Expect(cids6[0].Transactions[0]).To(Equal(expectedTxCID)) Expect(len(cids6[0].StateNodes)).To(Equal(0)) Expect(len(cids6[0].StorageNodes)).To(Equal(0)) Expect(len(cids6[0].Receipts)).To(Equal(1)) expectedReceiptCID = test_helpers.MockCIDWrapper.Receipts[1] - expectedReceiptCID.ID = cids6[0].Receipts[0].ID expectedReceiptCID.TxID = cids6[0].Receipts[0].TxID expectedReceiptCID.LeafCID = expectedRctCIDsAndLeafNodes[1].LeafCID expectedReceiptCID.LeafMhKey = expectedRctCIDsAndLeafNodes[1].LeafMhKey @@ -420,7 +411,6 @@ var _ = Describe("Retriever", func() { Expect(len(cids7[0].StorageNodes)).To(Equal(0)) Expect(len(cids7[0].StateNodes)).To(Equal(1)) Expect(cids7[0].StateNodes[0]).To(Equal(models.StateNodeModel{ - ID: cids7[0].StateNodes[0].ID, HeaderID: cids7[0].StateNodes[0].HeaderID, NodeType: 2, StateKey: common.BytesToHash(test_helpers.AccountLeafKey).Hex(), @@ -444,7 +434,7 @@ var _ = Describe("Retriever", func() { tx, err := diffIndexer.PushBlock(test_helpers.MockBlock, test_helpers.MockReceipts, test_helpers.MockBlock.Difficulty()) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) num, err := retriever.RetrieveFirstBlockNumber() @@ -458,7 +448,7 @@ var _ = Describe("Retriever", func() { tx, err := diffIndexer.PushBlock(payload.Block, payload.Receipts, payload.Block.Difficulty()) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) num, err := retriever.RetrieveFirstBlockNumber() @@ -473,12 +463,12 @@ var _ = Describe("Retriever", func() { payload2.Block = newMockBlock(5) tx, err := diffIndexer.PushBlock(payload1.Block, payload1.Receipts, payload1.Block.Difficulty()) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) tx, err = diffIndexer.PushBlock(payload2.Block, payload2.Receipts, payload2.Block.Difficulty()) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) num, err := retriever.RetrieveFirstBlockNumber() @@ -495,7 +485,7 @@ var _ = Describe("Retriever", func() { It("Gets the number of the latest block that has data in the database", func() { tx, err := diffIndexer.PushBlock(test_helpers.MockBlock, test_helpers.MockReceipts, test_helpers.MockBlock.Difficulty()) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) num, err := retriever.RetrieveLastBlockNumber() @@ -509,7 +499,7 @@ var _ = Describe("Retriever", func() { tx, err := diffIndexer.PushBlock(payload.Block, payload.Receipts, payload.Block.Difficulty()) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) num, err := retriever.RetrieveLastBlockNumber() @@ -524,12 +514,12 @@ var _ = Describe("Retriever", func() { payload2.Block = newMockBlock(5) tx, err := diffIndexer.PushBlock(payload1.Block, payload1.Receipts, payload1.Block.Difficulty()) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) tx, err = diffIndexer.PushBlock(payload2.Block, payload2.Receipts, payload2.Block.Difficulty()) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) num, err := retriever.RetrieveLastBlockNumber() diff --git a/pkg/eth/eth_state_test.go b/pkg/eth/eth_state_test.go index 581c0628..74f703c7 100644 --- a/pkg/eth/eth_state_test.go +++ b/pkg/eth/eth_state_test.go @@ -21,6 +21,7 @@ import ( "context" "io/ioutil" "math/big" + "time" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" @@ -31,9 +32,8 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/statediff" - "github.com/ethereum/go-ethereum/statediff/indexer" - "github.com/ethereum/go-ethereum/statediff/indexer/postgres" sdtypes "github.com/ethereum/go-ethereum/statediff/types" + "github.com/jmoiron/sqlx" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -64,7 +64,7 @@ var _ = Describe("eth state reading tests", func() { blocks []*types.Block receipts []types.Receipts chain *core.BlockChain - db *postgres.DB + db *sqlx.DB api *eth.PublicEthAPI backend *eth.Backend chainConfig = params.TestChainConfig @@ -74,11 +74,8 @@ var _ = Describe("eth state reading tests", func() { It("test init", func() { // db and type initializations var err error - db, err = SetupDB() - Expect(err).ToNot(HaveOccurred()) - - transformer, err := indexer.NewStateDiffIndexer(chainConfig, db) - Expect(err).ToNot(HaveOccurred()) + db = eth.SetupTestDB() + transformer := eth.SetupTestStateDiffIndexer(ctx, chainConfig, test_helpers.Genesis.Hash()) backend, err = eth.NewEthBackend(db, ð.Config{ ChainConfig: chainConfig, @@ -150,21 +147,20 @@ var _ = Describe("eth state reading tests", func() { Expect(err).ToNot(HaveOccurred()) for _, node := range diff.Nodes { - err = transformer.PushStateNode(tx, node) + err = transformer.PushStateNode(tx, node, block.Hash().String()) Expect(err).ToNot(HaveOccurred()) } - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) } // Insert some non-canonical data into the database so that we test our ability to discern canonicity - indexAndPublisher, err := indexer.NewStateDiffIndexer(chainConfig, db) - Expect(err).ToNot(HaveOccurred()) + indexAndPublisher := eth.SetupTestStateDiffIndexer(ctx, chainConfig, test_helpers.Genesis.Hash()) tx, err := indexAndPublisher.PushBlock(test_helpers.MockBlock, test_helpers.MockReceipts, test_helpers.MockBlock.Difficulty()) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) // The non-canonical header has a child @@ -179,11 +175,13 @@ var _ = Describe("eth state reading tests", func() { err = indexAndPublisher.PushCodeAndCodeHash(tx, hash) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + // wait for tx batch process to complete. + time.Sleep(600 * time.Millisecond) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) }) defer It("test teardown", func() { - eth.TearDownDB(db) + eth.TearDownTestDB(db) chain.Stop() }) diff --git a/pkg/eth/filterer_test.go b/pkg/eth/filterer_test.go index 84f4de44..6894f99c 100644 --- a/pkg/eth/filterer_test.go +++ b/pkg/eth/filterer_test.go @@ -19,7 +19,7 @@ package eth_test import ( "bytes" - "github.com/ethereum/go-ethereum/statediff/indexer/ipfs" + "github.com/ethereum/go-ethereum/statediff/indexer/models" sdtypes "github.com/ethereum/go-ethereum/statediff/types" . "github.com/onsi/ginkgo" @@ -46,7 +46,7 @@ var _ = Describe("Filterer", func() { Expect(iplds).ToNot(BeNil()) Expect(iplds.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) Expect(iplds.Header).To(Equal(test_helpers.MockIPLDs.Header)) - var expectedEmptyUncles []ipfs.BlockModel + var expectedEmptyUncles []models.IPLDModel Expect(iplds.Uncles).To(Equal(expectedEmptyUncles)) Expect(len(iplds.Transactions)).To(Equal(4)) Expect(shared.IPLDsContainBytes(iplds.Transactions, test_helpers.Tx1)).To(BeTrue()) @@ -60,15 +60,15 @@ var _ = Describe("Filterer", func() { for _, stateNode := range iplds.StateNodes { Expect(stateNode.Type).To(Equal(sdtypes.Leaf)) if bytes.Equal(stateNode.StateLeafKey.Bytes(), test_helpers.AccountLeafKey) { - Expect(stateNode.IPLD).To(Equal(ipfs.BlockModel{ + Expect(stateNode.IPLD).To(Equal(models.IPLDModel{ Data: test_helpers.State2IPLD.RawData(), - CID: test_helpers.State2IPLD.Cid().String(), + Key: test_helpers.State2IPLD.Cid().String(), })) } if bytes.Equal(stateNode.StateLeafKey.Bytes(), test_helpers.ContractLeafKey) { - Expect(stateNode.IPLD).To(Equal(ipfs.BlockModel{ + Expect(stateNode.IPLD).To(Equal(models.IPLDModel{ Data: test_helpers.State1IPLD.RawData(), - CID: test_helpers.State1IPLD.Cid().String(), + Key: test_helpers.State1IPLD.Cid().String(), })) } } @@ -80,67 +80,67 @@ var _ = Describe("Filterer", func() { Expect(err).ToNot(HaveOccurred()) Expect(iplds1).ToNot(BeNil()) Expect(iplds1.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) - Expect(iplds1.Header).To(Equal(ipfs.BlockModel{})) + Expect(iplds1.Header).To(Equal(models.IPLDModel{})) Expect(len(iplds1.Uncles)).To(Equal(0)) Expect(len(iplds1.Transactions)).To(Equal(0)) Expect(len(iplds1.StorageNodes)).To(Equal(0)) Expect(len(iplds1.StateNodes)).To(Equal(0)) Expect(len(iplds1.Receipts)).To(Equal(1)) - Expect(iplds1.Receipts[0]).To(Equal(ipfs.BlockModel{ + Expect(iplds1.Receipts[0]).To(Equal(models.IPLDModel{ Data: test_helpers.Rct1IPLD, - CID: test_helpers.Rct1CID.String(), + Key: test_helpers.Rct1CID.String(), })) iplds2, err := filterer.Filter(rctTopicsFilter, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds2).ToNot(BeNil()) Expect(iplds2.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) - Expect(iplds2.Header).To(Equal(ipfs.BlockModel{})) + Expect(iplds2.Header).To(Equal(models.IPLDModel{})) Expect(len(iplds2.Uncles)).To(Equal(0)) Expect(len(iplds2.Transactions)).To(Equal(0)) Expect(len(iplds2.StorageNodes)).To(Equal(0)) Expect(len(iplds2.StateNodes)).To(Equal(0)) Expect(len(iplds2.Receipts)).To(Equal(1)) - Expect(iplds2.Receipts[0]).To(Equal(ipfs.BlockModel{ + Expect(iplds2.Receipts[0]).To(Equal(models.IPLDModel{ Data: test_helpers.Rct1IPLD, - CID: test_helpers.Rct1CID.String(), + Key: test_helpers.Rct1CID.String(), })) iplds3, err := filterer.Filter(rctTopicsAndAddressFilter, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds3).ToNot(BeNil()) Expect(iplds3.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) - Expect(iplds3.Header).To(Equal(ipfs.BlockModel{})) + Expect(iplds3.Header).To(Equal(models.IPLDModel{})) Expect(len(iplds3.Uncles)).To(Equal(0)) Expect(len(iplds3.Transactions)).To(Equal(0)) Expect(len(iplds3.StorageNodes)).To(Equal(0)) Expect(len(iplds3.StateNodes)).To(Equal(0)) Expect(len(iplds3.Receipts)).To(Equal(1)) - Expect(iplds3.Receipts[0]).To(Equal(ipfs.BlockModel{ + Expect(iplds3.Receipts[0]).To(Equal(models.IPLDModel{ Data: test_helpers.Rct1IPLD, - CID: test_helpers.Rct1CID.String(), + Key: test_helpers.Rct1CID.String(), })) iplds4, err := filterer.Filter(rctAddressesAndTopicFilter, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds4).ToNot(BeNil()) Expect(iplds4.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) - Expect(iplds4.Header).To(Equal(ipfs.BlockModel{})) + Expect(iplds4.Header).To(Equal(models.IPLDModel{})) Expect(len(iplds4.Uncles)).To(Equal(0)) Expect(len(iplds4.Transactions)).To(Equal(0)) Expect(len(iplds4.StorageNodes)).To(Equal(0)) Expect(len(iplds4.StateNodes)).To(Equal(0)) Expect(len(iplds4.Receipts)).To(Equal(1)) - Expect(iplds4.Receipts[0]).To(Equal(ipfs.BlockModel{ + Expect(iplds4.Receipts[0]).To(Equal(models.IPLDModel{ Data: test_helpers.Rct2IPLD, - CID: test_helpers.Rct2CID.String(), + Key: test_helpers.Rct2CID.String(), })) iplds5, err := filterer.Filter(rctsForAllCollectedTrxs, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds5).ToNot(BeNil()) Expect(iplds5.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) - Expect(iplds5.Header).To(Equal(ipfs.BlockModel{})) + Expect(iplds5.Header).To(Equal(models.IPLDModel{})) Expect(len(iplds5.Uncles)).To(Equal(0)) Expect(len(iplds5.Transactions)).To(Equal(4)) Expect(shared.IPLDsContainBytes(iplds5.Transactions, test_helpers.Tx1)).To(BeTrue()) @@ -157,39 +157,39 @@ var _ = Describe("Filterer", func() { Expect(err).ToNot(HaveOccurred()) Expect(iplds6).ToNot(BeNil()) Expect(iplds6.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) - Expect(iplds6.Header).To(Equal(ipfs.BlockModel{})) + Expect(iplds6.Header).To(Equal(models.IPLDModel{})) Expect(len(iplds6.Uncles)).To(Equal(0)) Expect(len(iplds6.Transactions)).To(Equal(1)) Expect(shared.IPLDsContainBytes(iplds5.Transactions, test_helpers.Tx2)).To(BeTrue()) Expect(len(iplds6.StorageNodes)).To(Equal(0)) Expect(len(iplds6.StateNodes)).To(Equal(0)) Expect(len(iplds6.Receipts)).To(Equal(1)) - Expect(iplds4.Receipts[0]).To(Equal(ipfs.BlockModel{ + Expect(iplds4.Receipts[0]).To(Equal(models.IPLDModel{ Data: test_helpers.Rct2IPLD, - CID: test_helpers.Rct2CID.String(), + Key: test_helpers.Rct2CID.String(), })) iplds7, err := filterer.Filter(stateFilter, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds7).ToNot(BeNil()) Expect(iplds7.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) - Expect(iplds7.Header).To(Equal(ipfs.BlockModel{})) + Expect(iplds7.Header).To(Equal(models.IPLDModel{})) Expect(len(iplds7.Uncles)).To(Equal(0)) Expect(len(iplds7.Transactions)).To(Equal(0)) Expect(len(iplds7.StorageNodes)).To(Equal(0)) Expect(len(iplds7.Receipts)).To(Equal(0)) Expect(len(iplds7.StateNodes)).To(Equal(1)) Expect(iplds7.StateNodes[0].StateLeafKey.Bytes()).To(Equal(test_helpers.AccountLeafKey)) - Expect(iplds7.StateNodes[0].IPLD).To(Equal(ipfs.BlockModel{ + Expect(iplds7.StateNodes[0].IPLD).To(Equal(models.IPLDModel{ Data: test_helpers.State2IPLD.RawData(), - CID: test_helpers.State2IPLD.Cid().String(), + Key: test_helpers.State2IPLD.Cid().String(), })) iplds8, err := filterer.Filter(rctTopicsAndAddressFilterFail, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds8).ToNot(BeNil()) Expect(iplds8.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) - Expect(iplds8.Header).To(Equal(ipfs.BlockModel{})) + Expect(iplds8.Header).To(Equal(models.IPLDModel{})) Expect(len(iplds8.Uncles)).To(Equal(0)) Expect(len(iplds8.Transactions)).To(Equal(0)) Expect(len(iplds8.StorageNodes)).To(Equal(0)) diff --git a/pkg/eth/ipld_fetcher_test.go b/pkg/eth/ipld_fetcher_test.go index c6699a09..f5521188 100644 --- a/pkg/eth/ipld_fetcher_test.go +++ b/pkg/eth/ipld_fetcher_test.go @@ -18,8 +18,8 @@ package eth_test import ( "github.com/ethereum/go-ethereum/params" - "github.com/ethereum/go-ethereum/statediff/indexer" - "github.com/ethereum/go-ethereum/statediff/indexer/postgres" + "github.com/ethereum/go-ethereum/statediff/indexer/interfaces" + "github.com/jmoiron/sqlx" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -29,34 +29,32 @@ import ( var _ = Describe("IPLDFetcher", func() { var ( - db *postgres.DB - pubAndIndexer *indexer.StateDiffIndexer + db *sqlx.DB + pubAndIndexer interfaces.StateDiffIndexer fetcher *eth.IPLDFetcher ) Describe("Fetch", func() { BeforeEach(func() { var ( err error - tx *indexer.BlockTx + tx interfaces.Batch ) - db, err = SetupDB() - Expect(err).ToNot(HaveOccurred()) - pubAndIndexer, err = indexer.NewStateDiffIndexer(params.TestChainConfig, db) - Expect(err).ToNot(HaveOccurred()) + db = eth.SetupTestDB() + pubAndIndexer = eth.SetupTestStateDiffIndexer(ctx, params.TestChainConfig, test_helpers.Genesis.Hash()) tx, err = pubAndIndexer.PushBlock(test_helpers.MockBlock, test_helpers.MockReceipts, test_helpers.MockBlock.Difficulty()) for _, node := range test_helpers.MockStateNodes { - err = pubAndIndexer.PushStateNode(tx, node) + err = pubAndIndexer.PushStateNode(tx, node, test_helpers.MockBlock.Hash().String()) Expect(err).ToNot(HaveOccurred()) } - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) fetcher = eth.NewIPLDFetcher(db) }) AfterEach(func() { - eth.TearDownDB(db) + eth.TearDownTestDB(db) }) It("Fetches and returns IPLDs for the CIDs provided in the CIDWrapper", func() { diff --git a/pkg/eth/test_helpers.go b/pkg/eth/test_helpers.go index 6aef4ab0..c609c121 100644 --- a/pkg/eth/test_helpers.go +++ b/pkg/eth/test_helpers.go @@ -17,17 +17,34 @@ package eth import ( + "context" + "os" + "strconv" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/statediff/indexer" + "github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres" + "github.com/ethereum/go-ethereum/statediff/indexer/interfaces" "github.com/ethereum/go-ethereum/statediff/indexer/models" + "github.com/ethereum/go-ethereum/statediff/indexer/node" "github.com/jmoiron/sqlx" . "github.com/onsi/gomega" + "github.com/vulcanize/ipld-eth-server/pkg/shared" ) -// TearDownDB is used to tear down the watcher dbs after tests -func TearDownDB(db *sqlx.DB) { - tx, err := db.Beginx() +func SetupTestDB() *sqlx.DB { + config := getTestDBConfig() + + db, err := shared.NewDB(config.DbConnectionString(), config) Expect(err).NotTo(HaveOccurred()) - _, err = tx.Exec(`DELETE FROM eth.header_cids`) + return db +} + +// TearDownTestDB is used to tear down the watcher dbs after tests +func TearDownTestDB(db *sqlx.DB) { + tx, err := db.Beginx() Expect(err).NotTo(HaveOccurred()) _, err = tx.Exec(`DELETE FROM eth.transaction_cids`) Expect(err).NotTo(HaveOccurred()) @@ -46,6 +63,21 @@ func TearDownDB(db *sqlx.DB) { Expect(err).NotTo(HaveOccurred()) } +func SetupTestStateDiffIndexer(ctx context.Context, chainConfig *params.ChainConfig, genHash common.Hash) interfaces.StateDiffIndexer { + testInfo := node.Info{ + GenesisBlock: genHash.String(), + NetworkID: "1", + ID: "1", + ClientName: "geth", + ChainID: params.TestChainConfig.ChainID.Uint64(), + } + + stateDiffIndexer, err := indexer.NewStateDiffIndexer(ctx, chainConfig, testInfo, getTestDBConfig()) + Expect(err).NotTo(HaveOccurred()) + + return stateDiffIndexer +} + // TxModelsContainsCID used to check if a list of TxModels contains a specific cid string func TxModelsContainsCID(txs []models.TxModel, cid string) bool { for _, tx := range txs { @@ -65,3 +97,15 @@ func ReceiptModelsContainsCID(rcts []models.ReceiptModel, cid string) bool { } return false } + +func getTestDBConfig() postgres.Config { + port, _ := strconv.Atoi(os.Getenv("DATABASE_PORT")) + return postgres.Config{ + Hostname: os.Getenv("DATABASE_HOSTNAME"), + DatabaseName: os.Getenv("DATABASE_NAME"), + Username: os.Getenv("DATABASE_USER"), + Password: os.Getenv("DATABASE_PASSWORD"), + Port: port, + Driver: postgres.SQLX, + } +} diff --git a/pkg/eth/test_helpers/test_data.go b/pkg/eth/test_helpers/test_data.go index fff0806b..eae6e7d6 100644 --- a/pkg/eth/test_helpers/test_data.go +++ b/pkg/eth/test_helpers/test_data.go @@ -28,13 +28,10 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" - "github.com/ethereum/go-ethereum/statediff/indexer" - "github.com/ethereum/go-ethereum/statediff/indexer/ipfs" - "github.com/ethereum/go-ethereum/statediff/indexer/ipfs/ipld" + "github.com/ethereum/go-ethereum/statediff/indexer/ipld" "github.com/ethereum/go-ethereum/statediff/indexer/models" - "github.com/ethereum/go-ethereum/statediff/indexer/postgres" "github.com/ethereum/go-ethereum/statediff/indexer/shared" - "github.com/ethereum/go-ethereum/statediff/testhelpers" + testhelpers "github.com/ethereum/go-ethereum/statediff/test_helpers" sdtypes "github.com/ethereum/go-ethereum/statediff/types" "github.com/ethereum/go-ethereum/trie" blocks "github.com/ipfs/go-block-format" @@ -47,7 +44,6 @@ import ( // Test variables var ( // block data - db *postgres.DB BlockNumber = big.NewInt(1) MockHeader = types.Header{ Time: 0, @@ -439,8 +435,7 @@ var ( StateNodes: MockStateNodes, } - Reward = indexer.CalcEthBlockReward(MockBlock.Header(), MockBlock.Uncles(), MockBlock.Transactions(), MockReceipts) - + Reward = shared.CalcEthBlockReward(MockBlock.Header(), MockBlock.Uncles(), MockBlock.Transactions(), MockReceipts) MockCIDWrapper = ð.CIDWrapper{ BlockNumber: new(big.Int).Set(BlockNumber), Header: models.HeaderModel{ @@ -486,62 +481,62 @@ var ( MockIPLDs = eth.IPLDs{ BlockNumber: new(big.Int).Set(BlockNumber), - Header: ipfs.BlockModel{ + Header: models.IPLDModel{ Data: HeaderIPLD.RawData(), - CID: HeaderIPLD.Cid().String(), + Key: HeaderIPLD.Cid().String(), }, - Transactions: []ipfs.BlockModel{ + Transactions: []models.IPLDModel{ { Data: Trx1IPLD.RawData(), - CID: Trx1IPLD.Cid().String(), + Key: Trx1IPLD.Cid().String(), }, { Data: Trx2IPLD.RawData(), - CID: Trx2IPLD.Cid().String(), + Key: Trx2IPLD.Cid().String(), }, { Data: Trx3IPLD.RawData(), - CID: Trx3IPLD.Cid().String(), + Key: Trx3IPLD.Cid().String(), }, { Data: Trx4IPLD.RawData(), - CID: Trx4IPLD.Cid().String(), + Key: Trx4IPLD.Cid().String(), }, }, - Receipts: []ipfs.BlockModel{ + Receipts: []models.IPLDModel{ { Data: Rct1IPLD, - CID: Rct1CID.String(), + Key: Rct1CID.String(), }, { Data: Rct2IPLD, - CID: Rct2CID.String(), + Key: Rct2CID.String(), }, { Data: Rct3IPLD, - CID: Rct3CID.String(), + Key: Rct3CID.String(), }, { Data: Rct4IPLD, - CID: Rct4CID.String(), + Key: Rct4CID.String(), }, }, StateNodes: []eth.StateNode{ { StateLeafKey: common.BytesToHash(ContractLeafKey), Type: sdtypes.Leaf, - IPLD: ipfs.BlockModel{ + IPLD: models.IPLDModel{ Data: State1IPLD.RawData(), - CID: State1IPLD.Cid().String(), + Key: State1IPLD.Cid().String(), }, Path: []byte{'\x06'}, }, { StateLeafKey: common.BytesToHash(AccountLeafKey), Type: sdtypes.Leaf, - IPLD: ipfs.BlockModel{ + IPLD: models.IPLDModel{ Data: State2IPLD.RawData(), - CID: State2IPLD.Cid().String(), + Key: State2IPLD.Cid().String(), }, Path: []byte{'\x0c'}, }, @@ -551,9 +546,9 @@ var ( StateLeafKey: common.BytesToHash(ContractLeafKey), StorageLeafKey: common.BytesToHash(StorageLeafKey), Type: sdtypes.Leaf, - IPLD: ipfs.BlockModel{ + IPLD: models.IPLDModel{ Data: StorageIPLD.RawData(), - CID: StorageIPLD.Cid().String(), + Key: StorageIPLD.Cid().String(), }, Path: []byte{}, }, diff --git a/pkg/graphql/graphql_test.go b/pkg/graphql/graphql_test.go index ceb87c9d..dec0d0dd 100644 --- a/pkg/graphql/graphql_test.go +++ b/pkg/graphql/graphql_test.go @@ -20,8 +20,6 @@ import ( "context" "fmt" "math/big" - "os" - "strconv" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -32,10 +30,8 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/statediff" - "github.com/ethereum/go-ethereum/statediff/indexer" - "github.com/ethereum/go-ethereum/statediff/indexer/node" - "github.com/ethereum/go-ethereum/statediff/indexer/postgres" sdtypes "github.com/ethereum/go-ethereum/statediff/types" + "github.com/jmoiron/sqlx" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -45,19 +41,6 @@ import ( ethServerShared "github.com/vulcanize/ipld-eth-server/pkg/shared" ) -// SetupDB is use to setup a db for watcher tests -func SetupDB() (*postgres.DB, error) { - port, _ := strconv.Atoi(os.Getenv("DATABASE_PORT")) - uri := postgres.DbConnectionString(postgres.ConnectionParams{ - User: os.Getenv("DATABASE_USER"), - Password: os.Getenv("DATABASE_PASSWORD"), - Hostname: os.Getenv("DATABASE_HOSTNAME"), - Name: os.Getenv("DATABASE_NAME"), - Port: port, - }) - return postgres.NewDB(uri, postgres.ConnectionConfig{}, node.Info{}) -} - var _ = Describe("GraphQL", func() { const ( gqlEndPoint = "127.0.0.1:8083" @@ -68,7 +51,7 @@ var _ = Describe("GraphQL", func() { blocks []*types.Block receipts []types.Receipts chain *core.BlockChain - db *postgres.DB + db *sqlx.DB blockHashes []common.Hash backend *eth.Backend graphQLServer *graphql.Service @@ -82,11 +65,9 @@ var _ = Describe("GraphQL", func() { It("test init", func() { var err error - db, err = SetupDB() - Expect(err).ToNot(HaveOccurred()) + db = eth.SetupTestDB() + transformer := eth.SetupTestStateDiffIndexer(ctx, chainConfig, test_helpers.Genesis.Hash()) - transformer, err := indexer.NewStateDiffIndexer(chainConfig, db) - Expect(err).ToNot(HaveOccurred()) backend, err = eth.NewEthBackend(db, ð.Config{ ChainConfig: chainConfig, VMConfig: vm.Config{}, @@ -132,7 +113,7 @@ var _ = Describe("GraphQL", func() { rcts = receipts[i-1] } - var diff statediff.StateObject + var diff sdtypes.StateObject diff, err = builder.BuildStateDiffObject(args, params) Expect(err).ToNot(HaveOccurred()) @@ -140,17 +121,16 @@ var _ = Describe("GraphQL", func() { Expect(err).ToNot(HaveOccurred()) for _, node := range diff.Nodes { - err = transformer.PushStateNode(tx, node) + err = transformer.PushStateNode(tx, node, block.Hash().String()) Expect(err).ToNot(HaveOccurred()) } - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) } // Insert some non-canonical data into the database so that we test our ability to discern canonicity - indexAndPublisher, err := indexer.NewStateDiffIndexer(chainConfig, db) - Expect(err).ToNot(HaveOccurred()) + indexAndPublisher := eth.SetupTestStateDiffIndexer(ctx, chainConfig, test_helpers.Genesis.Hash()) blockHash = test_helpers.MockBlock.Hash() contractAddress = test_helpers.ContractAddr @@ -158,7 +138,7 @@ var _ = Describe("GraphQL", func() { tx, err := indexAndPublisher.PushBlock(test_helpers.MockBlock, test_helpers.MockReceipts, test_helpers.MockBlock.Difficulty()) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) // The non-canonical header has a child @@ -173,7 +153,7 @@ var _ = Describe("GraphQL", func() { err = indexAndPublisher.PushCodeAndCodeHash(tx, ccHash) Expect(err).ToNot(HaveOccurred()) - err = tx.Close(err) + err = tx.Submit(err) Expect(err).ToNot(HaveOccurred()) graphQLServer, err = graphql.New(backend, gqlEndPoint, nil, []string{"*"}, rpc.HTTPTimeouts{}) @@ -186,7 +166,7 @@ var _ = Describe("GraphQL", func() { defer It("test teardown", func() { err := graphQLServer.Stop() Expect(err).ToNot(HaveOccurred()) - eth.TearDownDB(db) + eth.TearDownTestDB(db) chain.Stop() })