11package utils
22
33import (
4- "errors"
54 "fmt"
6- "reflect"
75)
86
97var (
@@ -12,16 +10,20 @@ var (
1210 chunkDataSize = chunkSize - indicatorSize
1311)
1412
13+ // Flags to add to chunk delimiter.
1514type Flags struct {
1615 skipEvmExecution bool
1716}
1817
18+ // RawBlob type which will contain flags and data for serialization.
1919type RawBlob struct {
2020 flags Flags
2121 data []byte
2222}
2323
24- // ConvertInterface converts inputted interface to the required type of interface, ex: slice.
24+ /*
25+ * Might be needed in part 2 of serialisation
26+ * ConvertInterface converts inputted interface to the required type of interface, ex: slice.
2527func ConvertInterfacetoBytes(arg interface{}) ([]byte, error) {
2628 val := reflect.ValueOf(arg)
2729 if val.Kind() == reflect.Slice {
@@ -39,26 +41,12 @@ func ConvertInterfacetoBytes(arg interface{}) ([]byte, error) {
3941 return nil, err
4042
4143}
44+ */
4245
43- func convertbyteToInterface (arg []byte ) []interface {} {
44- length := int64 (len (arg ))
45- newtype := make ([]interface {}, length )
46- for i , v := range arg {
47- newtype [i ] = v
48- }
49-
50- return newtype
51- }
52-
53- func ConvertToInterface (arg interface {}) []interface {} {
54- val := reflect .ValueOf (arg )
55- length := val .Len ()
56- newtype := make ([]interface {}, length )
57- for i := 0 ; i < length ; i ++ {
58- newtype [i ] = val .Index (i )
59- }
60-
61- return newtype
46+ // ConvertToRawBlob will convert any supported type into a the RawBlob type.
47+ func ConvertToRawBlob (arg interface {}) ([]RawBlob , error ) {
48+ //TODO: will be done in part 2 to convert any type to a rawBlob
49+ return nil , nil
6250}
6351
6452// serializeBlob parses the blob and serializes it appropriately.
@@ -148,9 +136,6 @@ func serializeBlob(cb RawBlob) ([]byte, error) {
148136func Serialize (rawblobs []RawBlob ) ([]byte , error ) {
149137 length := int64 (len (rawblobs ))
150138
151- if length == 0 {
152- return nil , fmt .Errorf ("Validation failed: Collation Body has to be a non-zero value" )
153- }
154139 serialisedData := []byte {}
155140
156141 //Loops through all the blobs and serializes them into chunks
@@ -168,9 +153,9 @@ func Serialize(rawblobs []RawBlob) ([]byte, error) {
168153}
169154
170155// Deserialize results in the byte array being deserialised and separated into its respective interfaces.
171- func Deserialize (collationbody []byte ) ([]RawBlob , error ) {
156+ func Deserialize (data []byte ) ([]RawBlob , error ) {
172157
173- length := int64 (len (collationbody ))
158+ length := int64 (len (data ))
174159 chunksNumber := length / chunkSize
175160 indicatorByte := byte (0 )
176161 tempbody := RawBlob {}
@@ -182,28 +167,28 @@ func Deserialize(collationbody []byte) ([]RawBlob, error) {
182167
183168 // Tests if the chunk delimiter is zero, if it is it will append the data chunk
184169 // to tempbody
185- if collationbody [indicatorIndex ] == indicatorByte || collationbody [indicatorIndex ] == byte (128 ) {
186- tempbody .data = append (tempbody .data , collationbody [(indicatorIndex + 1 ):(i )* chunkSize ]... )
170+ if data [indicatorIndex ] == indicatorByte || data [indicatorIndex ] == byte (128 ) {
171+ tempbody .data = append (tempbody .data , data [(indicatorIndex + 1 ):(i )* chunkSize ]... )
187172
188- } else if collationbody [indicatorIndex ] == byte (31 ) || collationbody [indicatorIndex ] == byte (159 ) {
189- if collationbody [indicatorIndex ] == byte (159 ) {
173+ } else if data [indicatorIndex ] == byte (31 ) || data [indicatorIndex ] == byte (159 ) {
174+ if data [indicatorIndex ] == byte (159 ) {
190175 tempbody .flags .skipEvmExecution = true
191176 }
192- tempbody .data = append (tempbody .data , collationbody [(indicatorIndex + 1 )] )
177+ tempbody .data = append (tempbody .data , data [(indicatorIndex + 1 ): indicatorIndex + 1 + chunkDataSize ] ... )
193178 deserializedblob = append (deserializedblob , tempbody )
194179 tempbody = RawBlob {}
195180
196181 } else {
197182 // Since the chunk delimiter in non-zero now we can infer that it is a terminal chunk and
198183 // add it and append to the deserializedblob slice. The tempbody signifies a single deserialized blob
199- terminalIndex := int64 (collationbody [indicatorIndex ])
184+ terminalIndex := int64 (data [indicatorIndex ])
200185 //Check if EVM flag is equal to 1
201- flagindex := collationbody [indicatorIndex ] >> 7
186+ flagindex := data [indicatorIndex ] >> 7
202187 if flagindex == byte (1 ) {
203- terminalIndex = int64 (collationbody [indicatorIndex ]) - 128
188+ terminalIndex = int64 (data [indicatorIndex ]) - 128
204189 tempbody .flags .skipEvmExecution = true
205190 }
206- tempbody .data = append (tempbody .data , collationbody [(indicatorIndex + 1 ):(indicatorIndex + 1 + terminalIndex )]... )
191+ tempbody .data = append (tempbody .data , data [(indicatorIndex + 1 ):(indicatorIndex + 1 + terminalIndex )]... )
207192 deserializedblob = append (deserializedblob , tempbody )
208193 tempbody = RawBlob {}
209194
0 commit comments