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-16 11:07:31 +08:00
"bytes"
2018-05-16 00:38:24 +08:00
"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-16 00:38:24 +08:00
// fieldAccess is to access unexported fields in structs in another package
func fieldAccess ( i interface { } , fields [ ] string ) reflect . Value {
val := reflect . ValueOf ( i )
for i := 0 ; i < len ( fields ) ; i ++ {
val = reflect . Indirect ( val ) . FieldByName ( fields [ i ] )
}
return val
}
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
2018-05-16 00:38:24 +08:00
//Tests that Transactions can be serialised
func TestSerialize_Deserialize ( t * testing . T ) {
2018-05-08 16:18:12 +08:00
tests := [ ] struct {
transactions [ ] * types . Transaction
} {
{
transactions : [ ] * types . Transaction {
makeTxWithGasLimit ( 0 ) ,
2018-05-16 00:38:24 +08:00
makeTxWithGasLimit ( 5 ) ,
makeTxWithGasLimit ( 20 ) ,
makeTxWithGasLimit ( 100 ) ,
2018-05-08 16:18:12 +08:00
} ,
} ,
}
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-16 00:38:24 +08:00
tx := c . transactions
results , err := c . Serialize ( )
if err != nil {
t . Errorf ( "Unable to Serialize transactions, %v" , err )
}
2018-05-16 11:07:31 +08:00
deserializedTxs , err := Deserialize ( results )
2018-05-15 20:35:47 +08:00
2018-05-14 18:13:45 +08:00
if err != nil {
2018-05-16 00:38:24 +08:00
t . Errorf ( "Unable to deserialize collation body, %v" , err )
}
2018-05-16 11:07:31 +08:00
if len ( tx ) != len ( * deserializedTxs ) {
t . Errorf ( "Transaction length is different before and after serialization: %v, %v" , len ( tx ) , len ( * deserializedTxs ) )
2018-05-15 20:35:47 +08:00
}
2018-05-16 00:38:24 +08:00
for i := 0 ; i < len ( tx ) ; i ++ {
2018-05-15 20:35:47 +08:00
2018-05-16 11:07:31 +08:00
beforeSerialization := tx [ i ]
afterDeserialization := ( * deserializedTxs ) [ i ]
2018-05-15 20:35:47 +08:00
2018-05-16 11:07:31 +08:00
if beforeSerialization . Nonce ( ) != afterDeserialization . Nonce ( ) {
2018-05-15 22:15:33 +08:00
2018-05-16 11:07:31 +08:00
t . Errorf ( "Data before serialization and after deserialization are not the same ,AccountNonce: %v, %v" , beforeSerialization . Nonce ( ) , afterDeserialization . Nonce ( ) )
2018-05-15 22:15:33 +08:00
2018-05-16 00:38:24 +08:00
}
2018-05-16 11:07:31 +08:00
if beforeSerialization . Gas ( ) != afterDeserialization . Gas ( ) {
2018-05-15 22:15:33 +08:00
2018-05-16 11:07:31 +08:00
t . Errorf ( "Data before serialization and after deserialization are not the same ,GasLimit: %v, %v" , beforeSerialization . Gas ( ) , afterDeserialization . Gas ( ) )
2018-05-16 00:38:24 +08:00
}
2018-05-16 11:07:31 +08:00
if beforeSerialization . GasPrice ( ) . Cmp ( afterDeserialization . GasPrice ( ) ) != 0 {
2018-05-16 00:38:24 +08:00
2018-05-16 11:07:31 +08:00
t . Errorf ( "Data before serialization and after deserialization are not the same ,Price: %v, %v" , beforeSerialization . GasPrice ( ) , afterDeserialization . GasPrice ( ) )
2018-05-16 00:38:24 +08:00
}
2018-05-16 11:07:31 +08:00
beforeAddress := reflect . ValueOf ( beforeSerialization . To ( ) )
afterAddress := reflect . ValueOf ( afterDeserialization . To ( ) )
2018-05-16 00:38:24 +08:00
2018-05-16 11:07:31 +08:00
if reflect . DeepEqual ( beforeAddress , afterAddress ) {
t . Errorf ( "Data before serialization and after deserialization are not the same ,Recipient: %v, %v" , beforeAddress , afterAddress )
2018-05-16 00:38:24 +08:00
}
2018-05-16 11:07:31 +08:00
if beforeSerialization . Value ( ) . Cmp ( afterDeserialization . Value ( ) ) != 0 {
2018-05-16 00:38:24 +08:00
2018-05-16 11:07:31 +08:00
t . Errorf ( "Data before serialization and after deserialization are not the same ,Amount: %v, %v" , beforeSerialization . Value ( ) , afterDeserialization . Value ( ) )
2018-05-16 00:38:24 +08:00
}
2018-05-16 11:07:31 +08:00
beforeData := beforeSerialization . Data ( )
afterData := afterDeserialization . Data ( )
if ! bytes . Equal ( beforeData , afterData ) {
2018-05-16 00:38:24 +08:00
2018-05-16 11:07:31 +08:00
t . Errorf ( "Data before serialization and after deserialization are not the same ,Payload: %v, %v" , beforeData , afterData )
2018-05-16 00:38:24 +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*/ )
}