Renamed Address to Account

This commit is contained in:
obscuren 2014-03-03 11:05:12 +01:00
parent bfed1c7cac
commit 9d492b0509
4 changed files with 23 additions and 23 deletions

View File

@ -5,31 +5,31 @@ import (
"math/big" "math/big"
) )
type Address struct { type Account struct {
Amount *big.Int Amount *big.Int
Nonce uint64 Nonce uint64
} }
func NewAddress(amount *big.Int) *Address { func NewAccount(amount *big.Int) *Account {
return &Address{Amount: amount, Nonce: 0} return &Account{Amount: amount, Nonce: 0}
} }
func NewAddressFromData(data []byte) *Address { func NewAccountFromData(data []byte) *Account {
address := &Address{} address := &Account{}
address.RlpDecode(data) address.RlpDecode(data)
return address return address
} }
func (a *Address) AddFee(fee *big.Int) { func (a *Account) AddFee(fee *big.Int) {
a.Amount.Add(a.Amount, fee) a.Amount.Add(a.Amount, fee)
} }
func (a *Address) RlpEncode() []byte { func (a *Account) RlpEncode() []byte {
return ethutil.Encode([]interface{}{a.Amount, a.Nonce}) return ethutil.Encode([]interface{}{a.Amount, a.Nonce})
} }
func (a *Address) RlpDecode(data []byte) { func (a *Account) RlpDecode(data []byte) {
decoder := ethutil.NewValueFromBytes(data) decoder := ethutil.NewValueFromBytes(data)
a.Amount = decoder.Get(0).BigInt() a.Amount = decoder.Get(0).BigInt()
@ -37,24 +37,24 @@ func (a *Address) RlpDecode(data []byte) {
} }
type AddrStateStore struct { type AddrStateStore struct {
states map[string]*AddressState states map[string]*AccountState
} }
func NewAddrStateStore() *AddrStateStore { func NewAddrStateStore() *AddrStateStore {
return &AddrStateStore{states: make(map[string]*AddressState)} return &AddrStateStore{states: make(map[string]*AccountState)}
} }
func (s *AddrStateStore) Add(addr []byte, account *Address) *AddressState { func (s *AddrStateStore) Add(addr []byte, account *Account) *AccountState {
state := &AddressState{Nonce: account.Nonce, Account: account} state := &AccountState{Nonce: account.Nonce, Account: account}
s.states[string(addr)] = state s.states[string(addr)] = state
return state return state
} }
func (s *AddrStateStore) Get(addr []byte) *AddressState { func (s *AddrStateStore) Get(addr []byte) *AccountState {
return s.states[string(addr)] return s.states[string(addr)]
} }
type AddressState struct { type AccountState struct {
Nonce uint64 Nonce uint64
Account *Address Account *Account
} }

View File

@ -142,7 +142,7 @@ func (block *Block) PayFee(addr []byte, fee *big.Int) bool {
data := block.state.trie.Get(string(block.Coinbase)) data := block.state.trie.Get(string(block.Coinbase))
// Get the ether (Coinbase) and add the fee (gief fee to miner) // Get the ether (Coinbase) and add the fee (gief fee to miner)
ether := NewAddressFromData([]byte(data)) ether := NewAccountFromData([]byte(data))
base = new(big.Int) base = new(big.Int)
ether.Amount = base.Add(ether.Amount, fee) ether.Amount = base.Add(ether.Amount, fee)

View File

@ -85,17 +85,17 @@ func NewBlockManager(speaker PublicSpeaker) *BlockManager {
} }
// Watches any given address and puts it in the address state store // Watches any given address and puts it in the address state store
func (bm *BlockManager) WatchAddr(addr []byte) *AddressState { func (bm *BlockManager) WatchAddr(addr []byte) *AccountState {
account := bm.bc.CurrentBlock.state.GetAccount(addr) account := bm.bc.CurrentBlock.state.GetAccount(addr)
return bm.addrStateStore.Add(addr, account) return bm.addrStateStore.Add(addr, account)
} }
func (bm *BlockManager) GetAddrState(addr []byte) *AddressState { func (bm *BlockManager) GetAddrState(addr []byte) *AccountState {
account := bm.addrStateStore.Get(addr) account := bm.addrStateStore.Get(addr)
if account == nil { if account == nil {
a := bm.bc.CurrentBlock.state.GetAccount(addr) a := bm.bc.CurrentBlock.state.GetAccount(addr)
account = &AddressState{Nonce: a.Nonce, Account: a} account = &AccountState{Nonce: a.Nonce, Account: a}
} }
return account return account

View File

@ -93,18 +93,18 @@ func Compile(code []string) (script []string) {
return return
} }
func (s *State) GetAccount(addr []byte) (account *Address) { func (s *State) GetAccount(addr []byte) (account *Account) {
data := s.trie.Get(string(addr)) data := s.trie.Get(string(addr))
if data == "" { if data == "" {
account = NewAddress(big.NewInt(0)) account = NewAccount(big.NewInt(0))
} else { } else {
account = NewAddressFromData([]byte(data)) account = NewAccountFromData([]byte(data))
} }
return return
} }
func (s *State) UpdateAccount(addr []byte, account *Address) { func (s *State) UpdateAccount(addr []byte, account *Account) {
s.trie.Update(string(addr), string(account.RlpEncode())) s.trie.Update(string(addr), string(account.RlpEncode()))
} }