Skip to content

Commit

Permalink
gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rpl-ffl committed Sep 19, 2024
1 parent 8850e6c commit dd122db
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 28 deletions.
33 changes: 7 additions & 26 deletions db/substate_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func newSubstateDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.Re

type substateDB struct {
*codeDB
decodeSubstate decoderFunc
}

func (db *substateDB) GetFirstSubstate() *substate.Substate {
Expand All @@ -110,19 +111,11 @@ func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, err
return nil, fmt.Errorf("cannot get substate block: %v, tx: %v from db; %w", block, tx, err)
}

//rlpSubstate, err := rlp.Decode(val)
//if err != nil {
// return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %w", block, tx, err)
//}

//return rlpSubstate.ToSubstate(db.GetCode, block, tx)

pbSubstate := &pb.Substate{}
if err := proto.Unmarshal(val, pbSubstate); err != nil {
return nil, err
if db.decodeSubstate == nil {
db.decodeUsing("default")
}

return pbSubstate.Decode(db.GetCode, block, tx)
return db.decodeSubstate(val, block, tx)
}

// GetBlockSubstates returns substates for given block if exists within DB.
Expand All @@ -147,26 +140,14 @@ func (db *substateDB) GetBlockSubstates(block uint64) (map[int]*substate.Substat
return nil, fmt.Errorf("record-replay: GetBlockSubstates(%v) iterated substates from block %v", block, b)
}

//rlpSubstate, err := rlp.Decode(value)
//if err != nil {
// return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %w", block, tx, err)
//}

//sbstt, err := rlpSubstate.ToSubstate(db.GetCode, block, tx)
//if err != nil {
// return nil, fmt.Errorf("cannot decode data into substate: %w", err)
//}

pbSubstate := &pb.Substate{}
if err := proto.Unmarshal(value, pbSubstate); err != nil {
return nil, err
if db.decodeSubstate == nil {
db.decodeUsing("default")
}

sbstt, err := pbSubstate.Decode(db.GetCode, block, tx)
sbstt, err := db.decodeSubstate(val, block, tx)
if err != nil {
return nil, err
}

txSubstate[tx] = sbstt
}
iter.Release()
Expand Down
69 changes: 69 additions & 0 deletions db/substate_decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package db

import (
pb "github.com/Fantom-foundation/Substate/protobuf"
"github.com/Fantom-foundation/Substate/rlp"
"github.com/Fantom-foundation/Substate/substate"
"github.com/Fantom-foundation/Substate/types"
trlp "github.com/Fantom-foundation/Substate/types/rlp"
"github.com/golang/protobuf/proto"
)

// decodeUsing sets the runtime parsing behavior of substateDB
// intended usage:
//
// db := &substateDB{..} // initializing db
// db.decodeUsing(<encoding>) // end of init, or right before decoding
func (db *substateDB) decodeUsing(encoding string) *substateDB {
db.decodeSubstate = getDecoderFunc(encoding, db.GetCode)
return db
}

type substateDecoder interface {
Decode(bytes []byte, block uint64, tx int) (*substate.Substate, error)
}

// decoderFunc aliases the common function used to decode substate
type decoderFunc func([]byte, uint64, int) (*substate.Substate, error)

func (decode *decoderFunc) Decode(bytes []byte, block uint64, tx int) {
decode(bytes, block, tx)
}

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

func getDecoderFunc(encoding string, lookup codeLookup) decoderFunc {
switch encoding {
case "protobuf", "pb":
return func(bytes []byte, block uint64, tx int) (*substate.Substate, error) {
return decodeProtobuf(bytes, lookup, block, tx)
}
default:
fmt.Println("Defaulting getDecoderFunc")
fallthrough
case "rlp":
return func(bytes []byte, block uint64, tx int) (*substate.Substate, error) {
return decodeRlp(bytes, lookup, block, tx)
}
}
}

// decodeRlp decodes into substate the provided rlp-encoded bytecode
func decodeRlp(bytes []byte, lookup codeLookup, block uint64, tx int) (*substate.Substate, error) {
rlpSubstate, err := rlp.Decode(bytes)
if err != nil {
return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %w", block, tx, err)
}

return rlpSubstate.ToSubstate(lookup, block, tx)
}

// decodeProtobuf decodes into substate the provided protobuf-encoded bytecode
func decodeProtobuf(bytes []byte, lookup codeLookup, block uint64, tx int) (*substate.Substate, error) {
pbSubstate := &pb.Substate{}
if err := proto.Unmarshal(bytes, pbSubstate); err != nil {
return nil, fmt.Errorf("cannot decode data into protobuf block: %v, tx %v; %w", block, tx, err)
}

return pbSubstate.Decode(lookup, block, tx)
}
4 changes: 2 additions & 2 deletions protobuf/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (msg *Substate_TxMessage) decode(lookup dbGetCode) (*substate.Message, erro
// Berlin hard fork, EIP-2930: Optional access lists
var accessList types.AccessList = nil // nil if EIP-2930 is not activated
switch txType {
case Substate_TxMessage_TXTYPE_ACCESSLIST,
case Substate_TxMessage_TXTYPE_ACCESSLIST,
Substate_TxMessage_TXTYPE_DYNAMICFEE,
Substate_TxMessage_TXTYPE_BLOB:

Expand Down Expand Up @@ -194,7 +194,7 @@ func (msg *Substate_TxMessage) decode(lookup dbGetCode) (*substate.Message, erro
var gasFeeCap *big.Int = types.BytesToBigInt(msg.GetGasPrice())
var gasTipCap *big.Int = types.BytesToBigInt(msg.GetGasPrice())
switch txType {
case Substate_TxMessage_TXTYPE_DYNAMICFEE,
case Substate_TxMessage_TXTYPE_DYNAMICFEE,
Substate_TxMessage_TXTYPE_BLOB:

gasFeeCap = BytesValueToBigInt(msg.GetGasFeeCap())
Expand Down

0 comments on commit dd122db

Please sign in to comment.