瀏覽代碼

accounts: use time.Duration correctly

There is no point to using time.Duration if the value is interpreted as
milliseconds. Callers should use the standard multiplication idiom to
choose the unit. In fact, the only caller outside of the tests already
does so.
Felix Lange 10 年之前
父節點
當前提交
afc530ea41
共有 2 個文件被更改,包括 14 次插入13 次删除
  1. 10 9
      accounts/account_manager.go
  2. 4 4
      accounts/accounts_test.go

+ 10 - 9
accounts/account_manager.go

@@ -33,6 +33,7 @@ and accounts persistence is derived from stored keys' addresses
 package accounts
 package accounts
 
 
 import (
 import (
+	"crypto/ecdsa"
 	crand "crypto/rand"
 	crand "crypto/rand"
 
 
 	"errors"
 	"errors"
@@ -52,17 +53,17 @@ type Account struct {
 }
 }
 
 
 type AccountManager struct {
 type AccountManager struct {
-	keyStore           crypto.KeyStore2
-	unlockedKeys       map[string]crypto.Key
-	unlockMilliseconds time.Duration
-	mutex              sync.RWMutex
+	keyStore     crypto.KeyStore2
+	unlockedKeys map[string]crypto.Key
+	unlockTime   time.Duration
+	mutex        sync.RWMutex
 }
 }
 
 
-func NewAccountManager(keyStore crypto.KeyStore2, unlockMilliseconds time.Duration) *AccountManager {
+func NewAccountManager(keyStore crypto.KeyStore2, unlockTime time.Duration) *AccountManager {
 	return &AccountManager{
 	return &AccountManager{
-		keyStore:           keyStore,
-		unlockedKeys:       make(map[string]crypto.Key),
-		unlockMilliseconds: unlockMilliseconds,
+		keyStore:     keyStore,
+		unlockedKeys: make(map[string]crypto.Key),
+		unlockTime:   unlockTime,
 	}
 	}
 }
 }
 
 
@@ -144,7 +145,7 @@ func (am *AccountManager) Accounts() ([]Account, error) {
 
 
 func unlockLater(am *AccountManager, addr []byte) {
 func unlockLater(am *AccountManager, addr []byte) {
 	select {
 	select {
-	case <-time.After(time.Millisecond * am.unlockMilliseconds):
+	case <-time.After(am.unlockTime):
 	}
 	}
 	am.mutex.RLock()
 	am.mutex.RLock()
 	// TODO: how do we know the key is actually gone from memory?
 	// TODO: how do we know the key is actually gone from memory?

+ 4 - 4
accounts/accounts_test.go

@@ -12,7 +12,7 @@ import (
 
 
 func TestAccountManager(t *testing.T) {
 func TestAccountManager(t *testing.T) {
 	ks := crypto.NewKeyStorePlain(ethutil.DefaultDataDir() + "/testaccounts")
 	ks := crypto.NewKeyStorePlain(ethutil.DefaultDataDir() + "/testaccounts")
-	am := NewAccountManager(ks, 100)
+	am := NewAccountManager(ks, 100*time.Millisecond)
 	pass := "" // not used but required by API
 	pass := "" // not used but required by API
 	a1, err := am.NewAccount(pass)
 	a1, err := am.NewAccount(pass)
 	toSign := randentropy.GetEntropyCSPRNG(32)
 	toSign := randentropy.GetEntropyCSPRNG(32)
@@ -22,7 +22,7 @@ func TestAccountManager(t *testing.T) {
 	}
 	}
 
 
 	// Cleanup
 	// Cleanup
-	time.Sleep(time.Millisecond * 150) // wait for locking
+	time.Sleep(150 * time.Millisecond) // wait for locking
 
 
 	accounts, err := am.Accounts()
 	accounts, err := am.Accounts()
 	if err != nil {
 	if err != nil {
@@ -38,7 +38,7 @@ func TestAccountManager(t *testing.T) {
 
 
 func TestAccountManagerLocking(t *testing.T) {
 func TestAccountManagerLocking(t *testing.T) {
 	ks := crypto.NewKeyStorePassphrase(ethutil.DefaultDataDir() + "/testaccounts")
 	ks := crypto.NewKeyStorePassphrase(ethutil.DefaultDataDir() + "/testaccounts")
-	am := NewAccountManager(ks, 200)
+	am := NewAccountManager(ks, 200*time.Millisecond)
 	pass := "foo"
 	pass := "foo"
 	a1, err := am.NewAccount(pass)
 	a1, err := am.NewAccount(pass)
 	toSign := randentropy.GetEntropyCSPRNG(32)
 	toSign := randentropy.GetEntropyCSPRNG(32)
@@ -62,7 +62,7 @@ func TestAccountManagerLocking(t *testing.T) {
 	}
 	}
 
 
 	// Signing without passphrase fails after automatic locking
 	// Signing without passphrase fails after automatic locking
-	time.Sleep(time.Millisecond * time.Duration(250))
+	time.Sleep(250 * time.Millisecond)
 
 
 	_, err = am.Sign(a1, toSign)
 	_, err = am.Sign(a1, toSign)
 	if err != ErrLocked {
 	if err != ErrLocked {