bsc/contract.go

82 lines
1.6 KiB
Go
Raw Normal View History

2014-01-03 01:43:49 +02:00
package main
import (
2014-01-04 01:32:01 +02:00
_"fmt"
2014-01-03 01:43:49 +02:00
)
type Contract struct {
2014-01-04 01:32:01 +02:00
t uint32 // contract is always 1
amount uint64 // ???
2014-01-03 01:43:49 +02:00
state *Trie
}
2014-01-04 01:32:01 +02:00
func NewContract(amount uint64, root []byte) *Contract {
contract := &Contract{t: 1, amount: amount}
2014-01-03 01:43:49 +02:00
contract.state = NewTrie(Db, string(root))
return contract
}
2014-01-04 01:32:01 +02:00
func (c *Contract) MarshalRlp() []byte {
return Encode([]interface{}{c.t, c.amount, c.state.root})
2014-01-03 01:43:49 +02:00
}
func (c *Contract) UnmarshalRlp(data []byte) {
t, _ := Decode(data, 0)
if slice, ok := t.([]interface{}); ok {
2014-01-04 01:32:01 +02:00
if t, ok := slice[0].(uint8); ok {
c.t = uint32(t)
2014-01-03 01:43:49 +02:00
}
if amount, ok := slice[1].(uint8); ok {
2014-01-04 01:32:01 +02:00
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
2014-01-03 01:43:49 +02:00
}
if root, ok := slice[2].([]uint8); ok {
c.state = NewTrie(Db, string(root))
}
}
}
2014-01-04 01:32:01 +02:00
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)
}
}
}