From 505f1fbcbbc00b3f4750b9e9a7c119f882c620bf Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 25 Mar 2015 17:05:29 +0100 Subject: [PATCH] added tx tests and fixed block tests --- core/block_processor.go | 2 +- core/types/derive_sha.go | 2 +- core/types/transaction.go | 6 ++-- core/types/transaction_test.go | 53 ++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/core/block_processor.go b/core/block_processor.go index dfac6e0cc..bc3274eb5 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -195,7 +195,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big return } - // The transactions Trie's root (R = (Tr [[H1, T1], [H2, T2], ... [Hn, Tn]])) + // The transactions Trie's root (R = (Tr [[i, RLP(T1)], [i, RLP(T2)], ... [n, RLP(Tn)]])) // can be used by light clients to make sure they've received the correct Txs txSha := types.DeriveSha(block.Transactions()) if txSha != header.TxHash { diff --git a/core/types/derive_sha.go b/core/types/derive_sha.go index 10e3d7446..f25e5937e 100644 --- a/core/types/derive_sha.go +++ b/core/types/derive_sha.go @@ -16,7 +16,7 @@ func DeriveSha(list DerivableList) common.Hash { db, _ := ethdb.NewMemDatabase() trie := trie.New(nil, db) for i := 0; i < list.Len(); i++ { - key, _ := rlp.EncodeToBytes(i) + key, _ := rlp.EncodeToBytes(uint(i)) trie.Update(key, list.GetRlp(i)) } diff --git a/core/types/transaction.go b/core/types/transaction.go index 7aef5ce94..35e8f5ac8 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -79,6 +79,7 @@ func (self *Transaction) From() (common.Address, error) { if len(pubkey) == 0 || pubkey[0] != 4 { return common.Address{}, errors.New("invalid public key") } + var addr common.Address copy(addr[:], crypto.Sha3(pubkey[1:])[12:]) return addr, nil @@ -110,8 +111,9 @@ func (tx *Transaction) PublicKey() []byte { sig := append(r, s...) sig = append(sig, v-27) - //pubkey := crypto.Ecrecover(append(hash, sig...)) - pubkey, _ := secp256k1.RecoverPubkey(hash[:], sig) + //pubkey := crypto.Ecrecover(append(hash[:], sig...)) + //pubkey, _ := secp256k1.RecoverPubkey(hash[:], sig) + pubkey := crypto.FromECDSAPub(crypto.SigToPub(hash[:], sig)) return pubkey } diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index 0b0dfe3ff..6a015cb9a 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -2,10 +2,12 @@ package types import ( "bytes" + "crypto/ecdsa" "math/big" "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" ) @@ -56,3 +58,54 @@ func TestTransactionEncode(t *testing.T) { t.Errorf("encoded RLP mismatch, got %x", txb) } } + +func decodeTx(data []byte) (*Transaction, error) { + var tx Transaction + return &tx, rlp.Decode(bytes.NewReader(data), &tx) +} + +func defaultTestKey() (*ecdsa.PrivateKey, []byte) { + key := crypto.ToECDSA(common.Hex2Bytes("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8")) + addr := crypto.PubkeyToAddress(key.PublicKey) + return key, addr +} + +func TestRecipientEmpty(t *testing.T) { + _, addr := defaultTestKey() + + tx, err := decodeTx(common.Hex2Bytes("f8498080808080011ca09b16de9d5bdee2cf56c28d16275a4da68cd30273e2525f3959f5d62557489921a0372ebd8fb3345f7db7b5a86d42e24d36e983e259b0664ceb8c227ec9af572f3d")) + if err != nil { + t.Error(err) + t.FailNow() + } + + from, err := tx.From() + if err != nil { + t.Error(err) + t.FailNow() + } + + if !bytes.Equal(addr, from.Bytes()) { + t.Error("derived address doesn't match") + } +} + +func TestRecipientNormal(t *testing.T) { + _, addr := defaultTestKey() + + tx, err := decodeTx(common.Hex2Bytes("f85d80808094000000000000000000000000000000000000000080011ca0527c0d8f5c63f7b9f41324a7c8a563ee1190bcbf0dac8ab446291bdbf32f5c79a0552c4ef0a09a04395074dab9ed34d3fbfb843c2f2546cc30fe89ec143ca94ca6")) + if err != nil { + t.Error(err) + t.FailNow() + } + + from, err := tx.From() + if err != nil { + t.Error(err) + t.FailNow() + } + + if !bytes.Equal(addr, from.Bytes()) { + t.Error("derived address doesn't match") + } +} -- GitLab