sharding: merklize body into chunkroot and save in db

Former-commit-id: 30327145ac18f8822bdfc661c50591aa33917547 [formerly bc513e68023723a4d848d2e773e77efeb7ddc9df]
Former-commit-id: 3da18bf84b164cab555f5bde8f3cf5ae19b18b26
This commit is contained in:
Eli 2018-05-23 10:22:49 -07:00
parent 24ad83c17d
commit 1c3387fca7
2 changed files with 25 additions and 5 deletions

View File

@ -190,3 +190,20 @@ func Deserialize(serialisedBlob []byte) (*[]*types.Transaction, error) {
return &txs, nil
}
// Chunks is a wrapper around a chunk array to implement DerivableList,
// which allows us to Merklize the chunks into the chunkRoot
type Chunks []byte
// Len returns the number of chunks in this list.
func (ch Chunks) Len() int { return len(ch) }
// GetRlp returns the RLP encoding of one chunk from the list.
func (ch Chunks) GetRlp(i int) []byte {
bytes, err := rlp.EncodeToBytes(ch[i])
if err != nil {
panic(err)
}
return bytes
}

View File

@ -6,6 +6,7 @@ import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
)
@ -179,11 +180,13 @@ func (s *Shard) SaveHeader(header *CollationHeader) error {
// SaveBody adds the collation body to the shardDB and sets availability.
func (s *Shard) SaveBody(body []byte) error {
// TODO: check if body is empty and throw error.
// TODO: dependent on blob serialization.
// 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)
// check if body is empty and throw error.
if body == nil {
return fmt.Errorf("body is empty")
}
chunks := Chunks(body)
chunkRoot := types.DeriveSha(chunks) // merklize the serialized blobs
s.SetAvailability(&chunkRoot, true)
return s.shardDB.Put(chunkRoot, body)
}