Skip to content

Commit

Permalink
Merge pull request ethereum#136 from terenc3t/collation-serializer
Browse files Browse the repository at this point in the history
Serialize Function Should be Pure
  • Loading branch information
terencechain authored May 25, 2018
2 parents 9db6161 + 6ecf124 commit a457757
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 35 deletions.
46 changes: 13 additions & 33 deletions sharding/collation.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,6 @@ func (c *Collation) CalculateChunkRoot() {
c.header.data.ChunkRoot = &chunkRoot
}

// CreateRawBlobs creates raw blobs from transactions.
func (c Collation) CreateRawBlobs() ([]*utils.RawBlob, error) {

// It does not skip evm execution by default
blobs := make([]*utils.RawBlob, len(c.transactions))
for i := 0; i < len(c.transactions); i++ {

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

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

}

return blobs, nil

}

// ConvertBackToTx converts raw blobs back to their original transactions.
func ConvertBackToTx(rawBlobs []utils.RawBlob) ([]*types.Transaction, error) {

Expand All @@ -149,33 +129,33 @@ func ConvertBackToTx(rawBlobs []utils.RawBlob) ([]*types.Transaction, error) {

}

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

blobs, err := c.CreateRawBlobs()
// SerializeTxToBlob method serializes the input tx
// and returns the blobs in byte array.
func SerializeTxToBlob(txs []*types.Transaction) ([]byte, error) {

if err != nil {
return nil, fmt.Errorf("%v", err)
blobs := make([]*utils.RawBlob, len(txs))
for i := 0; i < len(txs); i++ {
err := error(nil)
blobs[i], err = utils.NewRawBlob(txs[i], false)
if err != nil {
return nil, fmt.Errorf("%v", err)
}
}

serializedTx, err := utils.Serialize(blobs)

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

if int64(len(serializedTx)) > collationSizelimit {

return nil, fmt.Errorf("The serialized body exceeded the collation size limit: %v", serializedTx)

}

return serializedTx, nil

}

// Deserialize takes a byte array and converts its back to its original transactions.
func Deserialize(serialisedBlob []byte) (*[]*types.Transaction, error) {
// DeserializeBlobToTx takes byte array blob and converts it back
// to original txs and returns the txs in tx array.
func DeserializeBlobToTx(serialisedBlob []byte) (*[]*types.Transaction, error) {

deserializedBlobs, err := utils.Deserialize(serialisedBlob)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions sharding/collation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ func TestSerialize_Deserialize(t *testing.T) {

tx := c.transactions

results, err := c.Serialize()
results, err := SerializeTxToBlob(tx)

if err != nil {
t.Errorf("Unable to Serialize transactions, %v", err)
}

deserializedTxs, err := Deserialize(results)
deserializedTxs, err := DeserializeBlobToTx(results)

if err != nil {
t.Errorf("Unable to deserialize collation body, %v", err)
Expand Down

0 comments on commit a457757

Please sign in to comment.