From f9b0325cc001c121126c2103b64e25cd60f3e8ea Mon Sep 17 00:00:00 2001 From: Ian Norden Date: Thu, 8 Oct 2020 23:30:06 -0500 Subject: [PATCH] eth_call test --- pkg/eth/api.go | 15 +- pkg/eth/api_test.go | 229 ++++++++------- pkg/eth/backend.go | 2 +- pkg/eth/cid_retriever_test.go | 102 +++---- pkg/eth/eth_call_test.go | 165 +++++++++++ pkg/eth/filterer_test.go | 106 +++---- pkg/eth/ipld_fetcher_test.go | 31 +- pkg/eth/mocks/chain_maker.go | 284 ------------------- pkg/eth/test_helpers/abi.json | 47 +++ pkg/eth/test_helpers/chain_maker.go | 105 +++++++ pkg/eth/test_helpers/compiler_output.json | 6 + pkg/eth/test_helpers/test_contract.sol | 28 ++ pkg/eth/{mocks => test_helpers}/test_data.go | 84 +----- 13 files changed, 590 insertions(+), 614 deletions(-) create mode 100644 pkg/eth/eth_call_test.go delete mode 100644 pkg/eth/mocks/chain_maker.go create mode 100644 pkg/eth/test_helpers/abi.json create mode 100644 pkg/eth/test_helpers/chain_maker.go create mode 100644 pkg/eth/test_helpers/compiler_output.json create mode 100644 pkg/eth/test_helpers/test_contract.sol rename pkg/eth/{mocks => test_helpers}/test_data.go (86%) diff --git a/pkg/eth/api.go b/pkg/eth/api.go index b3a7801c4..cc09bde22 100644 --- a/pkg/eth/api.go +++ b/pkg/eth/api.go @@ -32,6 +32,7 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" "github.com/sirupsen/logrus" + "github.com/vulcanize/ipld-eth-indexer/pkg/eth" "github.com/vulcanize/ipld-eth-server/pkg/shared" ) @@ -42,10 +43,6 @@ const APIName = "eth" // APIVersion is the version of the watcher's eth api const APIVersion = "0.0.1" -var ( - txRequiresSenderErr = errors.New("default sender address not set; tx requires sender address") -) - type PublicEthAPI struct { B *Backend } @@ -217,7 +214,10 @@ func (pea *PublicEthAPI) Call(ctx context.Context, args CallArgs, blockNrOrHash if overrides != nil { accounts = *overrides } - result, _, _, err := DoCall(ctx, pea.B, args, blockNrOrHash, accounts, 5*time.Second, pea.B.Config.RPCGasCap) + result, _, failed, err := DoCall(ctx, pea.B, args, blockNrOrHash, accounts, 5*time.Second, pea.B.Config.RPCGasCap) + if failed && err == nil { + return nil, errors.New("eth_call failed without error") + } return (hexutil.Bytes)(result), err } @@ -256,10 +256,9 @@ func DoCall(ctx context.Context, b *Backend, args CallArgs, blockNrOrHash rpc.Bl // Set sender address or use a default if none specified var addr common.Address if args.From == nil { - if b.Config.DefaultSender == nil { - return nil, 0, false, txRequiresSenderErr + if b.Config.DefaultSender != nil { + addr = *b.Config.DefaultSender } - addr = *b.Config.DefaultSender } else { addr = *args.From } diff --git a/pkg/eth/api_test.go b/pkg/eth/api_test.go index cd6baa4a3..e7fe496b4 100644 --- a/pkg/eth/api_test.go +++ b/pkg/eth/api_test.go @@ -32,59 +32,57 @@ import ( "github.com/vulcanize/ipld-eth-indexer/pkg/postgres" "github.com/vulcanize/ipld-eth-server/pkg/eth" - "github.com/vulcanize/ipld-eth-server/pkg/eth/mocks" + "github.com/vulcanize/ipld-eth-server/pkg/eth/test_helpers" "github.com/vulcanize/ipld-eth-server/pkg/shared" ) var ( expectedBlock = map[string]interface{}{ - "number": (*hexutil.Big)(mocks.MockBlock.Number()), - "hash": mocks.MockBlock.Hash(), - "parentHash": mocks.MockBlock.ParentHash(), - "nonce": mocks.MockBlock.Header().Nonce, - "mixHash": mocks.MockBlock.MixDigest(), - "sha3Uncles": mocks.MockBlock.UncleHash(), - "logsBloom": mocks.MockBlock.Bloom(), - "stateRoot": mocks.MockBlock.Root(), - "miner": mocks.MockBlock.Coinbase(), - "difficulty": (*hexutil.Big)(mocks.MockBlock.Difficulty()), - "extraData": hexutil.Bytes(mocks.MockBlock.Header().Extra), - "gasLimit": hexutil.Uint64(mocks.MockBlock.GasLimit()), - "gasUsed": hexutil.Uint64(mocks.MockBlock.GasUsed()), - "timestamp": hexutil.Uint64(mocks.MockBlock.Time()), - "transactionsRoot": mocks.MockBlock.TxHash(), - "receiptsRoot": mocks.MockBlock.ReceiptHash(), - "totalDifficulty": (*hexutil.Big)(mocks.MockBlock.Difficulty()), - "size": hexutil.Uint64(mocks.MockBlock.Size()), + "number": (*hexutil.Big)(test_helpers.MockBlock.Number()), + "hash": test_helpers.MockBlock.Hash(), + "parentHash": test_helpers.MockBlock.ParentHash(), + "nonce": test_helpers.MockBlock.Header().Nonce, + "mixHash": test_helpers.MockBlock.MixDigest(), + "sha3Uncles": test_helpers.MockBlock.UncleHash(), + "logsBloom": test_helpers.MockBlock.Bloom(), + "stateRoot": test_helpers.MockBlock.Root(), + "miner": test_helpers.MockBlock.Coinbase(), + "difficulty": (*hexutil.Big)(test_helpers.MockBlock.Difficulty()), + "extraData": hexutil.Bytes(test_helpers.MockBlock.Header().Extra), + "gasLimit": hexutil.Uint64(test_helpers.MockBlock.GasLimit()), + "gasUsed": hexutil.Uint64(test_helpers.MockBlock.GasUsed()), + "timestamp": hexutil.Uint64(test_helpers.MockBlock.Time()), + "transactionsRoot": test_helpers.MockBlock.TxHash(), + "receiptsRoot": test_helpers.MockBlock.ReceiptHash(), + "totalDifficulty": (*hexutil.Big)(test_helpers.MockBlock.Difficulty()), + "size": hexutil.Uint64(test_helpers.MockBlock.Size()), } expectedHeader = map[string]interface{}{ - "number": (*hexutil.Big)(mocks.MockBlock.Header().Number), - "hash": mocks.MockBlock.Header().Hash(), - "parentHash": mocks.MockBlock.Header().ParentHash, - "nonce": mocks.MockBlock.Header().Nonce, - "mixHash": mocks.MockBlock.Header().MixDigest, - "sha3Uncles": mocks.MockBlock.Header().UncleHash, - "logsBloom": mocks.MockBlock.Header().Bloom, - "stateRoot": mocks.MockBlock.Header().Root, - "miner": mocks.MockBlock.Header().Coinbase, - "difficulty": (*hexutil.Big)(mocks.MockBlock.Header().Difficulty), - "extraData": hexutil.Bytes(mocks.MockBlock.Header().Extra), - "size": hexutil.Uint64(mocks.MockBlock.Header().Size()), - "gasLimit": hexutil.Uint64(mocks.MockBlock.Header().GasLimit), - "gasUsed": hexutil.Uint64(mocks.MockBlock.Header().GasUsed), - "timestamp": hexutil.Uint64(mocks.MockBlock.Header().Time), - "transactionsRoot": mocks.MockBlock.Header().TxHash, - "receiptsRoot": mocks.MockBlock.Header().ReceiptHash, - "totalDifficulty": (*hexutil.Big)(mocks.MockBlock.Header().Difficulty), + "number": (*hexutil.Big)(test_helpers.MockBlock.Header().Number), + "hash": test_helpers.MockBlock.Header().Hash(), + "parentHash": test_helpers.MockBlock.Header().ParentHash, + "nonce": test_helpers.MockBlock.Header().Nonce, + "mixHash": test_helpers.MockBlock.Header().MixDigest, + "sha3Uncles": test_helpers.MockBlock.Header().UncleHash, + "logsBloom": test_helpers.MockBlock.Header().Bloom, + "stateRoot": test_helpers.MockBlock.Header().Root, + "miner": test_helpers.MockBlock.Header().Coinbase, + "difficulty": (*hexutil.Big)(test_helpers.MockBlock.Header().Difficulty), + "extraData": hexutil.Bytes(test_helpers.MockBlock.Header().Extra), + "size": hexutil.Uint64(test_helpers.MockBlock.Header().Size()), + "gasLimit": hexutil.Uint64(test_helpers.MockBlock.Header().GasLimit), + "gasUsed": hexutil.Uint64(test_helpers.MockBlock.Header().GasUsed), + "timestamp": hexutil.Uint64(test_helpers.MockBlock.Header().Time), + "transactionsRoot": test_helpers.MockBlock.Header().TxHash, + "receiptsRoot": test_helpers.MockBlock.Header().ReceiptHash, + "totalDifficulty": (*hexutil.Big)(test_helpers.MockBlock.Header().Difficulty), } - expectedTransaction = eth.NewRPCTransaction(mocks.MockTransactions[0], mocks.MockBlock.Hash(), mocks.MockBlock.NumberU64(), 0) + expectedTransaction = eth.NewRPCTransaction(test_helpers.MockTransactions[0], test_helpers.MockBlock.Hash(), test_helpers.MockBlock.NumberU64(), 0) ) var _ = Describe("API", func() { var ( db *postgres.DB - retriever *eth.CIDRetriever - fetcher *eth.IPLDFetcher indexAndPublisher *eth2.IPLDPublisher backend *eth.Backend api *eth.PublicEthAPI @@ -93,18 +91,13 @@ var _ = Describe("API", func() { var err error db, err = shared.SetupDB() Expect(err).ToNot(HaveOccurred()) - retriever = eth.NewCIDRetriever(db) - fetcher = eth.NewIPLDFetcher(db) indexAndPublisher = eth2.NewIPLDPublisher(db) - backend = ð.Backend{ - Retriever: retriever, - Fetcher: fetcher, - DB: db, - } + backend, err = eth.NewEthBackend(db, ð.Config{}) + Expect(err).ToNot(HaveOccurred()) api = eth.NewPublicEthAPI(backend) - err = indexAndPublisher.Publish(mocks.MockConvertedPayload) + err = indexAndPublisher.Publish(test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) - uncles := mocks.MockBlock.Uncles() + uncles := test_helpers.MockBlock.Uncles() uncleHashes := make([]common.Hash, len(uncles)) for i, uncle := range uncles { uncleHashes[i] = uncle.Hash() @@ -119,13 +112,13 @@ var _ = Describe("API", func() { bn := api.BlockNumber() ubn := (uint64)(bn) subn := strconv.FormatUint(ubn, 10) - Expect(subn).To(Equal(mocks.BlockNumber.String())) + Expect(subn).To(Equal(test_helpers.BlockNumber.String())) }) }) Describe("GetTransactionByHash", func() { It("Retrieves a transaction by hash", func() { - hash := mocks.MockTransactions[0].Hash() + hash := test_helpers.MockTransactions[0].Hash() tx, err := api.GetTransactionByHash(context.Background(), hash) Expect(err).ToNot(HaveOccurred()) Expect(tx).To(Equal(expectedTransaction)) @@ -135,12 +128,12 @@ var _ = Describe("API", func() { Describe("GetBlockByNumber", func() { It("Retrieves a block by number", func() { // without full txs - number, err := strconv.ParseInt(mocks.BlockNumber.String(), 10, 64) + number, err := strconv.ParseInt(test_helpers.BlockNumber.String(), 10, 64) Expect(err).ToNot(HaveOccurred()) block, err := api.GetBlockByNumber(context.Background(), rpc.BlockNumber(number), false) Expect(err).ToNot(HaveOccurred()) - transactionHashes := make([]interface{}, len(mocks.MockBlock.Transactions())) - for i, trx := range mocks.MockBlock.Transactions() { + transactionHashes := make([]interface{}, len(test_helpers.MockBlock.Transactions())) + for i, trx := range test_helpers.MockBlock.Transactions() { transactionHashes[i] = trx.Hash() } expectedBlock["transactions"] = transactionHashes @@ -150,9 +143,9 @@ var _ = Describe("API", func() { // with full txs block, err = api.GetBlockByNumber(context.Background(), rpc.BlockNumber(number), true) Expect(err).ToNot(HaveOccurred()) - transactions := make([]interface{}, len(mocks.MockBlock.Transactions())) - for i, trx := range mocks.MockBlock.Transactions() { - transactions[i] = eth.NewRPCTransactionFromBlockHash(mocks.MockBlock, trx.Hash()) + transactions := make([]interface{}, len(test_helpers.MockBlock.Transactions())) + for i, trx := range test_helpers.MockBlock.Transactions() { + transactions[i] = eth.NewRPCTransactionFromBlockHash(test_helpers.MockBlock, trx.Hash()) } expectedBlock["transactions"] = transactions for key, val := range expectedBlock { @@ -163,7 +156,7 @@ var _ = Describe("API", func() { Describe("GetHeaderByNumber", func() { It("Retrieves a header by number", func() { - number, err := strconv.ParseInt(mocks.BlockNumber.String(), 10, 64) + number, err := strconv.ParseInt(test_helpers.BlockNumber.String(), 10, 64) Expect(err).ToNot(HaveOccurred()) header, err := api.GetHeaderByNumber(context.Background(), rpc.BlockNumber(number)) Expect(err).ToNot(HaveOccurred()) @@ -171,7 +164,7 @@ var _ = Describe("API", func() { }) It("Throws an error if a header cannot be found", func() { - number, err := strconv.ParseInt(mocks.BlockNumber.String(), 10, 64) + number, err := strconv.ParseInt(test_helpers.BlockNumber.String(), 10, 64) Expect(err).ToNot(HaveOccurred()) header, err := api.GetHeaderByNumber(context.Background(), rpc.BlockNumber(number+1)) Expect(err).To(HaveOccurred()) @@ -185,10 +178,10 @@ var _ = Describe("API", func() { Describe("GetBlockByHash", func() { It("Retrieves a block by hash", func() { // without full txs - block, err := api.GetBlockByHash(context.Background(), mocks.MockBlock.Hash(), false) + block, err := api.GetBlockByHash(context.Background(), test_helpers.MockBlock.Hash(), false) Expect(err).ToNot(HaveOccurred()) - transactionHashes := make([]interface{}, len(mocks.MockBlock.Transactions())) - for i, trx := range mocks.MockBlock.Transactions() { + transactionHashes := make([]interface{}, len(test_helpers.MockBlock.Transactions())) + for i, trx := range test_helpers.MockBlock.Transactions() { transactionHashes[i] = trx.Hash() } expectedBlock["transactions"] = transactionHashes @@ -196,11 +189,11 @@ var _ = Describe("API", func() { Expect(val).To(Equal(block[key])) } // with full txs - block, err = api.GetBlockByHash(context.Background(), mocks.MockBlock.Hash(), true) + block, err = api.GetBlockByHash(context.Background(), test_helpers.MockBlock.Hash(), true) Expect(err).ToNot(HaveOccurred()) - transactions := make([]interface{}, len(mocks.MockBlock.Transactions())) - for i, trx := range mocks.MockBlock.Transactions() { - transactions[i] = eth.NewRPCTransactionFromBlockHash(mocks.MockBlock, trx.Hash()) + transactions := make([]interface{}, len(test_helpers.MockBlock.Transactions())) + for i, trx := range test_helpers.MockBlock.Transactions() { + transactions[i] = eth.NewRPCTransactionFromBlockHash(test_helpers.MockBlock, trx.Hash()) } expectedBlock["transactions"] = transactions for key, val := range expectedBlock { @@ -217,13 +210,13 @@ var _ = Describe("API", func() { common.HexToHash("0x04"), }, }, - FromBlock: mocks.MockBlock.Number(), - ToBlock: mocks.MockBlock.Number(), + FromBlock: test_helpers.MockBlock.Number(), + ToBlock: test_helpers.MockBlock.Number(), } logs, err := api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1})) crit = ethereum.FilterQuery{ Topics: [][]common.Hash{ @@ -232,13 +225,13 @@ var _ = Describe("API", func() { common.HexToHash("0x05"), }, }, - FromBlock: mocks.MockBlock.Number(), - ToBlock: mocks.MockBlock.Number(), + FromBlock: test_helpers.MockBlock.Number(), + ToBlock: test_helpers.MockBlock.Number(), } logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(2)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1, mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1, test_helpers.MockLog2})) crit = ethereum.FilterQuery{ Topics: [][]common.Hash{ @@ -247,13 +240,13 @@ var _ = Describe("API", func() { common.HexToHash("0x06"), }, }, - FromBlock: mocks.MockBlock.Number(), - ToBlock: mocks.MockBlock.Number(), + FromBlock: test_helpers.MockBlock.Number(), + ToBlock: test_helpers.MockBlock.Number(), } logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1})) crit = ethereum.FilterQuery{ Topics: [][]common.Hash{ @@ -264,8 +257,8 @@ var _ = Describe("API", func() { common.HexToHash("0x07"), }, }, - FromBlock: mocks.MockBlock.Number(), - ToBlock: mocks.MockBlock.Number(), + FromBlock: test_helpers.MockBlock.Number(), + ToBlock: test_helpers.MockBlock.Number(), } logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) @@ -280,13 +273,13 @@ var _ = Describe("API", func() { common.HexToHash("0x06"), }, }, - FromBlock: mocks.MockBlock.Number(), - ToBlock: mocks.MockBlock.Number(), + FromBlock: test_helpers.MockBlock.Number(), + ToBlock: test_helpers.MockBlock.Number(), } logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1})) crit = ethereum.FilterQuery{ Topics: [][]common.Hash{ @@ -297,13 +290,13 @@ var _ = Describe("API", func() { common.HexToHash("0x07"), }, }, - FromBlock: mocks.MockBlock.Number(), - ToBlock: mocks.MockBlock.Number(), + FromBlock: test_helpers.MockBlock.Number(), + ToBlock: test_helpers.MockBlock.Number(), } logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog2})) crit = ethereum.FilterQuery{ Topics: [][]common.Hash{ @@ -315,13 +308,13 @@ var _ = Describe("API", func() { common.HexToHash("0x07"), }, }, - FromBlock: mocks.MockBlock.Number(), - ToBlock: mocks.MockBlock.Number(), + FromBlock: test_helpers.MockBlock.Number(), + ToBlock: test_helpers.MockBlock.Number(), } logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog2})) crit = ethereum.FilterQuery{ Topics: [][]common.Hash{ @@ -334,13 +327,13 @@ var _ = Describe("API", func() { common.HexToHash("0x07"), }, }, - FromBlock: mocks.MockBlock.Number(), - ToBlock: mocks.MockBlock.Number(), + FromBlock: test_helpers.MockBlock.Number(), + ToBlock: test_helpers.MockBlock.Number(), } logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(2)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1, mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1, test_helpers.MockLog2})) crit = ethereum.FilterQuery{ Topics: [][]common.Hash{ @@ -349,13 +342,13 @@ var _ = Describe("API", func() { common.HexToHash("0x07"), }, }, - FromBlock: mocks.MockBlock.Number(), - ToBlock: mocks.MockBlock.Number(), + FromBlock: test_helpers.MockBlock.Number(), + ToBlock: test_helpers.MockBlock.Number(), } logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog2})) crit = ethereum.FilterQuery{ Topics: [][]common.Hash{ @@ -364,27 +357,27 @@ var _ = Describe("API", func() { common.HexToHash("0x06"), }, }, - FromBlock: mocks.MockBlock.Number(), - ToBlock: mocks.MockBlock.Number(), + FromBlock: test_helpers.MockBlock.Number(), + ToBlock: test_helpers.MockBlock.Number(), } logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1})) crit = ethereum.FilterQuery{ Topics: [][]common.Hash{}, - FromBlock: mocks.MockBlock.Number(), - ToBlock: mocks.MockBlock.Number(), + FromBlock: test_helpers.MockBlock.Number(), + ToBlock: test_helpers.MockBlock.Number(), } logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(2)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1, mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1, test_helpers.MockLog2})) }) It("Uses the provided blockhash if one is provided", func() { - hash := mocks.MockBlock.Hash() + hash := test_helpers.MockBlock.Hash() crit := ethereum.FilterQuery{ BlockHash: &hash, Topics: [][]common.Hash{ @@ -397,7 +390,7 @@ var _ = Describe("API", func() { logs, err := api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1})) crit = ethereum.FilterQuery{ BlockHash: &hash, @@ -413,7 +406,7 @@ var _ = Describe("API", func() { logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1})) crit = ethereum.FilterQuery{ BlockHash: &hash, @@ -427,7 +420,7 @@ var _ = Describe("API", func() { logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog2})) crit = ethereum.FilterQuery{ BlockHash: &hash, @@ -443,7 +436,7 @@ var _ = Describe("API", func() { logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog2})) crit = ethereum.FilterQuery{ BlockHash: &hash, @@ -475,7 +468,7 @@ var _ = Describe("API", func() { logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog2})) crit = ethereum.FilterQuery{ BlockHash: &hash, @@ -489,7 +482,7 @@ var _ = Describe("API", func() { logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(2)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1, mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1, test_helpers.MockLog2})) crit = ethereum.FilterQuery{ BlockHash: &hash, @@ -507,7 +500,7 @@ var _ = Describe("API", func() { logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(2)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1, mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1, test_helpers.MockLog2})) crit = ethereum.FilterQuery{ BlockHash: &hash, @@ -516,15 +509,15 @@ var _ = Describe("API", func() { logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(2)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1, mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1, test_helpers.MockLog2})) }) It("Filters on contract address if any are provided", func() { - hash := mocks.MockBlock.Hash() + hash := test_helpers.MockBlock.Hash() crit := ethereum.FilterQuery{ BlockHash: &hash, Addresses: []common.Address{ - mocks.Address, + test_helpers.Address, }, Topics: [][]common.Hash{ { @@ -540,14 +533,14 @@ var _ = Describe("API", func() { logs, err := api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(1)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1})) - hash = mocks.MockBlock.Hash() + hash = test_helpers.MockBlock.Hash() crit = ethereum.FilterQuery{ BlockHash: &hash, Addresses: []common.Address{ - mocks.Address, - mocks.AnotherAddress, + test_helpers.Address, + test_helpers.AnotherAddress, }, Topics: [][]common.Hash{ { @@ -563,20 +556,20 @@ var _ = Describe("API", func() { logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(2)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1, mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1, test_helpers.MockLog2})) - hash = mocks.MockBlock.Hash() + hash = test_helpers.MockBlock.Hash() crit = ethereum.FilterQuery{ BlockHash: &hash, Addresses: []common.Address{ - mocks.Address, - mocks.AnotherAddress, + test_helpers.Address, + test_helpers.AnotherAddress, }, } logs, err = api.GetLogs(context.Background(), crit) Expect(err).ToNot(HaveOccurred()) Expect(len(logs)).To(Equal(2)) - Expect(logs).To(Equal([]*types.Log{mocks.MockLog1, mocks.MockLog2})) + Expect(logs).To(Equal([]*types.Log{test_helpers.MockLog1, test_helpers.MockLog2})) }) }) }) diff --git a/pkg/eth/backend.go b/pkg/eth/backend.go index 11cede4f8..ba51dd546 100644 --- a/pkg/eth/backend.go +++ b/pkg/eth/backend.go @@ -59,7 +59,7 @@ type Backend struct { // underlying postgres db DB *postgres.DB - // postgres db types + // postgres db interfaces Retriever *CIDRetriever Fetcher *IPLDFetcher IPLDRetriever *IPLDRetriever diff --git a/pkg/eth/cid_retriever_test.go b/pkg/eth/cid_retriever_test.go index b8735f37e..a6dda4f6e 100644 --- a/pkg/eth/cid_retriever_test.go +++ b/pkg/eth/cid_retriever_test.go @@ -28,7 +28,7 @@ import ( "github.com/vulcanize/ipld-eth-indexer/pkg/postgres" "github.com/vulcanize/ipld-eth-server/pkg/eth" - "github.com/vulcanize/ipld-eth-server/pkg/eth/mocks" + "github.com/vulcanize/ipld-eth-server/pkg/eth/test_helpers" "github.com/vulcanize/ipld-eth-server/pkg/shared" ) @@ -52,7 +52,7 @@ var ( Off: true, }, ReceiptFilter: eth.ReceiptFilter{ - LogAddresses: []string{mocks.Address.String()}, + LogAddresses: []string{test_helpers.Address.String()}, }, StateFilter: eth.StateFilter{ Off: true, @@ -94,7 +94,7 @@ var ( {"0x0000000000000000000000000000000000000000000000000000000000000004"}, {"0x0000000000000000000000000000000000000000000000000000000000000006"}, }, - LogAddresses: []string{mocks.Address.String()}, + LogAddresses: []string{test_helpers.Address.String()}, }, StateFilter: eth.StateFilter{ Off: true, @@ -117,7 +117,7 @@ var ( {"0x0000000000000000000000000000000000000000000000000000000000000004"}, {"0x0000000000000000000000000000000000000000000000000000000000000007"}, // This topic won't match on the mocks.Address.String() contract receipt }, - LogAddresses: []string{mocks.Address.String()}, + LogAddresses: []string{test_helpers.Address.String()}, }, StateFilter: eth.StateFilter{ Off: true, @@ -137,7 +137,7 @@ var ( }, ReceiptFilter: eth.ReceiptFilter{ Topics: [][]string{{"0x0000000000000000000000000000000000000000000000000000000000000005"}}, - LogAddresses: []string{mocks.Address.String(), mocks.AnotherAddress.String()}, + LogAddresses: []string{test_helpers.Address.String(), test_helpers.AnotherAddress.String()}, }, StateFilter: eth.StateFilter{ Off: true, @@ -172,7 +172,7 @@ var ( Off: true, }, TxFilter: eth.TxFilter{ - Dst: []string{mocks.AnotherAddress.String()}, // We only filter for one of the trxs so we will only get the one corresponding receipt + Dst: []string{test_helpers.AnotherAddress.String()}, // We only filter for one of the trxs so we will only get the one corresponding receipt }, ReceiptFilter: eth.ReceiptFilter{ MatchTxs: true, @@ -199,7 +199,7 @@ var ( Off: true, }, StateFilter: eth.StateFilter{ - Addresses: []string{mocks.AccountAddresss.Hex()}, + Addresses: []string{test_helpers.AccountAddresss.Hex()}, }, StorageFilter: eth.StorageFilter{ Off: true, @@ -226,7 +226,7 @@ var _ = Describe("Retriever", func() { Describe("Retrieve", func() { BeforeEach(func() { - err := repo.Publish(mocks.MockConvertedPayload) + err := repo.Publish(test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) }) It("Retrieves all CIDs for the given blocknumber when provided an open filter", func() { @@ -234,34 +234,34 @@ var _ = Describe("Retriever", func() { Expect(err).ToNot(HaveOccurred()) Expect(empty).ToNot(BeTrue()) Expect(len(cids)).To(Equal(1)) - Expect(cids[0].BlockNumber).To(Equal(mocks.MockCIDWrapper.BlockNumber)) - expectedHeaderCID := mocks.MockCIDWrapper.Header + Expect(cids[0].BlockNumber).To(Equal(test_helpers.MockCIDWrapper.BlockNumber)) + expectedHeaderCID := test_helpers.MockCIDWrapper.Header expectedHeaderCID.ID = cids[0].Header.ID expectedHeaderCID.NodeID = cids[0].Header.NodeID Expect(cids[0].Header).To(Equal(expectedHeaderCID)) Expect(len(cids[0].Transactions)).To(Equal(3)) - Expect(eth.TxModelsContainsCID(cids[0].Transactions, mocks.MockCIDWrapper.Transactions[0].CID)).To(BeTrue()) - Expect(eth.TxModelsContainsCID(cids[0].Transactions, mocks.MockCIDWrapper.Transactions[1].CID)).To(BeTrue()) - Expect(eth.TxModelsContainsCID(cids[0].Transactions, mocks.MockCIDWrapper.Transactions[2].CID)).To(BeTrue()) + Expect(eth.TxModelsContainsCID(cids[0].Transactions, test_helpers.MockCIDWrapper.Transactions[0].CID)).To(BeTrue()) + Expect(eth.TxModelsContainsCID(cids[0].Transactions, test_helpers.MockCIDWrapper.Transactions[1].CID)).To(BeTrue()) + Expect(eth.TxModelsContainsCID(cids[0].Transactions, test_helpers.MockCIDWrapper.Transactions[2].CID)).To(BeTrue()) Expect(len(cids[0].Receipts)).To(Equal(3)) - Expect(eth.ReceiptModelsContainsCID(cids[0].Receipts, mocks.MockCIDWrapper.Receipts[0].CID)).To(BeTrue()) - Expect(eth.ReceiptModelsContainsCID(cids[0].Receipts, mocks.MockCIDWrapper.Receipts[1].CID)).To(BeTrue()) - Expect(eth.ReceiptModelsContainsCID(cids[0].Receipts, mocks.MockCIDWrapper.Receipts[2].CID)).To(BeTrue()) + Expect(eth.ReceiptModelsContainsCID(cids[0].Receipts, test_helpers.MockCIDWrapper.Receipts[0].CID)).To(BeTrue()) + Expect(eth.ReceiptModelsContainsCID(cids[0].Receipts, test_helpers.MockCIDWrapper.Receipts[1].CID)).To(BeTrue()) + Expect(eth.ReceiptModelsContainsCID(cids[0].Receipts, test_helpers.MockCIDWrapper.Receipts[2].CID)).To(BeTrue()) Expect(len(cids[0].StateNodes)).To(Equal(2)) for _, stateNode := range cids[0].StateNodes { - if stateNode.CID == mocks.State1CID.String() { - Expect(stateNode.StateKey).To(Equal(common.BytesToHash(mocks.ContractLeafKey).Hex())) + if stateNode.CID == test_helpers.State1CID.String() { + Expect(stateNode.StateKey).To(Equal(common.BytesToHash(test_helpers.ContractLeafKey).Hex())) Expect(stateNode.NodeType).To(Equal(2)) Expect(stateNode.Path).To(Equal([]byte{'\x06'})) } - if stateNode.CID == mocks.State2CID.String() { - Expect(stateNode.StateKey).To(Equal(common.BytesToHash(mocks.AccountLeafKey).Hex())) + if stateNode.CID == test_helpers.State2CID.String() { + Expect(stateNode.StateKey).To(Equal(common.BytesToHash(test_helpers.AccountLeafKey).Hex())) Expect(stateNode.NodeType).To(Equal(2)) Expect(stateNode.Path).To(Equal([]byte{'\x0c'})) } } Expect(len(cids[0].StorageNodes)).To(Equal(1)) - expectedStorageNodeCIDs := mocks.MockCIDWrapper.StorageNodes + expectedStorageNodeCIDs := test_helpers.MockCIDWrapper.StorageNodes expectedStorageNodeCIDs[0].ID = cids[0].StorageNodes[0].ID expectedStorageNodeCIDs[0].StateID = cids[0].StorageNodes[0].StateID Expect(cids[0].StorageNodes).To(Equal(expectedStorageNodeCIDs)) @@ -272,13 +272,13 @@ var _ = Describe("Retriever", func() { Expect(err).ToNot(HaveOccurred()) Expect(empty).ToNot(BeTrue()) Expect(len(cids1)).To(Equal(1)) - Expect(cids1[0].BlockNumber).To(Equal(mocks.MockCIDWrapper.BlockNumber)) + Expect(cids1[0].BlockNumber).To(Equal(test_helpers.MockCIDWrapper.BlockNumber)) Expect(cids1[0].Header).To(Equal(eth2.HeaderModel{})) Expect(len(cids1[0].Transactions)).To(Equal(0)) Expect(len(cids1[0].StateNodes)).To(Equal(0)) Expect(len(cids1[0].StorageNodes)).To(Equal(0)) Expect(len(cids1[0].Receipts)).To(Equal(1)) - expectedReceiptCID := mocks.MockCIDWrapper.Receipts[0] + expectedReceiptCID := test_helpers.MockCIDWrapper.Receipts[0] expectedReceiptCID.ID = cids1[0].Receipts[0].ID expectedReceiptCID.TxID = cids1[0].Receipts[0].TxID Expect(cids1[0].Receipts[0]).To(Equal(expectedReceiptCID)) @@ -287,13 +287,13 @@ var _ = Describe("Retriever", func() { Expect(err).ToNot(HaveOccurred()) Expect(empty).ToNot(BeTrue()) Expect(len(cids2)).To(Equal(1)) - Expect(cids2[0].BlockNumber).To(Equal(mocks.MockCIDWrapper.BlockNumber)) + Expect(cids2[0].BlockNumber).To(Equal(test_helpers.MockCIDWrapper.BlockNumber)) Expect(cids2[0].Header).To(Equal(eth2.HeaderModel{})) Expect(len(cids2[0].Transactions)).To(Equal(0)) Expect(len(cids2[0].StateNodes)).To(Equal(0)) Expect(len(cids2[0].StorageNodes)).To(Equal(0)) Expect(len(cids2[0].Receipts)).To(Equal(1)) - expectedReceiptCID = mocks.MockCIDWrapper.Receipts[0] + expectedReceiptCID = test_helpers.MockCIDWrapper.Receipts[0] expectedReceiptCID.ID = cids2[0].Receipts[0].ID expectedReceiptCID.TxID = cids2[0].Receipts[0].TxID Expect(cids2[0].Receipts[0]).To(Equal(expectedReceiptCID)) @@ -302,13 +302,13 @@ var _ = Describe("Retriever", func() { Expect(err).ToNot(HaveOccurred()) Expect(empty).ToNot(BeTrue()) Expect(len(cids3)).To(Equal(1)) - Expect(cids3[0].BlockNumber).To(Equal(mocks.MockCIDWrapper.BlockNumber)) + Expect(cids3[0].BlockNumber).To(Equal(test_helpers.MockCIDWrapper.BlockNumber)) Expect(cids3[0].Header).To(Equal(eth2.HeaderModel{})) Expect(len(cids3[0].Transactions)).To(Equal(0)) Expect(len(cids3[0].StateNodes)).To(Equal(0)) Expect(len(cids3[0].StorageNodes)).To(Equal(0)) Expect(len(cids3[0].Receipts)).To(Equal(1)) - expectedReceiptCID = mocks.MockCIDWrapper.Receipts[0] + expectedReceiptCID = test_helpers.MockCIDWrapper.Receipts[0] expectedReceiptCID.ID = cids3[0].Receipts[0].ID expectedReceiptCID.TxID = cids3[0].Receipts[0].TxID Expect(cids3[0].Receipts[0]).To(Equal(expectedReceiptCID)) @@ -317,13 +317,13 @@ var _ = Describe("Retriever", func() { Expect(err).ToNot(HaveOccurred()) Expect(empty).ToNot(BeTrue()) Expect(len(cids4)).To(Equal(1)) - Expect(cids4[0].BlockNumber).To(Equal(mocks.MockCIDWrapper.BlockNumber)) + Expect(cids4[0].BlockNumber).To(Equal(test_helpers.MockCIDWrapper.BlockNumber)) Expect(cids4[0].Header).To(Equal(eth2.HeaderModel{})) Expect(len(cids4[0].Transactions)).To(Equal(0)) Expect(len(cids4[0].StateNodes)).To(Equal(0)) Expect(len(cids4[0].StorageNodes)).To(Equal(0)) Expect(len(cids4[0].Receipts)).To(Equal(1)) - expectedReceiptCID = mocks.MockCIDWrapper.Receipts[1] + expectedReceiptCID = test_helpers.MockCIDWrapper.Receipts[1] expectedReceiptCID.ID = cids4[0].Receipts[0].ID expectedReceiptCID.TxID = cids4[0].Receipts[0].TxID Expect(cids4[0].Receipts[0]).To(Equal(expectedReceiptCID)) @@ -332,34 +332,34 @@ var _ = Describe("Retriever", func() { Expect(err).ToNot(HaveOccurred()) Expect(empty).ToNot(BeTrue()) Expect(len(cids5)).To(Equal(1)) - Expect(cids5[0].BlockNumber).To(Equal(mocks.MockCIDWrapper.BlockNumber)) + Expect(cids5[0].BlockNumber).To(Equal(test_helpers.MockCIDWrapper.BlockNumber)) Expect(cids5[0].Header).To(Equal(eth2.HeaderModel{})) Expect(len(cids5[0].Transactions)).To(Equal(3)) - Expect(eth.TxModelsContainsCID(cids5[0].Transactions, mocks.Trx1CID.String())).To(BeTrue()) - Expect(eth.TxModelsContainsCID(cids5[0].Transactions, mocks.Trx2CID.String())).To(BeTrue()) - Expect(eth.TxModelsContainsCID(cids5[0].Transactions, mocks.Trx3CID.String())).To(BeTrue()) + Expect(eth.TxModelsContainsCID(cids5[0].Transactions, test_helpers.Trx1CID.String())).To(BeTrue()) + Expect(eth.TxModelsContainsCID(cids5[0].Transactions, test_helpers.Trx2CID.String())).To(BeTrue()) + Expect(eth.TxModelsContainsCID(cids5[0].Transactions, test_helpers.Trx3CID.String())).To(BeTrue()) Expect(len(cids5[0].StateNodes)).To(Equal(0)) Expect(len(cids5[0].StorageNodes)).To(Equal(0)) Expect(len(cids5[0].Receipts)).To(Equal(3)) - Expect(eth.ReceiptModelsContainsCID(cids5[0].Receipts, mocks.Rct1CID.String())).To(BeTrue()) - Expect(eth.ReceiptModelsContainsCID(cids5[0].Receipts, mocks.Rct2CID.String())).To(BeTrue()) - Expect(eth.ReceiptModelsContainsCID(cids5[0].Receipts, mocks.Rct3CID.String())).To(BeTrue()) + Expect(eth.ReceiptModelsContainsCID(cids5[0].Receipts, test_helpers.Rct1CID.String())).To(BeTrue()) + Expect(eth.ReceiptModelsContainsCID(cids5[0].Receipts, test_helpers.Rct2CID.String())).To(BeTrue()) + Expect(eth.ReceiptModelsContainsCID(cids5[0].Receipts, test_helpers.Rct3CID.String())).To(BeTrue()) cids6, empty, err := retriever.Retrieve(rctsForSelectCollectedTrxs, 1) Expect(err).ToNot(HaveOccurred()) Expect(empty).ToNot(BeTrue()) Expect(len(cids6)).To(Equal(1)) - Expect(cids6[0].BlockNumber).To(Equal(mocks.MockCIDWrapper.BlockNumber)) + Expect(cids6[0].BlockNumber).To(Equal(test_helpers.MockCIDWrapper.BlockNumber)) Expect(cids6[0].Header).To(Equal(eth2.HeaderModel{})) Expect(len(cids6[0].Transactions)).To(Equal(1)) - expectedTxCID := mocks.MockCIDWrapper.Transactions[1] + expectedTxCID := test_helpers.MockCIDWrapper.Transactions[1] expectedTxCID.ID = cids6[0].Transactions[0].ID 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 = mocks.MockCIDWrapper.Receipts[1] + expectedReceiptCID = test_helpers.MockCIDWrapper.Receipts[1] expectedReceiptCID.ID = cids6[0].Receipts[0].ID expectedReceiptCID.TxID = cids6[0].Receipts[0].TxID Expect(cids6[0].Receipts[0]).To(Equal(expectedReceiptCID)) @@ -368,7 +368,7 @@ var _ = Describe("Retriever", func() { Expect(err).ToNot(HaveOccurred()) Expect(empty).ToNot(BeTrue()) Expect(len(cids7)).To(Equal(1)) - Expect(cids7[0].BlockNumber).To(Equal(mocks.MockCIDWrapper.BlockNumber)) + Expect(cids7[0].BlockNumber).To(Equal(test_helpers.MockCIDWrapper.BlockNumber)) Expect(cids7[0].Header).To(Equal(eth2.HeaderModel{})) Expect(len(cids7[0].Transactions)).To(Equal(0)) Expect(len(cids7[0].Receipts)).To(Equal(0)) @@ -378,9 +378,9 @@ var _ = Describe("Retriever", func() { ID: cids7[0].StateNodes[0].ID, HeaderID: cids7[0].StateNodes[0].HeaderID, NodeType: 2, - StateKey: common.BytesToHash(mocks.AccountLeafKey).Hex(), - CID: mocks.State2CID.String(), - MhKey: mocks.State2MhKey, + StateKey: common.BytesToHash(test_helpers.AccountLeafKey).Hex(), + CID: test_helpers.State2CID.String(), + MhKey: test_helpers.State2MhKey, Path: []byte{'\x0c'}, })) @@ -396,7 +396,7 @@ var _ = Describe("Retriever", func() { Expect(err).To(HaveOccurred()) }) It("Gets the number of the first block that has data in the database", func() { - err := repo.Publish(mocks.MockConvertedPayload) + err := repo.Publish(test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) num, err := retriever.RetrieveFirstBlockNumber() Expect(err).ToNot(HaveOccurred()) @@ -404,7 +404,7 @@ var _ = Describe("Retriever", func() { }) It("Gets the number of the first block that has data in the database", func() { - payload := mocks.MockConvertedPayload + payload := test_helpers.MockConvertedPayload payload.Block = newMockBlock(1010101) err := repo.Publish(payload) Expect(err).ToNot(HaveOccurred()) @@ -414,7 +414,7 @@ var _ = Describe("Retriever", func() { }) It("Gets the number of the first block that has data in the database", func() { - payload1 := mocks.MockConvertedPayload + payload1 := test_helpers.MockConvertedPayload payload1.Block = newMockBlock(1010101) payload2 := payload1 payload2.Block = newMockBlock(5) @@ -434,7 +434,7 @@ var _ = Describe("Retriever", func() { Expect(err).To(HaveOccurred()) }) It("Gets the number of the latest block that has data in the database", func() { - err := repo.Publish(mocks.MockConvertedPayload) + err := repo.Publish(test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) num, err := retriever.RetrieveLastBlockNumber() Expect(err).ToNot(HaveOccurred()) @@ -442,7 +442,7 @@ var _ = Describe("Retriever", func() { }) It("Gets the number of the latest block that has data in the database", func() { - payload := mocks.MockConvertedPayload + payload := test_helpers.MockConvertedPayload payload.Block = newMockBlock(1010101) err := repo.Publish(payload) Expect(err).ToNot(HaveOccurred()) @@ -452,7 +452,7 @@ var _ = Describe("Retriever", func() { }) It("Gets the number of the latest block that has data in the database", func() { - payload1 := mocks.MockConvertedPayload + payload1 := test_helpers.MockConvertedPayload payload1.Block = newMockBlock(1010101) payload2 := payload1 payload2.Block = newMockBlock(5) @@ -468,7 +468,7 @@ var _ = Describe("Retriever", func() { }) func newMockBlock(blockNumber uint64) *types.Block { - header := mocks.MockHeader + header := test_helpers.MockHeader header.Number.SetUint64(blockNumber) - return types.NewBlock(&mocks.MockHeader, mocks.MockTransactions, nil, mocks.MockReceipts) + return types.NewBlock(&test_helpers.MockHeader, test_helpers.MockTransactions, nil, test_helpers.MockReceipts) } diff --git a/pkg/eth/eth_call_test.go b/pkg/eth/eth_call_test.go new file mode 100644 index 000000000..a416e27aa --- /dev/null +++ b/pkg/eth/eth_call_test.go @@ -0,0 +1,165 @@ +// VulcanizeDB +// Copyright © 2019 Vulcanize + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package eth_test + +import ( + "bytes" + "context" + "io/ioutil" + "math/big" + + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/rpc" + "github.com/ethereum/go-ethereum/statediff" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + eth2 "github.com/vulcanize/ipld-eth-indexer/pkg/eth" + "github.com/vulcanize/ipld-eth-indexer/pkg/postgres" + + "github.com/vulcanize/ipld-eth-server/pkg/eth" + "github.com/vulcanize/ipld-eth-server/pkg/eth/test_helpers" + "github.com/vulcanize/ipld-eth-server/pkg/shared" +) + +var ( + parsedABI abi.ABI +) + +func init() { + // load abi + abiBytes, err := ioutil.ReadFile("./test_helpers/abi.json") + if err != nil { + panic(err) + } + parsedABI, err = abi.JSON(bytes.NewReader(abiBytes)) + if err != nil { + panic(err) + } +} + +var _ = Describe("eth_call", func() { + var ( + blocks []*types.Block + receipts []types.Receipts + chain *core.BlockChain + db *postgres.DB + transformer *eth2.StateDiffTransformer + backend *eth.Backend + api *eth.PublicEthAPI + builder statediff.Builder + pams statediff.Params + chainConfig = params.TestChainConfig + mockTD = big.NewInt(1337) + ) + + BeforeEach(func() { + // db and type initializations + var err error + db, err = shared.SetupDB() + Expect(err).ToNot(HaveOccurred()) + transformer = eth2.NewStateDiffTransformer(chainConfig, db) + backend, err = eth.NewEthBackend(db, ð.Config{ + ChainConfig: chainConfig, + VmConfig: vm.Config{}, + RPCGasCap: big.NewInt(10000000000), + }) + Expect(err).ToNot(HaveOccurred()) + api = eth.NewPublicEthAPI(backend) + + // make the test blockchain (and state) + blocks, receipts, chain = test_helpers.MakeChain(4, test_helpers.Genesis, test_helpers.TestChainGen) + pams = statediff.Params{ + IntermediateStateNodes: true, + IntermediateStorageNodes: true, + } + // iterate over the blocks, generating statediff payloads, and transforming the data into Postgres + builder = statediff.NewBuilder(chain.StateCache()) + for i, block := range blocks { + var args statediff.Args + var rcts types.Receipts + if i == 0 { + args = statediff.Args{ + OldStateRoot: common.Hash{}, + NewStateRoot: block.Root(), + BlockNumber: block.Number(), + BlockHash: block.Hash(), + } + } else { + args = statediff.Args{ + OldStateRoot: blocks[i-1].Root(), + NewStateRoot: block.Root(), + BlockNumber: block.Number(), + BlockHash: block.Hash(), + } + rcts = receipts[i-1] + } + diff, err := builder.BuildStateDiffObject(args, pams) + Expect(err).ToNot(HaveOccurred()) + diffRlp, err := rlp.EncodeToBytes(diff) + Expect(err).ToNot(HaveOccurred()) + blockRlp, err := rlp.EncodeToBytes(block) + Expect(err).ToNot(HaveOccurred()) + receiptsRlp, err := rlp.EncodeToBytes(rcts) + Expect(err).ToNot(HaveOccurred()) + payload := statediff.Payload{ + StateObjectRlp: diffRlp, + BlockRlp: blockRlp, + ReceiptsRlp: receiptsRlp, + TotalDifficulty: mockTD, + } + _, err = transformer.Transform(0, payload) + Expect(err).ToNot(HaveOccurred()) + } + }) + AfterEach(func() { + //eth.TearDownDB(db) + chain.Stop() + }) + FDescribe("eth_call", func() { + It("Applies call args (tx data) on top of state, returning the result (e.g. a Getter method call)", func() { + data, err := parsedABI.Pack("data") + Expect(err).ToNot(HaveOccurred()) + bdata := hexutil.Bytes(data) + callArgs := eth.CallArgs{ + To: &test_helpers.ContractAddr, + Data: &bdata, + } + res, err := api.Call(context.Background(), callArgs, rpc.BlockNumberOrHashWithNumber(2), nil) + Expect(err).ToNot(HaveOccurred()) + expectedRes := hexutil.Bytes(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) + Expect(res).To(Equal(expectedRes)) + + res, err = api.Call(context.Background(), callArgs, rpc.BlockNumberOrHashWithNumber(3), nil) + Expect(err).ToNot(HaveOccurred()) + expectedRes = hexutil.Bytes(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003")) + Expect(res).To(Equal(expectedRes)) + + res, err = api.Call(context.Background(), callArgs, rpc.BlockNumberOrHashWithNumber(4), nil) + Expect(err).ToNot(HaveOccurred()) + expectedRes = hexutil.Bytes(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000009")) + Expect(res).To(Equal(expectedRes)) + }) + }) +}) diff --git a/pkg/eth/filterer_test.go b/pkg/eth/filterer_test.go index 9d495b286..2ae5c7946 100644 --- a/pkg/eth/filterer_test.go +++ b/pkg/eth/filterer_test.go @@ -26,7 +26,7 @@ import ( "github.com/vulcanize/ipld-eth-indexer/pkg/ipfs" "github.com/vulcanize/ipld-eth-server/pkg/eth" - "github.com/vulcanize/ipld-eth-server/pkg/eth/mocks" + "github.com/vulcanize/ipld-eth-server/pkg/eth/test_helpers" "github.com/vulcanize/ipld-eth-server/pkg/shared" ) @@ -41,45 +41,45 @@ var _ = Describe("Filterer", func() { }) It("Transcribes all the data from the IPLDPayload into the StreamPayload if given an open filter", func() { - iplds, err := filterer.Filter(openFilter, mocks.MockConvertedPayload) + iplds, err := filterer.Filter(openFilter, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds).ToNot(BeNil()) - Expect(iplds.BlockNumber.Int64()).To(Equal(mocks.MockIPLDs.BlockNumber.Int64())) - Expect(iplds.Header).To(Equal(mocks.MockIPLDs.Header)) + Expect(iplds.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) + Expect(iplds.Header).To(Equal(test_helpers.MockIPLDs.Header)) var expectedEmptyUncles []ipfs.BlockModel Expect(iplds.Uncles).To(Equal(expectedEmptyUncles)) Expect(len(iplds.Transactions)).To(Equal(3)) - Expect(shared.IPLDsContainBytes(iplds.Transactions, mocks.MockTransactions.GetRlp(0))).To(BeTrue()) - Expect(shared.IPLDsContainBytes(iplds.Transactions, mocks.MockTransactions.GetRlp(1))).To(BeTrue()) - Expect(shared.IPLDsContainBytes(iplds.Transactions, mocks.MockTransactions.GetRlp(2))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds.Transactions, test_helpers.MockTransactions.GetRlp(0))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds.Transactions, test_helpers.MockTransactions.GetRlp(1))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds.Transactions, test_helpers.MockTransactions.GetRlp(2))).To(BeTrue()) Expect(len(iplds.Receipts)).To(Equal(3)) - Expect(shared.IPLDsContainBytes(iplds.Receipts, mocks.MockReceipts.GetRlp(0))).To(BeTrue()) - Expect(shared.IPLDsContainBytes(iplds.Receipts, mocks.MockReceipts.GetRlp(1))).To(BeTrue()) - Expect(shared.IPLDsContainBytes(iplds.Receipts, mocks.MockReceipts.GetRlp(2))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds.Receipts, test_helpers.MockReceipts.GetRlp(0))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds.Receipts, test_helpers.MockReceipts.GetRlp(1))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds.Receipts, test_helpers.MockReceipts.GetRlp(2))).To(BeTrue()) Expect(len(iplds.StateNodes)).To(Equal(2)) for _, stateNode := range iplds.StateNodes { Expect(stateNode.Type).To(Equal(statediff.Leaf)) - if bytes.Equal(stateNode.StateLeafKey.Bytes(), mocks.AccountLeafKey) { + if bytes.Equal(stateNode.StateLeafKey.Bytes(), test_helpers.AccountLeafKey) { Expect(stateNode.IPLD).To(Equal(ipfs.BlockModel{ - Data: mocks.State2IPLD.RawData(), - CID: mocks.State2IPLD.Cid().String(), + Data: test_helpers.State2IPLD.RawData(), + CID: test_helpers.State2IPLD.Cid().String(), })) } - if bytes.Equal(stateNode.StateLeafKey.Bytes(), mocks.ContractLeafKey) { + if bytes.Equal(stateNode.StateLeafKey.Bytes(), test_helpers.ContractLeafKey) { Expect(stateNode.IPLD).To(Equal(ipfs.BlockModel{ - Data: mocks.State1IPLD.RawData(), - CID: mocks.State1IPLD.Cid().String(), + Data: test_helpers.State1IPLD.RawData(), + CID: test_helpers.State1IPLD.Cid().String(), })) } } - Expect(iplds.StorageNodes).To(Equal(mocks.MockIPLDs.StorageNodes)) + Expect(iplds.StorageNodes).To(Equal(test_helpers.MockIPLDs.StorageNodes)) }) It("Applies filters from the provided config.Subscription", func() { - iplds1, err := filterer.Filter(rctAddressFilter, mocks.MockConvertedPayload) + iplds1, err := filterer.Filter(rctAddressFilter, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds1).ToNot(BeNil()) - Expect(iplds1.BlockNumber.Int64()).To(Equal(mocks.MockIPLDs.BlockNumber.Int64())) + Expect(iplds1.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) Expect(iplds1.Header).To(Equal(ipfs.BlockModel{})) Expect(len(iplds1.Uncles)).To(Equal(0)) Expect(len(iplds1.Transactions)).To(Equal(0)) @@ -87,14 +87,14 @@ var _ = Describe("Filterer", func() { Expect(len(iplds1.StateNodes)).To(Equal(0)) Expect(len(iplds1.Receipts)).To(Equal(1)) Expect(iplds1.Receipts[0]).To(Equal(ipfs.BlockModel{ - Data: mocks.Rct1IPLD.RawData(), - CID: mocks.Rct1IPLD.Cid().String(), + Data: test_helpers.Rct1IPLD.RawData(), + CID: test_helpers.Rct1IPLD.Cid().String(), })) - iplds2, err := filterer.Filter(rctTopicsFilter, mocks.MockConvertedPayload) + iplds2, err := filterer.Filter(rctTopicsFilter, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds2).ToNot(BeNil()) - Expect(iplds2.BlockNumber.Int64()).To(Equal(mocks.MockIPLDs.BlockNumber.Int64())) + Expect(iplds2.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) Expect(iplds2.Header).To(Equal(ipfs.BlockModel{})) Expect(len(iplds2.Uncles)).To(Equal(0)) Expect(len(iplds2.Transactions)).To(Equal(0)) @@ -102,14 +102,14 @@ var _ = Describe("Filterer", func() { Expect(len(iplds2.StateNodes)).To(Equal(0)) Expect(len(iplds2.Receipts)).To(Equal(1)) Expect(iplds2.Receipts[0]).To(Equal(ipfs.BlockModel{ - Data: mocks.Rct1IPLD.RawData(), - CID: mocks.Rct1IPLD.Cid().String(), + Data: test_helpers.Rct1IPLD.RawData(), + CID: test_helpers.Rct1IPLD.Cid().String(), })) - iplds3, err := filterer.Filter(rctTopicsAndAddressFilter, mocks.MockConvertedPayload) + iplds3, err := filterer.Filter(rctTopicsAndAddressFilter, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds3).ToNot(BeNil()) - Expect(iplds3.BlockNumber.Int64()).To(Equal(mocks.MockIPLDs.BlockNumber.Int64())) + Expect(iplds3.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) Expect(iplds3.Header).To(Equal(ipfs.BlockModel{})) Expect(len(iplds3.Uncles)).To(Equal(0)) Expect(len(iplds3.Transactions)).To(Equal(0)) @@ -117,14 +117,14 @@ var _ = Describe("Filterer", func() { Expect(len(iplds3.StateNodes)).To(Equal(0)) Expect(len(iplds3.Receipts)).To(Equal(1)) Expect(iplds3.Receipts[0]).To(Equal(ipfs.BlockModel{ - Data: mocks.Rct1IPLD.RawData(), - CID: mocks.Rct1IPLD.Cid().String(), + Data: test_helpers.Rct1IPLD.RawData(), + CID: test_helpers.Rct1IPLD.Cid().String(), })) - iplds4, err := filterer.Filter(rctAddressesAndTopicFilter, mocks.MockConvertedPayload) + iplds4, err := filterer.Filter(rctAddressesAndTopicFilter, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds4).ToNot(BeNil()) - Expect(iplds4.BlockNumber.Int64()).To(Equal(mocks.MockIPLDs.BlockNumber.Int64())) + Expect(iplds4.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) Expect(iplds4.Header).To(Equal(ipfs.BlockModel{})) Expect(len(iplds4.Uncles)).To(Equal(0)) Expect(len(iplds4.Transactions)).To(Equal(0)) @@ -132,63 +132,63 @@ var _ = Describe("Filterer", func() { Expect(len(iplds4.StateNodes)).To(Equal(0)) Expect(len(iplds4.Receipts)).To(Equal(1)) Expect(iplds4.Receipts[0]).To(Equal(ipfs.BlockModel{ - Data: mocks.Rct2IPLD.RawData(), - CID: mocks.Rct2IPLD.Cid().String(), + Data: test_helpers.Rct2IPLD.RawData(), + CID: test_helpers.Rct2IPLD.Cid().String(), })) - iplds5, err := filterer.Filter(rctsForAllCollectedTrxs, mocks.MockConvertedPayload) + iplds5, err := filterer.Filter(rctsForAllCollectedTrxs, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds5).ToNot(BeNil()) - Expect(iplds5.BlockNumber.Int64()).To(Equal(mocks.MockIPLDs.BlockNumber.Int64())) + Expect(iplds5.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) Expect(iplds5.Header).To(Equal(ipfs.BlockModel{})) Expect(len(iplds5.Uncles)).To(Equal(0)) Expect(len(iplds5.Transactions)).To(Equal(3)) - Expect(shared.IPLDsContainBytes(iplds5.Transactions, mocks.MockTransactions.GetRlp(0))).To(BeTrue()) - Expect(shared.IPLDsContainBytes(iplds5.Transactions, mocks.MockTransactions.GetRlp(1))).To(BeTrue()) - Expect(shared.IPLDsContainBytes(iplds5.Transactions, mocks.MockTransactions.GetRlp(2))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds5.Transactions, test_helpers.MockTransactions.GetRlp(0))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds5.Transactions, test_helpers.MockTransactions.GetRlp(1))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds5.Transactions, test_helpers.MockTransactions.GetRlp(2))).To(BeTrue()) Expect(len(iplds5.StorageNodes)).To(Equal(0)) Expect(len(iplds5.StateNodes)).To(Equal(0)) Expect(len(iplds5.Receipts)).To(Equal(3)) - Expect(shared.IPLDsContainBytes(iplds5.Receipts, mocks.MockReceipts.GetRlp(0))).To(BeTrue()) - Expect(shared.IPLDsContainBytes(iplds5.Receipts, mocks.MockReceipts.GetRlp(1))).To(BeTrue()) - Expect(shared.IPLDsContainBytes(iplds5.Receipts, mocks.MockReceipts.GetRlp(2))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds5.Receipts, test_helpers.MockReceipts.GetRlp(0))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds5.Receipts, test_helpers.MockReceipts.GetRlp(1))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds5.Receipts, test_helpers.MockReceipts.GetRlp(2))).To(BeTrue()) - iplds6, err := filterer.Filter(rctsForSelectCollectedTrxs, mocks.MockConvertedPayload) + iplds6, err := filterer.Filter(rctsForSelectCollectedTrxs, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds6).ToNot(BeNil()) - Expect(iplds6.BlockNumber.Int64()).To(Equal(mocks.MockIPLDs.BlockNumber.Int64())) + Expect(iplds6.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) Expect(iplds6.Header).To(Equal(ipfs.BlockModel{})) Expect(len(iplds6.Uncles)).To(Equal(0)) Expect(len(iplds6.Transactions)).To(Equal(1)) - Expect(shared.IPLDsContainBytes(iplds5.Transactions, mocks.MockTransactions.GetRlp(1))).To(BeTrue()) + Expect(shared.IPLDsContainBytes(iplds5.Transactions, test_helpers.MockTransactions.GetRlp(1))).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{ - Data: mocks.Rct2IPLD.RawData(), - CID: mocks.Rct2IPLD.Cid().String(), + Data: test_helpers.Rct2IPLD.RawData(), + CID: test_helpers.Rct2IPLD.Cid().String(), })) - iplds7, err := filterer.Filter(stateFilter, mocks.MockConvertedPayload) + iplds7, err := filterer.Filter(stateFilter, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds7).ToNot(BeNil()) - Expect(iplds7.BlockNumber.Int64()).To(Equal(mocks.MockIPLDs.BlockNumber.Int64())) + Expect(iplds7.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) Expect(iplds7.Header).To(Equal(ipfs.BlockModel{})) 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(mocks.AccountLeafKey)) + Expect(iplds7.StateNodes[0].StateLeafKey.Bytes()).To(Equal(test_helpers.AccountLeafKey)) Expect(iplds7.StateNodes[0].IPLD).To(Equal(ipfs.BlockModel{ - Data: mocks.State2IPLD.RawData(), - CID: mocks.State2IPLD.Cid().String(), + Data: test_helpers.State2IPLD.RawData(), + CID: test_helpers.State2IPLD.Cid().String(), })) - iplds8, err := filterer.Filter(rctTopicsAndAddressFilterFail, mocks.MockConvertedPayload) + iplds8, err := filterer.Filter(rctTopicsAndAddressFilterFail, test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) Expect(iplds8).ToNot(BeNil()) - Expect(iplds8.BlockNumber.Int64()).To(Equal(mocks.MockIPLDs.BlockNumber.Int64())) + Expect(iplds8.BlockNumber.Int64()).To(Equal(test_helpers.MockIPLDs.BlockNumber.Int64())) Expect(iplds8.Header).To(Equal(ipfs.BlockModel{})) Expect(len(iplds8.Uncles)).To(Equal(0)) Expect(len(iplds8.Transactions)).To(Equal(0)) diff --git a/pkg/eth/ipld_fetcher_test.go b/pkg/eth/ipld_fetcher_test.go index 58452a226..76cd9a8ca 100644 --- a/pkg/eth/ipld_fetcher_test.go +++ b/pkg/eth/ipld_fetcher_test.go @@ -24,24 +24,23 @@ import ( "github.com/vulcanize/ipld-eth-indexer/pkg/postgres" "github.com/vulcanize/ipld-eth-server/pkg/eth" - "github.com/vulcanize/ipld-eth-server/pkg/eth/mocks" + "github.com/vulcanize/ipld-eth-server/pkg/eth/test_helpers" "github.com/vulcanize/ipld-eth-server/pkg/shared" ) -var ( - db *postgres.DB - pubAndIndexer *eth2.IPLDPublisher - fetcher *eth.IPLDFetcher -) - var _ = Describe("IPLDFetcher", func() { + var ( + db *postgres.DB + pubAndIndexer *eth2.IPLDPublisher + fetcher *eth.IPLDFetcher + ) Describe("Fetch", func() { BeforeEach(func() { var err error db, err = shared.SetupDB() Expect(err).ToNot(HaveOccurred()) pubAndIndexer = eth2.NewIPLDPublisher(db) - err = pubAndIndexer.Publish(mocks.MockConvertedPayload) + err = pubAndIndexer.Publish(test_helpers.MockConvertedPayload) Expect(err).ToNot(HaveOccurred()) fetcher = eth.NewIPLDFetcher(db) }) @@ -50,17 +49,17 @@ var _ = Describe("IPLDFetcher", func() { }) It("Fetches and returns IPLDs for the CIDs provided in the CIDWrapper", func() { - iplds, err := fetcher.Fetch(*mocks.MockCIDWrapper) + iplds, err := fetcher.Fetch(*test_helpers.MockCIDWrapper) Expect(err).ToNot(HaveOccurred()) Expect(iplds).ToNot(BeNil()) - Expect(iplds.TotalDifficulty).To(Equal(mocks.MockConvertedPayload.TotalDifficulty)) - Expect(iplds.BlockNumber).To(Equal(mocks.MockConvertedPayload.Block.Number())) - Expect(iplds.Header).To(Equal(mocks.MockIPLDs.Header)) + Expect(iplds.TotalDifficulty).To(Equal(test_helpers.MockConvertedPayload.TotalDifficulty)) + Expect(iplds.BlockNumber).To(Equal(test_helpers.MockConvertedPayload.Block.Number())) + Expect(iplds.Header).To(Equal(test_helpers.MockIPLDs.Header)) Expect(len(iplds.Uncles)).To(Equal(0)) - Expect(iplds.Transactions).To(Equal(mocks.MockIPLDs.Transactions)) - Expect(iplds.Receipts).To(Equal(mocks.MockIPLDs.Receipts)) - Expect(iplds.StateNodes).To(Equal(mocks.MockIPLDs.StateNodes)) - Expect(iplds.StorageNodes).To(Equal(mocks.MockIPLDs.StorageNodes)) + Expect(iplds.Transactions).To(Equal(test_helpers.MockIPLDs.Transactions)) + Expect(iplds.Receipts).To(Equal(test_helpers.MockIPLDs.Receipts)) + Expect(iplds.StateNodes).To(Equal(test_helpers.MockIPLDs.StateNodes)) + Expect(iplds.StorageNodes).To(Equal(test_helpers.MockIPLDs.StorageNodes)) }) }) }) diff --git a/pkg/eth/mocks/chain_maker.go b/pkg/eth/mocks/chain_maker.go deleted file mode 100644 index e80afeb27..000000000 --- a/pkg/eth/mocks/chain_maker.go +++ /dev/null @@ -1,284 +0,0 @@ -package mocks - -import ( - "math/big" - "math/rand" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/consensus/ethash" - "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/core/rawdb" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/core/vm" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/params" - "github.com/ethereum/go-ethereum/rlp" -) - -// Test variables -var ( - EvenLeafFlag = []byte{byte(2) << 4} - MockBlockNumber = big.NewInt(rand.Int63()) - BlockHash = "0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73" - NullCodeHash = crypto.Keccak256Hash([]byte{}) - StoragePath = common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").Bytes() - StorageKey = common.HexToHash("0000000000000000000000000000000000000000000000000000000000000001").Bytes() - MockStorageValue = common.Hex2Bytes("0x03") - NullHash = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000") - - Testdb = rawdb.NewMemoryDatabase() - TestBankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") - TestBankAddress = crypto.PubkeyToAddress(TestBankKey.PublicKey) //0x71562b71999873DB5b286dF957af199Ec94617F7 - BankLeafKey = AddressToLeafKey(TestBankAddress) - TestBankFunds = big.NewInt(100000000) - Genesis = core.GenesisBlockForTesting(Testdb, TestBankAddress, TestBankFunds) - - Account1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") - Account2Key, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") - Account1Addr = crypto.PubkeyToAddress(Account1Key.PublicKey) //0x703c4b2bD70c169f5717101CaeE543299Fc946C7 - Account2Addr = crypto.PubkeyToAddress(Account2Key.PublicKey) //0x0D3ab14BBaD3D99F4203bd7a11aCB94882050E7e - Account1LeafKey = AddressToLeafKey(Account1Addr) - Account2LeafKey = AddressToLeafKey(Account2Addr) - ContractCode = common.Hex2Bytes("608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506040518060200160405280600160ff16815250600190600161007492919061007a565b506100e4565b82606481019282156100ae579160200282015b828111156100ad578251829060ff1690559160200191906001019061008d565b5b5090506100bb91906100bf565b5090565b6100e191905b808211156100dd5760008160009055506001016100c5565b5090565b90565b610294806100f36000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c806343d726d61461005157806360cd26851461005b578063c16431b91461009d578063f0ba8440146100d5575b600080fd5b610059610117565b005b6100876004803603602081101561007157600080fd5b81019080803590602001909291905050506101f6565b6040518082815260200191505060405180910390f35b6100d3600480360360408110156100b357600080fd5b81019080803590602001909291908035906020019092919050505061020e565b005b610101600480360360208110156100eb57600080fd5b8101908080359060200190929190505050610225565b6040518082815260200191505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101bc576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602281526020018061023e6022913960400191505060405180910390fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b60006001826064811061020557fe5b01549050919050565b806001836064811061021c57fe5b01819055505050565b6001816064811061023257fe5b01600091509050548156fe4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f6e2ea265627a7a72315820965c55d5aacd556fdc2807cef18b41da28c73ba7a04927a2234f617033285c5e64736f6c63430005110032") - ContractAddr common.Address - - EmptyRootNode, _ = rlp.EncodeToBytes([]byte{}) - EmptyContractRoot = crypto.Keccak256Hash(EmptyRootNode) -) - -/* test contract -pragma solidity ^0.5.10; - -contract test { - address payable owner; - - modifier onlyOwner { - require( - msg.sender == owner, - "Only owner can call this function." - ); - _; - } - - uint256[100] public data; - - constructor() public { - owner = msg.sender; - data = [1]; - } - - function Put(uint256 addr, uint256 value) public { - data[addr] = value; - } - - function Get(uint256 addr) public view returns(uint256) { - return data[addr]; - } - - function close() public onlyOwner { //onlyOwner is custom modifier - selfdestruct(owner); // `owner` is the owners address - } -} -*/ - -/* test ABI -[ - { - "inputs": [], - "payable": false, - "stateMutability": "nonpayable", - "type": "constructor" - }, - { - "constant": true, - "inputs": [ - { - "internalType": "uint256", - "name": "addr", - "type": "uint256" - } - ], - "name": "Get", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - }, - { - "constant": false, - "inputs": [ - { - "internalType": "uint256", - "name": "addr", - "type": "uint256" - }, - { - "internalType": "uint256", - "name": "value", - "type": "uint256" - } - ], - "name": "Put", - "outputs": [], - "payable": false, - "stateMutability": "nonpayable", - "type": "function" - }, - { - "constant": false, - "inputs": [], - "name": "close", - "outputs": [], - "payable": false, - "stateMutability": "nonpayable", - "type": "function" - }, - { - "constant": true, - "inputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "name": "data", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - } -] -*/ - -/* test compiler output -{ - "linkReferences": {}, - "object": "608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506040518060200160405280600160ff16815250600190600161007492919061007a565b506100e4565b82606481019282156100ae579160200282015b828111156100ad578251829060ff1690559160200191906001019061008d565b5b5090506100bb91906100bf565b5090565b6100e191905b808211156100dd5760008160009055506001016100c5565b5090565b90565b610294806100f36000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c806343d726d61461005157806360cd26851461005b578063c16431b91461009d578063f0ba8440146100d5575b600080fd5b610059610117565b005b6100876004803603602081101561007157600080fd5b81019080803590602001909291905050506101f6565b6040518082815260200191505060405180910390f35b6100d3600480360360408110156100b357600080fd5b81019080803590602001909291908035906020019092919050505061020e565b005b610101600480360360208110156100eb57600080fd5b8101908080359060200190929190505050610225565b6040518082815260200191505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101bc576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602281526020018061023e6022913960400191505060405180910390fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b60006001826064811061020557fe5b01549050919050565b806001836064811061021c57fe5b01819055505050565b6001816064811061023257fe5b01600091509050548156fe4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f6e2ea265627a7a72315820965c55d5aacd556fdc2807cef18b41da28c73ba7a04927a2234f617033285c5e64736f6c63430005110032", - "opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP CALLER PUSH1 0x0 DUP1 PUSH2 0x100 EXP DUP2 SLOAD DUP2 PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF MUL NOT AND SWAP1 DUP4 PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND MUL OR SWAP1 SSTORE POP PUSH1 0x40 MLOAD DUP1 PUSH1 0x20 ADD PUSH1 0x40 MSTORE DUP1 PUSH1 0x1 PUSH1 0xFF AND DUP2 MSTORE POP PUSH1 0x1 SWAP1 PUSH1 0x1 PUSH2 0x74 SWAP3 SWAP2 SWAP1 PUSH2 0x7A JUMP JUMPDEST POP PUSH2 0xE4 JUMP JUMPDEST DUP3 PUSH1 0x64 DUP2 ADD SWAP3 DUP3 ISZERO PUSH2 0xAE JUMPI SWAP2 PUSH1 0x20 MUL DUP3 ADD JUMPDEST DUP3 DUP2 GT ISZERO PUSH2 0xAD JUMPI DUP3 MLOAD DUP3 SWAP1 PUSH1 0xFF AND SWAP1 SSTORE SWAP2 PUSH1 0x20 ADD SWAP2 SWAP1 PUSH1 0x1 ADD SWAP1 PUSH2 0x8D JUMP JUMPDEST JUMPDEST POP SWAP1 POP PUSH2 0xBB SWAP2 SWAP1 PUSH2 0xBF JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST PUSH2 0xE1 SWAP2 SWAP1 JUMPDEST DUP1 DUP3 GT ISZERO PUSH2 0xDD JUMPI PUSH1 0x0 DUP2 PUSH1 0x0 SWAP1 SSTORE POP PUSH1 0x1 ADD PUSH2 0xC5 JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST SWAP1 JUMP JUMPDEST PUSH2 0x294 DUP1 PUSH2 0xF3 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN INVALID PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x4 CALLDATASIZE LT PUSH2 0x4C JUMPI PUSH1 0x0 CALLDATALOAD PUSH1 0xE0 SHR DUP1 PUSH4 0x43D726D6 EQ PUSH2 0x51 JUMPI DUP1 PUSH4 0x60CD2685 EQ PUSH2 0x5B JUMPI DUP1 PUSH4 0xC16431B9 EQ PUSH2 0x9D JUMPI DUP1 PUSH4 0xF0BA8440 EQ PUSH2 0xD5 JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH2 0x59 PUSH2 0x117 JUMP JUMPDEST STOP JUMPDEST PUSH2 0x87 PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x20 DUP2 LT ISZERO PUSH2 0x71 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x1F6 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH2 0xD3 PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x40 DUP2 LT ISZERO PUSH2 0xB3 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x20E JUMP JUMPDEST STOP JUMPDEST PUSH2 0x101 PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x20 DUP2 LT ISZERO PUSH2 0xEB JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x225 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH1 0x0 DUP1 SWAP1 SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND CALLER PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND EQ PUSH2 0x1BC JUMPI PUSH1 0x40 MLOAD PUSH32 0x8C379A000000000000000000000000000000000000000000000000000000000 DUP2 MSTORE PUSH1 0x4 ADD DUP1 DUP1 PUSH1 0x20 ADD DUP3 DUP2 SUB DUP3 MSTORE PUSH1 0x22 DUP2 MSTORE PUSH1 0x20 ADD DUP1 PUSH2 0x23E PUSH1 0x22 SWAP2 CODECOPY PUSH1 0x40 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 REVERT JUMPDEST PUSH1 0x0 DUP1 SWAP1 SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND SELFDESTRUCT JUMPDEST PUSH1 0x0 PUSH1 0x1 DUP3 PUSH1 0x64 DUP2 LT PUSH2 0x205 JUMPI INVALID JUMPDEST ADD SLOAD SWAP1 POP SWAP2 SWAP1 POP JUMP JUMPDEST DUP1 PUSH1 0x1 DUP4 PUSH1 0x64 DUP2 LT PUSH2 0x21C JUMPI INVALID JUMPDEST ADD DUP2 SWAP1 SSTORE POP POP POP JUMP JUMPDEST PUSH1 0x1 DUP2 PUSH1 0x64 DUP2 LT PUSH2 0x232 JUMPI INVALID JUMPDEST ADD PUSH1 0x0 SWAP2 POP SWAP1 POP SLOAD DUP2 JUMP INVALID 0x4F PUSH15 0x6C79206F776E65722063616E206361 PUSH13 0x6C20746869732066756E637469 PUSH16 0x6E2EA265627A7A72315820965C55D5AA 0xCD SSTORE PUSH16 0xDC2807CEF18B41DA28C73BA7A04927A2 0x23 0x4F PUSH2 0x7033 0x28 0x5C 0x5E PUSH5 0x736F6C6343 STOP SDIV GT STOP ORIGIN ", - "sourceMap": "26:553:0:-;;;221:65;8:9:-1;5:2;;;30:1;27;20:12;5:2;221:65:0;258:10;250:5;;:18;;;;;;;;;;;;;;;;;;272:10;;;;;;;;280:1;272:10;;;;;:4;:10;;;;;;;:::i;:::-;;26:553;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;:::o;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;:::o;:::-;;;;;;;" -} -*/ - -/* test function signatures -put function sig: c16431b9 -close function sig: 43d726d6 -data function sig: 60cd2685 -*/ - -// MakeChain creates a chain of n blocks starting at and including parent. -// the returned hash chain is ordered head->parent. -func MakeChain(n int, parent *types.Block, chainGen func(int, *core.BlockGen)) ([]*types.Block, *core.BlockChain) { - config := params.TestChainConfig - blocks, _ := core.GenerateChain(config, parent, ethash.NewFaker(), Testdb, n, chainGen) - chain, _ := core.NewBlockChain(Testdb, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil) - return blocks, chain -} - -func TestSelfDestructChainGen(i int, block *core.BlockGen) { - signer := types.HomesteadSigner{} - switch i { - case 0: - // Block 1 is mined by Account1Addr - // Account1Addr creates a new contract - block.SetCoinbase(TestBankAddress) - tx, _ := types.SignTx(types.NewContractCreation(0, big.NewInt(0), 1000000, big.NewInt(0), ContractCode), signer, TestBankKey) - ContractAddr = crypto.CreateAddress(TestBankAddress, 0) - block.AddTx(tx) - case 1: - // Block 2 is mined by Account1Addr - // Account1Addr self-destructs the contract - block.SetCoinbase(TestBankAddress) - data := common.Hex2Bytes("43D726D6") - tx, _ := types.SignTx(types.NewTransaction(1, ContractAddr, big.NewInt(0), 100000, nil, data), signer, TestBankKey) - block.AddTx(tx) - } -} - -func TestChainGen(i int, block *core.BlockGen) { - signer := types.HomesteadSigner{} - switch i { - case 0: - // In block 1, the test bank sends account #1 some ether. - tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(TestBankAddress), Account1Addr, big.NewInt(10000), params.TxGas, nil, nil), signer, TestBankKey) - block.AddTx(tx) - case 1: - // In block 2, the test bank sends some more ether to account #1. - // Account1Addr passes it on to account #2. - // Account1Addr creates a test contract. - tx1, _ := types.SignTx(types.NewTransaction(block.TxNonce(TestBankAddress), Account1Addr, big.NewInt(1000), params.TxGas, nil, nil), signer, TestBankKey) - nonce := block.TxNonce(Account1Addr) - tx2, _ := types.SignTx(types.NewTransaction(nonce, Account2Addr, big.NewInt(1000), params.TxGas, nil, nil), signer, Account1Key) - nonce++ - tx3, _ := types.SignTx(types.NewContractCreation(nonce, big.NewInt(0), 1000000, big.NewInt(0), ContractCode), signer, Account1Key) - ContractAddr = crypto.CreateAddress(Account1Addr, nonce) - block.AddTx(tx1) - block.AddTx(tx2) - block.AddTx(tx3) - case 2: - // Block 3 has a single tx from the bankAccount to the contract, that transfers no value - // Block 3 is mined by Account2Addr - block.SetCoinbase(Account2Addr) - data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003") - tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(TestBankAddress), ContractAddr, big.NewInt(0), 100000, nil, data), signer, TestBankKey) - block.AddTx(tx) - case 3: - // Block 4 has three txs from bankAccount to the contract, that transfer no value - // Two set the two original slot positions to 0 and one sets another position to a new value - // Block 4 is mined by Account2Addr - block.SetCoinbase(Account2Addr) - data1 := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") - data2 := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000") - data3 := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000009") - - nonce := block.TxNonce(TestBankAddress) - tx1, _ := types.SignTx(types.NewTransaction(nonce, ContractAddr, big.NewInt(0), 100000, nil, data1), signer, TestBankKey) - nonce++ - tx2, _ := types.SignTx(types.NewTransaction(nonce, ContractAddr, big.NewInt(0), 100000, nil, data2), signer, TestBankKey) - nonce++ - tx3, _ := types.SignTx(types.NewTransaction(nonce, ContractAddr, big.NewInt(0), 100000, nil, data3), signer, TestBankKey) - block.AddTx(tx1) - block.AddTx(tx2) - block.AddTx(tx3) - case 4: - // Block 5 has one tx from bankAccount to the contract, that transfers no value - // It sets the remaining storage value to zero - // Block 5 is mined by Account1Addr - block.SetCoinbase(Account1Addr) - data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000") - nonce := block.TxNonce(TestBankAddress) - tx, _ := types.SignTx(types.NewTransaction(nonce, ContractAddr, big.NewInt(0), 100000, nil, data), signer, TestBankKey) - block.AddTx(tx) - case 5: - // Block 6 has a tx from Account1Key which self-destructs the contract, it transfers no value - // Block 6 is mined by Account2Addr - block.SetCoinbase(Account2Addr) - data := common.Hex2Bytes("43D726D6") - tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(Account1Addr), ContractAddr, big.NewInt(0), 100000, nil, data), signer, Account1Key) - block.AddTx(tx) - } -} - -// AddressToLeafKey hashes an returns an address -func AddressToLeafKey(address common.Address) []byte { - return crypto.Keccak256(address[:]) -} - -// AddressToEncodedPath hashes an address and appends the even-number leaf flag to it -func AddressToEncodedPath(address common.Address) []byte { - addrHash := crypto.Keccak256(address[:]) - decodedPath := append(EvenLeafFlag, addrHash...) - return decodedPath -} diff --git a/pkg/eth/test_helpers/abi.json b/pkg/eth/test_helpers/abi.json new file mode 100644 index 000000000..6c726383c --- /dev/null +++ b/pkg/eth/test_helpers/abi.json @@ -0,0 +1,47 @@ +[ + { + "inputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "constant": false, + "inputs": [ + { + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "Put", + "outputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [], + "name": "close", + "outputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "data", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + } +] \ No newline at end of file diff --git a/pkg/eth/test_helpers/chain_maker.go b/pkg/eth/test_helpers/chain_maker.go new file mode 100644 index 000000000..29e1169b8 --- /dev/null +++ b/pkg/eth/test_helpers/chain_maker.go @@ -0,0 +1,105 @@ +// VulcanizeDB +// Copyright © 2020 Vulcanize + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package test_helpers + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/consensus/ethash" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/params" +) + +// Test variables +var ( + Testdb = rawdb.NewMemoryDatabase() + TestBankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") + TestBankAddress = crypto.PubkeyToAddress(TestBankKey.PublicKey) //0x71562b71999873DB5b286dF957af199Ec94617F7 + TestBankFunds = big.NewInt(100000000) + Genesis = core.GenesisBlockForTesting(Testdb, TestBankAddress, TestBankFunds) + + Account1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") + Account2Key, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") + Account1Addr = crypto.PubkeyToAddress(Account1Key.PublicKey) //0x703c4b2bD70c169f5717101CaeE543299Fc946C7 + Account2Addr = crypto.PubkeyToAddress(Account2Key.PublicKey) //0x0D3ab14BBaD3D99F4203bd7a11aCB94882050E7e + ContractCode = common.Hex2Bytes("608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600180819055506101e2806100676000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806343d726d61461004657806365f3c31a1461005057806373d4a13a1461007e575b600080fd5b61004e61009c565b005b61007c6004803603602081101561006657600080fd5b810190808035906020019092919050505061017b565b005b610086610185565b6040518082815260200191505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610141576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602281526020018061018c6022913960400191505060405180910390fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b8060018190555050565b6001548156fe4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f6e2ea265627a7a723158205ba91466129f45285f53176d805117208c231ec6343d7896790e6fc4165b802b64736f6c63430005110032") + ContractAddr common.Address +) + +/* test function signatures +put function sig: 65f3c31a +close function sig: 43d726d6 +data function sig: 73d4a13a +*/ + +// MakeChain creates a chain of n blocks starting at and including parent. +// the returned hash chain is ordered head->parent. +func MakeChain(n int, parent *types.Block, chainGen func(int, *core.BlockGen)) ([]*types.Block, []types.Receipts, *core.BlockChain) { + config := params.TestChainConfig + blocks, receipts := core.GenerateChain(config, parent, ethash.NewFaker(), Testdb, n, chainGen) + chain, _ := core.NewBlockChain(Testdb, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil) + return append([]*types.Block{parent}, blocks...), receipts, chain +} + +func TestChainGen(i int, block *core.BlockGen) { + signer := types.HomesteadSigner{} + switch i { + case 0: + // In block 1, the test bank sends account #1 some ether. + tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(TestBankAddress), Account1Addr, big.NewInt(10000), params.TxGas, nil, nil), signer, TestBankKey) + block.AddTx(tx) + case 1: + // In block 2, the test bank sends some more ether to account #1. + // Account1Addr passes it on to account #2. + // Account1Addr creates a test contract. + tx1, _ := types.SignTx(types.NewTransaction(block.TxNonce(TestBankAddress), Account1Addr, big.NewInt(1000), params.TxGas, nil, nil), signer, TestBankKey) + nonce := block.TxNonce(Account1Addr) + tx2, _ := types.SignTx(types.NewTransaction(nonce, Account2Addr, big.NewInt(1000), params.TxGas, nil, nil), signer, Account1Key) + nonce++ + tx3, _ := types.SignTx(types.NewContractCreation(nonce, big.NewInt(0), 1000000, big.NewInt(0), ContractCode), signer, Account1Key) + ContractAddr = crypto.CreateAddress(Account1Addr, nonce) + block.AddTx(tx1) + block.AddTx(tx2) + block.AddTx(tx3) + case 2: + block.SetCoinbase(Account2Addr) + data := common.Hex2Bytes("65F3C31A0000000000000000000000000000000000000000000000000000000000000003") + tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(TestBankAddress), ContractAddr, big.NewInt(0), 100000, nil, data), signer, TestBankKey) + block.AddTx(tx) + case 3: + block.SetCoinbase(Account2Addr) + data := common.Hex2Bytes("65F3C31A0000000000000000000000000000000000000000000000000000000000000009") + tx1, _ := types.SignTx(types.NewTransaction(block.TxNonce(TestBankAddress), ContractAddr, big.NewInt(0), 100000, nil, data), signer, TestBankKey) + block.AddTx(tx1) + case 4: + block.SetCoinbase(Account1Addr) + data := common.Hex2Bytes("65F3C31A0000000000000000000000000000000000000000000000000000000000000000") + tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(TestBankAddress), ContractAddr, big.NewInt(0), 100000, nil, data), signer, TestBankKey) + block.AddTx(tx) + } +} + +/* +608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600180819055506101e2806100676000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806343d726d61461004657806365f3c31a1461005057806373d4a13a1461007e575b600080fd5b61004e61009c565b005b61007c6004803603602081101561006657600080fd5b810190808035906020019092919050505061017b565b005b610086610185565b6040518082815260200191505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610141576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602281526020018061018c6022913960400191505060405180910390fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b8060018190555050565b6001548156fe4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f6e2ea265627a7a723158205ba91466129f45285f53176d805117208c231ec6343d7896790e6fc4165b802b64736f6c63430005110032 +608060405234801561001057600080fd5b50600436106100415760003560e01c806343d726d61461004657806365f3c31a1461005057806373d4a13a1461007e575b600080fd5b61004e61009c565b005b61007c6004803603602081101561006657600080fd5b810190808035906020019092919050505061017b565b005b610086610185565b6040518082815260200191505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610141576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602281526020018061018c6022913960400191505060405180910390fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b8060018190555050565b6001548156fe4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f6e2ea265627a7a723158205ba91466129f45285f53176d805117208c231ec6343d7896790e6fc4165b802b64736f6c63430005110032 +608060405234801561001057600080fd5b50600436106100415760003560e01c806343d726d61461004657806365f3c31a1461005057806373d4a13a1461007e575b600080fd5b61004e61009c565b005b61007c6004803603602081101561006657600080fd5b810190808035906020019092919050505061017b565b005b610086610185565b6040518082815260200191505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610141576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602281526020018061018c6022913960400191505060405180910390fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b8060018190555050565b6001548156fe4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f6e2ea265627a7a723158205ba91466129f45285f53176d805117208c231ec6343d7896790e6fc4165b802b64736f6c63430005110032 + */ \ No newline at end of file diff --git a/pkg/eth/test_helpers/compiler_output.json b/pkg/eth/test_helpers/compiler_output.json new file mode 100644 index 000000000..f4c83349e --- /dev/null +++ b/pkg/eth/test_helpers/compiler_output.json @@ -0,0 +1,6 @@ +{ + "linkReferences": {}, + "object": "608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600180819055506101e2806100676000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806343d726d61461004657806365f3c31a1461005057806373d4a13a1461007e575b600080fd5b61004e61009c565b005b61007c6004803603602081101561006657600080fd5b810190808035906020019092919050505061017b565b005b610086610185565b6040518082815260200191505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610141576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602281526020018061018c6022913960400191505060405180910390fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b8060018190555050565b6001548156fe4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f6e2ea265627a7a723158205ba91466129f45285f53176d805117208c231ec6343d7896790e6fc4165b802b64736f6c63430005110032", + "opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP CALLER PUSH1 0x0 DUP1 PUSH2 0x100 EXP DUP2 SLOAD DUP2 PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF MUL NOT AND SWAP1 DUP4 PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND MUL OR SWAP1 SSTORE POP PUSH1 0x1 DUP1 DUP2 SWAP1 SSTORE POP PUSH2 0x1E2 DUP1 PUSH2 0x67 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN INVALID PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x4 CALLDATASIZE LT PUSH2 0x41 JUMPI PUSH1 0x0 CALLDATALOAD PUSH1 0xE0 SHR DUP1 PUSH4 0x43D726D6 EQ PUSH2 0x46 JUMPI DUP1 PUSH4 0x65F3C31A EQ PUSH2 0x50 JUMPI DUP1 PUSH4 0x73D4A13A EQ PUSH2 0x7E JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH2 0x4E PUSH2 0x9C JUMP JUMPDEST STOP JUMPDEST PUSH2 0x7C PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x20 DUP2 LT ISZERO PUSH2 0x66 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x17B JUMP JUMPDEST STOP JUMPDEST PUSH2 0x86 PUSH2 0x185 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH1 0x0 DUP1 SWAP1 SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND CALLER PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND EQ PUSH2 0x141 JUMPI PUSH1 0x40 MLOAD PUSH32 0x8C379A000000000000000000000000000000000000000000000000000000000 DUP2 MSTORE PUSH1 0x4 ADD DUP1 DUP1 PUSH1 0x20 ADD DUP3 DUP2 SUB DUP3 MSTORE PUSH1 0x22 DUP2 MSTORE PUSH1 0x20 ADD DUP1 PUSH2 0x18C PUSH1 0x22 SWAP2 CODECOPY PUSH1 0x40 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 REVERT JUMPDEST PUSH1 0x0 DUP1 SWAP1 SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND SELFDESTRUCT JUMPDEST DUP1 PUSH1 0x1 DUP2 SWAP1 SSTORE POP POP JUMP JUMPDEST PUSH1 0x1 SLOAD DUP2 JUMP INVALID 0x4F PUSH15 0x6C79206F776E65722063616E206361 PUSH13 0x6C20746869732066756E637469 PUSH16 0x6E2EA265627A7A723158205BA9146612 SWAP16 GASLIMIT 0x28 0x5F MSTORE8 OR PUSH14 0x805117208C231EC6343D7896790E PUSH16 0xC4165B802B64736F6C63430005110032 ", + "sourceMap": "26:449:0:-;;;253:74;8:9:-1;5:2;;;30:1;27;20:12;5:2;253:74:0;292:10;284:5;;:18;;;;;;;;;;;;;;;;;;319:1;312:4;:8;;;;26:449;;;;;;" +} \ No newline at end of file diff --git a/pkg/eth/test_helpers/test_contract.sol b/pkg/eth/test_helpers/test_contract.sol new file mode 100644 index 000000000..b3a3d6e8f --- /dev/null +++ b/pkg/eth/test_helpers/test_contract.sol @@ -0,0 +1,28 @@ +pragma solidity ^0.5.10; + +contract test { + address payable owner; + + modifier onlyOwner { + require( + msg.sender == owner, + "Only owner can call this function." + ); + _; + } + + uint256 public data; + + constructor() public { + owner = msg.sender; + data = 1; + } + + function Put(uint256 value) public { + data = value; + } + + function close() public onlyOwner { + selfdestruct(owner); + } +} \ No newline at end of file diff --git a/pkg/eth/mocks/test_data.go b/pkg/eth/test_helpers/test_data.go similarity index 86% rename from pkg/eth/mocks/test_data.go rename to pkg/eth/test_helpers/test_data.go index b5fa42223..115fb6f56 100644 --- a/pkg/eth/mocks/test_data.go +++ b/pkg/eth/test_helpers/test_data.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -package mocks +package test_helpers import ( "crypto/ecdsa" @@ -59,7 +59,6 @@ var ( MockTransactions, MockReceipts, SenderAddr = createTransactionsAndReceipts() ReceiptsRlp, _ = rlp.EncodeToBytes(MockReceipts) MockBlock = types.NewBlock(&MockHeader, MockTransactions, nil, MockReceipts) - MockBlockRlp, _ = rlp.EncodeToBytes(MockBlock) MockHeaderRlp, _ = rlp.EncodeToBytes(MockBlock.Header()) Address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592") AnotherAddress = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476593") @@ -288,36 +287,6 @@ var ( Account, }) - StateDiffs = []statediff.StateNode{ - { - Path: []byte{'\x06'}, - NodeType: statediff.Leaf, - LeafKey: ContractLeafKey, - NodeValue: ContractLeafNode, - StorageNodes: []statediff.StorageNode{ - { - Path: []byte{}, - NodeType: statediff.Leaf, - LeafKey: StorageLeafKey, - NodeValue: StorageLeafNode, - }, - }, - }, - { - Path: []byte{'\x0c'}, - NodeType: statediff.Leaf, - LeafKey: AccountLeafKey, - NodeValue: AccountLeafNode, - StorageNodes: []statediff.StorageNode{}, - }, - } - - MockStateDiff = statediff.StateObject{ - BlockNumber: new(big.Int).Set(BlockNumber), - BlockHash: MockBlock.Hash(), - Nodes: StateDiffs, - } - MockStateDiffBytes, _ = rlp.EncodeToBytes(MockStateDiff) MockStateNodes = []eth.TrieNode{ { LeafKey: common.BytesToHash(ContractLeafKey), @@ -369,57 +338,6 @@ var ( StateNodes: MockStateNodes, } - MockCIDPayload = eth.CIDPayload{ - HeaderCID: eth.HeaderModel{ - BlockHash: MockBlock.Hash().String(), - BlockNumber: MockBlock.Number().String(), - CID: HeaderCID.String(), - MhKey: HeaderMhKey, - ParentHash: MockBlock.ParentHash().String(), - TotalDifficulty: MockBlock.Difficulty().String(), - Reward: "5000000000000000000", - StateRoot: MockBlock.Root().String(), - RctRoot: MockBlock.ReceiptHash().String(), - TxRoot: MockBlock.TxHash().String(), - UncleRoot: MockBlock.UncleHash().String(), - Bloom: MockBlock.Bloom().Bytes(), - Timestamp: MockBlock.Time(), - }, - UncleCIDs: []eth.UncleModel{}, - TransactionCIDs: MockTrxMetaPostPublsh, - ReceiptCIDs: map[common.Hash]eth.ReceiptModel{ - MockTransactions[0].Hash(): MockRctMetaPostPublish[0], - MockTransactions[1].Hash(): MockRctMetaPostPublish[1], - MockTransactions[2].Hash(): MockRctMetaPostPublish[2], - }, - StateNodeCIDs: MockStateMetaPostPublish, - StorageNodeCIDs: map[string][]eth.StorageNodeModel{ - contractPath: { - { - CID: StorageCID.String(), - MhKey: StorageMhKey, - Path: []byte{}, - StorageKey: common.BytesToHash(StorageLeafKey).Hex(), - NodeType: 2, - }, - }, - }, - StateAccounts: map[string]eth.StateAccountModel{ - contractPath: { - Balance: big.NewInt(0).String(), - Nonce: nonce1, - CodeHash: ContractCodeHash.Bytes(), - StorageRoot: common.HexToHash(ContractRoot).String(), - }, - accountPath: { - Balance: big.NewInt(1000).String(), - Nonce: nonce0, - CodeHash: AccountCodeHash.Bytes(), - StorageRoot: common.HexToHash(AccountRoot).String(), - }, - }, - } - MockCIDWrapper = ð2.CIDWrapper{ BlockNumber: new(big.Int).Set(BlockNumber), Header: eth.HeaderModel{