Skip to content

Commit

Permalink
sharding/client: Adding tests and modifying utils(#92)
Browse files Browse the repository at this point in the history
Former-commit-id: edbf5bd3d11b46f463bf26027178fb7d06fc13fb [formerly 73e4692]
Former-commit-id: ca76a1e
  • Loading branch information
nisdas committed May 16, 2018
1 parent 2026d42 commit 1df5683
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
32 changes: 22 additions & 10 deletions sharding/client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math"
"reflect"
//"runtime"

"github.com/ethereum/go-ethereum/log"
)
Expand All @@ -15,7 +16,6 @@ var (
indicatorSize = int64(1)
numberOfChunks = collationsizelimit / chunkSize
chunkDataSize = chunkSize - indicatorSize
totalDatasize = numberOfChunks * chunkDataSize
)

// convertInterface converts inputted interface to the required type, ex: slice.
Expand All @@ -29,6 +29,16 @@ func convertInterface(arg interface{}, kind reflect.Kind) (reflect.Value, error)
return val, err
}

func convertbyteToInterface(arg []byte) []interface{} {
length := int64(len(arg))
newtype := make([]interface{}, length)
for i, v := range arg {
newtype[i] = v
}

return newtype
}

// serializeBlob parses the blob and serializes it appropriately.
func serializeBlob(cb interface{}) ([]byte, error) {

Expand All @@ -39,7 +49,7 @@ func serializeBlob(cb interface{}) ([]byte, error) {
length := int64(blob.Len())
terminalLength := length % chunkDataSize
chunksNumber := length / chunkDataSize
finalchunkIndex := totalDatasize + (terminalLength + 1)
finalchunkIndex := length - 1
indicatorByte := make([]byte, 1)
indicatorByte[0] = 0
tempbody := []byte{}
Expand Down Expand Up @@ -81,6 +91,7 @@ func serializeBlob(cb interface{}) ([]byte, error) {
// This loop loops through all non-terminal chunks and add a indicator byte of 00000000, each chunk
// is created by appending the indcator byte to the data chunks. The data chunks are separated into sets of
// 31

for i := int64(1); i <= chunksNumber; i++ {

tempbody = append(tempbody,
Expand All @@ -91,11 +102,10 @@ func serializeBlob(cb interface{}) ([]byte, error) {
// Appends indicator bytes to terminal-chunks , and if the index of the chunk delimiter is non-zero adds it to the chunk.
// Also pads empty bytes to the terminal chunk.chunkDataSize*chunksNumber refers to the total size of the blob.
// finalchunkIndex refers to the index of the last data byte

indicatorByte[0] = byte(terminalLength)
tempbody = append(tempbody,
append(indicatorByte,
blob.Bytes()[chunkDataSize*chunksNumber:finalchunkIndex]...)...)
blob.Bytes()[chunkDataSize*chunksNumber-1:finalchunkIndex]...)...)

emptyBytes := make([]byte, (chunkDataSize - terminalLength))
tempbody = append(tempbody, emptyBytes...)
Expand Down Expand Up @@ -139,31 +149,33 @@ func Serialize(rawtx []interface{}) ([]byte, error) {
func Deserializebody(collationbody []byte, rawtx []interface{}) error {

length := int64(len(collationbody))
chunksNumber := chunkSize / length
indicatorByte := make([]byte, 1)
indicatorByte[0] = 0
chunksNumber := length / chunkSize
indicatorByte := byte(0)
tempbody := []byte{}
deserializedblob := []byte{}

// This separates the collation body into its respective transaction blobs
for i := int64(1); i <= chunksNumber; i++ {
for i := int64(1); i <= chunksNumber+1; i++ {
indicatorIndex := (i - 1) * chunkSize
// Tests if the chunk delimiter is zero, if it is it will append the data chunk
// to tempbody
if collationbody[indicatorIndex] == indicatorByte[0] {
if collationbody[indicatorIndex] == indicatorByte {
tempbody = append(tempbody, collationbody[(indicatorIndex+1):(i)*chunkSize]...)

// Since the chunk delimiter in non-zero now we can infer that it is a terminal chunk and
// add it and append to the rawtx slice. The tempbody signifies a deserialized blob
} else {
terminalIndex := int64(collationbody[indicatorIndex])
tempbody = append(tempbody, collationbody[(indicatorIndex+1):(indicatorIndex+2+terminalIndex)]...)
rawtx = append(rawtx, tempbody)
deserializedblob = append(deserializedblob, tempbody...)
tempbody = []byte{}

}

}

rawtx = convertbyteToInterface(deserializedblob)

return nil

}
53 changes: 53 additions & 0 deletions sharding/client/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package client

import (
"math/rand"
"reflect"
"runtime"
"testing"
)

var testbody []interface{}

func buildblob() []byte {

tempbody := make([]byte, 500)
for i := int64(0); i < 500; i++ {
tempbody[i] = byte(rand.Int())

}

return tempbody

}
func TestConvertInterface(t *testing.T) {
var slice interface{}
slice = []interface{}{0, 1, 2, 3, 4, 5}
convertedValue, err := convertInterface(slice, reflect.Slice)
if err != nil {
t.Fatalf("Error: %v %v", err, convertedValue)
}

}

func TestSerializeblob(t *testing.T) {

blob := buildblob()

serializedblob, err := serializeBlob(blob)

if err != nil {
t.Fatalf("Error Serializing blob:%v %v", err, serializedblob)
}
runtime.Breakpoint()
err2 := Deserializebody(serializedblob, testbody)
if err2 != nil {
t.Fatalf("Error Serializing blob:%v", err2)
}

if !reflect.DeepEqual(blob, testbody) {

t.Fatalf("Error Serializing blob with %v %v", blob, testbody)
}

}

0 comments on commit 1df5683

Please sign in to comment.