瀏覽代碼

accounts: use pointers consistently

Account is now always a non-pointer. This will be important once
the manager starts remembering accounts.

AccountManager is now always a pointer because it contains locks
and locks cannot be copied.
Felix Lange 10 年之前
父節點
當前提交
fda7b4c79d
共有 3 個文件被更改,包括 14 次插入17 次删除
  1. 11 15
      accounts/account_manager.go
  2. 2 1
      accounts/accounts_test.go
  3. 1 1
      xeth/xeth.go

+ 11 - 15
accounts/account_manager.go

@@ -47,7 +47,6 @@ var (
 	ErrNoKeys = errors.New("no keys in store")
 )
 
-// TODO: better name for this struct?
 type Account struct {
 	Address []byte
 }
@@ -74,10 +73,10 @@ func (am *AccountManager) Coinbase() (addr []byte, err error) {
 }
 
 // MainAccount returns the primary account used for transactions.
-func (am *AccountManager) Default() (*Account, error) {
+func (am *AccountManager) Default() (Account, error) {
 	// TODO: persist main account address on disk
 	addr, err := am.firstAddr()
-	return &Account{Address: addr}, err
+	return Account{Address: addr}, err
 }
 
 func (am *AccountManager) firstAddr() ([]byte, error) {
@@ -95,9 +94,9 @@ func (am *AccountManager) DeleteAccount(address []byte, auth string) error {
 	return am.keyStore.DeleteKey(address, auth)
 }
 
-func (am *AccountManager) Sign(fromAccount *Account, toSign []byte) (signature []byte, err error) {
+func (am *AccountManager) Sign(a Account, toSign []byte) (signature []byte, err error) {
 	am.mutex.RLock()
-	unlockedKey := am.unlockedKeys[string(fromAccount.Address)]
+	unlockedKey := am.unlockedKeys[string(a.Address)]
 	am.mutex.RUnlock()
 	if unlockedKey.Address == nil {
 		return nil, ErrLocked
@@ -106,28 +105,25 @@ func (am *AccountManager) Sign(fromAccount *Account, toSign []byte) (signature [
 	return signature, err
 }
 
-func (am *AccountManager) SignLocked(fromAccount *Account, keyAuth string, toSign []byte) (signature []byte, err error) {
-	key, err := am.keyStore.GetKey(fromAccount.Address, keyAuth)
+func (am *AccountManager) SignLocked(a Account, keyAuth string, toSign []byte) (signature []byte, err error) {
+	key, err := am.keyStore.GetKey(a.Address, keyAuth)
 	if err != nil {
 		return nil, err
 	}
 	am.mutex.RLock()
-	am.unlockedKeys[string(fromAccount.Address)] = *key
+	am.unlockedKeys[string(a.Address)] = *key
 	am.mutex.RUnlock()
-	go unlockLater(am, fromAccount.Address)
+	go unlockLater(am, a.Address)
 	signature, err = crypto.Sign(toSign, key.PrivateKey)
 	return signature, err
 }
 
-func (am AccountManager) NewAccount(auth string) (*Account, error) {
+func (am *AccountManager) NewAccount(auth string) (Account, error) {
 	key, err := am.keyStore.GenerateNewKey(crand.Reader, auth)
 	if err != nil {
-		return nil, err
-	}
-	ua := &Account{
-		Address: key.Address,
+		return Account{}, err
 	}
-	return ua, err
+	return Account{Address: key.Address}, nil
 }
 
 func (am *AccountManager) Accounts() ([]Account, error) {

+ 2 - 1
accounts/accounts_test.go

@@ -3,10 +3,11 @@ package accounts
 import (
 	"testing"
 
+	"time"
+
 	"github.com/ethereum/go-ethereum/crypto"
 	"github.com/ethereum/go-ethereum/crypto/randentropy"
 	"github.com/ethereum/go-ethereum/ethutil"
-	"time"
 )
 
 func TestAccountManager(t *testing.T) {

+ 1 - 1
xeth/xeth.go

@@ -311,7 +311,7 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt
 	nonce := state.GetNonce(from)
 
 	tx.SetNonce(nonce)
-	sig, err := self.accountManager.Sign(&accounts.Account{Address: from}, tx.Hash())
+	sig, err := self.accountManager.Sign(accounts.Account{Address: from}, tx.Hash())
 	if err != nil {
 		return "", err
 	}