2014-02-15 00:56:09 +02:00
|
|
|
package ethchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
|
|
|
"math/big"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Contract struct {
|
|
|
|
Amount *big.Int
|
|
|
|
Nonce uint64
|
2014-03-03 12:03:16 +02:00
|
|
|
//state *ethutil.Trie
|
|
|
|
state *State
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewContract(Amount *big.Int, root []byte) *Contract {
|
|
|
|
contract := &Contract{Amount: Amount, Nonce: 0}
|
2014-03-03 12:03:16 +02:00
|
|
|
contract.state = NewState(ethutil.NewTrie(ethutil.Config.Db, string(root)))
|
2014-02-15 00:56:09 +02:00
|
|
|
|
|
|
|
return contract
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contract) RlpEncode() []byte {
|
2014-03-03 12:03:16 +02:00
|
|
|
return ethutil.Encode([]interface{}{c.Amount, c.Nonce, c.state.trie.Root})
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contract) RlpDecode(data []byte) {
|
2014-02-15 02:34:18 +02:00
|
|
|
decoder := ethutil.NewValueFromBytes(data)
|
2014-02-15 00:56:09 +02:00
|
|
|
|
2014-02-15 02:34:18 +02:00
|
|
|
c.Amount = decoder.Get(0).BigInt()
|
|
|
|
c.Nonce = decoder.Get(1).Uint()
|
2014-03-03 12:03:16 +02:00
|
|
|
c.state = NewState(ethutil.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface()))
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
2014-02-21 00:10:36 +02:00
|
|
|
func (c *Contract) Addr(addr []byte) *ethutil.Value {
|
2014-03-03 12:03:16 +02:00
|
|
|
return ethutil.NewValueFromBytes([]byte(c.state.trie.Get(string(addr))))
|
2014-02-21 00:10:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contract) SetAddr(addr []byte, value interface{}) {
|
2014-03-03 12:03:16 +02:00
|
|
|
c.state.trie.Update(string(addr), string(ethutil.NewValue(value).Encode()))
|
2014-02-21 00:10:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-03 12:03:16 +02:00
|
|
|
func (c *Contract) State() *State {
|
2014-02-15 00:56:09 +02:00
|
|
|
return c.state
|
|
|
|
}
|
2014-02-24 13:12:24 +02:00
|
|
|
|
2014-03-20 18:25:11 +02:00
|
|
|
func (c *Contract) GetMem(num int64) *ethutil.Value {
|
|
|
|
nb := ethutil.BigToBytes(big.NewInt(num), 256)
|
2014-02-24 13:12:24 +02:00
|
|
|
|
|
|
|
return c.Addr(nb)
|
|
|
|
}
|
|
|
|
|
2014-03-20 18:25:11 +02:00
|
|
|
// Return the gas back to the origin. Used by the Virtual machine or Closures
|
|
|
|
func (c *Contract) ReturnGas(val *big.Int, state *State) {
|
|
|
|
c.Amount.Add(c.Amount, val)
|
|
|
|
}
|
|
|
|
|
2014-02-24 13:12:24 +02:00
|
|
|
func MakeContract(tx *Transaction, state *State) *Contract {
|
|
|
|
// Create contract if there's no recipient
|
|
|
|
if tx.IsContract() {
|
|
|
|
addr := tx.Hash()[12:]
|
|
|
|
|
|
|
|
value := tx.Value
|
|
|
|
contract := NewContract(value, []byte(""))
|
|
|
|
state.trie.Update(string(addr), string(contract.RlpEncode()))
|
|
|
|
for i, val := range tx.Data {
|
|
|
|
if len(val) > 0 {
|
|
|
|
bytNum := ethutil.BigToBytes(big.NewInt(int64(i)), 256)
|
2014-03-03 12:03:16 +02:00
|
|
|
contract.state.trie.Update(string(bytNum), string(ethutil.Encode(val)))
|
2014-02-24 13:12:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
state.trie.Update(string(addr), string(contract.RlpEncode()))
|
|
|
|
|
|
|
|
return contract
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|