2014-02-22 18:56:48 -06:00
|
|
|
package ethchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
|
|
|
"math/big"
|
|
|
|
)
|
|
|
|
|
2014-03-03 04:05:12 -06:00
|
|
|
type Account struct {
|
2014-02-22 18:56:48 -06:00
|
|
|
Amount *big.Int
|
|
|
|
Nonce uint64
|
|
|
|
}
|
|
|
|
|
2014-03-03 04:05:12 -06:00
|
|
|
func NewAccount(amount *big.Int) *Account {
|
|
|
|
return &Account{Amount: amount, Nonce: 0}
|
2014-02-22 18:56:48 -06:00
|
|
|
}
|
|
|
|
|
2014-03-03 04:05:12 -06:00
|
|
|
func NewAccountFromData(data []byte) *Account {
|
|
|
|
address := &Account{}
|
2014-02-22 18:56:48 -06:00
|
|
|
address.RlpDecode(data)
|
|
|
|
|
|
|
|
return address
|
|
|
|
}
|
|
|
|
|
2014-03-03 04:05:12 -06:00
|
|
|
func (a *Account) AddFee(fee *big.Int) {
|
2014-02-22 18:56:48 -06:00
|
|
|
a.Amount.Add(a.Amount, fee)
|
|
|
|
}
|
|
|
|
|
2014-03-03 04:05:12 -06:00
|
|
|
func (a *Account) RlpEncode() []byte {
|
2014-02-22 18:56:48 -06:00
|
|
|
return ethutil.Encode([]interface{}{a.Amount, a.Nonce})
|
|
|
|
}
|
|
|
|
|
2014-03-03 04:05:12 -06:00
|
|
|
func (a *Account) RlpDecode(data []byte) {
|
2014-02-22 18:56:48 -06:00
|
|
|
decoder := ethutil.NewValueFromBytes(data)
|
|
|
|
|
|
|
|
a.Amount = decoder.Get(0).BigInt()
|
|
|
|
a.Nonce = decoder.Get(1).Uint()
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddrStateStore struct {
|
2014-03-03 04:05:12 -06:00
|
|
|
states map[string]*AccountState
|
2014-02-22 18:56:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAddrStateStore() *AddrStateStore {
|
2014-03-03 04:05:12 -06:00
|
|
|
return &AddrStateStore{states: make(map[string]*AccountState)}
|
2014-02-22 18:56:48 -06:00
|
|
|
}
|
|
|
|
|
2014-03-03 04:05:12 -06:00
|
|
|
func (s *AddrStateStore) Add(addr []byte, account *Account) *AccountState {
|
|
|
|
state := &AccountState{Nonce: account.Nonce, Account: account}
|
2014-02-22 18:56:48 -06:00
|
|
|
s.states[string(addr)] = state
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2014-03-03 04:05:12 -06:00
|
|
|
func (s *AddrStateStore) Get(addr []byte) *AccountState {
|
2014-02-22 18:56:48 -06:00
|
|
|
return s.states[string(addr)]
|
|
|
|
}
|
|
|
|
|
2014-03-03 04:05:12 -06:00
|
|
|
type AccountState struct {
|
2014-02-22 18:56:48 -06:00
|
|
|
Nonce uint64
|
2014-03-03 04:05:12 -06:00
|
|
|
Account *Account
|
2014-02-22 18:56:48 -06:00
|
|
|
}
|