sharding/client: Cleaning Up and adding errors(#92)

Former-commit-id: 2047d553c3226550d07852d91352ba5287b86f6f [formerly 1257adb782b252461bb9f4b176e1d7f0a09f7b92]
Former-commit-id: 75ad116eb88c6d8e0e94de65ec5c137d47e6429a
This commit is contained in:
nisdas 2018-05-02 16:57:59 +08:00
parent 1129d94303
commit efb8f3a80f

View File

@ -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
}