Skip to content

Commit

Permalink
encode -> toProtobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
rpl-ffl committed Sep 30, 2024
1 parent a660d9b commit e68fe3c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 37 deletions.
14 changes: 2 additions & 12 deletions db/substate_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func newSubstateEncoding(encoding string, lookup codeLookupFunc) (*substateEncod
decode: func(bytes []byte, block uint64, tx int) (*substate.Substate, error) {
return decodeProtobuf(bytes, lookup, block, tx)
},
encode: encodeProtobuf,
encode: pb.Encode,
}, nil

default:
Expand Down Expand Up @@ -98,7 +98,7 @@ func decodeRlp(bytes []byte, lookup codeLookupFunc, block uint64, tx int) (*subs
return rlpSubstate.ToSubstate(lookup, block, tx)
}

// encodeRlp encodes substate into rlp-encoded bytes
// encodeRlp encodes substate into rlp-encoded bytes
func encodeRlp(ss *substate.Substate, block uint64, tx int) ([]byte, error) {
bytes, err := trlp.EncodeToBytes(rlp.NewRLP(ss))
if err != nil {
Expand All @@ -117,13 +117,3 @@ func decodeProtobuf(bytes []byte, lookup codeLookupFunc, block uint64, tx int) (

return pbSubstate.Decode(lookup, block, tx)
}

// encodeProtobuf encodes substate into protobuf-encoded bytes
func encodeProtobuf(ss *substate.Substate, block uint64, tx int) ([]byte, error) {
bytes, err := proto.Marshal(pb.Encode(ss))
if err != nil {
return nil, fmt.Errorf("cannot encode substate into protobuf block: %v, tx %v; %w", block, tx, err)
}

return bytes, nil
}
18 changes: 6 additions & 12 deletions db/substate_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
pb "github.com/Fantom-foundation/Substate/protobuf"
"github.com/Fantom-foundation/Substate/rlp"
trlp "github.com/Fantom-foundation/Substate/types/rlp"
"google.golang.org/protobuf/proto"
)

type encTest struct {
Expand All @@ -17,19 +16,14 @@ type encTest struct {
}

var (
simplePb, _ = proto.Marshal(pb.Encode(testSubstate))
testPb = encTest{
bytes: simplePb,
blk: testSubstate.Block,
tx: testSubstate.Transaction,
}
blk = testSubstate.Block
tx = testSubstate.Transaction

simplePb, _ = pb.Encode(testSubstate, blk, tx)
testPb = encTest{bytes: simplePb, blk: blk, tx: tx}

simpleRlp, _ = trlp.EncodeToBytes(rlp.NewRLP(testSubstate))
testRlp = encTest{
bytes: simpleRlp,
blk: testSubstate.Block,
tx: testSubstate.Transaction,
}
testRlp = encTest{bytes: simpleRlp, blk: blk, tx: tx}

supportedEncoding = map[string]encTest{
"rlp": testRlp,
Expand Down
2 changes: 1 addition & 1 deletion protobuf/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type getCodeFunc = func(types.Hash) ([]byte, error)

// Decode converts protobuf-encoded Substate into aida-comprehensible substate
// Decode converts protobuf-encoded bytes into aida substate
func (s *Substate) Decode(lookup getCodeFunc, block uint64, tx int) (*substate.Substate, error) {
input, err := s.GetInputAlloc().decode(lookup)
if err != nil {
Expand Down
34 changes: 23 additions & 11 deletions protobuf/encode.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package protobuf

import (
"fmt"

"github.com/Fantom-foundation/Substate/substate"
"github.com/Fantom-foundation/Substate/types"
"github.com/Fantom-foundation/Substate/types/hash"
"google.golang.org/protobuf/proto"
)

// Encode converts aida-substate into protobuf-encoded message
func Encode(ss *substate.Substate) *Substate {
func Encode(ss *substate.Substate, block uint64, tx int) ([]byte, error) {
bytes, err := proto.Marshal(toProtobufSubstate(ss))
if err != nil {
return nil, fmt.Errorf("cannot encode substate into protobuf block: %v,tx %v; %w", block, tx, err)
}

return bytes, nil
}

func toProtobufSubstate(ss *substate.Substate) *Substate {
return &Substate{
InputAlloc: encodeWorldState(ss.InputSubstate),
OutputAlloc: encodeWorldState(ss.OutputSubstate),
BlockEnv: encodeEnv(ss.Env),
TxMessage: encodeMessage(ss.Message),
Result: encodeResult(ss.Result),
InputAlloc: toProtobufAlloc(ss.InputSubstate),
OutputAlloc: toProtobufAlloc(ss.OutputSubstate),
BlockEnv: toProtobufBlockEnv(ss.Env),
TxMessage: toProtobufTxMessage(ss.Message),
Result: toProtobufResult(ss.Result),
}
}

// encodeWorldState converts substate.WorldState into protobuf-encoded Substate_Alloc
func encodeWorldState(sw substate.WorldState) *Substate_Alloc {
// toProtobufAlloc converts substate.WorldState into protobuf-encoded Substate_Alloc
func toProtobufAlloc(sw substate.WorldState) *Substate_Alloc {
world := make([]*Substate_AllocEntry, 0, len(sw))
for addr, acct := range sw {
storage := make([]*Substate_Account_StorageEntry, 0, len(acct.Storage))
Expand Down Expand Up @@ -46,7 +58,7 @@ func encodeWorldState(sw substate.WorldState) *Substate_Alloc {
}

// encode converts substate.Env into protobuf-encoded Substate_BlockEnv
func encodeEnv(se *substate.Env) *Substate_BlockEnv {
func toProtobufBlockEnv(se *substate.Env) *Substate_BlockEnv {
blockHashes := make([]*Substate_BlockEnv_BlockHashEntry, 0, len(se.BlockHashes))
for number, hash := range se.BlockHashes {
blockHashes = append(blockHashes, &Substate_BlockEnv_BlockHashEntry{
Expand All @@ -69,7 +81,7 @@ func encodeEnv(se *substate.Env) *Substate_BlockEnv {
}

// encode converts substate.Message into protobuf-encoded Substate_TxMessage
func encodeMessage(sm *substate.Message) *Substate_TxMessage {
func toProtobufTxMessage(sm *substate.Message) *Substate_TxMessage {
dt := Substate_TxMessage_TXTYPE_LEGACY
txType := &dt
if sm.ProtobufTxType != nil {
Expand Down Expand Up @@ -118,7 +130,7 @@ func (entry *Substate_TxMessage_AccessListEntry) encode(sat *types.AccessTuple)
}

// encode converts substate.Results into protobuf-encoded Substate_Result
func encodeResult(sr *substate.Result) *Substate_Result {
func toProtobufResult(sr *substate.Result) *Substate_Result {
logs := make([]*Substate_Result_Log, len(sr.Logs))
for i, log := range sr.Logs {
logs[i].encode(log)
Expand Down
2 changes: 1 addition & 1 deletion protobuf/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ func BytesToBigInt(b []byte) *big.Int {
if b == nil {
return nil
}
return new(big.Int).SetBytes(b)
return new(big.Int).SetBytes(b)
}

0 comments on commit e68fe3c

Please sign in to comment.