Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull go-ethereum up to 26aea736 (7 Feb 2019) #3

Merged
merged 3 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ func initGenesis(ctx *cli.Context) error {
}
// Open an initialise both full and light databases
stack := makeFullNode(ctx)
defer stack.Close()

for _, name := range []string{"chaindata", "lightchaindata"} {
chaindb, err := stack.OpenDatabase(name)
if err != nil {
Expand All @@ -207,6 +209,8 @@ func importChain(ctx *cli.Context) error {
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
defer stack.Close()

chain, chainDb := utils.MakeChain(ctx, stack)
defer chainDb.Close()

Expand Down Expand Up @@ -259,6 +263,8 @@ func exportChain(ctx *cli.Context) error {
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
defer stack.Close()

chain, _ := utils.MakeChain(ctx, stack)
start := time.Now()

Expand Down Expand Up @@ -292,9 +298,11 @@ func importPreimages(ctx *cli.Context) error {
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
diskdb := utils.MakeChainDatabase(ctx, stack).(*ethdb.LDBDatabase)
defer stack.Close()

diskdb := utils.MakeChainDatabase(ctx, stack).(*ethdb.LDBDatabase)
start := time.Now()

if err := utils.ImportPreimages(diskdb, ctx.Args().First()); err != nil {
utils.Fatalf("Import error: %v\n", err)
}
Expand All @@ -308,9 +316,11 @@ func exportPreimages(ctx *cli.Context) error {
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
diskdb := utils.MakeChainDatabase(ctx, stack).(*ethdb.LDBDatabase)
defer stack.Close()

diskdb := utils.MakeChainDatabase(ctx, stack).(*ethdb.LDBDatabase)
start := time.Now()

if err := utils.ExportPreimages(diskdb, ctx.Args().First()); err != nil {
utils.Fatalf("Export error: %v\n", err)
}
Expand All @@ -325,8 +335,9 @@ func copyDb(ctx *cli.Context) error {
}
// Initialize a new chain for the running node to sync into
stack := makeFullNode(ctx)
chain, chainDb := utils.MakeChain(ctx, stack)
defer stack.Close()

chain, chainDb := utils.MakeChain(ctx, stack)
syncmode := *utils.GlobalTextMarshaler(ctx, utils.SyncModeFlag.Name).(*downloader.SyncMode)
dl := downloader.New(syncmode, chainDb, new(event.TypeMux), chain, nil, nil)

Expand Down Expand Up @@ -389,6 +400,8 @@ func removeDB(ctx *cli.Context) error {

func dump(ctx *cli.Context) error {
stack := makeFullNode(ctx)
defer stack.Close()

chain, chainDb := utils.MakeChain(ctx, stack)
for _, arg := range ctx.Args() {
var block *types.Block
Expand Down
4 changes: 2 additions & 2 deletions cmd/geth/consolecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func localConsole(ctx *cli.Context) error {
// Create and start the node based on the CLI flags
node := makeFullNode(ctx)
startNode(ctx, node)
defer node.Stop()
defer node.Close()

// Attach to the newly started node and start the JavaScript console
client, err := node.Attach()
Expand Down Expand Up @@ -180,7 +180,7 @@ func ephemeralConsole(ctx *cli.Context) error {
// Create and start the node based on the CLI flags
node := makeFullNode(ctx)
startNode(ctx, node)
defer node.Stop()
defer node.Close()

// Attach to the newly started node and start the JavaScript console
client, err := node.Attach()
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func geth(ctx *cli.Context) error {
return fmt.Errorf("invalid command: %q", args[0])
}
node := makeFullNode(ctx)
defer node.Close()
startNode(ctx, node)
node.Wait()
return nil
Expand Down
4 changes: 2 additions & 2 deletions console/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ func (env *tester) Close(t *testing.T) {
if err := env.console.Stop(false); err != nil {
t.Errorf("failed to stop embedded console: %v", err)
}
if err := env.stack.Stop(); err != nil {
t.Errorf("failed to stop embedded node: %v", err)
if err := env.stack.Close(); err != nil {
t.Errorf("failed to tear down embedded node: %v", err)
}
os.RemoveAll(env.workspace)
}
Expand Down
2 changes: 1 addition & 1 deletion core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ func (self *StateDB) Copy() *StateDB {
refund: self.refund,
logs: make(map[common.Hash][]*types.Log, len(self.logs)),
logSize: self.logSize,
preimages: make(map[common.Hash][]byte),
preimages: make(map[common.Hash][]byte, len(self.preimages)),
journal: newJournal(),
}
// Copy the dirty states, logs, and preimages
Expand Down
9 changes: 9 additions & 0 deletions core/state/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,15 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction {
},
args: make([]int64, 1),
},
{
name: "AddPreimage",
fn: func(a testAction, s *StateDB) {
preimage := []byte{1}
hash := common.BytesToHash(preimage)
s.AddPreimage(hash, preimage)
},
args: make([]int64, 1),
},
}
action := actions[r.Intn(len(actions))]
var nameargs []string
Expand Down
2 changes: 1 addition & 1 deletion miner/stress_clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func main() {
if err != nil {
panic(err)
}
defer node.Stop()
defer node.Close()

for node.Server().NodeInfo().Ports.Listener == 0 {
time.Sleep(250 * time.Millisecond)
Expand Down
2 changes: 1 addition & 1 deletion miner/stress_ethash.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func main() {
if err != nil {
panic(err)
}
defer node.Stop()
defer node.Close()

for node.Server().NodeInfo().Ports.Listener == 0 {
time.Sleep(250 * time.Millisecond)
Expand Down
18 changes: 15 additions & 3 deletions node/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,22 @@ func TestDatadirCreation(t *testing.T) {
}
defer os.RemoveAll(dir)

if _, err := New(&Config{DataDir: dir}); err != nil {
node, err := New(&Config{DataDir: dir})
if err != nil {
t.Fatalf("failed to create stack with existing datadir: %v", err)
}
if err := node.Close(); err != nil {
t.Fatalf("failed to close node: %v", err)
}
// Generate a long non-existing datadir path and check that it gets created by a node
dir = filepath.Join(dir, "a", "b", "c", "d", "e", "f")
if _, err := New(&Config{DataDir: dir}); err != nil {
node, err = New(&Config{DataDir: dir})
if err != nil {
t.Fatalf("failed to create stack with creatable datadir: %v", err)
}
if err := node.Close(); err != nil {
t.Fatalf("failed to close node: %v", err)
}
if _, err := os.Stat(dir); err != nil {
t.Fatalf("freshly created datadir not accessible: %v", err)
}
Expand All @@ -57,8 +65,12 @@ func TestDatadirCreation(t *testing.T) {
defer os.Remove(file.Name())

dir = filepath.Join(file.Name(), "invalid/path")
if _, err := New(&Config{DataDir: dir}); err == nil {
node, err = New(&Config{DataDir: dir})
if err == nil {
t.Fatalf("protocol stack created with an invalid datadir")
if err := node.Close(); err != nil {
t.Fatalf("failed to close node: %v", err)
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,29 @@ func New(conf *Config) (*Node, error) {
}, nil
}

// Close stops the Node and releases resources acquired in
// Node constructor New.
func (n *Node) Close() error {
var errs []error

// Terminate all subsystems and collect any errors
if err := n.Stop(); err != nil && err != ErrNodeStopped {
errs = append(errs, err)
}
if err := n.accman.Close(); err != nil {
errs = append(errs, err)
}
// Report any errors that might have occurred
switch len(errs) {
case 0:
return nil
case 1:
return errs[0]
default:
return fmt.Errorf("%v", errs)
}
}

// Register injects a new service into the node's stack. The service created by
// the passed constructor must be unique in its type with regard to sibling ones.
func (n *Node) Register(constructor ServiceConstructor) error {
Expand Down
2 changes: 2 additions & 0 deletions node/node_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func ExampleService() {
if err != nil {
log.Fatalf("Failed to create network node: %v", err)
}
defer stack.Close()

// Create and register a simple network service. This is done through the definition
// of a node.ServiceConstructor that will instantiate a node.Service. The reason for
// the factory method approach is to support service restarts without relying on the
Expand Down
24 changes: 24 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func TestNodeLifeCycle(t *testing.T) {
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
defer stack.Close()

// Ensure that a stopped node can be stopped again
for i := 0; i < 3; i++ {
if err := stack.Stop(); err != ErrNodeStopped {
Expand Down Expand Up @@ -88,6 +90,8 @@ func TestNodeUsedDataDir(t *testing.T) {
if err != nil {
t.Fatalf("failed to create original protocol stack: %v", err)
}
defer original.Close()

if err := original.Start(); err != nil {
t.Fatalf("failed to start original protocol stack: %v", err)
}
Expand All @@ -98,6 +102,8 @@ func TestNodeUsedDataDir(t *testing.T) {
if err != nil {
t.Fatalf("failed to create duplicate protocol stack: %v", err)
}
defer duplicate.Close()

if err := duplicate.Start(); err != ErrDatadirUsed {
t.Fatalf("duplicate datadir failure mismatch: have %v, want %v", err, ErrDatadirUsed)
}
Expand All @@ -109,6 +115,8 @@ func TestServiceRegistry(t *testing.T) {
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
defer stack.Close()

// Register a batch of unique services and ensure they start successfully
services := []ServiceConstructor{NewNoopServiceA, NewNoopServiceB, NewNoopServiceC}
for i, constructor := range services {
Expand Down Expand Up @@ -141,6 +149,8 @@ func TestServiceLifeCycle(t *testing.T) {
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
defer stack.Close()

// Register a batch of life-cycle instrumented services
services := map[string]InstrumentingWrapper{
"A": InstrumentedServiceMakerA,
Expand Down Expand Up @@ -191,6 +201,8 @@ func TestServiceRestarts(t *testing.T) {
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
defer stack.Close()

// Define a service that does not support restarts
var (
running bool
Expand Down Expand Up @@ -239,6 +251,8 @@ func TestServiceConstructionAbortion(t *testing.T) {
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
defer stack.Close()

// Define a batch of good services
services := map[string]InstrumentingWrapper{
"A": InstrumentedServiceMakerA,
Expand Down Expand Up @@ -286,6 +300,8 @@ func TestServiceStartupAbortion(t *testing.T) {
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
defer stack.Close()

// Register a batch of good services
services := map[string]InstrumentingWrapper{
"A": InstrumentedServiceMakerA,
Expand Down Expand Up @@ -339,6 +355,8 @@ func TestServiceTerminationGuarantee(t *testing.T) {
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
defer stack.Close()

// Register a batch of good services
services := map[string]InstrumentingWrapper{
"A": InstrumentedServiceMakerA,
Expand Down Expand Up @@ -414,6 +432,8 @@ func TestServiceRetrieval(t *testing.T) {
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
defer stack.Close()

if err := stack.Register(NewNoopService); err != nil {
t.Fatalf("noop service registration failed: %v", err)
}
Expand Down Expand Up @@ -449,6 +469,8 @@ func TestProtocolGather(t *testing.T) {
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
defer stack.Close()

// Register a batch of services with some configured number of protocols
services := map[string]struct {
Count int
Expand Down Expand Up @@ -505,6 +527,8 @@ func TestAPIGather(t *testing.T) {
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
defer stack.Close()

// Register a batch of services with some configured APIs
calls := make(chan string, 1)
makeAPI := func(result string) *OneMethodAPI {
Expand Down
1 change: 1 addition & 0 deletions node/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestContextServices(t *testing.T) {
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
defer stack.Close()
// Define a verifier that ensures a NoopA is before it and NoopB after
verifier := func(ctx *ServiceContext) (Service, error) {
var objA *NoopServiceA
Expand Down
6 changes: 6 additions & 0 deletions p2p/simulations/adapters/inproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ type SimNode struct {
registerOnce sync.Once
}

// Close closes the underlaying node.Node to release
// acquired resources.
func (sn *SimNode) Close() error {
return sn.node.Close()
}

// Addr returns the node's discovery address
func (sn *SimNode) Addr() []byte {
return []byte(sn.Node().String())
Expand Down
7 changes: 7 additions & 0 deletions p2p/simulations/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"sync"
"time"
Expand Down Expand Up @@ -569,6 +570,12 @@ func (net *Network) Shutdown() {
if err := node.Stop(); err != nil {
log.Warn("Can't stop node", "id", node.ID(), "err", err)
}
// If the node has the close method, call it.
if closer, ok := node.Node.(io.Closer); ok {
if err := closer.Close(); err != nil {
log.Warn("Can't close node", "id", node.ID(), "err", err)
}
}
}
close(net.quitc)
}
Expand Down