diff --git a/sharding/db.go b/sharding/db.go index 733fda6b9..b20936c27 100644 --- a/sharding/db.go +++ b/sharding/db.go @@ -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 } diff --git a/sharding/shard.go b/sharding/shard.go index abaac5305..baff07ddc 100644 --- a/sharding/shard.go +++ b/sharding/shard.go @@ -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 } diff --git a/sharding/shard_test.go b/sharding/shard_test.go index 5116e00d4..644bcddc9 100644 --- a/sharding/shard_test.go +++ b/sharding/shard_test.go @@ -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) + } +}