2018-05-07 20:30:06 +00:00
package sharding
import (
2018-05-07 21:29:51 +00:00
"fmt"
2018-05-07 20:51:27 +00:00
"math/big"
2018-05-07 20:30:06 +00:00
"testing"
)
func TestShard_ValidateShardID ( t * testing . T ) {
2018-05-07 20:51:27 +00:00
header := & CollationHeader { shardID : big . NewInt ( 4 ) }
shard := MakeShard ( big . NewInt ( 3 ) )
if err := shard . ValidateShardID ( header ) ; err == nil {
2018-05-07 22:00:02 +00:00
t . Fatalf ( "Shard ID validation incorrect. Function should throw error when shardID's do not match. want=%d. got=%d" , header . ShardID ( ) . Int64 ( ) , shard . ShardID ( ) . Int64 ( ) )
2018-05-07 20:30:06 +00:00
}
2018-05-07 20:51:27 +00:00
header2 := & CollationHeader { shardID : big . NewInt ( 100 ) }
shard2 := MakeShard ( big . NewInt ( 100 ) )
if err := shard2 . ValidateShardID ( header2 ) ; err != nil {
2018-05-07 22:00:02 +00:00
t . Fatalf ( "Shard ID validation incorrect. Function should not throw error when shardID's match. want=%d. got=%d" , header2 . ShardID ( ) . Int64 ( ) , shard2 . ShardID ( ) . Int64 ( ) )
2018-05-07 20:30:06 +00:00
}
}
2018-05-07 21:29:51 +00:00
func TestShard_GetHeaderByHash ( t * testing . T ) {
header := & CollationHeader { shardID : big . NewInt ( 1 ) }
shard := MakeShard ( big . NewInt ( 1 ) )
if err := shard . SaveHeader ( header ) ; err != nil {
t . Fatal ( err )
}
hash := header . Hash ( )
fmt . Printf ( "In Test: %s\n" , hash . String ( ) )
// It's being saved, but the .Get func doesn't fetch the value...?
dbHeader , err := shard . GetHeaderByHash ( & hash )
if err != nil {
t . Fatal ( err )
}
2018-05-07 22:00:02 +00:00
// Compare the hashes.
if header . Hash ( ) != dbHeader . Hash ( ) {
2018-05-07 21:29:51 +00:00
t . Fatalf ( "Headers do not match. want=%v. got=%v" , header , dbHeader )
}
}
2018-05-07 21:33:35 +00:00
2018-05-07 22:00:02 +00:00
// func TestShard_GetCollationByHash(t *testing.T) {
// collation := &Collation{
// header: &CollationHeader{shardID: big.NewInt(1)},
// body: []byte{1, 2, 3},
// }
// shard := MakeShard(big.NewInt(1))
2018-05-07 21:33:35 +00:00
2018-05-07 22:00:02 +00:00
// if err := shard.SaveCollation(collation); err != nil {
// t.Fatal(err)
// }
// hash := collation.Header().Hash()
// fmt.Printf("In Test: %s\n", hash.String())
2018-05-07 21:33:35 +00:00
2018-05-07 22:00:02 +00:00
// // It's being saved, but the .Get func doesn't fetch the value...?
// dbCollation, err := shard.GetCollationByHash(&hash)
// if err != nil {
// t.Fatal(err)
// }
// // TODO: decode the RLP
// if collation.Hash() != dbCollation.Hash() {
// t.Fatalf("Collations do not match. want=%v. got=%v", collation, dbCollation)
// }
// }