Skip to content

Commit

Permalink
sharding/utils : Adding RLP encoding (ethereum#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
nisdas committed May 16, 2018
1 parent eb582cb commit 798666a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 34 deletions.
39 changes: 37 additions & 2 deletions sharding/collation.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,41 @@ func (c *Collation) CalculateChunkRoot() {
c.header.data.ChunkRoot = &chunkRoot
}

func (c *Collation) CreateRawBlobs() ([]*utils.RawBlob, error) {

// It does not skip evm execution by default

blobs := make([]*utils.RawBlob, len(c.transactions))
for i, v := range c.transactions {

err := error(nil)
blobs[i], err = utils.NewRawBlob(v, false)

if err != nil {
return nil, fmt.Errorf("Creation of raw blobs from transactions failed %v", err)
}

}

return blobs, nil

}

// Serialize method serializes the collation body
func (c *Collation) Serialize() ([]byte, error) {

blob, err := utils.ConvertToRawBlob(c.transactions)
/*blob, err := utils.ConvertToRawBlob(c.transactions)
if err != nil {
return nil, fmt.Errorf("%v", err)
}**/

blobs, err := c.CreateRawBlobs()

if err != nil {
return nil, fmt.Errorf("%v", err)
}

serializedtx, err := utils.Serialize(blob)
serializedtx, err := utils.Serialize(blobs)

if err != nil {
return nil, fmt.Errorf("%v", err)
Expand All @@ -137,3 +163,12 @@ func (c *Collation) Serialize() ([]byte, error) {
return serializedtx, nil

}

func (c *Collation) Deserialize(serialisedblob []byte) error {
var blobs []utils.RawBlob

deserializedblobs, err := utils.Deserialize(serialisedblob)
if err != nil {
return fmt.Errorf("%v", err)
}
}
21 changes: 20 additions & 1 deletion sharding/collation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package sharding

import (
"math/big"
//"github.com/ethereum/go-ethereum/rlp"
//"reflect"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -52,8 +54,25 @@ func TestSerialize(t *testing.T) {
c.AddTransaction(tx)
}

results, err := c.Serialize()
/*var tests *types.Transaction
yadd := reflect.ValueOf(*c.transactions[3])
d := yadd.FieldByName("data").FieldByName("Hash")
test, err := rlp.EncodeToBytes(c.transactions[3])
if err != nil {
t.Fatalf("%v\n %v\n %v", err, test, *(c.transactions[0]))
}
erx := rlp.DecodeBytes(test, &tests)
dd := reflect.ValueOf(*tests)
cv := dd.FieldByName("data").FieldByName("Hash")
if cv != d {
t.Fatalf("%v\n %v\n %v", erx, cv, d)
} */

results, err := c.Serialize()
if err == nil {
t.Fatalf("%v\n %v\n %v", err, results, c.transactions)
}

Expand Down
36 changes: 16 additions & 20 deletions sharding/utils/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"github.com/ethereum/go-ethereum/rlp"
)

var (
Expand All @@ -21,27 +22,22 @@ type RawBlob struct {
data []byte
}

/*
* Might be needed in part 2 of serialisation
* ConvertInterface converts inputted interface to the required type of interface, ex: slice.
func ConvertInterfacetoBytes(arg interface{}) ([]byte, error) {
val := reflect.ValueOf(arg)
if val.Kind() == reflect.Slice {
length := val.Len()
newtype := make([]byte, length)
for i := 0; i < length; i++ {
newtype[i] = val.Index(i).Interface().(byte)
}
return newtype, nil
func NewRawBlob(i interface{}, skipevm bool) (*RawBlob, error) {
data, err := rlp.EncodeToBytes(i)
if err != nil {
return nil, fmt.Errorf("RLP encoding was a failure:%v", err)
}
return &RawBlob{data: data, flags: Flags{skipEvmExecution: skipevm}}, nil
}

func ConvertfromRawBlob(blob RawBlob, i interface{}) error {
err := rlp.DecodeBytes(blob.data, i)
if err != nil {
return fmt.Errorf("RLP decoding was a failure:%v", err)
}
err := errors.New("Interface Conversion a failure")
return nil, err

return nil
}
*/

// ConvertToRawBlob will convert any supported type into a the RawBlob type.
func ConvertToRawBlob(arg interface{}) ([]RawBlob, error) {
Expand All @@ -50,7 +46,7 @@ func ConvertToRawBlob(arg interface{}) ([]RawBlob, error) {
}

// serializeBlob parses the blob and serializes it appropriately.
func serializeBlob(cb RawBlob) ([]byte, error) {
func SerializeBlob(cb RawBlob) ([]byte, error) {

length := int64(len(cb.data))
terminalLength := length % chunkDataSize
Expand Down Expand Up @@ -133,7 +129,7 @@ func serializeBlob(cb RawBlob) ([]byte, error) {
}

// Serialize takes a set of blobs and converts them to a single byte array.
func Serialize(rawblobs []RawBlob) ([]byte, error) {
func Serialize(rawblobs []*RawBlob) ([]byte, error) {
length := int64(len(rawblobs))

serialisedData := []byte{}
Expand All @@ -142,7 +138,7 @@ func Serialize(rawblobs []RawBlob) ([]byte, error) {
for i := int64(0); i < length; i++ {

data := rawblobs[i]
refinedData, err := serializeBlob(data)
refinedData, err := SerializeBlob(*data)
if err != nil {
return nil, fmt.Errorf("Index %v : %v", i, err)
}
Expand Down
11 changes: 0 additions & 11 deletions sharding/utils/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ func buildblob(size int64) []byte {

}

/*
Might be required in the future for part 2 of serialization
func TestConvertInterface(t *testing.T) {
slice := []interface{}{0, 1, 2, 3, 4, 5}
convertedValue, err := ConvertInterface(slice, reflect.Slice)
if err != nil {
t.Fatalf("Error: %v %v", err, convertedValue)
}
} */
func TestSize(t *testing.T) {
for i := 0; i < 300; i++ {
size := int64(i)
Expand Down

0 comments on commit 798666a

Please sign in to comment.