Updated transaction model

Changed the behaviour of decoding rlp data. Something is considered to
be creating a contract if the 4th item is a list.

Changed constructors.
This commit is contained in:
obscuren 2014-03-27 19:49:47 +01:00
parent 7660e1ed90
commit 00c5f9b9a6
1 changed files with 12 additions and 30 deletions

View File

@ -2,6 +2,7 @@ package ethchain
import (
"bytes"
"fmt"
"github.com/ethereum/eth-go/ethutil"
"github.com/obscuren/secp256k1-go"
"math/big"
@ -23,33 +24,14 @@ type Transaction struct {
contractCreation bool
}
/*
func NewTransaction(to []byte, value *big.Int, data []string) *Transaction {
tx := Transaction{Recipient: to, Value: value, Nonce: 0, Data: data}
return &tx
}
*/
func NewContractCreationTx(value, gasprice *big.Int, data []string) *Transaction {
return &Transaction{Value: value, Gasprice: gasprice, Data: data, contractCreation: true}
}
func NewContractMessageTx(to []byte, value, gasprice, gas *big.Int, data []string) *Transaction {
func NewTransactionMessage(to []byte, value, gasprice, gas *big.Int, data []string) *Transaction {
return &Transaction{Recipient: to, Value: value, Gasprice: gasprice, Gas: gas, Data: data}
}
func NewTx(to []byte, value *big.Int, data []string) *Transaction {
return &Transaction{Recipient: to, Value: value, Gasprice: big.NewInt(0), Gas: big.NewInt(0), Nonce: 0, Data: data}
}
/*
// XXX Deprecated
func NewTransactionFromData(data []byte) *Transaction {
return NewTransactionFromBytes(data)
}
*/
func NewTransactionFromBytes(data []byte) *Transaction {
tx := &Transaction{}
tx.RlpDecode(data)
@ -131,16 +113,13 @@ func (tx *Transaction) Sign(privk []byte) error {
}
func (tx *Transaction) RlpData() interface{} {
// Prepare the transaction for serialization
return []interface{}{
tx.Nonce,
tx.Recipient,
tx.Value,
ethutil.NewSliceValue(tx.Data).Slice(),
tx.v,
tx.r,
tx.s,
data := []interface{}{tx.Nonce, tx.Value, tx.Gasprice}
if !tx.contractCreation {
data = append(data, tx.Recipient, tx.Gas)
}
return append(data, ethutil.NewSliceValue(tx.Data).Slice(), tx.v, tx.r, tx.s)
}
func (tx *Transaction) RlpValue() *ethutil.Value {
@ -156,14 +135,16 @@ func (tx *Transaction) RlpDecode(data []byte) {
}
// [ NONCE, VALUE, GASPRICE, TO, GAS, DATA, V, R, S ]
//["" "\x03\xe8" "" "\xaa" "\x03\xe8" [] '\x1c' "\x10C\x15\xfc\xe5\xd0\t\xe4\r\xe7\xefa\xf5aE\xd6\x14\xaed\xb5.\xf5\x18\xa1S_j\xe0A\xdc5U" "dQ\nqy\xf8\x17+\xbf\xd7Jx\xda-\xcb\xd7\xcfQ\x1bI\xb8_9\b\x80\xea듎i|\x1f"]
func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) {
fmt.Println(decoder)
tx.Nonce = decoder.Get(0).Uint()
tx.Value = decoder.Get(1).BigInt()
tx.Gasprice = decoder.Get(2).BigInt()
// If the 4th item is a list(slice) this tx
// is a contract creation tx
if decoder.Get(3).IsSlice() {
if decoder.Get(3).IsList() {
d := decoder.Get(3)
tx.Data = make([]string, d.Len())
for i := 0; i < d.Len(); i++ {
@ -173,6 +154,7 @@ func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) {
tx.v = byte(decoder.Get(4).Uint())
tx.r = decoder.Get(5).Bytes()
tx.s = decoder.Get(6).Bytes()
tx.contractCreation = true
} else {
tx.Recipient = decoder.Get(3).Bytes()