diff --git a/db/substate_encoding.go b/db/substate_encoding.go index f5458f8..af808e5 100644 --- a/db/substate_encoding.go +++ b/db/substate_encoding.go @@ -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: @@ -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 { @@ -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 -} diff --git a/db/substate_encoding_test.go b/db/substate_encoding_test.go index 1583ae4..23d57c5 100644 --- a/db/substate_encoding_test.go +++ b/db/substate_encoding_test.go @@ -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 { @@ -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, diff --git a/protobuf/decode.go b/protobuf/decode.go index 94c837a..edfff38 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -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 { diff --git a/protobuf/encode.go b/protobuf/encode.go index 3284fa4..0c3527b 100644 --- a/protobuf/encode.go +++ b/protobuf/encode.go @@ -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)) @@ -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{ @@ -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 { @@ -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) diff --git a/protobuf/utils.go b/protobuf/utils.go index f38ed48..3fde686 100644 --- a/protobuf/utils.go +++ b/protobuf/utils.go @@ -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) }