sharding: added tests for get header by hash

Former-commit-id: b880b352c90326e0c3649b5fa932f77f4938a26e [formerly 37d96a59e637f2f7348b6be0fe1d12889ae272f7]
Former-commit-id: 557473d2699b0a29a127fd0994887f5d87328463
This commit is contained in:
Raul Jordan 2018-05-07 17:29:51 -04:00
parent 191568b957
commit b56056668c
3 changed files with 33 additions and 3 deletions

View File

@ -15,8 +15,12 @@ func makeShardKV() *shardKV {
}
func (sb *shardKV) Get(k *common.Hash) ([]byte, error) {
v := sb.kv[k]
if v == nil {
v, ok := sb.kv[k]
fmt.Printf("Map: %v\n", sb.kv)
fmt.Printf("Key: %v\n", k)
fmt.Printf("Val: %v\n", sb.kv[k])
fmt.Printf("Ok: %v\n", ok)
if !ok {
return nil, fmt.Errorf("Key Not Found")
}
return v, nil
@ -32,6 +36,7 @@ func (sb *shardKV) Has(k *common.Hash) bool {
func (sb *shardKV) Put(k *common.Hash, v []byte) {
sb.kv[k] = v
fmt.Printf("Put: %v\n", sb.kv[k])
return
}

View File

@ -42,7 +42,7 @@ func (s *Shard) ValidateShardID(h *CollationHeader) error {
func (s *Shard) GetHeaderByHash(hash *common.Hash) (*CollationHeader, error) {
encoded, err := s.shardDB.Get(hash)
if err != nil {
return nil, fmt.Errorf("Error: Header Not Found")
return nil, fmt.Errorf("Error: Header Not Found: %v", err)
}
var header CollationHeader
if err := rlp.DecodeBytes(encoded, &header); err != nil {
@ -143,6 +143,7 @@ func (s *Shard) SaveHeader(header *CollationHeader) error {
}
// Uses the hash of the header as the key.
hash := header.Hash()
fmt.Printf("In SaveHeader: %s\n", hash.String())
s.shardDB.Put(&hash, encoded)
return nil
}

View File

@ -1,8 +1,11 @@
package sharding
import (
"fmt"
"math/big"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestShard_ValidateShardID(t *testing.T) {
@ -20,3 +23,24 @@ func TestShard_ValidateShardID(t *testing.T) {
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())
}
}
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)
}
// TODO: decode the RLP
if !cmp.Equal(header, dbHeader) {
t.Fatalf("Headers do not match. want=%v. got=%v", header, dbHeader)
}
}