added tx tests and fixed block tests

This commit is contained in:
obscuren 2015-03-25 17:05:29 +01:00
parent 65ea55bccd
commit 505f1fbcbb
4 changed files with 59 additions and 4 deletions

View File

@ -195,7 +195,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
return 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 // can be used by light clients to make sure they've received the correct Txs
txSha := types.DeriveSha(block.Transactions()) txSha := types.DeriveSha(block.Transactions())
if txSha != header.TxHash { if txSha != header.TxHash {

View File

@ -16,7 +16,7 @@ func DeriveSha(list DerivableList) common.Hash {
db, _ := ethdb.NewMemDatabase() db, _ := ethdb.NewMemDatabase()
trie := trie.New(nil, db) trie := trie.New(nil, db)
for i := 0; i < list.Len(); i++ { for i := 0; i < list.Len(); i++ {
key, _ := rlp.EncodeToBytes(i) key, _ := rlp.EncodeToBytes(uint(i))
trie.Update(key, list.GetRlp(i)) trie.Update(key, list.GetRlp(i))
} }

View File

@ -79,6 +79,7 @@ func (self *Transaction) From() (common.Address, error) {
if len(pubkey) == 0 || pubkey[0] != 4 { if len(pubkey) == 0 || pubkey[0] != 4 {
return common.Address{}, errors.New("invalid public key") return common.Address{}, errors.New("invalid public key")
} }
var addr common.Address var addr common.Address
copy(addr[:], crypto.Sha3(pubkey[1:])[12:]) copy(addr[:], crypto.Sha3(pubkey[1:])[12:])
return addr, nil return addr, nil
@ -110,8 +111,9 @@ func (tx *Transaction) PublicKey() []byte {
sig := append(r, s...) sig := append(r, s...)
sig = append(sig, v-27) sig = append(sig, v-27)
//pubkey := crypto.Ecrecover(append(hash, sig...)) //pubkey := crypto.Ecrecover(append(hash[:], sig...))
pubkey, _ := secp256k1.RecoverPubkey(hash[:], sig) //pubkey, _ := secp256k1.RecoverPubkey(hash[:], sig)
pubkey := crypto.FromECDSAPub(crypto.SigToPub(hash[:], sig))
return pubkey return pubkey
} }

View File

@ -2,10 +2,12 @@ package types
import ( import (
"bytes" "bytes"
"crypto/ecdsa"
"math/big" "math/big"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
) )
@ -56,3 +58,54 @@ func TestTransactionEncode(t *testing.T) {
t.Errorf("encoded RLP mismatch, got %x", txb) 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")
}
}