2018-02-20 23:57:52 -05:00
|
|
|
package sharding
|
|
|
|
|
|
|
|
import (
|
2018-05-14 09:53:20 -04:00
|
|
|
"math/big"
|
2018-05-15 20:35:47 +08:00
|
|
|
//"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
//"reflect"
|
2018-05-03 16:32:42 -05:00
|
|
|
"testing"
|
|
|
|
|
2018-02-20 23:57:52 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
)
|
|
|
|
|
2018-05-14 09:53:20 -04:00
|
|
|
func TestCollation_Transactions(t *testing.T) {
|
|
|
|
header := NewCollationHeader(big.NewInt(1), nil, big.NewInt(1), nil, []byte{})
|
|
|
|
body := []byte{}
|
|
|
|
transactions := []*types.Transaction{
|
|
|
|
makeTxWithGasLimit(0),
|
|
|
|
makeTxWithGasLimit(1),
|
|
|
|
makeTxWithGasLimit(2),
|
|
|
|
makeTxWithGasLimit(3),
|
2018-05-03 16:32:42 -05:00
|
|
|
}
|
2018-02-20 23:57:52 -05:00
|
|
|
|
2018-05-14 09:53:20 -04:00
|
|
|
collation := NewCollation(header, body, transactions)
|
|
|
|
|
|
|
|
for i, tx := range collation.Transactions() {
|
|
|
|
if tx.Hash().String() != transactions[i].Hash().String() {
|
|
|
|
t.Errorf("initialized collation struct does not contain correct transactions")
|
2018-05-03 16:32:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-20 23:57:52 -05:00
|
|
|
|
2018-05-14 20:03:18 +08:00
|
|
|
//TODO: Add test for converting *types.Transaction into raw blobs
|
|
|
|
|
|
|
|
//Tests thta Transactions can be serialised
|
2018-05-08 16:18:12 +08:00
|
|
|
func TestSerialize(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
transactions []*types.Transaction
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
transactions: []*types.Transaction{
|
|
|
|
makeTxWithGasLimit(0),
|
|
|
|
makeTxWithGasLimit(1),
|
|
|
|
makeTxWithGasLimit(2),
|
|
|
|
makeTxWithGasLimit(3),
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
transactions: []*types.Transaction{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
c := &Collation{}
|
2018-05-08 17:12:11 +08:00
|
|
|
for _, tx := range tt.transactions {
|
|
|
|
c.AddTransaction(tx)
|
2018-05-08 16:18:12 +08:00
|
|
|
}
|
2018-05-14 18:13:45 +08:00
|
|
|
|
2018-05-15 20:35:47 +08:00
|
|
|
/*var tests *types.Transaction
|
|
|
|
yadd := reflect.ValueOf(*c.transactions[3])
|
|
|
|
d := yadd.FieldByName("data").FieldByName("Hash")
|
|
|
|
|
|
|
|
test, err := rlp.EncodeToBytes(c.transactions[3])
|
2018-05-14 18:13:45 +08:00
|
|
|
if err != nil {
|
2018-05-15 20:35:47 +08:00
|
|
|
t.Fatalf("%v\n %v\n %v", err, test, *(c.transactions[0]))
|
|
|
|
}
|
|
|
|
erx := rlp.DecodeBytes(test, &tests)
|
|
|
|
|
|
|
|
dd := reflect.ValueOf(*tests)
|
|
|
|
cv := dd.FieldByName("data").FieldByName("Hash")
|
|
|
|
|
|
|
|
if cv != d {
|
|
|
|
t.Fatalf("%v\n %v\n %v", erx, cv, d)
|
|
|
|
} */
|
|
|
|
|
|
|
|
results, err := c.Serialize()
|
|
|
|
if err == nil {
|
2018-05-14 20:03:18 +08:00
|
|
|
t.Fatalf("%v\n %v\n %v", err, results, c.transactions)
|
2018-05-14 18:13:45 +08:00
|
|
|
}
|
2018-05-08 16:18:12 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-05-14 09:53:20 -04:00
|
|
|
|
2018-02-20 23:57:52 -05:00
|
|
|
func makeTxWithGasLimit(gl uint64) *types.Transaction {
|
|
|
|
return types.NewTransaction(0 /*nonce*/, common.HexToAddress("0x0") /*to*/, nil /*amount*/, gl, nil /*gasPrice*/, nil /*data*/)
|
|
|
|
}
|