Skip to content

Commit

Permalink
sharding/client: Cleaning up and refining tests(#92)
Browse files Browse the repository at this point in the history
Former-commit-id: 45ebe18e2281496741d6b08fae04419a55f008c6 [formerly 5473045]
Former-commit-id: 4df6d79
  • Loading branch information
nisdas committed May 16, 2018
1 parent e0282e9 commit c25de51
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
29 changes: 18 additions & 11 deletions sharding/client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"fmt"
"math"
"reflect"
//"runtime"

"github.com/ethereum/go-ethereum/log"
)

var (
Expand Down Expand Up @@ -39,14 +36,25 @@ func convertbyteToInterface(arg []byte) []interface{} {
return newtype
}

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

return newtype
}

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

blob, err := convertInterface(cb, reflect.Slice)
interfaceblob, err := convertInterface(cb, reflect.Slice)
if err != nil {
return nil, fmt.Errorf("Error: %v", err)
}
length := int64(blob.Len())
blob := interfacetoByte(interfaceblob.Interface().([]interface{}))
length := int64(len(blob))
terminalLength := length % chunkDataSize
chunksNumber := length / chunkDataSize
finalchunkIndex := length - 1
Expand All @@ -59,7 +67,7 @@ func serializeBlob(cb interface{}) ([]byte, error) {
if chunksNumber == 0 {
paddedbytes := make([]byte, (length - terminalLength))
indicatorByte[0] = byte(terminalLength)
tempbody = append(indicatorByte, append(blob.Bytes(), paddedbytes...)...)
tempbody = append(indicatorByte, append(blob, paddedbytes...)...)
return tempbody, nil
}

Expand All @@ -74,15 +82,15 @@ func serializeBlob(cb interface{}) ([]byte, error) {
// 31
tempbody = append(tempbody,
append(indicatorByte,
blob.Bytes()[(i-1)*chunkDataSize:i*chunkDataSize]...)...)
blob[(i-1)*chunkDataSize:i*chunkDataSize]...)...)

}
indicatorByte[0] = byte(chunkDataSize)

// Terminal chunk has its indicator byte added, chunkDataSize*chunksNumber refers to the total size of the blob
tempbody = append(tempbody,
append(indicatorByte,
blob.Bytes()[(chunksNumber-1)*chunkDataSize:chunkDataSize*chunksNumber]...)...)
blob[(chunksNumber-1)*chunkDataSize:chunkDataSize*chunksNumber]...)...)

return tempbody, nil

Expand All @@ -96,7 +104,7 @@ func serializeBlob(cb interface{}) ([]byte, error) {

tempbody = append(tempbody,
append(indicatorByte,
blob.Bytes()[(i-1)*chunkDataSize:i*chunkDataSize]...)...)
blob[(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.
Expand All @@ -105,7 +113,7 @@ func serializeBlob(cb interface{}) ([]byte, error) {
indicatorByte[0] = byte(terminalLength)
tempbody = append(tempbody,
append(indicatorByte,
blob.Bytes()[chunkDataSize*chunksNumber-1:finalchunkIndex]...)...)
blob[chunkDataSize*chunksNumber-1:finalchunkIndex]...)...)

emptyBytes := make([]byte, (chunkDataSize - terminalLength))
tempbody = append(tempbody, emptyBytes...)
Expand Down Expand Up @@ -135,7 +143,6 @@ func Serialize(rawtx []interface{}) ([]byte, error) {
serialisedData = append(serialisedData, refinedData...)

if int64(len(serialisedData)) > collationsizelimit {
log.Info(fmt.Sprintf("The total number of interfaces added to the collation body are: %d", i))
serialisedData = serialisedData[:blobLength]
return serialisedData, nil

Expand Down
15 changes: 6 additions & 9 deletions sharding/client/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"math/rand"
"reflect"
//"runtime"
"testing"
)

Expand Down Expand Up @@ -39,10 +38,10 @@ func TestConvertInterface(t *testing.T) {

}
func TestSize(t *testing.T) {
size := int64(20)
size := int64(84)
blob := buildtxblobs(size)
chunksafterSerialize := size / chunkDataSize
terminalchunk := size % chunkDataSize
chunksafterSerialize := size * size / chunkDataSize
terminalchunk := size * size % chunkDataSize
if terminalchunk != 0 {
chunksafterSerialize = chunksafterSerialize + 1
}
Expand All @@ -59,17 +58,15 @@ func TestSize(t *testing.T) {
}

}
func TestSerializeblob(t *testing.T) {
func TestSerializeAndDeserializeblob(t *testing.T) {

blob := buildblob(200)
blob := buildtxblobs(31)

serializedblob, err := serializeBlob(blob)
serializedblob, err := Serialize(blob)

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

0 comments on commit c25de51

Please sign in to comment.