contract.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package ethchain
  2. import (
  3. "github.com/ethereum/eth-go/ethutil"
  4. "math/big"
  5. )
  6. type Contract struct {
  7. Amount *big.Int
  8. Nonce uint64
  9. state *ethutil.Trie
  10. }
  11. func NewContract(Amount *big.Int, root []byte) *Contract {
  12. contract := &Contract{Amount: Amount, Nonce: 0}
  13. contract.state = ethutil.NewTrie(ethutil.Config.Db, string(root))
  14. return contract
  15. }
  16. func (c *Contract) RlpEncode() []byte {
  17. return ethutil.Encode([]interface{}{c.Amount, c.Nonce, c.state.Root})
  18. }
  19. func (c *Contract) RlpDecode(data []byte) {
  20. decoder := ethutil.NewValueFromBytes(data)
  21. c.Amount = decoder.Get(0).BigInt()
  22. c.Nonce = decoder.Get(1).Uint()
  23. c.state = ethutil.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface())
  24. }
  25. func (c *Contract) State() *ethutil.Trie {
  26. return c.state
  27. }
  28. type Address struct {
  29. Amount *big.Int
  30. Nonce uint64
  31. }
  32. func NewAddress(amount *big.Int) *Address {
  33. return &Address{Amount: amount, Nonce: 0}
  34. }
  35. func NewAddressFromData(data []byte) *Address {
  36. address := &Address{}
  37. address.RlpDecode(data)
  38. return address
  39. }
  40. func (a *Address) AddFee(fee *big.Int) {
  41. a.Amount.Add(a.Amount, fee)
  42. }
  43. func (a *Address) RlpEncode() []byte {
  44. return ethutil.Encode([]interface{}{a.Amount, a.Nonce})
  45. }
  46. func (a *Address) RlpDecode(data []byte) {
  47. decoder := ethutil.NewValueFromBytes(data)
  48. a.Amount = decoder.Get(0).BigInt()
  49. a.Nonce = decoder.Get(1).Uint()
  50. }