diff options
Diffstat (limited to 'ethchain/contract.go')
-rw-r--r-- | ethchain/contract.go | 53 |
1 files changed, 29 insertions, 24 deletions
diff --git a/ethchain/contract.go b/ethchain/contract.go index 70189593b..dbcbb3697 100644 --- a/ethchain/contract.go +++ b/ethchain/contract.go @@ -30,37 +30,42 @@ func (c *Contract) RlpDecode(data []byte) { c.state = ethutil.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface()) } -func (c *Contract) State() *ethutil.Trie { - return c.state -} - -type Address struct { - Amount *big.Int - Nonce uint64 +func (c *Contract) Addr(addr []byte) *ethutil.Value { + return ethutil.NewValueFromBytes([]byte(c.state.Get(string(addr)))) } -func NewAddress(amount *big.Int) *Address { - return &Address{Amount: amount, Nonce: 0} +func (c *Contract) SetAddr(addr []byte, value interface{}) { + c.state.Update(string(addr), string(ethutil.NewValue(value).Encode())) } -func NewAddressFromData(data []byte) *Address { - address := &Address{} - address.RlpDecode(data) - - return address +func (c *Contract) State() *ethutil.Trie { + return c.state } -func (a *Address) AddFee(fee *big.Int) { - a.Amount.Add(a.Amount, fee) -} +func (c *Contract) GetMem(num int) *ethutil.Value { + nb := ethutil.BigToBytes(big.NewInt(int64(num)), 256) -func (a *Address) RlpEncode() []byte { - return ethutil.Encode([]interface{}{a.Amount, a.Nonce}) + return c.Addr(nb) } -func (a *Address) RlpDecode(data []byte) { - decoder := ethutil.NewValueFromBytes(data) - - a.Amount = decoder.Get(0).BigInt() - a.Nonce = decoder.Get(1).Uint() +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) + contract.state.Update(string(bytNum), string(ethutil.Encode(val))) + } + } + state.trie.Update(string(addr), string(contract.RlpEncode())) + + return contract + } + + return nil } |