Changed how txs define their data & added init field

pull/150/head
obscuren 11 years ago
parent e09f0a5f2c
commit 720521ed4a
  1. 66
      ethchain/transaction.go
  2. 53
      ethchain/transaction_test.go

@ -14,7 +14,8 @@ type Transaction struct {
Value *big.Int Value *big.Int
Gas *big.Int Gas *big.Int
Gasprice *big.Int Gasprice *big.Int
Data []string Data []byte
Init []byte
v byte v byte
r, s []byte r, s []byte
@ -22,11 +23,11 @@ type Transaction struct {
contractCreation bool contractCreation bool
} }
func NewContractCreationTx(value, gasprice *big.Int, data []string) *Transaction { func NewContractCreationTx(value, gasprice *big.Int, data []byte) *Transaction {
return &Transaction{Value: value, Gasprice: gasprice, Data: data, contractCreation: true} return &Transaction{Value: value, Gasprice: gasprice, Data: data, contractCreation: true}
} }
func NewTransactionMessage(to []byte, value, gasprice, gas *big.Int, data []string) *Transaction { func NewTransactionMessage(to []byte, value, gasprice, gas *big.Int, data []byte) *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}
} }
@ -45,19 +46,12 @@ func NewTransactionFromValue(val *ethutil.Value) *Transaction {
} }
func (tx *Transaction) Hash() []byte { func (tx *Transaction) Hash() []byte {
data := make([]interface{}, len(tx.Data)) data := []interface{}{tx.Nonce, tx.Value, tx.Gasprice, tx.Gas, tx.Recipient, string(tx.Data)}
for i, val := range tx.Data { if tx.contractCreation {
data[i] = val data = append(data, string(tx.Init))
} }
preEnc := []interface{}{ return ethutil.Sha3Bin(ethutil.NewValue(data).Encode())
tx.Nonce,
tx.Recipient,
tx.Value,
data,
}
return ethutil.Sha3Bin(ethutil.Encode(preEnc))
} }
func (tx *Transaction) IsContract() bool { func (tx *Transaction) IsContract() bool {
@ -110,15 +104,17 @@ func (tx *Transaction) Sign(privk []byte) error {
return nil return nil
} }
// [ NONCE, VALUE, GASPRICE, GAS, TO, DATA, V, R, S ]
// [ NONCE, VALUE, GASPRICE, GAS, 0, CODE, INIT, V, R, S ]
func (tx *Transaction) RlpData() interface{} { func (tx *Transaction) RlpData() interface{} {
data := []interface{}{tx.Nonce, tx.Value, tx.Gasprice} data := []interface{}{tx.Nonce, tx.Value, tx.Gasprice, tx.Gas, tx.Recipient, tx.Data}
if !tx.contractCreation { if tx.contractCreation {
data = append(data, tx.Recipient, tx.Gas) data = append(data, tx.Init)
} }
d := ethutil.NewSliceValue(tx.Data).Slice() //d := ethutil.NewSliceValue(tx.Data).Slice()
return append(data, d, tx.v, tx.r, tx.s) return append(data, tx.v, tx.r, tx.s)
} }
func (tx *Transaction) RlpValue() *ethutil.Value { func (tx *Transaction) RlpValue() *ethutil.Value {
@ -137,31 +133,19 @@ func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) {
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()
tx.Gas = decoder.Get(3).BigInt()
tx.Recipient = decoder.Get(4).Bytes()
tx.Data = decoder.Get(5).Bytes()
// If the 4th item is a list(slice) this tx // If the list is of length 10 it's a contract creation tx
// is a contract creation tx if decoder.Len() == 10 {
if decoder.Get(3).IsList() {
d := decoder.Get(3)
tx.Data = make([]string, d.Len())
for i := 0; i < d.Len(); i++ {
tx.Data[i] = d.Get(i).Str()
}
tx.v = byte(decoder.Get(4).Uint())
tx.r = decoder.Get(5).Bytes()
tx.s = decoder.Get(6).Bytes()
tx.contractCreation = true tx.contractCreation = true
} else { tx.Init = decoder.Get(6).Bytes()
tx.Recipient = decoder.Get(3).Bytes()
tx.Gas = decoder.Get(4).BigInt()
d := decoder.Get(5)
tx.Data = make([]string, d.Len())
for i := 0; i < d.Len(); i++ {
tx.Data[i] = d.Get(i).Str()
}
tx.v = byte(decoder.Get(7).Uint())
tx.r = decoder.Get(8).Bytes()
tx.s = decoder.Get(9).Bytes()
} else {
tx.v = byte(decoder.Get(6).Uint()) tx.v = byte(decoder.Get(6).Uint())
tx.r = decoder.Get(7).Bytes() tx.r = decoder.Get(7).Bytes()
tx.s = decoder.Get(8).Bytes() tx.s = decoder.Get(8).Bytes()

@ -1,54 +1 @@
package ethchain package ethchain
import (
"encoding/hex"
"math/big"
"testing"
)
func TestAddressRetrieval(t *testing.T) {
// TODO
// 88f9b82462f6c4bf4a0fb15e5c3971559a316e7f
key, _ := hex.DecodeString("3ecb44df2159c26e0f995712d4f39b6f6e499b40749b1cf1246c37f9516cb6a4")
tx := &Transaction{
Nonce: 0,
Recipient: ZeroHash160,
Value: big.NewInt(0),
Data: nil,
}
//fmt.Printf("rlp %x\n", tx.RlpEncode())
//fmt.Printf("sha rlp %x\n", tx.Hash())
tx.Sign(key)
//fmt.Printf("hex tx key %x\n", tx.PublicKey())
//fmt.Printf("seder %x\n", tx.Sender())
}
func TestAddressRetrieval2(t *testing.T) {
// TODO
// 88f9b82462f6c4bf4a0fb15e5c3971559a316e7f
key, _ := hex.DecodeString("3ecb44df2159c26e0f995712d4f39b6f6e499b40749b1cf1246c37f9516cb6a4")
addr, _ := hex.DecodeString("944400f4b88ac9589a0f17ed4671da26bddb668b")
tx := &Transaction{
Nonce: 0,
Recipient: addr,
Value: big.NewInt(1000),
Data: nil,
}
tx.Sign(key)
//data, _ := hex.DecodeString("f85d8094944400f4b88ac9589a0f17ed4671da26bddb668b8203e8c01ca0363b2a410de00bc89be40f468d16e70e543b72191fbd8a684a7c5bef51dc451fa02d8ecf40b68f9c64ed623f6ee24c9c878943b812e1e76bd73ccb2bfef65579e7")
//tx := NewTransactionFromData(data)
/*
fmt.Println(tx.RlpValue())
fmt.Printf("rlp %x\n", tx.RlpEncode())
fmt.Printf("sha rlp %x\n", tx.Hash())
//tx.Sign(key)
fmt.Printf("hex tx key %x\n", tx.PublicKey())
fmt.Printf("seder %x\n", tx.Sender())
*/
}

Loading…
Cancel
Save