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.
This commit is contained in:
Felix Lange 2015-03-08 00:18:13 +01:00
parent a2810c06d7
commit fda7b4c79d
3 changed files with 14 additions and 17 deletions

View File

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

View File

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

View File

@ -311,7 +311,7 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt
nonce := state.GetNonce(from) nonce := state.GetNonce(from)
tx.SetNonce(nonce) 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 { if err != nil {
return "", err return "", err
} }