From 1793e7d21d88283719404b004b6d67ddf14ff491 Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Thu, 3 May 2018 11:54:05 -0500 Subject: [PATCH] sharding: fetch body by chunk root Former-commit-id: 5021890d223501bf163d6beae4e4e732673950d2 [formerly 92e89aed0c61909971cf9262cd3002bf2ae2328a] Former-commit-id: 0fd4497f880213878965107de3f782b44c33f065 --- sharding/db.go | 8 ++++---- sharding/shard.go | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/sharding/db.go b/sharding/db.go index 9e3889eeb..b016f7f8e 100644 --- a/sharding/db.go +++ b/sharding/db.go @@ -6,11 +6,11 @@ import ( "github.com/ethereum/go-ethereum/common" ) -type shardBackend struct { +type shardKV struct { kv map[common.Hash][]byte } -func (sb *shardBackend) Get(k common.Hash) ([]byte, error) { +func (sb *shardKV) Get(k common.Hash) ([]byte, error) { v := sb.kv[k] if v == nil { return nil, fmt.Errorf("Key Not Found") @@ -18,12 +18,12 @@ func (sb *shardBackend) Get(k common.Hash) ([]byte, error) { return v, nil } -func (sb *shardBackend) Put(k common.Hash, v []byte) { +func (sb *shardKV) Put(k common.Hash, v []byte) { sb.kv[k] = v return } -func (sb *shardBackend) Delete(k common.Hash) { +func (sb *shardKV) Delete(k common.Hash) { delete(sb.kv, k) return } diff --git a/sharding/shard.go b/sharding/shard.go index 38de4f9b7..2ecaf185b 100644 --- a/sharding/shard.go +++ b/sharding/shard.go @@ -10,7 +10,7 @@ import ( // Shard base struct. type Shard struct { - shardDB *shardBackend + shardDB *shardKV shardID *big.Int } @@ -22,8 +22,8 @@ func (s *Shard) ValidateShardID(h *CollationHeader) error { return nil } -// SetHeader adds the collation header to shardDB -func (s *Shard) SetHeader(h *CollationHeader) error { +// SaveHeader adds the collation header to shardDB. +func (s *Shard) SaveHeader(h *CollationHeader) error { if err := s.ValidateShardID(h); err != nil { return err } @@ -35,6 +35,15 @@ func (s *Shard) SetHeader(h *CollationHeader) error { return nil } +// SaveCollationBody adds the collation body to shardDB. +func (s *Shard) SaveCollationBody(body []byte) error { + // TODO: dependent on blob serialization. + // chunkRoot := getChunkRoot(body) using the blob algorithm utils. + // s.shardDB.Put(chunkRoot, body) + // s.SetAvailability(chunkRoot, true) + return nil +} + // GetHeaderByHash of collation. func (s *Shard) GetHeaderByHash(hash common.Hash) (*CollationHeader, error) { encoded, err := s.shardDB.Get(hash) @@ -61,7 +70,7 @@ func (s *Shard) GetCollationByHash(hash common.Hash) (*Collation, error) { return &Collation{header: header, body: body}, nil } -// GetBodyByChunkRoot fetches a collation body +// GetBodyByChunkRoot fetches a collation body. func (s *Shard) GetBodyByChunkRoot(chunkRoot common.Hash) ([]byte, error) { body, err := s.shardDB.Get(chunkRoot) if err != nil { @@ -75,7 +84,7 @@ func (s *Shard) CheckAvailability(header *CollationHeader) bool { return true } -// SetUnavailable ensures to set a collation as unavailable in the shardDB. -func (s *Shard) SetUnavailable(header *CollationHeader) error { +// SetAvailability saves the availability of the chunk root in the shardDB. +func (s *Shard) SetAvailability(chunkRoot *common.Hash) error { return nil }