aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain/address.go
blob: f1f27a1a5ae2ff4591e3ee293af9b7e71ff65f8a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package ethchain

import (
    "github.com/ethereum/eth-go/ethutil"
    "math/big"
)

type Account struct {
    Amount *big.Int
    Nonce  uint64
}

func NewAccount(amount *big.Int) *Account {
    return &Account{Amount: amount, Nonce: 0}
}

func NewAccountFromData(data []byte) *Account {
    address := &Account{}
    address.RlpDecode(data)

    return address
}

func (a *Account) AddFee(fee *big.Int) {
    a.AddFunds(fee)
}

func (a *Account) AddFunds(funds *big.Int) {
    a.Amount.Add(a.Amount, funds)
}

func (a *Account) RlpEncode() []byte {
    return ethutil.Encode([]interface{}{a.Amount, a.Nonce})
}

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 {
    states map[string]*AccountState
}

func NewAddrStateStore() *AddrStateStore {
    return &AddrStateStore{states: make(map[string]*AccountState)}
}

func (s *AddrStateStore) Add(addr []byte, account *Account) *AccountState {
    state := &AccountState{Nonce: account.Nonce, Account: account}
    s.states[string(addr)] = state
    return state
}

func (s *AddrStateStore) Get(addr []byte) *AccountState {
    return s.states[string(addr)]
}

type AccountState struct {
    Nonce   uint64
    Account *Account
}