diff --git a/sharding/collation.go b/sharding/collation.go index 56dd826df..19f1b8d05 100644 --- a/sharding/collation.go +++ b/sharding/collation.go @@ -11,13 +11,21 @@ import ( // Collation base struct. type Collation struct { - header *CollationHeader - body []byte + header *CollationHeader + // body represents the serialized blob of a collation's transactions. + body []byte + // transactions serves as a useful slice to store deserialized chunks from the + // collation's body. Every time this transactions slice is updated, the serialized + // body would need to be recalculated. This will be a useful property for proposers + // in our system. transactions []*types.Transaction } // CollationHeader base struct. type CollationHeader struct { + // RLP decoding only works on exported properties of structs. In this case, we want + // to keep collation properties as read-only and only accessible through getters. + // We can accomplish this through this nested data property. data collationHeaderData } diff --git a/sharding/database/inmemory.go b/sharding/database/inmemory.go index 26d672af7..0e2a5c44b 100644 --- a/sharding/database/inmemory.go +++ b/sharding/database/inmemory.go @@ -35,11 +35,15 @@ func (sb *ShardKV) Has(k common.Hash) bool { } // Put updates a key's value in the mapping. -func (sb *ShardKV) Put(k common.Hash, v []byte) { +func (sb *ShardKV) Put(k common.Hash, v []byte) error { + // there is no error in a simple setting of a value in a go map. sb.kv[k] = v + return nil } // Delete removes the key and value from the mapping. -func (sb *ShardKV) Delete(k common.Hash) { +func (sb *ShardKV) Delete(k common.Hash) error { + // There is no return value for deleting a simple key in a go map. delete(sb.kv, k) + return nil } diff --git a/sharding/database/inmemory_test.go b/sharding/database/inmemory_test.go index e21886e52..b994a6550 100644 --- a/sharding/database/inmemory_test.go +++ b/sharding/database/inmemory_test.go @@ -18,4 +18,10 @@ func Test_ShardKVGet(t *testing.T) { if val == nil { t.Errorf("no value stored for key") } + + hash2 := common.StringToHash("") + val2, err := kv.Get(hash2) + if err == nil { + t.Errorf("non-existent key should not have a value. key=%v, value=%v", hash2, val2) + } } diff --git a/sharding/shard.go b/sharding/shard.go index 1d6182c04..8a4edc710 100644 --- a/sharding/shard.go +++ b/sharding/shard.go @@ -12,8 +12,8 @@ import ( type shardBackend interface { Get(k common.Hash) ([]byte, error) Has(k common.Hash) bool - Put(k common.Hash, val []byte) - Delete(k common.Hash) + Put(k common.Hash, val []byte) error + Delete(k common.Hash) error } // Shard base struct. @@ -137,13 +137,17 @@ func (s *Shard) SetAvailability(chunkRoot *common.Hash, availability bool) error if err != nil { return fmt.Errorf("cannot RLP encode availability: %v", err) } - s.shardDB.Put(key, enc) + if err := s.shardDB.Put(key, enc); err != nil { + return fmt.Errorf("cannot update shardDB: %v", err) + } } else { enc, err := rlp.EncodeToBytes(false) if err != nil { return fmt.Errorf("cannot RLP encode availability: %v", err) } - s.shardDB.Put(key, enc) + if err := s.shardDB.Put(key, enc); err != nil { + return fmt.Errorf("cannot update shardDB: %v", err) + } } return nil } @@ -156,7 +160,9 @@ func (s *Shard) SaveHeader(header *CollationHeader) error { } // Uses the hash of the header as the key. - s.shardDB.Put(header.Hash(), encoded) + if err := s.shardDB.Put(header.Hash(), encoded); err != nil { + return fmt.Errorf("cannot update shardDB: %v", err) + } return nil } @@ -167,7 +173,9 @@ func (s *Shard) SaveBody(body []byte) error { // chunkRoot := getChunkRoot(body) using the blob algorithm utils. // right now we will just take the raw keccak256 of the body until #92 is merged. chunkRoot := common.BytesToHash(body) - s.shardDB.Put(chunkRoot, body) + if err := s.shardDB.Put(chunkRoot, body); err != nil { + return fmt.Errorf("cannot update shardDB: %v", err) + } s.SetAvailability(&chunkRoot, true) return nil } @@ -200,7 +208,9 @@ func (s *Shard) SetCanonical(header *CollationHeader) error { if err != nil { return fmt.Errorf("cannot encode header: %v", err) } - s.shardDB.Put(key, encoded) + if err := s.shardDB.Put(key, encoded); err != nil { + return fmt.Errorf("cannot update shardDB: %v", err) + } return nil }