aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain/state_object.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-04-26 07:47:55 +0800
committerobscuren <geffobscura@gmail.com>2014-04-26 07:48:40 +0800
commit0f93da400ab7fd238eb7286f14c229d780f73636 (patch)
tree32645ca22a9f4ac216cdebb7026f13d2bf3a434f /ethchain/state_object.go
parentf3818478e2601df1d9cfc9cc36b021366f870856 (diff)
downloadgo-tangerine-0f93da400ab7fd238eb7286f14c229d780f73636.tar.gz
go-tangerine-0f93da400ab7fd238eb7286f14c229d780f73636.tar.zst
go-tangerine-0f93da400ab7fd238eb7286f14c229d780f73636.zip
Added new state object change echanism
Diffstat (limited to 'ethchain/state_object.go')
-rw-r--r--ethchain/state_object.go67
1 files changed, 36 insertions, 31 deletions
diff --git a/ethchain/state_object.go b/ethchain/state_object.go
index f562e5b04..8d86ef44e 100644
--- a/ethchain/state_object.go
+++ b/ethchain/state_object.go
@@ -18,6 +18,28 @@ type StateObject struct {
initScript []byte
}
+// Converts an transaction in to a state object
+func MakeContract(tx *Transaction, state *State) *StateObject {
+ // Create contract if there's no recipient
+ if tx.IsContract() {
+ // FIXME
+ addr := tx.Hash()[12:]
+
+ value := tx.Value
+ contract := NewContract(addr, value, []byte(""))
+ state.UpdateStateObject(contract)
+
+ contract.script = tx.Data
+ contract.initScript = tx.Init
+
+ state.UpdateStateObject(contract)
+
+ return contract
+ }
+
+ return nil
+}
+
func NewContract(address []byte, Amount *big.Int, root []byte) *StateObject {
contract := &StateObject{address: address, Amount: Amount, Nonce: 0}
contract.state = NewState(ethutil.NewTrie(ethutil.Config.Db, string(root)))
@@ -39,6 +61,10 @@ func NewStateObjectFromBytes(address, data []byte) *StateObject {
return object
}
+func (c *StateObject) State() *State {
+ return c.state
+}
+
func (c *StateObject) Addr(addr []byte) *ethutil.Value {
return ethutil.NewValueFromBytes([]byte(c.state.trie.Get(string(addr))))
}
@@ -47,8 +73,10 @@ func (c *StateObject) SetAddr(addr []byte, value interface{}) {
c.state.trie.Update(string(addr), string(ethutil.NewValue(value).Encode()))
}
-func (c *StateObject) State() *State {
- return c.state
+func (c *StateObject) SetMem(num *big.Int, val *ethutil.Value) {
+ addr := ethutil.BigToBytes(num, 256)
+ c.SetAddr(addr, val)
+ //c.state.trie.Update(string(addr), string(val.Encode()))
}
func (c *StateObject) GetMem(num *big.Int) *ethutil.Value {
@@ -65,11 +93,6 @@ func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value {
return ethutil.NewValueFromBytes([]byte{c.script[pc.Int64()]})
}
-func (c *StateObject) SetMem(num *big.Int, val *ethutil.Value) {
- addr := ethutil.BigToBytes(num, 256)
- c.state.trie.Update(string(addr), string(val.Encode()))
-}
-
// Return the gas back to the origin. Used by the Virtual machine or Closures
func (c *StateObject) ReturnGas(gas, price *big.Int, state *State) {
remainder := new(big.Int).Mul(gas, price)
@@ -77,11 +100,15 @@ func (c *StateObject) ReturnGas(gas, price *big.Int, state *State) {
}
func (c *StateObject) AddAmount(amount *big.Int) {
- c.Amount.Add(c.Amount, amount)
+ c.SetAmount(new(big.Int).Add(c.Amount, amount))
}
func (c *StateObject) SubAmount(amount *big.Int) {
- c.Amount.Sub(c.Amount, amount)
+ c.SetAmount(new(big.Int).Sub(c.Amount, amount))
+}
+
+func (c *StateObject) SetAmount(amount *big.Int) {
+ c.Amount = amount
}
func (c *StateObject) ConvertGas(gas, price *big.Int) error {
@@ -130,28 +157,6 @@ func (c *StateObject) RlpDecode(data []byte) {
c.script = decoder.Get(3).Bytes()
}
-// Converts an transaction in to a state object
-func MakeContract(tx *Transaction, state *State) *StateObject {
- // Create contract if there's no recipient
- if tx.IsContract() {
- // FIXME
- addr := tx.Hash()[12:]
-
- value := tx.Value
- contract := NewContract(addr, value, []byte(""))
- state.UpdateStateObject(contract)
-
- contract.script = tx.Data
- contract.initScript = tx.Init
-
- state.UpdateStateObject(contract)
-
- return contract
- }
-
- return nil
-}
-
// The cached state and state object cache are helpers which will give you somewhat
// control over the nonce. When creating new transactions you're interested in the 'next'
// nonce rather than the current nonce. This to avoid creating invalid-nonce transactions.