aboutsummaryrefslogtreecommitdiffstats
path: root/contract.go
blob: 778f3578d4bbcccb5ed0ebc08090f23df1f1f395 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main

import (
  _"fmt"
)

type Contract struct {
  t uint32 // contract is always 1
  amount uint64 // ???
  state  *Trie
}

func NewContract(amount uint64, root []byte) *Contract {
  contract := &Contract{t: 1, amount: amount}
  contract.state = NewTrie(Db, string(root))

  return  contract
}

func (c *Contract) MarshalRlp() []byte {
  return Encode([]interface{}{c.t, c.amount, c.state.root})
}

func (c *Contract) UnmarshalRlp(data []byte) {
  t, _ := Decode(data, 0)

  if slice, ok := t.([]interface{}); ok {
    if t, ok := slice[0].(uint8); ok {
      c.t = uint32(t)
    }

    if amount, ok := slice[1].(uint8); ok {
      c.amount = uint64(amount)
    } else if amount, ok := slice[1].(uint16); ok {
      c.amount = uint64(amount)
    } else if amount, ok := slice[1].(uint32); ok {
      c.amount = uint64(amount)
    } else if amount, ok := slice[1].(uint64); ok {
      c.amount = amount
    }

    if root, ok := slice[2].([]uint8); ok {
      c.state = NewTrie(Db, string(root))
    }
  }
}

type Ether struct {
  t uint32
  amount uint64
  nonce string
}

func NewEtherFromData(data []byte) *Ether {
  ether := &Ether{}
  ether.UnmarshalRlp(data)

  return ether
}

func (e *Ether) MarshalRlp() []byte {
  return Encode([]interface{}{e.t, e.amount, e.nonce})
}

func (e *Ether) UnmarshalRlp(data []byte) {
  t, _ := Decode(data, 0)

  if slice, ok := t.([]interface{}); ok {
    if t, ok := slice[0].(uint8); ok {
      e.t = uint32(t)
    }

    if amount, ok := slice[1].(uint8); ok {
      e.amount = uint64(amount)
    }

    if nonce, ok := slice[2].([]uint8); ok {
      e.nonce = string(nonce)
    }
  }
}