Skip to content

Commit

Permalink
sharding/client: Cleaning Up and adding errors(ethereum#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
nisdas committed May 16, 2018
1 parent c1ff070 commit bc174f3
Showing 1 changed file with 58 additions and 59 deletions.
117 changes: 58 additions & 59 deletions sharding/client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"math"
"reflect"

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

var (
Expand All @@ -19,7 +21,6 @@ type txblob []byte

type blob interface {
length() int64
validateBody() error
serializeBlob() []byte
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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

}

0 comments on commit bc174f3

Please sign in to comment.