Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Setup Fake Node for Tests #48

Open
rmulhol opened this issue Apr 18, 2018 · 1 comment
Open

Setup Fake Node for Tests #48

rmulhol opened this issue Apr 18, 2018 · 1 comment
Assignees

Comments

@rmulhol
Copy link
Contributor

rmulhol commented Apr 18, 2018

Testing against Infura leads to some flakiness, would be ideal to have a fake node that returns static data for our integration tests

@mkrump mkrump closed this as completed May 3, 2018
@mkrump mkrump reopened this May 3, 2018
@mkrump mkrump self-assigned this May 3, 2018
@mkrump
Copy link
Contributor

mkrump commented May 3, 2018

Need to figure out the appropriate seam to inject the test blockchain (1st test) into test node (2nd test). In the meantime, hopefully change made in 9df602d makes this test more reliable.


import (
	"math/big"

	"context"
	"io/ioutil"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/consensus/ethash"
	ethcore "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/crypto"
	"github.com/ethereum/go-ethereum/eth"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/ethereum/go-ethereum/ethdb"
	"github.com/ethereum/go-ethereum/node"
	"github.com/ethereum/go-ethereum/params"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Reading from the Geth blockchain", func() {

	It("sets up and reads from test blockchain", func() {
		var (
			mainnetChainConfig = params.ChainConfig{
				ChainId:        big.NewInt(1),
				HomesteadBlock: big.NewInt(1150000),
				DAOForkBlock:   big.NewInt(1920000),
				DAOForkSupport: true,
				EIP150Block:    big.NewInt(2463000),
				EIP150Hash:     common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
				EIP155Block:    big.NewInt(2675000),
				EIP158Block:    big.NewInt(2675000),
				ByzantiumBlock: big.NewInt(4370000),
			}
			db, _   = ethdb.NewMemDatabase()
			key, _  = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
			address = crypto.PubkeyToAddress(key.PublicKey)
			funds   = big.NewInt(1000000000)
			gspec   = &ethcore.Genesis{
				Config: &mainnetChainConfig,
				Alloc:  ethcore.GenesisAlloc{address: {Balance: funds}},
			}
			genesis = gspec.MustCommit(db)
		)

		blockchain, _ := ethcore.NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
		defer blockchain.Stop()
		blocks, _ := ethcore.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 10, func(i int, block *ethcore.BlockGen) {
			var (
				tx      *types.Transaction
				err     error
				basicTx = func(signer types.Signer) (*types.Transaction, error) {
					return types.SignTx(types.NewTransaction(block.TxNonce(address), common.Address{}, new(big.Int), 21000, new(big.Int), nil), signer, key)
				}
			)
			tx, err = basicTx(types.HomesteadSigner{})
			if err != nil {
				Expect(err).ToNot(HaveOccurred())
			}
			block.AddTx(tx)
		})
		if _, err := blockchain.InsertChain(blocks); err != nil {
			Expect(err).ToNot(HaveOccurred())
		}
		Expect(blockchain.CurrentBlock().Header().Number.Uint64()).To(Equal(uint64(10)))
	})

	It("creates a test node", func() {
		const (
			testInstance = "console-tester"
			testAddress  = "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
		)
		workspace, err := ioutil.TempDir("", "console-tester-")
		if err != nil {
			Expect(err).ToNot(HaveOccurred(), "failed to create temporary keystore: %v")
		}

		//TODO need to inject test blockchain from above into this
		stack, err := node.New(&node.Config{DataDir: workspace, UseLightweightKDF: true, Name: testInstance})

		if err != nil {
			Expect(err).ToNot(HaveOccurred(), "failed to create node: %v")
		}
		ethConf := &eth.Config{
			NetworkId: 1,
			Genesis:   ethcore.DeveloperGenesisBlock(15, common.Address{}),
			Etherbase: common.HexToAddress(testAddress),
			Ethash: ethash.Config{
				PowMode: ethash.ModeTest,
			},
		}
		if err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
			return eth.New(ctx, ethConf)
		}); err != nil {
			Expect(err).ToNot(HaveOccurred(), "failed to register Ethereum protocol: %v")
		}
		// Start the node and assemble the JavaScript console around it
		if err = stack.Start(); err != nil {
			Expect(err).ToNot(HaveOccurred(), "failed to start test stack: %v")
		}
		client, err := stack.Attach()
		e := ethclient.NewClient(client)

		//Now have access to all ethereum client calls
		//need to figure out how inject blockchain in first
		//test into the Ethereum object (i.e. eth.New)
		id, err := e.NetworkID(context.Background())
		Expect(err).ToNot(HaveOccurred())
		Expect(id.Int64()).To(Equal(int64(1)))
	})
})

i-norden pushed a commit that referenced this issue Apr 9, 2019
i-norden pushed a commit that referenced this issue Apr 18, 2019
i-norden pushed a commit that referenced this issue Apr 18, 2019
Gslaughl pushed a commit that referenced this issue Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants