From 742b6104cedcc4f4882b3b495b679e106ef6c24c Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Sat, 12 May 2018 09:57:34 -0400 Subject: [PATCH] sharding: include rest of tests, 2 still need to pass Former-commit-id: 7042ae002b01c64c61d70b92e734de28bc616df2 [formerly e5fa5056c649b4718f3cd64184f6dbf07c62c0eb] Former-commit-id: 0e669fd0fca32fc6da1a1bc7beb6b5c07254d72b --- sharding/shard_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/sharding/shard_test.go b/sharding/shard_test.go index 8f1eae80a..791237c26 100644 --- a/sharding/shard_test.go +++ b/sharding/shard_test.go @@ -1,6 +1,7 @@ package sharding import ( + "bytes" "math/big" "testing" @@ -121,3 +122,65 @@ func TestShard_CanonicalHeaderHash(t *testing.T) { t.Errorf("header hashes do not match. want=%v. got=%v", headerHash.String(), canonicalHeaderHash.String()) } } + +func TestShard_CanonicalCollation(t *testing.T) { + shardID := big.NewInt(1) + period := big.NewInt(1) + proposerAddress := common.StringToAddress("") + proposerSignature := []byte{} + emptyHash := common.StringToHash("") + header := NewCollationHeader(shardID, &emptyHash, period, &proposerAddress, proposerSignature) + + shardDB := database.MakeShardKV() + shard := MakeShard(shardID, shardDB) + + collation := &Collation{ + header: header, + body: []byte{1, 2, 3}, + } + + // We set the chunk root. + collation.CalculateChunkRoot() + + if err := shard.SaveCollation(collation); err != nil { + t.Fatalf("failed to save collation to shardDB: %v", err) + } + + if err := shard.SetCanonical(header); err != nil { + t.Fatalf("failed to set header as canonical: %v", err) + } + + canonicalCollation, err := shard.CanonicalCollation(shardID, period) + if err != nil { + t.Fatalf("failed to get canonical collation from shardDB: %v", err) + } +} + +func TestShard_BodyByChunkRoot(t *testing.T) { + body := []byte{1, 2, 3, 4, 5} + shardID := big.NewInt(1) + shardDB := database.MakeShardKV() + shard := MakeShard(shardID, shardDB) + + if err := shard.SaveBody(body); err != nil { + t.Fatalf("cannot save body: %v", err) + } + + // Right now this just hashes the body. We will instead need to + // blob serialize. + // TODO: blob serialization. + chunkRoot := common.BytesToHash(body) + + dbBody, err := shard.BodyByChunkRoot(&chunkRoot) + if err != nil { + t.Errorf("cannot fetch body by chunk root: %v", err) + } + + if !bytes.Equal(body, dbBody) { + t.Errorf("bodies not equal. want=%v. got=%v", body, dbBody) + } +} + +func TestShard_CheckAvailability(t *testing.T) { + t.Fatalf("cannot save availability") +}