From 1c3387fca703de0fac9a54ff2bf9c0f83187eaff Mon Sep 17 00:00:00 2001 From: Eli Date: Wed, 23 May 2018 10:22:49 -0700 Subject: [PATCH] sharding: merklize body into chunkroot and save in db Former-commit-id: 30327145ac18f8822bdfc661c50591aa33917547 [formerly bc513e68023723a4d848d2e773e77efeb7ddc9df] Former-commit-id: 3da18bf84b164cab555f5bde8f3cf5ae19b18b26 --- sharding/collation.go | 17 +++++++++++++++++ sharding/shard.go | 13 ++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/sharding/collation.go b/sharding/collation.go index 3ce4b55e3..dfa923430 100644 --- a/sharding/collation.go +++ b/sharding/collation.go @@ -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 +} diff --git a/sharding/shard.go b/sharding/shard.go index 3923c4b63..cf967745b 100644 --- a/sharding/shard.go +++ b/sharding/shard.go @@ -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) }