diff --git a/sharding/collation.go b/sharding/collation.go index 3ce4b55e33..e7ec5104a2 100644 --- a/sharding/collation.go +++ b/sharding/collation.go @@ -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) { @@ -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) { +// SerializeTxToBlob method serializes the input tx +// and returns the blobs in byte array. +func SerializeTxToBlob(txs []*types.Transaction) ([]byte, error) { - blobs, err := c.CreateRawBlobs() - - 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 { diff --git a/sharding/collation_test.go b/sharding/collation_test.go index c0e4cb7355..297cdb3f23 100644 --- a/sharding/collation_test.go +++ b/sharding/collation_test.go @@ -2,6 +2,7 @@ package sharding import ( "bytes" + "crypto/rand" "math/big" "reflect" "testing" @@ -56,13 +57,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) @@ -126,3 +127,38 @@ func TestSerialize_Deserialize(t *testing.T) { func makeTxWithGasLimit(gl uint64) *types.Transaction { return types.NewTransaction(0 /*nonce*/, common.HexToAddress("0x0") /*to*/, nil /*amount*/, gl, nil /*gasPrice*/, nil /*data*/) } + +// BENCHMARK TESTS + +// Helper function to generate test that completes round trip serialization tests for a specific number of transactions. +func runBenchTest(b *testing.B, numTransactions int) { + var txs []*types.Transaction + for i := 0; i < numTransactions; i++ { + data := make([]byte, 650) + rand.Read(data) + txs = append(txs, types.NewTransaction(0 /*nonce*/, common.HexToAddress("0x0") /*to*/, nil /*amount*/, 0 /*gasLimit*/, nil /*gasPrice*/, data)) + } + b.ResetTimer() + + for i := 0; i < b.N; i++ { + results, _ := SerializeTxToBlob(txs) + _, _ = DeserializeBlobToTx(results) + } + +} + +func BenchmarkSerialization10(b *testing.B) { + runBenchTest(b, 10) +} + +func BenchmarkSerialization100(b *testing.B) { + runBenchTest(b, 100) +} + +func BenchmarkSerialization1000(b *testing.B) { + runBenchTest(b, 1000) +} + +func BenchmarkSerialization10000(b *testing.B) { + runBenchTest(b, 10000) +}