aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain/contract.go
blob: 68ec39f0b4276bd3ba0ff6bbfd96582b6d7e8a55 (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
package ethchain

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

type Contract struct {
    Amount *big.Int
    Nonce  uint64
    state  *ethutil.Trie
}

func NewContract(Amount *big.Int, root []byte) *Contract {
    contract := &Contract{Amount: Amount, Nonce: 0}
    contract.state = ethutil.NewTrie(ethutil.Config.Db, string(root))

    return contract
}

func (c *Contract) RlpEncode() []byte {
    return ethutil.Encode([]interface{}{c.Amount, c.Nonce, c.state.Root})
}

func (c *Contract) RlpDecode(data []byte) {
    decoder := ethutil.NewValueFromBytes(data)

    c.Amount = decoder.Get(0).BigInt()
    c.Nonce = decoder.Get(1).Uint()
    c.state = ethutil.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface())
}

func (c *Contract) Addr(addr []byte) *ethutil.Value {
    return ethutil.NewValueFromBytes([]byte(c.state.Get(string(addr))))
}

func (c *Contract) SetAddr(addr []byte, value interface{}) {
    c.state.Update(string(addr), string(ethutil.NewValue(value).Encode()))
}

func (c *Contract) State() *ethutil.Trie {
    return c.state
}