Skip to content

Commit

Permalink
sharding/client: Make serialization similar to JSON marshalling(ether…
Browse files Browse the repository at this point in the history
  • Loading branch information
nisdas committed May 16, 2018
1 parent bc174f3 commit 19aa0eb
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions sharding/client/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"errors"
"fmt"
"math"
"reflect"
Expand All @@ -17,77 +18,80 @@ var (
totalDatasize = numberOfChunks * chunkDataSize
)

type txblob []byte
func convertInterface(arg interface{}, kind reflect.Kind) (reflect.Value, error) {
val := reflect.ValueOf(arg)
if val.Kind() == kind {
return val, nil

type blob interface {
length() int64
serializeBlob() []byte
}

func (cb txblob) length() int64 {

return int64(len(cb))
}
err := errors.New("Interface Conversion a failure")
return val, err
}

// Parse blob and modify it accordingly

func (cb txblob) serializeBlob() []byte {
func serializeBlob(cb interface{}) ([]byte, error) {

terminalLength := cb.length() % chunkDataSize
chunksNumber := cb.length() / chunkDataSize
blob, err := convertInterface(cb, reflect.Slice)
if err != nil {
return nil, fmt.Errorf("Error: %v", err)
}
length := int64(blob.Len())
terminalLength := length % chunkDataSize
chunksNumber := length / chunkDataSize
indicatorByte := make([]byte, 1)
indicatorByte[0] = 0
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)
paddedbytes := make([]byte, length-terminalLength)
indicatorByte[0] = byte(terminalLength)
tempbody = append(indicatorByte, append(cb, paddedbytes...)...)
return tempbody
tempbody = append(indicatorByte, append(cb.([]byte), paddedbytes...)...)
return tempbody, nil
}

//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]...)...)
tempbody = append(tempbody, append(indicatorByte, blob.Bytes()[(i-1)*chunkDataSize:i*chunkDataSize]...)...)

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

}

// 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]...)...)
tempbody = append(tempbody, append(indicatorByte, blob.Bytes()[(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
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...)
tempbody = append(tempbody, append(indicatorByte, blob.Bytes()[chunksNumber*chunkDataSize:(chunksNumber*chunkDataSize)+(terminalLength+1)]...)...)
emptyBytes := make([]byte, (chunkDataSize - terminalLength))
tempbody = append(tempbody, emptyBytes...)

return tempbody
return tempbody, nil

}

func Serialize(rawtx []blob) ([]byte, error) {
func Serialize(rawtx []interface{}) ([]byte, error) {
length := int64(len(rawtx))

if length == 0 {
return nil, fmt.Errorf("Validation failed: Collation Body has to be a non-zero value")
}
serialisedData := []byte{}
serialisedData := []byte{0, 2}

for i := int64(0); i < length; i++ {

blobLength := txblob(serialisedData).length()
blobLength := length(serialisedData)
data := rawtx[i].(txblob)
refinedData := data.serializeBlob()
serialisedData = append(serialisedData, refinedData...)
Expand Down

0 comments on commit 19aa0eb

Please sign in to comment.