From e46714c9a88712f3fef536652a960bb1c3af3883 Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 8 May 2018 15:34:48 +0800 Subject: [PATCH] sharding/utils: Fixed serialization tests(#92) Former-commit-id: 7bc42f4bf78f03ddecd07d309699e5771bf2e501 [formerly 9367511015a33dd13d1c605c7d4da94617a0138d] Former-commit-id: 0dd05673b86cee96f5b5d436b58584d9c78c3e0b --- sharding/utils/marshal.go | 9 +++++++-- sharding/utils/marshal_test.go | 5 +++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/sharding/utils/marshal.go b/sharding/utils/marshal.go index bc9e70dab..e0bcf8262 100644 --- a/sharding/utils/marshal.go +++ b/sharding/utils/marshal.go @@ -58,7 +58,6 @@ func serializeBlob(cb interface{}) ([]byte, error) { length := int64(len(blob)) terminalLength := length % chunkDataSize chunksNumber := length / chunkDataSize - finalchunkIndex := length - 1 indicatorByte := make([]byte, 1) indicatorByte[0] = 0 tempbody := []byte{} @@ -114,7 +113,7 @@ func serializeBlob(cb interface{}) ([]byte, error) { indicatorByte[0] = byte(terminalLength) tempbody = append(tempbody, append(indicatorByte, - blob[chunkDataSize*chunksNumber-1:finalchunkIndex]...)...) + blob[chunkDataSize*chunksNumber:length]...)...) emptyBytes := make([]byte, (chunkDataSize - terminalLength)) tempbody = append(tempbody, emptyBytes...) @@ -172,6 +171,12 @@ func Deserialize(collationbody []byte, rawtx interface{}) error { // 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 if collationbody[indicatorIndex] == byte(1) { + + tempbody = append(tempbody, collationbody[(indicatorIndex+1)]) + deserializedblob = append(deserializedblob, convertbyteToInterface(tempbody)) + tempbody = []byte{} + } else { terminalIndex := int64(collationbody[indicatorIndex]) tempbody = append(tempbody, collationbody[(indicatorIndex+1):(indicatorIndex+1+terminalIndex)]...) diff --git a/sharding/utils/marshal_test.go b/sharding/utils/marshal_test.go index 152531de9..a9b8fd13a 100644 --- a/sharding/utils/marshal_test.go +++ b/sharding/utils/marshal_test.go @@ -3,6 +3,7 @@ package utils import ( "math/rand" "reflect" + //"runtime" "testing" ) @@ -61,7 +62,7 @@ func TestSerializeAndDeserializeblob(t *testing.T) { var testbody interface{} - blob := buildtxblobs(31) + blob := buildtxblobs(1000) serializedblob, err := Serialize(blob) @@ -75,7 +76,7 @@ func TestSerializeAndDeserializeblob(t *testing.T) { if !reflect.DeepEqual(blob, testbody) { - t.Fatalf("Error Serializing blobs, the serialized and deserialized versions are not the same: %v ------ %v", blob, testbody) + t.Fatalf("Error Serializing blobs, the serialized and deserialized versions are not the same: %v ------%v ------ %v", blob, serializedblob, testbody) } }