generated from rollkit/template-da-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
creating Batch type and serialization for better handling of the prot…
…obuf
- Loading branch information
1 parent
d5aeeb7
commit c47d0a7
Showing
7 changed files
with
147 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package sequencing | ||
|
||
import ( | ||
pbseq "github.com/rollkit/go-sequencing/types/pb/sequencing" | ||
) | ||
|
||
func (batch *Batch) ToProto() *pbseq.Batch { | ||
return &pbseq.Batch{Transactions: txsToByteSlices(batch.Transactions)} | ||
} | ||
|
||
func (batch *Batch) FromProto(pb *pbseq.Batch) { | ||
batch.Transactions = byteSlicesToTxs(pb.Transactions) | ||
} | ||
|
||
func txsToByteSlices(txs []Tx) [][]byte { | ||
if txs == nil { | ||
return nil | ||
} | ||
bytes := make([][]byte, len(txs)) | ||
copy(bytes, txs) | ||
return bytes | ||
} | ||
|
||
func byteSlicesToTxs(bytes [][]byte) []Tx { | ||
if len(bytes) == 0 { | ||
return nil | ||
} | ||
txs := make([]Tx, len(bytes)) | ||
copy(txs, bytes) | ||
return txs | ||
} | ||
|
||
func (batch *Batch) Marshal() ([]byte, error) { | ||
pb := batch.ToProto() | ||
return pb.Marshal() | ||
} | ||
|
||
func (batch *Batch) Unmarshal(data []byte) error { | ||
var pb pbseq.Batch | ||
if err := pb.Unmarshal(data); err != nil { | ||
return err | ||
} | ||
batch.FromProto(&pb) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.