op-geth/ethchain/address.go

77 lines
1.5 KiB
Go
Raw Normal View History

package ethchain
import (
"github.com/ethereum/eth-go/ethutil"
"math/big"
)
2014-03-03 10:05:12 +00:00
type Account struct {
address []byte
Amount *big.Int
Nonce uint64
}
func NewAccount(address []byte, amount *big.Int) *Account {
return &Account{address, amount, 0}
}
func NewAccountFromData(address, data []byte) *Account {
account := &Account{address: address}
account.RlpDecode(data)
return account
}
2014-03-03 10:05:12 +00:00
func (a *Account) AddFee(fee *big.Int) {
2014-03-20 16:24:02 +00:00
a.AddFunds(fee)
}
func (a *Account) AddFunds(funds *big.Int) {
a.Amount.Add(a.Amount, funds)
}
func (a *Account) Address() []byte {
return a.address
}
// Implements Callee
func (a *Account) ReturnGas(value *big.Int, state *State) {
// Return the value back to the sender
a.AddFunds(value)
state.UpdateAccount(a.address, a)
}
2014-03-03 10:05:12 +00:00
func (a *Account) RlpEncode() []byte {
return ethutil.Encode([]interface{}{a.Amount, a.Nonce})
}
2014-03-03 10:05:12 +00:00
func (a *Account) RlpDecode(data []byte) {
decoder := ethutil.NewValueFromBytes(data)
a.Amount = decoder.Get(0).BigInt()
a.Nonce = decoder.Get(1).Uint()
}
type AddrStateStore struct {
2014-03-03 10:05:12 +00:00
states map[string]*AccountState
}
func NewAddrStateStore() *AddrStateStore {
2014-03-03 10:05:12 +00:00
return &AddrStateStore{states: make(map[string]*AccountState)}
}
2014-03-03 10:05:12 +00:00
func (s *AddrStateStore) Add(addr []byte, account *Account) *AccountState {
state := &AccountState{Nonce: account.Nonce, Account: account}
s.states[string(addr)] = state
return state
}
2014-03-03 10:05:12 +00:00
func (s *AddrStateStore) Get(addr []byte) *AccountState {
return s.states[string(addr)]
}
2014-03-03 10:05:12 +00:00
type AccountState struct {
Nonce uint64
2014-03-03 10:05:12 +00:00
Account *Account
}