-
Notifications
You must be signed in to change notification settings - Fork 940
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sharding: address review comments om #100, fix linter issues
Former-commit-id: 5febe72a5a1936ce643488067e0990da810f1f5e [formerly 74c85fc] Former-commit-id: 0cc6d45
- Loading branch information
1 parent
a617eba
commit d1aa843
Showing
4 changed files
with
97 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,82 @@ | ||
package sharding | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto/sha3" | ||
"github.com/ethereum/go-ethereum/rlp" | ||
) | ||
|
||
// Hash returns the hash of a collation's entire contents. Useful for comparison tests. | ||
func (c *Collation) Hash() (hash common.Hash) { | ||
hw := sha3.NewKeccak256() | ||
rlp.Encode(hw, c) | ||
hw.Sum(hash[:0]) | ||
return hash | ||
} | ||
func TestShard_ValidateShardID(t *testing.T) { | ||
header := &CollationHeader{shardID: big.NewInt(4)} | ||
shard := MakeShard(big.NewInt(3)) | ||
shardDB := makeShardKV() | ||
shard := MakeShard(big.NewInt(3), shardDB) | ||
|
||
if err := shard.ValidateShardID(header); err == nil { | ||
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()) | ||
t.Errorf("ShardID validation incorrect. Function should throw error when ShardID's do not match. want=%d. got=%d", header.ShardID().Int64(), shard.ShardID().Int64()) | ||
} | ||
|
||
header2 := &CollationHeader{shardID: big.NewInt(100)} | ||
shard2 := MakeShard(big.NewInt(100)) | ||
shard2 := MakeShard(big.NewInt(100), shardDB) | ||
|
||
if err := shard2.ValidateShardID(header2); err != nil { | ||
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()) | ||
t.Errorf("ShardID validation incorrect. Function should not throw error when ShardID's match. want=%d. got=%d", header2.ShardID().Int64(), shard2.ShardID().Int64()) | ||
} | ||
} | ||
|
||
func TestShard_GetHeaderByHash(t *testing.T) { | ||
header := &CollationHeader{shardID: big.NewInt(1)} | ||
shard := MakeShard(big.NewInt(1)) | ||
func TestShard_HeaderByHash(t *testing.T) { | ||
root := common.StringToHash("hi") | ||
header := &CollationHeader{shardID: big.NewInt(1), chunkRoot: &root} | ||
shardDB := makeShardKV() | ||
shard := MakeShard(big.NewInt(1), shardDB) | ||
|
||
if err := shard.SaveHeader(header); err != nil { | ||
t.Fatal(err) | ||
t.Fatalf("cannot save collation header: %v", 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) | ||
dbHeader, err := shard.HeaderByHash(&hash) | ||
if err != nil { | ||
t.Fatal(err) | ||
t.Fatalf("could not fetch collation header by hash: %v", err) | ||
} | ||
log.Printf("header in first test: %v", header.ChunkRoot().String()) | ||
log.Printf("db header in first test: %v", dbHeader.ChunkRoot().String()) | ||
// Compare the hashes. | ||
if header.Hash() != dbHeader.Hash() { | ||
t.Fatalf("Headers do not match. want=%v. got=%v", header, dbHeader) | ||
t.Errorf("headers do not match. want=%v. got=%v", header, dbHeader) | ||
} | ||
} | ||
|
||
// func TestShard_GetCollationByHash(t *testing.T) { | ||
// collation := &Collation{ | ||
// header: &CollationHeader{shardID: big.NewInt(1)}, | ||
// body: []byte{1, 2, 3}, | ||
// } | ||
// shard := MakeShard(big.NewInt(1)) | ||
func TestShard_CollationByHash(t *testing.T) { | ||
collation := &Collation{ | ||
header: &CollationHeader{shardID: big.NewInt(1)}, | ||
body: []byte{1, 2, 3}, | ||
} | ||
shardDB := makeShardKV() | ||
shard := MakeShard(big.NewInt(1), shardDB) | ||
|
||
if err := shard.SaveCollation(collation); err != nil { | ||
t.Fatalf("cannot save collation: %v", err) | ||
} | ||
hash := collation.Header().Hash() | ||
|
||
// if err := shard.SaveCollation(collation); err != nil { | ||
// t.Fatal(err) | ||
// } | ||
// hash := collation.Header().Hash() | ||
// fmt.Printf("In Test: %s\n", hash.String()) | ||
dbCollation, err := shard.CollationByHash(&hash) | ||
if err != nil { | ||
t.Fatalf("could not fetch collation by hash: %v", err) | ||
} | ||
|
||
// // 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) | ||
// } | ||
// } | ||
// Compare the hashes. | ||
if collation.Hash() != dbCollation.Hash() { | ||
t.Errorf("collations do not match. want=%v. got=%v", collation, dbCollation) | ||
} | ||
} |