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.
pull/150/head
obscuren 11 years ago
parent 7660e1ed90
commit 00c5f9b9a6
  1. 42
      ethchain/transaction.go

@ -2,6 +2,7 @@ package ethchain
import ( import (
"bytes" "bytes"
"fmt"
"github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethutil"
"github.com/obscuren/secp256k1-go" "github.com/obscuren/secp256k1-go"
"math/big" "math/big"
@ -23,33 +24,14 @@ type Transaction struct {
contractCreation bool 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 { func NewContractCreationTx(value, gasprice *big.Int, data []string) *Transaction {
return &Transaction{Value: value, Gasprice: gasprice, Data: data, contractCreation: true} 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} 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 { func NewTransactionFromBytes(data []byte) *Transaction {
tx := &Transaction{} tx := &Transaction{}
tx.RlpDecode(data) tx.RlpDecode(data)
@ -131,16 +113,13 @@ func (tx *Transaction) Sign(privk []byte) error {
} }
func (tx *Transaction) RlpData() interface{} { func (tx *Transaction) RlpData() interface{} {
// Prepare the transaction for serialization data := []interface{}{tx.Nonce, tx.Value, tx.Gasprice}
return []interface{}{
tx.Nonce, if !tx.contractCreation {
tx.Recipient, data = append(data, tx.Recipient, tx.Gas)
tx.Value,
ethutil.NewSliceValue(tx.Data).Slice(),
tx.v,
tx.r,
tx.s,
} }
return append(data, ethutil.NewSliceValue(tx.Data).Slice(), tx.v, tx.r, tx.s)
} }
func (tx *Transaction) RlpValue() *ethutil.Value { 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 ] // [ 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) { func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) {
fmt.Println(decoder)
tx.Nonce = decoder.Get(0).Uint() tx.Nonce = decoder.Get(0).Uint()
tx.Value = decoder.Get(1).BigInt() tx.Value = decoder.Get(1).BigInt()
tx.Gasprice = decoder.Get(2).BigInt() tx.Gasprice = decoder.Get(2).BigInt()
// If the 4th item is a list(slice) this tx // If the 4th item is a list(slice) this tx
// is a contract creation tx // is a contract creation tx
if decoder.Get(3).IsSlice() { if decoder.Get(3).IsList() {
d := decoder.Get(3) d := decoder.Get(3)
tx.Data = make([]string, d.Len()) tx.Data = make([]string, d.Len())
for i := 0; i < d.Len(); i++ { 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.v = byte(decoder.Get(4).Uint())
tx.r = decoder.Get(5).Bytes() tx.r = decoder.Get(5).Bytes()
tx.s = decoder.Get(6).Bytes() tx.s = decoder.Get(6).Bytes()
tx.contractCreation = true tx.contractCreation = true
} else { } else {
tx.Recipient = decoder.Get(3).Bytes() tx.Recipient = decoder.Get(3).Bytes()

Loading…
Cancel
Save