diff --git a/db/substate_db.go b/db/substate_db.go index 79484ec..94c9213 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -43,6 +43,9 @@ type SubstateDB interface { // GetLastSubstate returns last substate (block and transaction wise) inside given DB. GetLastSubstate() (*substate.Substate, error) + + // SetDecoder sets the decoder func to the provided encoding + SetDecoder(encoding string) *substateDB } // NewDefaultSubstateDB creates new instance of SubstateDB with default options. diff --git a/db/substate_decoder.go b/db/substate_decoder.go index e432976..707d5dd 100644 --- a/db/substate_decoder.go +++ b/db/substate_decoder.go @@ -10,12 +10,12 @@ import ( "github.com/golang/protobuf/proto" ) -// decodeUsing sets the runtime parsing behavior of substateDB +// SetDecoder sets the runtime parsing behavior of substateDB // intended usage: // -// db := &substateDB{..} // initializing db -// db.DecodeSubstateUsing() // end of init, or right before decoding -func (db *substateDB) DecodeSubstateUsing(encoding string) *substateDB { +// db := &substateDB{..} // initializing db +// .SetDecoder() // end of init, or right before decoding +func (db *substateDB) SetDecoder(encoding string) *substateDB { db.decodeSubstate = getDecoderFunc(encoding, db.GetCode) return db } @@ -26,7 +26,7 @@ type substateDecoder interface { func (db *substateDB) DecodeSubstate(bytes []byte, block uint64, tx int) (*substate.Substate, error) { if db.decodeSubstate == nil { - db.decodeUsing("default") + db.SetDecoder("default") } return db.decodeSubstate(bytes, block, tx) } @@ -47,7 +47,6 @@ func getDecoderFunc(encoding string, lookup codeLookup) decoderFunc { 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) { diff --git a/db/substate_iterator.go b/db/substate_iterator.go index 9512801..54f196f 100644 --- a/db/substate_iterator.go +++ b/db/substate_iterator.go @@ -3,11 +3,8 @@ package db import ( "fmt" - "github.com/golang/protobuf/proto" - "github.com/syndtr/goleveldb/leveldb/util" - - pb "github.com/Fantom-foundation/Substate/protobuf" "github.com/Fantom-foundation/Substate/substate" + "github.com/syndtr/goleveldb/leveldb/util" ) func newSubstateIterator(db *substateDB, start []byte) *substateIterator { @@ -34,15 +31,7 @@ func (i *substateIterator) decode(data rawEntry) (*substate.Substate, error) { return nil, fmt.Errorf("invalid substate key: %v; %w", key, err) } - //rlpSubstate, err := rlp.Decode(value) - //return rlpSubstate.ToSubstate(i.db.GetCode, block, tx) - - pbSubstate := &pb.Substate{} - if err := proto.Unmarshal(value, pbSubstate); err != nil { - return nil, err - } - - return pbSubstate.Decode(i.db.GetCode, block, tx) + return i.db.DecodeSubstate(value, block, tx) } func (i *substateIterator) start(numWorkers int) {