aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain/address.go
blob: a228c7566f0a3a5438f2905a6da35c2fc9f9eec3 (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
package ethchain

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

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

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

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

    return address
}

func (a *Address) AddFee(fee *big.Int) {
    a.Amount.Add(a.Amount, fee)
}

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

func (a *Address) 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]*AddressState
}

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

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

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

type AddressState struct {
    Nonce   uint64
    Account *Address
}