aboutsummaryrefslogtreecommitdiffstats
path: root/cmd
diff options
context:
space:
mode:
authorBas van Kervel <basvankervel@ziggo.nl>2015-04-13 16:13:52 +0800
committerBas van Kervel <basvankervel@ziggo.nl>2015-04-13 16:13:52 +0800
commit49a513bdebd7c4402b3a7f2f169a31c34f2ca9df (patch)
tree2cb976e970a8aa757ce31ee867970cf3d53ad15c /cmd
parent4de1e1609abb2e5be7e5cc5b8f206d305af8ce27 (diff)
downloadgo-tangerine-49a513bdebd7c4402b3a7f2f169a31c34f2ca9df.tar
go-tangerine-49a513bdebd7c4402b3a7f2f169a31c34f2ca9df.tar.gz
go-tangerine-49a513bdebd7c4402b3a7f2f169a31c34f2ca9df.tar.bz2
go-tangerine-49a513bdebd7c4402b3a7f2f169a31c34f2ca9df.tar.lz
go-tangerine-49a513bdebd7c4402b3a7f2f169a31c34f2ca9df.tar.xz
go-tangerine-49a513bdebd7c4402b3a7f2f169a31c34f2ca9df.tar.zst
go-tangerine-49a513bdebd7c4402b3a7f2f169a31c34f2ca9df.zip
Added blockchain DB versioning support, closes #650
Diffstat (limited to 'cmd')
-rw-r--r--cmd/geth/main.go95
-rw-r--r--cmd/utils/cmd.go25
-rw-r--r--cmd/utils/flags.go60
3 files changed, 154 insertions, 26 deletions
diff --git a/cmd/geth/main.go b/cmd/geth/main.go
index 4853a16fc..ba8bb6450 100644
--- a/cmd/geth/main.go
+++ b/cmd/geth/main.go
@@ -34,11 +34,13 @@ import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/logger"
"github.com/peterh/liner"
+ "path"
)
const (
@@ -205,12 +207,18 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
Name: "export",
Usage: `export blockchain into file`,
},
+ {
+ Action: upgradeDb,
+ Name: "upgradedb",
+ Usage: "upgrade chainblock database",
+ },
}
app.Flags = []cli.Flag{
utils.UnlockedAccountFlag,
utils.PasswordFileFlag,
utils.BootnodesFlag,
utils.DataDirFlag,
+ utils.BlockchainVersionFlag,
utils.JSpathFlag,
utils.ListenPortFlag,
utils.MaxPeersFlag,
@@ -429,13 +437,29 @@ func importchain(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
utils.Fatalf("This command requires an argument.")
}
- chainmgr, _, _ := utils.GetChain(ctx)
+
+ cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
+ cfg.SkipBcVersionCheck = true
+
+ ethereum, err := eth.New(cfg)
+ if err != nil {
+ utils.Fatalf("%v\n", err)
+ }
+
+ chainmgr := ethereum.ChainManager()
start := time.Now()
- err := utils.ImportChain(chainmgr, ctx.Args().First())
+ err = utils.ImportChain(chainmgr, ctx.Args().First())
if err != nil {
utils.Fatalf("Import error: %v\n", err)
}
+
+ // force database flush
+ ethereum.BlockDb().Close()
+ ethereum.StateDb().Close()
+ ethereum.ExtraDb().Close()
+
fmt.Printf("Import done in %v", time.Since(start))
+
return
}
@@ -443,9 +467,18 @@ func exportchain(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
utils.Fatalf("This command requires an argument.")
}
- chainmgr, _, _ := utils.GetChain(ctx)
+
+ cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
+ cfg.SkipBcVersionCheck = true
+
+ ethereum, err := eth.New(cfg)
+ if err != nil {
+ utils.Fatalf("%v\n", err)
+ }
+
+ chainmgr := ethereum.ChainManager()
start := time.Now()
- err := utils.ExportChain(chainmgr, ctx.Args().First())
+ err = utils.ExportChain(chainmgr, ctx.Args().First())
if err != nil {
utils.Fatalf("Export error: %v\n", err)
}
@@ -453,6 +486,60 @@ func exportchain(ctx *cli.Context) {
return
}
+func upgradeDb(ctx *cli.Context) {
+ fmt.Println("Upgrade blockchain DB")
+
+ cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
+ cfg.SkipBcVersionCheck = true
+
+ ethereum, err := eth.New(cfg)
+ if err != nil {
+ utils.Fatalf("%v\n", err)
+ }
+
+ v, _ := ethereum.BlockDb().Get([]byte("BlockchainVersion"))
+ bcVersion := int(common.NewValue(v).Uint())
+
+ if bcVersion == 0 {
+ bcVersion = core.BlockChainVersion
+ }
+
+ filename := fmt.Sprintf("blockchain_%d_%s.chain", bcVersion, time.Now().Format("2006-01-02_15:04:05"))
+ exportFile := path.Join(ctx.GlobalString(utils.DataDirFlag.Name), filename)
+
+ err = utils.ExportChain(ethereum.ChainManager(), exportFile)
+ if err != nil {
+ utils.Fatalf("Unable to export chain for reimport %s\n", err)
+ }
+
+ ethereum.BlockDb().Close()
+ ethereum.StateDb().Close()
+ ethereum.ExtraDb().Close()
+
+ os.RemoveAll(path.Join(ctx.GlobalString(utils.DataDirFlag.Name), "blockchain"))
+
+ ethereum, err = eth.New(cfg)
+ if err != nil {
+ utils.Fatalf("%v\n", err)
+ }
+
+ ethereum.BlockDb().Put([]byte("BlockchainVersion"), common.NewValue(core.BlockChainVersion).Bytes())
+
+ err = utils.ImportChain(ethereum.ChainManager(), exportFile)
+ if err != nil {
+ utils.Fatalf("Import error %v (a backup is made in %s, use the import command to import it)\n", err, exportFile)
+ }
+
+ // force database flush
+ ethereum.BlockDb().Close()
+ ethereum.StateDb().Close()
+ ethereum.ExtraDb().Close()
+
+ os.Remove(exportFile)
+
+ fmt.Println("Import finished")
+}
+
func dump(ctx *cli.Context) {
chainmgr, _, stateDb := utils.GetChain(ctx)
for _, arg := range ctx.Args() {
diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go
index a6140d233..7286f5c5e 100644
--- a/cmd/utils/cmd.go
+++ b/cmd/utils/cmd.go
@@ -155,7 +155,11 @@ func ImportChain(chainmgr *core.ChainManager, fn string) error {
chainmgr.Reset()
stream := rlp.NewStream(fh)
- var i int
+ var i, n int
+
+ batchSize := 2500
+ blocks := make(types.Blocks, batchSize)
+
for ; ; i++ {
var b types.Block
if err := stream.Decode(&b); err == io.EOF {
@@ -163,10 +167,25 @@ func ImportChain(chainmgr *core.ChainManager, fn string) error {
} else if err != nil {
return fmt.Errorf("at block %d: %v", i, err)
}
- if err := chainmgr.InsertChain(types.Blocks{&b}); err != nil {
- return fmt.Errorf("invalid block %d: %v", i, err)
+
+ blocks[n] = &b
+ n++
+
+ if n == batchSize {
+ if err := chainmgr.InsertChain(blocks); err != nil {
+ return fmt.Errorf("invalid block %v", err)
+ }
+ n = 0
+ blocks = make(types.Blocks, batchSize)
+ }
+ }
+
+ if n > 0 {
+ if err := chainmgr.InsertChain(blocks[:n]); err != nil {
+ return fmt.Errorf("invalid block %v", err)
}
}
+
fmt.Printf("imported %d blocks\n", i)
return nil
}
diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go
index 3ad06653e..8141fae82 100644
--- a/cmd/utils/flags.go
+++ b/cmd/utils/flags.go
@@ -7,6 +7,7 @@ import (
"runtime"
"github.com/codegangsta/cli"
+ "github.com/ethereum/ethash"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
@@ -83,6 +84,11 @@ var (
Usage: "Network Id",
Value: eth.NetworkId,
}
+ BlockchainVersionFlag = cli.IntFlag{
+ Name: "blockchainversion",
+ Usage: "Blockchain version",
+ Value: core.BlockChainVersion,
+ }
// miner settings
MinerThreadsFlag = cli.IntFlag{
@@ -237,29 +243,32 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name))
return &eth.Config{
- Name: common.MakeName(clientID, version),
- DataDir: ctx.GlobalString(DataDirFlag.Name),
- ProtocolVersion: ctx.GlobalInt(ProtocolVersionFlag.Name),
- NetworkId: ctx.GlobalInt(NetworkIdFlag.Name),
- LogFile: ctx.GlobalString(LogFileFlag.Name),
- LogLevel: ctx.GlobalInt(LogLevelFlag.Name),
- LogJSON: ctx.GlobalString(LogJSONFlag.Name),
- Etherbase: ctx.GlobalString(EtherbaseFlag.Name),
- MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
- AccountManager: GetAccountManager(ctx),
- VmDebug: ctx.GlobalBool(VMDebugFlag.Name),
- MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
- Port: ctx.GlobalString(ListenPortFlag.Name),
- NAT: GetNAT(ctx),
- NodeKey: GetNodeKey(ctx),
- Shh: true,
- Dial: true,
- BootNodes: ctx.GlobalString(BootnodesFlag.Name),
+ Name: common.MakeName(clientID, version),
+ DataDir: ctx.GlobalString(DataDirFlag.Name),
+ ProtocolVersion: ctx.GlobalInt(ProtocolVersionFlag.Name),
+ BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name),
+ SkipBcVersionCheck: false,
+ NetworkId: ctx.GlobalInt(NetworkIdFlag.Name),
+ LogFile: ctx.GlobalString(LogFileFlag.Name),
+ LogLevel: ctx.GlobalInt(LogLevelFlag.Name),
+ LogJSON: ctx.GlobalString(LogJSONFlag.Name),
+ Etherbase: ctx.GlobalString(EtherbaseFlag.Name),
+ MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
+ AccountManager: GetAccountManager(ctx),
+ VmDebug: ctx.GlobalBool(VMDebugFlag.Name),
+ MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
+ Port: ctx.GlobalString(ListenPortFlag.Name),
+ NAT: GetNAT(ctx),
+ NodeKey: GetNodeKey(ctx),
+ Shh: true,
+ Dial: true,
+ BootNodes: ctx.GlobalString(BootnodesFlag.Name),
}
}
func GetChain(ctx *cli.Context) (*core.ChainManager, common.Database, common.Database) {
dataDir := ctx.GlobalString(DataDirFlag.Name)
+
blockDb, err := ethdb.NewLDBDatabase(path.Join(dataDir, "blockchain"))
if err != nil {
Fatalf("Could not open database: %v", err)
@@ -269,7 +278,20 @@ func GetChain(ctx *cli.Context) (*core.ChainManager, common.Database, common.Dat
if err != nil {
Fatalf("Could not open database: %v", err)
}
- return core.NewChainManager(blockDb, stateDb, new(event.TypeMux)), blockDb, stateDb
+
+ extraDb, err := ethdb.NewLDBDatabase(path.Join(dataDir, "extra"))
+ if err != nil {
+ Fatalf("Could not open database: %v", err)
+ }
+
+ eventMux := new(event.TypeMux)
+ chainManager := core.NewChainManager(blockDb, stateDb, eventMux)
+ pow := ethash.New(chainManager)
+ txPool := core.NewTxPool(eventMux, chainManager.State)
+ blockProcessor := core.NewBlockProcessor(stateDb, extraDb, pow, txPool, chainManager, eventMux)
+ chainManager.SetProcessor(blockProcessor)
+
+ return chainManager, blockDb, stateDb
}
func GetAccountManager(ctx *cli.Context) *accounts.Manager {