forked from cerc-io/plugeth
core/types: fix Transaction.Hash and add support for encoding with package rlp
This commit is contained in:
parent
ad78db4d62
commit
d5de6489d7
@ -3,6 +3,7 @@ package types
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
@ -27,12 +28,12 @@ type Transaction struct {
|
|||||||
R, S []byte
|
R, S []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContractCreationTx(Amount, gasAmount, price *big.Int, data []byte) *Transaction {
|
func NewContractCreationTx(amount, gasAmount, price *big.Int, data []byte) *Transaction {
|
||||||
return NewTransactionMessage(common.Address{}, Amount, gasAmount, price, data)
|
return NewTransactionMessage(common.Address{}, amount, gasAmount, price, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTransactionMessage(to common.Address, Amount, gasAmount, price *big.Int, data []byte) *Transaction {
|
func NewTransactionMessage(to common.Address, amount, gasAmount, price *big.Int, data []byte) *Transaction {
|
||||||
return &Transaction{Recipient: to, Amount: Amount, Price: price, GasLimit: gasAmount, Payload: data}
|
return &Transaction{Recipient: to, Amount: amount, Price: price, GasLimit: gasAmount, Payload: data}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTransactionFromBytes(data []byte) *Transaction {
|
func NewTransactionFromBytes(data []byte) *Transaction {
|
||||||
@ -44,7 +45,7 @@ func NewTransactionFromBytes(data []byte) *Transaction {
|
|||||||
func (tx *Transaction) Hash() (a common.Hash) {
|
func (tx *Transaction) Hash() (a common.Hash) {
|
||||||
h := sha3.NewKeccak256()
|
h := sha3.NewKeccak256()
|
||||||
rlp.Encode(h, []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload})
|
rlp.Encode(h, []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload})
|
||||||
h.Sum(a[:])
|
h.Sum(a[:0])
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +85,6 @@ func (tx *Transaction) Curve() (v byte, r []byte, s []byte) {
|
|||||||
v = byte(tx.V)
|
v = byte(tx.V)
|
||||||
r = common.LeftPadBytes(tx.R, 32)
|
r = common.LeftPadBytes(tx.R, 32)
|
||||||
s = common.LeftPadBytes(tx.S, 32)
|
s = common.LeftPadBytes(tx.S, 32)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,6 +124,19 @@ func (tx *Transaction) SetSignatureValues(sig []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tx Transaction) EncodeRLP(w io.Writer) error {
|
||||||
|
return rlp.Encode(w, []interface{}{
|
||||||
|
tx.AccountNonce,
|
||||||
|
tx.Price, tx.GasLimit,
|
||||||
|
tx.Recipient,
|
||||||
|
tx.Amount,
|
||||||
|
tx.Payload,
|
||||||
|
tx.V,
|
||||||
|
tx.R,
|
||||||
|
tx.S,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: remove
|
// TODO: remove
|
||||||
func (tx *Transaction) RlpData() interface{} {
|
func (tx *Transaction) RlpData() interface{} {
|
||||||
data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload}
|
data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload}
|
||||||
|
@ -1 +1,57 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"math/big"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The values in those tests are from the Transaction Tests
|
||||||
|
// at github.com/ethereum/tests.
|
||||||
|
|
||||||
|
var (
|
||||||
|
emptyTx = NewTransactionMessage(
|
||||||
|
common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"),
|
||||||
|
big.NewInt(0), big.NewInt(0), big.NewInt(0),
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
|
||||||
|
rightvrsTx = &Transaction{
|
||||||
|
Recipient: common.HexToAddress("b94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
|
||||||
|
AccountNonce: 3,
|
||||||
|
Price: big.NewInt(1),
|
||||||
|
GasLimit: big.NewInt(2000),
|
||||||
|
Amount: big.NewInt(10),
|
||||||
|
Payload: common.FromHex("5544"),
|
||||||
|
V: 28,
|
||||||
|
R: common.FromHex("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a"),
|
||||||
|
S: common.FromHex("8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3"),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTransactionHash(t *testing.T) {
|
||||||
|
// "EmptyTransaction"
|
||||||
|
if emptyTx.Hash() != common.HexToHash("c775b99e7ad12f50d819fcd602390467e28141316969f4b57f0626f74fe3b386") {
|
||||||
|
t.Errorf("empty transaction hash mismatch, got %x", emptyTx.Hash())
|
||||||
|
}
|
||||||
|
|
||||||
|
// "RightVRSTest"
|
||||||
|
if rightvrsTx.Hash() != common.HexToHash("fe7a79529ed5f7c3375d06b26b186a8644e0e16c373d7a12be41c62d6042b77a") {
|
||||||
|
t.Errorf("RightVRS transaction hash mismatch, got %x", rightvrsTx.Hash())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTransactionEncode(t *testing.T) {
|
||||||
|
// "RightVRSTest"
|
||||||
|
txb, err := rlp.EncodeToBytes(rightvrsTx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("encode error: %v", err)
|
||||||
|
}
|
||||||
|
should := common.FromHex("f86103018207d094b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a8255441ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3")
|
||||||
|
if !bytes.Equal(txb, should) {
|
||||||
|
t.Errorf("encoded RLP mismatch, got %x", txb)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user