Skip to content

Commit

Permalink
sharding/client: Change to serialize blobs(ethereum#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
nisdas committed May 16, 2018
1 parent c36155c commit c01612f
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions sharding/client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ package client

import (
"fmt"
"math"
)

type collationbody []byte

var (
collationsizelimit = int64(2 ^ 20)
collationsizelimit = int64(math.Pow(float64(2), float64(20)))
chunkSize = int64(32)
indicatorSize = int64(1)
numberOfChunks = collationsizelimit / chunkSize
chunkDataSize = chunkSize - indicatorSize
totalDatasize = numberOfChunks * chunkDataSize
)

type collationbody []byte

type body interface {
length() int64
validateblob() error
ParseBlob()
}

func (cb collationbody) length() int64 {

return int64(len(cb))
Expand All @@ -39,31 +46,48 @@ func (cb collationbody) validateBody() error {

// Parse Collation body and modify it accordingly

func (cb collationbody) ParseBlob() {
func (cb collationbody) serializeBlob() []byte {

terminalLength := cb.length() % chunkDataSize
chunksNumber := cb.length() / chunkDataSize
indicatorByte := make([]byte, 1)
indicatorByte[0] = 0
var tempbody collationbody
var tempbody []byte

// if blob is less than 31 bytes, it adds the indicator chunk and pads the remaining empty bytes to the right

if chunksNumber == 0 {
paddedbytes := make([]byte, cb.length()-terminalLength)
indicatorByte[0] = byte(terminalLength)
tempbody = append(indicatorByte, append(cb, paddedbytes...)...)
return tempbody
}

//if there is no need to pad empty bytes, then the indicator byte is added as 00011111

if terminalLength == 0 {

for i := int64(1); i < chunksNumber; i++ {
tempbody = append(tempbody, append(indicatorByte, cb[(i-1)*chunkDataSize:i*chunkDataSize]...)...)

}
indicatorByte[0] = byte(chunkDataSize)
tempbody = append(tempbody, append(indicatorByte, cb[(chunksNumber-1)*chunkDataSize:chunksNumber*chunkDataSize]...)...)
return tempbody

}

// Appends empty indicator bytes to non terminal-chunks
for i := int64(1); i <= chunksNumber; i++ {
tempbody = append(tempbody, append(indicatorByte, cb[(i-1)*chunkDataSize:i*chunkDataSize]...)...)

}
// Appends indicator bytes to terminal-chunks , and if the index of the chunk delimiter is non-zero adds it to the chunk
if terminalLength != 0 {
indicatorByte[0] = byte(terminalLength)
tempbody = append(tempbody, append(indicatorByte, cb[chunksNumber*chunkDataSize:(chunksNumber*chunkDataSize)+(terminalLength+1)]...)...)
indicatorByte[0] = byte(terminalLength)
tempbody = append(tempbody, append(indicatorByte, cb[chunksNumber*chunkDataSize:(chunksNumber*chunkDataSize)+(terminalLength+1)]...)...)
emptyBytes := make([]byte, (chunkDataSize - cb.length()))
cb = append(cb, emptyBytes...)

}
cb = tempbody
return tempbody

// Pad the collation body with empty bytes until it is equal to 1 Mib
if cb.length() < collationsizelimit {
emptyBytes := make([]byte, (collationsizelimit - cb.length()))
cb = append(cb, emptyBytes...)

}
}

0 comments on commit c01612f

Please sign in to comment.