Skip to content

Commit eb582cb

Browse files
author
nisdas
committed
sharding: Fix tests and clean up(ethereum#92)
1 parent 4470434 commit eb582cb

File tree

4 files changed

+60
-64
lines changed

4 files changed

+60
-64
lines changed

sharding/collation.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ func (c *Collation) CalculateChunkRoot() {
118118
// Serialize method serializes the collation body
119119
func (c *Collation) Serialize() ([]byte, error) {
120120

121-
blob := utils.ConvertToInterface(c.transactions)
121+
blob, err := utils.ConvertToRawBlob(c.transactions)
122+
if err != nil {
123+
return nil, fmt.Errorf("%v", err)
124+
}
122125

123126
serializedtx, err := utils.Serialize(blob)
124127

sharding/collation_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ func TestCollation_Transactions(t *testing.T) {
2727
}
2828
}
2929

30+
//TODO: Add test for converting *types.Transaction into raw blobs
31+
32+
//Tests thta Transactions can be serialised
3033
func TestSerialize(t *testing.T) {
3134
tests := []struct {
3235
transactions []*types.Transaction
@@ -51,7 +54,7 @@ func TestSerialize(t *testing.T) {
5154

5255
results, err := c.Serialize()
5356
if err != nil {
54-
t.Fatalf("%v ----%v---%v", err, results, c.transactions)
57+
t.Fatalf("%v\n %v\n %v", err, results, c.transactions)
5558
}
5659

5760
}

sharding/utils/marshal.go

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package utils
22

33
import (
4-
"errors"
54
"fmt"
6-
"reflect"
75
)
86

97
var (
@@ -12,16 +10,20 @@ var (
1210
chunkDataSize = chunkSize - indicatorSize
1311
)
1412

13+
// Flags to add to chunk delimiter.
1514
type Flags struct {
1615
skipEvmExecution bool
1716
}
1817

18+
// RawBlob type which will contain flags and data for serialization.
1919
type 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.
2527
func 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) {
148136
func 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

sharding/utils/marshal_test.go

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,44 +48,49 @@ func TestConvertInterface(t *testing.T) {
4848
4949
} */
5050
func TestSize(t *testing.T) {
51-
size := int64(8)
52-
blob := buildrawblob(size)
53-
chunksafterSerialize := size / chunkDataSize
54-
terminalchunk := size % chunkDataSize
55-
if terminalchunk != 0 {
56-
chunksafterSerialize = chunksafterSerialize + 1
57-
}
58-
chunksafterSerialize = chunksafterSerialize * size
59-
sizeafterSerialize := chunksafterSerialize * chunkSize
60-
serializedblob, err := Serialize(blob)
61-
if err != nil {
62-
t.Fatalf("Error Serializing blob:%v %v", err, serializedblob)
63-
}
51+
for i := 0; i < 300; i++ {
52+
size := int64(i)
53+
blob := buildrawblob(size)
54+
chunksafterSerialize := size / chunkDataSize
55+
terminalchunk := size % chunkDataSize
56+
if terminalchunk != 0 {
57+
chunksafterSerialize = chunksafterSerialize + 1
58+
}
59+
chunksafterSerialize = chunksafterSerialize * size
60+
sizeafterSerialize := chunksafterSerialize * chunkSize
61+
serializedblob, err := Serialize(blob)
62+
if err != nil {
63+
t.Errorf("Error Serializing blob:%v\n %v", err, serializedblob)
64+
}
6465

65-
if int64(len(serializedblob)) != sizeafterSerialize {
66+
if int64(len(serializedblob)) != sizeafterSerialize {
6667

67-
t.Fatalf("Error Serializing blobs the lengths are not the same: %v , %v", int64(len(serializedblob)), sizeafterSerialize)
68+
t.Errorf("Error Serializing blobs the lengths are not the same:\n %d \n %d", int64(len(serializedblob)), sizeafterSerialize)
6869

70+
}
6971
}
7072

7173
}
7274
func TestSerializeAndDeserializeblob(t *testing.T) {
7375

74-
blob := buildrawblob(330)
76+
for i := 1; i < 300; i++ {
7577

76-
serializedblob, err := Serialize(blob)
78+
blob := buildrawblob(int64(i))
7779

78-
if err != nil {
79-
t.Fatalf("Error Serializing blob:%v %v", err, serializedblob)
80-
}
81-
raw, err2 := Deserialize(serializedblob)
82-
if err2 != nil {
83-
t.Fatalf("Error Serializing blob:%v due to %v", raw, err2)
84-
}
80+
serializedblob, err := Serialize(blob)
81+
82+
if err != nil {
83+
t.Errorf("Error Serializing blob at index %d:\n%v\n%v", i, err, serializedblob)
84+
}
85+
raw, err2 := Deserialize(serializedblob)
86+
if err2 != nil {
87+
t.Errorf("Error Serializing blob at index %d:\n%v due to \n%v", i, raw, err2)
88+
}
8589

86-
if !reflect.DeepEqual(blob, raw) {
90+
if !reflect.DeepEqual(blob, raw) {
8791

88-
t.Fatalf("Error Serializing blobs, the serialized and deserialized versions are not the same:\n\n %v \n\n %v \n\n %v", blob, serializedblob, raw)
92+
t.Errorf("Error Serializing blobs at index %d, the serialized and deserialized versions are not the same:\n\n %v \n\n %v \n\n %v", i, blob, serializedblob, raw)
93+
}
8994
}
9095

9196
}

0 commit comments

Comments
 (0)