quorum/ethchain/contract.go

120 lines
2.8 KiB
Go
Raw Normal View History

2014-02-14 14:56:09 -08:00
package ethchain
import (
"github.com/ethereum/eth-go/ethutil"
"math/big"
)
type Contract struct {
Amount *big.Int
Nonce uint64
//state *ethutil.Trie
2014-04-09 09:27:54 -07:00
state *State
address []byte
script []byte
initScript []byte
2014-02-14 14:56:09 -08:00
}
func NewContract(address []byte, Amount *big.Int, root []byte) *Contract {
contract := &Contract{address: address, Amount: Amount, Nonce: 0}
contract.state = NewState(ethutil.NewTrie(ethutil.Config.Db, string(root)))
2014-02-14 14:56:09 -08:00
return contract
}
func NewContractFromBytes(address, data []byte) *Contract {
contract := &Contract{address: address}
contract.RlpDecode(data)
2014-02-14 14:56:09 -08:00
return contract
2014-02-14 14:56:09 -08:00
}
2014-02-20 14:10:36 -08:00
func (c *Contract) Addr(addr []byte) *ethutil.Value {
return ethutil.NewValueFromBytes([]byte(c.state.trie.Get(string(addr))))
2014-02-20 14:10:36 -08:00
}
func (c *Contract) SetAddr(addr []byte, value interface{}) {
c.state.trie.Update(string(addr), string(ethutil.NewValue(value).Encode()))
2014-02-20 14:10:36 -08:00
}
func (c *Contract) State() *State {
2014-02-14 14:56:09 -08:00
return c.state
}
2014-02-24 03:12:24 -08:00
func (c *Contract) GetMem(num *big.Int) *ethutil.Value {
nb := ethutil.BigToBytes(num, 256)
2014-02-24 03:12:24 -08:00
return c.Addr(nb)
}
2014-04-09 05:08:18 -07:00
func (c *Contract) GetInstr(pc *big.Int) *ethutil.Value {
if int64(len(c.script)-1) < pc.Int64() {
return ethutil.NewValue(0)
}
return ethutil.NewValueFromBytes([]byte{c.script[pc.Int64()]})
}
func (c *Contract) SetMem(num *big.Int, val *ethutil.Value) {
addr := ethutil.BigToBytes(num, 256)
c.state.trie.Update(string(addr), string(val.Encode()))
}
2014-03-20 09:25:11 -07: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)
}
func (c *Contract) Address() []byte {
return c.address
}
2014-04-11 10:29:57 -07:00
func (c *Contract) Script() []byte {
return c.script
}
func (c *Contract) Init() []byte {
return c.initScript
}
func (c *Contract) RlpEncode() []byte {
2014-04-10 11:40:12 -07:00
return ethutil.Encode([]interface{}{c.Amount, c.Nonce, c.state.trie.Root, c.script, c.initScript})
}
func (c *Contract) RlpDecode(data []byte) {
decoder := ethutil.NewValueFromBytes(data)
c.Amount = decoder.Get(0).BigInt()
c.Nonce = decoder.Get(1).Uint()
c.state = NewState(ethutil.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface()))
2014-04-10 11:40:12 -07:00
c.script = decoder.Get(3).Bytes()
c.initScript = decoder.Get(4).Bytes()
}
2014-02-24 03:12:24 -08: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(addr, value, []byte(""))
2014-02-24 03:12:24 -08:00
state.trie.Update(string(addr), string(contract.RlpEncode()))
2014-04-09 09:27:54 -07:00
contract.script = tx.Data
contract.initScript = tx.Init
/*
for i, val := range tx.Data {
if len(val) > 0 {
bytNum := ethutil.BigToBytes(big.NewInt(int64(i)), 256)
contract.state.trie.Update(string(bytNum), string(ethutil.Encode(val)))
}
2014-02-24 03:12:24 -08:00
}
2014-04-09 09:27:54 -07:00
*/
2014-02-24 03:12:24 -08:00
state.trie.Update(string(addr), string(contract.RlpEncode()))
return contract
}
return nil
}