diff --git a/sharding/client/utils.go b/sharding/client/utils.go index c0d2c13fd..ec5efe9ee 100644 --- a/sharding/client/utils.go +++ b/sharding/client/utils.go @@ -4,6 +4,8 @@ import ( "fmt" "math" "reflect" + + "github.com/ethereum/go-ethereum/log" ) var ( @@ -19,7 +21,6 @@ type txblob []byte type blob interface { length() int64 - validateBody() error serializeBlob() []byte } @@ -28,64 +29,6 @@ func (cb txblob) length() int64 { return int64(len(cb)) } -/* Validate that the collation body is within its bounds and if -the size of the body is below the limit it is simply appended -till it reaches the required limit */ - -func (cb txblob) validateBody() error { - - if cb.length() == 0 { - return fmt.Errorf("Collation Body has to be a non-zero value") - } - - if cb.length() > totalDatasize { - return fmt.Errorf("Collation Body is over the size limit") - } - - return nil -} - -func deserializebody(collationbody []byte) []blob { - length := int64(len(collationbody)) - chunksNumber := chunkSize / length - indicatorByte := make([]byte, 1) - indicatorByte[0] = 0 - txblobs := []blob{} - var tempbody txblob - - for i := int64(1); i <= chunksNumber; i++ { - - if reflect.TypeOf(collationbody[(i-1)*chunkSize]) == reflect.TypeOf(indicatorByte) { - tempbody = append(tempbody, collationbody[((i-1)*chunkSize+1):(i)*chunkSize]...) - - } else { - terminalIndex := int64(collationbody[(i-1)*chunkSize]) - tempbody = append(tempbody, collationbody[((i-1)*chunkSize+1):((i-1)*chunkSize+2+terminalIndex)]...) - txblobs = append(txblobs, tempbody) - tempbody = txblob{} - - } - - } - - return txblobs - -} - -func serialize(rawtx []blob) []byte { - length := int64(len(rawtx)) - serialisedData := []byte{} - - for i := int64(1); i < length; i++ { - data := rawtx[length].(txblob) - refinedData := data.serializeBlob() - serialisedData = append(serialisedData, refinedData...) - txblob(serialisedData).validateBody() - - } - return serialisedData -} - // Parse blob and modify it accordingly func (cb txblob) serializeBlob() []byte { @@ -133,3 +76,59 @@ func (cb txblob) serializeBlob() []byte { return tempbody } + +func Serialize(rawtx []blob) ([]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{} + + for i := int64(0); i < length; i++ { + + blobLength := txblob(serialisedData).length() + data := rawtx[i].(txblob) + refinedData := data.serializeBlob() + serialisedData = append(serialisedData, refinedData...) + + if txblob(serialisedData).length() > collationsizelimit { + log.Info(fmt.Sprintf("The total number of interfaces added to the collation body are: %d", i)) + serialisedData = serialisedData[:blobLength] + return serialisedData, nil + + } + + } + return serialisedData, nil +} + +// Collation body deserialised and separated into its respective interfaces + +func Deserializebody(collationbody []byte) []blob { + + length := int64(len(collationbody)) + chunksNumber := chunkSize / length + indicatorByte := make([]byte, 1) + indicatorByte[0] = 0 + txblobs := []blob{} + var tempbody txblob + + for i := int64(1); i <= chunksNumber; i++ { + + if reflect.TypeOf(collationbody[(i-1)*chunkSize]) == reflect.TypeOf(indicatorByte) { + tempbody = append(tempbody, collationbody[((i-1)*chunkSize+1):(i)*chunkSize]...) + + } else { + terminalIndex := int64(collationbody[(i-1)*chunkSize]) + tempbody = append(tempbody, collationbody[((i-1)*chunkSize+1):((i-1)*chunkSize+2+terminalIndex)]...) + txblobs = append(txblobs, tempbody) + tempbody = txblob{} + + } + + } + + return txblobs + +}