Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Commit

Permalink
Merge 5ac0cad into 5d69faf
Browse files Browse the repository at this point in the history
  • Loading branch information
whilei authored May 26, 2017
2 parents 5d69faf + 5ac0cad commit ba03cb3
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 29 deletions.
16 changes: 16 additions & 0 deletions accounts/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,19 @@ func TestAccountCache_WatchRemove(t *testing.T) {
}
t.Errorf("got: %v, want: %v", spew.Sdump(gotAccounts), spew.Sdump(wantAccounts))
}

func TestCacheFilePath (t *testing.T) {
dir := filepath.Join("testdata", "keystore")
dir, _ = filepath.Abs(dir)
cache := newAddrCache(dir)

accs := cache.accounts()

for _, a := range accs {
if !filepath.IsAbs(a.File) {
t.Errorf("wanted absolute filepath, got: %v", a.File)
}
}

cache.close()
}
20 changes: 20 additions & 0 deletions accounts/cachedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func TestCacheInitialReload_CacheDB(t *testing.T) {
if !reflect.DeepEqual(accounts, cachedbtestAccounts) {
t.Errorf("got initial accounts: %swant %s", spew.Sdump(accounts), spew.Sdump(cachedbtestAccounts))
}
cache.close()
}

func TestCacheAddDeleteOrder_CacheDB(t *testing.T) {
Expand Down Expand Up @@ -232,6 +233,7 @@ func TestCacheAddDeleteOrder_CacheDB(t *testing.T) {
if cache.hasAddress(wantAccounts[0].Address) {
t.Errorf("expected hasAccount(%x) to return false", wantAccounts[0].Address)
}
cache.close()
}

func TestCacheFind_CacheFind(t *testing.T) {
Expand Down Expand Up @@ -313,6 +315,7 @@ func TestCacheFind_CacheFind(t *testing.T) {
continue
}
}
cache.close()
}

func TestAccountCache_CacheDB_SyncFS2DB(t *testing.T) {
Expand Down Expand Up @@ -394,6 +397,23 @@ func TestAccountCache_CacheDB_SyncFS2DB(t *testing.T) {
ma = nil
}

func TestCacheDBFilePath (t *testing.T) {
dir := filepath.Join("testdata", "keystore")
dir, _ = filepath.Abs(dir)
cache := newCacheDB(dir)

accs := cache.accounts()

for _, a := range accs {
if filepath.IsAbs(a.File) {
t.Errorf("wanted relative filepath (wanted basename), got: %v", a.File)
}
}

cache.close()
cache = nil
}

//func TestAccountCache_CacheDB_WatchRemove(t *testing.T) {
// t.Parallel()
//
Expand Down
2 changes: 1 addition & 1 deletion accounts/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func storeNewKey(store *keyStore, secret string) (*key, Account, error) {
}

type keyStore struct {
baseDir string
baseDir string // absolute filepath to default/flagged value for eg datadir/mainnet/keystore
scryptN int
scryptP int
}
Expand Down
13 changes: 7 additions & 6 deletions accounts/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import (
"sync"
"time"

"encoding/json"
"github.com/ethereumproject/go-ethereum/common"
"github.com/ethereumproject/go-ethereum/crypto"
"path/filepath"
"encoding/json"
)

var (
Expand All @@ -46,8 +46,8 @@ var (
// Account represents a stored key.
// When used as an argument, it selects a unique key file to act on.
type Account struct {
Address common.Address // Ethereum account address derived from the key
EncryptedKey string // web3JSON format
Address common.Address // Ethereum account address derived from the key
EncryptedKey string // web3JSON format

// File contains the key file name.
// When Acccount is used as an argument to select a key, File can be left blank to
Expand All @@ -59,9 +59,9 @@ type Account struct {
// AccountJSON is an auxiliary between Account and EasyMarshal'd structs.
//easyjson:json
type AccountJSON struct {
Address string `json:"address"`
Address string `json:"address"`
EncryptedKey string `json:"key"`
File string `json:"file"`
File string `json:"file"`
}

func (acc *Account) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -99,6 +99,7 @@ const (
)

// NewManager creates a manager for the given directory.
// keydir is by default /Users/ia/Library/EthereumClassic/mainnet/keystore
func NewManager(keydir string, scryptN, scryptP int, wantCacheDB bool) (*Manager, error) {
store, err := newKeyStore(keydir, scryptN, scryptP)
if err != nil {
Expand Down Expand Up @@ -133,7 +134,7 @@ func NewManager(keydir string, scryptN, scryptP int, wantCacheDB bool) (*Manager
}

func (am *Manager) BuildIndexDB() []error {
return am.ac.Syncfs2db(time.Now().Add(-60*24*7*30*120*time.Minute)) // arbitrarily long "last updated"
return am.ac.Syncfs2db(time.Now().Add(-60 * 24 * 7 * 30 * 120 * time.Minute)) // arbitrarily long "last updated"
}

// HasAddress reports whether a key with the given address is present.
Expand Down
Binary file removed accounts/testdata/keystore/accounts.db
Binary file not shown.
Binary file removed accounts/testdata/keystore/keystore/accounts.db
Binary file not shown.
14 changes: 0 additions & 14 deletions accounts/testdata/make200

This file was deleted.

11 changes: 11 additions & 0 deletions cmd/geth/account.bats
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ teardown() {
[[ "$output" == *"Unlocked account f466859ead1932d743d622cb74fc058882e8648a"* ]]
}

@test "account unlock by index" {
cp -R $BATS_TEST_DIRNAME/../../accounts/testdata/keystore $DATA_DIR/mainnet
touch $DATA_DIR/empty.js

run $GETH_CMD --datadir $DATA_DIR --nat none --nodiscover --dev --unlock 0 js $DATA_DIR/empty.js <<< $'foobar\n'
echo "$output"

[ "$status" -eq 0 ]
[[ "$output" == *"Unlocked account 7ef5a6135f1fd6a02593eedc869c6d41d934aef8"* ]]
}

@test "account unlock pass mismatch" {
cp -R $BATS_TEST_DIRNAME/../../accounts/testdata/keystore $DATA_DIR/mainnet
touch $DATA_DIR/empty.js
Expand Down
20 changes: 17 additions & 3 deletions cmd/geth/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,15 @@ func mustMakeDataDir(ctx *cli.Context) string {
// A subdir of the datadir is used for each chain configuration ("/mainnet", "/testnet", "/my-custom-net").
// --> <home>/<EthereumClassic>/<mainnet|testnet|custom-net>, per --chain
func MustMakeChainDataDir(ctx *cli.Context) string {
return common.EnsureAbsolutePath(mustMakeDataDir(ctx), getChainConfigIDFromContext(ctx))
rp := common.EnsurePathAbsoluteOrRelativeTo(mustMakeDataDir(ctx), getChainConfigIDFromContext(ctx))
if !filepath.IsAbs(rp) {
af, e := filepath.Abs(rp)
if e != nil {
glog.Fatalf("cannot make absolute path for chain data dir: %v: %v", rp, e)
}
rp = af
}
return rp
}

// MakeIPCPath creates an IPC path configuration from the set command line flags,
Expand Down Expand Up @@ -301,8 +309,14 @@ func MakeAccountManager(ctx *cli.Context) *accounts.Manager {
datadir := MustMakeChainDataDir(ctx)

keydir := filepath.Join(datadir, "keystore")

if path := ctx.GlobalString(aliasableName(KeyStoreDirFlag.Name, ctx)); path != "" {
keydir = path
af, e := filepath.Abs(path)
if e != nil {
glog.V(logger.Error).Infof("keydir path could not be made absolute: %v: %v", path, e)
} else {
keydir = af
}
}

m, err := accounts.NewManager(keydir, scryptN, scryptP, ctx.GlobalBool(aliasableName(AccountsIndexFlag.Name, ctx)))
Expand Down Expand Up @@ -912,7 +926,7 @@ func MakeConsolePreloads(ctx *cli.Context) []string {

assets := ctx.GlobalString(aliasableName(JSpathFlag.Name, ctx))
for _, file := range strings.Split(ctx.GlobalString(aliasableName(PreloadJSFlag.Name, ctx)), ",") {
preloads = append(preloads, common.EnsureAbsolutePath(assets, strings.TrimSpace(file)))
preloads = append(preloads, common.EnsurePathAbsoluteOrRelativeTo(assets, strings.TrimSpace(file)))
}
return preloads
}
2 changes: 1 addition & 1 deletion cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func dumpChainConfig(ctx *cli.Context) error {

// pretty printy
cwd, _ := os.Getwd()
glog.V(logger.Info).Info(fmt.Sprintf("Dumping chain configuration JSON to \x1b[32m%s\x1b[39m, it may take a moment to tally genesis allocations...", common.EnsureAbsolutePath(cwd, chainConfigFilePath)))
glog.V(logger.Info).Info(fmt.Sprintf("Dumping chain configuration JSON to \x1b[32m%s\x1b[39m, it may take a moment to tally genesis allocations...", common.EnsurePathAbsoluteOrRelativeTo(cwd, chainConfigFilePath)))

db := MakeChainDatabase(ctx)

Expand Down
2 changes: 1 addition & 1 deletion common/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"path/filepath"
)

func EnsureAbsolutePath(Datadir string, filename string) string {
func EnsurePathAbsoluteOrRelativeTo(Datadir string, filename string) string {
if filepath.IsAbs(filename) {
return filename
}
Expand Down
4 changes: 2 additions & 2 deletions internal/jsre/jsre.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (self *JSRE) Stop(waitForCallbacks bool) {
// Exec(file) loads and runs the contents of a file
// if a relative path is given, the jsre's assetPath is used
func (self *JSRE) Exec(file string) error {
code, err := ioutil.ReadFile(common.EnsureAbsolutePath(self.assetPath, file))
code, err := ioutil.ReadFile(common.EnsurePathAbsoluteOrRelativeTo(self.assetPath, file))
if err != nil {
return err
}
Expand Down Expand Up @@ -282,7 +282,7 @@ func (self *JSRE) loadScript(call otto.FunctionCall) otto.Value {
// TODO: throw exception
return otto.FalseValue()
}
file = common.EnsureAbsolutePath(self.assetPath, file)
file = common.EnsurePathAbsoluteOrRelativeTo(self.assetPath, file)
source, err := ioutil.ReadFile(file)
if err != nil {
// TODO: throw exception
Expand Down
2 changes: 1 addition & 1 deletion logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func openLogFile(datadir string, filename string) *os.File {
path := common.EnsureAbsolutePath(datadir, filename)
path := common.EnsurePathAbsoluteOrRelativeTo(datadir, filename)
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(fmt.Sprintf("error opening log file '%s': %v", filename, err))
Expand Down

0 comments on commit ba03cb3

Please sign in to comment.