aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-05-23 00:24:04 +0800
committerobscuren <geffobscura@gmail.com>2014-05-23 00:24:04 +0800
commit281559d42712c2b2ae903cb893b2ddc82318307d (patch)
treeffc5c980bc283d7ec92bad6643e95d4b41cb6333
parentcc8464ce805279735f637ac710b25e2fb264f9aa (diff)
downloaddexon-281559d42712c2b2ae903cb893b2ddc82318307d.tar
dexon-281559d42712c2b2ae903cb893b2ddc82318307d.tar.gz
dexon-281559d42712c2b2ae903cb893b2ddc82318307d.tar.bz2
dexon-281559d42712c2b2ae903cb893b2ddc82318307d.tar.lz
dexon-281559d42712c2b2ae903cb893b2ddc82318307d.tar.xz
dexon-281559d42712c2b2ae903cb893b2ddc82318307d.tar.zst
dexon-281559d42712c2b2ae903cb893b2ddc82318307d.zip
Canonical contract creation
-rw-r--r--ethchain/state_manager.go22
-rw-r--r--ethchain/state_object.go3
-rw-r--r--ethchain/transaction.go9
-rw-r--r--ethchain/types.go7
-rw-r--r--ethpub/pub.go4
5 files changed, 28 insertions, 17 deletions
diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go
index 3b67381ea..4f009b6d3 100644
--- a/ethchain/state_manager.go
+++ b/ethchain/state_manager.go
@@ -84,7 +84,7 @@ func (sm *StateManager) BlockChain() *BlockChain {
return sm.bc
}
-func (sm *StateManager) MakeContract(state *State, tx *Transaction) *StateObject {
+func (sm *StateManager) MakeStateObject(state *State, tx *Transaction) *StateObject {
contract := MakeContract(tx, state)
if contract != nil {
state.states[string(tx.CreationAddress())] = contract.state
@@ -123,18 +123,24 @@ func (sm *StateManager) ApplyTransaction(state *State, block *Block, tx *Transac
// If there's no recipient, it's a contract
// Check if this is a contract creation traction and if so
// create a contract of this tx.
+ // TODO COMMENT THIS SECTION
totalGasUsed := big.NewInt(0)
if tx.IsContract() {
err := sm.Ethereum.TxPool().ProcessTransaction(tx, state, false)
if err == nil {
- contract := sm.MakeContract(state, tx)
+ contract := sm.MakeStateObject(state, tx)
if contract != nil {
- sm.EvalScript(state, contract.Init(), contract, tx, block)
+ script, err := sm.EvalScript(state, contract.Init(), contract, tx, block)
+ if err != nil {
+ return nil, fmt.Errorf("[STATE] Error during init script run %v", err)
+ }
+ contract.script = script
+ state.UpdateStateObject(contract)
} else {
return nil, fmt.Errorf("[STATE] Unable to create contract")
}
} else {
- return nil, fmt.Errorf("[STATE] contract create:", err)
+ return nil, fmt.Errorf("[STATE] contract creation tx:", err)
}
} else {
err := sm.Ethereum.TxPool().ProcessTransaction(tx, state, false)
@@ -331,10 +337,10 @@ func (sm *StateManager) Stop() {
sm.bc.Stop()
}
-func (sm *StateManager) EvalScript(state *State, script []byte, object *StateObject, tx *Transaction, block *Block) {
+func (sm *StateManager) EvalScript(state *State, script []byte, object *StateObject, tx *Transaction, block *Block) (ret []byte, err error) {
account := state.GetAccount(tx.Sender())
- err := account.ConvertGas(tx.Gas, tx.GasPrice)
+ err = account.ConvertGas(tx.Gas, tx.GasPrice)
if err != nil {
ethutil.Config.Log.Debugln(err)
return
@@ -351,11 +357,13 @@ func (sm *StateManager) EvalScript(state *State, script []byte, object *StateObj
Value: tx.Value,
//Price: tx.GasPrice,
})
- closure.Call(vm, tx.Data, nil)
+ ret, err = closure.Call(vm, tx.Data, nil)
// Update the account (refunds)
state.UpdateStateObject(account)
state.UpdateStateObject(object)
+
+ return
}
func (sm *StateManager) notifyChanges(state *State) {
diff --git a/ethchain/state_object.go b/ethchain/state_object.go
index 4046054db..4d615e2fe 100644
--- a/ethchain/state_object.go
+++ b/ethchain/state_object.go
@@ -28,8 +28,7 @@ func MakeContract(tx *Transaction, state *State) *StateObject {
value := tx.Value
contract := NewContract(addr, value, ZeroHash256)
- contract.script = tx.Data
- contract.initScript = tx.Init
+ contract.initScript = tx.Data
state.UpdateStateObject(contract)
diff --git a/ethchain/transaction.go b/ethchain/transaction.go
index 6ae7e77e1..25d63879b 100644
--- a/ethchain/transaction.go
+++ b/ethchain/transaction.go
@@ -16,7 +16,6 @@ type Transaction struct {
Gas *big.Int
GasPrice *big.Int
Data []byte
- Init []byte
v byte
r, s []byte
@@ -24,8 +23,8 @@ type Transaction struct {
contractCreation bool
}
-func NewContractCreationTx(value, gas, gasPrice *big.Int, script []byte, init []byte) *Transaction {
- return &Transaction{Value: value, Gas: gas, GasPrice: gasPrice, Data: script, Init: init, contractCreation: true}
+func NewContractCreationTx(value, gas, gasPrice *big.Int, script []byte) *Transaction {
+ return &Transaction{Value: value, Gas: gas, GasPrice: gasPrice, Data: script, contractCreation: true}
}
func NewTransactionMessage(to []byte, value, gas, gasPrice *big.Int, data []byte) *Transaction {
@@ -115,10 +114,6 @@ func (tx *Transaction) Sign(privk []byte) error {
func (tx *Transaction) RlpData() interface{} {
data := []interface{}{tx.Nonce, tx.GasPrice, tx.Gas, tx.Recipient, tx.Value, tx.Data}
- if tx.contractCreation {
- data = append(data, tx.Init)
- }
-
return append(data, tx.v, tx.r, tx.s)
}
diff --git a/ethchain/types.go b/ethchain/types.go
index 9964bbe3b..e0fdd5191 100644
--- a/ethchain/types.go
+++ b/ethchain/types.go
@@ -322,3 +322,10 @@ func IsOpCode(s string) bool {
}
return false
}
+
+func AppendScript(init, script []byte) []byte {
+ s := append(init, byte(oRETURN))
+ s = append(s, script...)
+
+ return s
+}
diff --git a/ethpub/pub.go b/ethpub/pub.go
index cd002b500..e726c66f3 100644
--- a/ethpub/pub.go
+++ b/ethpub/pub.go
@@ -141,7 +141,9 @@ func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, in
}
}
- tx = ethchain.NewContractCreationTx(value, gas, gasPrice, mainScript, initScript)
+ script := ethchain.AppendScript(initScript, mainScript)
+
+ tx = ethchain.NewContractCreationTx(value, gas, gasPrice, script)
} else {
// Just in case it was submitted as a 0x prefixed string
if len(initStr) > 0 && initStr[0:2] == "0x" {