github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/swarm/db.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2017 Go Ethereum作者 10 //此文件是Go以太坊的一部分。 11 // 12 //Go以太坊是免费软件:您可以重新发布和/或修改它 13 //根据GNU通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊的分布希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU通用公共许可证了解更多详细信息。 21 // 22 //你应该已经收到一份GNU通用公共许可证的副本 23 //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package main 26 27 import ( 28 "fmt" 29 "io" 30 "os" 31 "path/filepath" 32 33 "github.com/ethereum/go-ethereum/cmd/utils" 34 "github.com/ethereum/go-ethereum/common" 35 "github.com/ethereum/go-ethereum/log" 36 "github.com/ethereum/go-ethereum/swarm/storage" 37 "gopkg.in/urfave/cli.v1" 38 ) 39 40 func dbExport(ctx *cli.Context) { 41 args := ctx.Args() 42 if len(args) != 3 { 43 utils.Fatalf("invalid arguments, please specify both <chunkdb> (path to a local chunk database), <file> (path to write the tar archive to, - for stdout) and the base key") 44 } 45 46 store, err := openLDBStore(args[0], common.Hex2Bytes(args[2])) 47 if err != nil { 48 utils.Fatalf("error opening local chunk database: %s", err) 49 } 50 defer store.Close() 51 52 var out io.Writer 53 if args[1] == "-" { 54 out = os.Stdout 55 } else { 56 f, err := os.Create(args[1]) 57 if err != nil { 58 utils.Fatalf("error opening output file: %s", err) 59 } 60 defer f.Close() 61 out = f 62 } 63 64 count, err := store.Export(out) 65 if err != nil { 66 utils.Fatalf("error exporting local chunk database: %s", err) 67 } 68 69 log.Info(fmt.Sprintf("successfully exported %d chunks", count)) 70 } 71 72 func dbImport(ctx *cli.Context) { 73 args := ctx.Args() 74 if len(args) != 3 { 75 utils.Fatalf("invalid arguments, please specify both <chunkdb> (path to a local chunk database), <file> (path to read the tar archive from, - for stdin) and the base key") 76 } 77 78 store, err := openLDBStore(args[0], common.Hex2Bytes(args[2])) 79 if err != nil { 80 utils.Fatalf("error opening local chunk database: %s", err) 81 } 82 defer store.Close() 83 84 var in io.Reader 85 if args[1] == "-" { 86 in = os.Stdin 87 } else { 88 f, err := os.Open(args[1]) 89 if err != nil { 90 utils.Fatalf("error opening input file: %s", err) 91 } 92 defer f.Close() 93 in = f 94 } 95 96 count, err := store.Import(in) 97 if err != nil { 98 utils.Fatalf("error importing local chunk database: %s", err) 99 } 100 101 log.Info(fmt.Sprintf("successfully imported %d chunks", count)) 102 } 103 104 func dbClean(ctx *cli.Context) { 105 args := ctx.Args() 106 if len(args) != 2 { 107 utils.Fatalf("invalid arguments, please specify <chunkdb> (path to a local chunk database) and the base key") 108 } 109 110 store, err := openLDBStore(args[0], common.Hex2Bytes(args[1])) 111 if err != nil { 112 utils.Fatalf("error opening local chunk database: %s", err) 113 } 114 defer store.Close() 115 116 store.Cleanup() 117 } 118 119 func openLDBStore(path string, basekey []byte) (*storage.LDBStore, error) { 120 if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil { 121 return nil, fmt.Errorf("invalid chunkdb path: %s", err) 122 } 123 124 storeparams := storage.NewDefaultStoreParams() 125 ldbparams := storage.NewLDBStoreParams(storeparams, path) 126 ldbparams.BaseKey = basekey 127 return storage.NewLDBStore(ldbparams) 128 }