sharding: shardDB put, delete can return error now

Former-commit-id: 8d96f95dbe6419d804d86b05dca8f1e90e2b56ed [formerly c42c9e84b82dcc29d50739e0b8eac5bd65f93024]
Former-commit-id: 527cb5170e3eb156bf7c0bb2ec69ef9f32cc8320
This commit is contained in:
Raul Jordan 2018-05-10 22:00:54 -04:00
parent 5f76297401
commit 5dc498f402
4 changed files with 39 additions and 11 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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
}