// newTester creates a test environment based on which the console can operate.
// Please ensure you call Close() on the returned tester to avoid leaks.
func newTester(t *testing.T, confOverride func(*eth.Config)) *tester {
	// Create a temporary storage for the node keys and initialize it
	workspace, err := ioutil.TempDir("", "console-tester-")
	if err != nil {
		t.Fatalf("failed to create temporary keystore: %v", err)
	}
	accman := accounts.NewPlaintextManager(filepath.Join(workspace, "keystore"))

	// Create a networkless protocol stack and start an Ethereum service within
	stack, err := node.New(&node.Config{DataDir: workspace, Name: testInstance, NoDiscovery: true})
	if err != nil {
		t.Fatalf("failed to create node: %v", err)
	}
	ethConf := &eth.Config{
		ChainConfig:    &core.ChainConfig{HomesteadBlock: new(big.Int)},
		Etherbase:      common.HexToAddress(testAddress),
		AccountManager: accman,
		PowTest:        true,
	}
	if confOverride != nil {
		confOverride(ethConf)
	}
	if err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil {
		t.Fatalf("failed to register Ethereum protocol: %v", err)
	}
	// Start the node and assemble the JavaScript console around it
	if err = stack.Start(); err != nil {
		t.Fatalf("failed to start test stack: %v", err)
	}
	client, err := stack.Attach()
	if err != nil {
		t.Fatalf("failed to attach to node: %v", err)
	}
	prompter := &hookedPrompter{scheduler: make(chan string)}
	printer := new(bytes.Buffer)

	console, err := New(Config{
		DataDir:  stack.DataDir(),
		DocRoot:  "testdata",
		Client:   client,
		Prompter: prompter,
		Printer:  printer,
		Preload:  []string{"preload.js"},
	})
	if err != nil {
		t.Fatalf("failed to create JavaScript console: %v", err)
	}
	// Create the final tester and return
	var ethereum *eth.Ethereum
	stack.Service(&ethereum)

	return &tester{
		workspace: workspace,
		stack:     stack,
		ethereum:  ethereum,
		console:   console,
		input:     prompter,
		output:    printer,
	}
}
Beispiel #2
0
// MakeSystemNode configures a protocol stack for the RPC tests based on a given
// keystore path and initial pre-state.
func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node.Node, error) {
	// Create a networkless protocol stack
	stack, err := node.New(&node.Config{
		IPCPath:     node.DefaultIPCEndpoint(),
		HTTPHost:    common.DefaultHTTPHost,
		HTTPPort:    common.DefaultHTTPPort,
		HTTPModules: []string{"admin", "db", "exp", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
		WSHost:      common.DefaultWSHost,
		WSPort:      common.DefaultWSPort,
		WSModules:   []string{"admin", "db", "exp", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
		NoDiscovery: true,
	})
	if err != nil {
		return nil, err
	}
	// Create the keystore and inject an unlocked account if requested
	accman := accounts.NewPlaintextManager(keydir)
	if len(privkey) > 0 {
		key, err := crypto.HexToECDSA(privkey)
		if err != nil {
			return nil, err
		}
		a, err := accman.ImportECDSA(key, "")
		if err != nil {
			return nil, err
		}
		if err := accman.Unlock(a, ""); err != nil {
			return nil, err
		}
	}
	// Initialize and register the Expanse protocol
	db, _ := ethdb.NewMemDatabase()
	if _, err := test.InsertPreState(db); err != nil {
		return nil, err
	}
	ethConf := &exp.Config{
		TestGenesisState: db,
		TestGenesisBlock: test.Genesis,
		ChainConfig:      &core.ChainConfig{HomesteadBlock: params.MainNetHomesteadBlock},
		AccountManager:   accman,
	}
	if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return exp.New(ctx, ethConf) }); err != nil {
		return nil, err
	}
	// Initialize and register the Whisper protocol
	if err := stack.Register(func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil {
		return nil, err
	}
	return stack, nil
}