-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor for the new version of the Substate #7
base: main
Are you sure you want to change the base?
Changes from 1 commit
e1b8ce4
9a39b0b
363e755
b1b9f80
1d2b9c2
51f6c8c
79aba9b
edfaa35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,14 +21,17 @@ import ( | |
"math" | ||
"math/big" | ||
|
||
substate "github.com/Fantom-foundation/Substate" | ||
"github.com/Fantom-foundation/Substate/substate" | ||
stypes "github.com/Fantom-foundation/Substate/types" | ||
"github.com/Fantom-foundation/Substate/types/hash" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/state" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/params" | ||
|
||
innerSubstate "github.com/Fantom-foundation/go-opera/substate" | ||
"github.com/Fantom-foundation/go-opera/utils/signers/gsignercache" | ||
"github.com/Fantom-foundation/go-opera/utils/signers/internaltx" | ||
) | ||
|
@@ -101,17 +104,48 @@ func (p *StateProcessor) Process( | |
if err != nil { | ||
return nil, nil, nil, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) | ||
} | ||
if substate.RecordReplay { | ||
if innerSubstate.RecordReplay { | ||
// save tx substate into DBs, merge block hashes to env | ||
etherBlock := block.RecordingEthBlock() | ||
to := stypes.Address(msg.To().Bytes()) | ||
dataHash := hash.Keccak256Hash(msg.Data()) | ||
recording := substate.NewSubstate( | ||
statedb.SubstatePreAlloc, | ||
statedb.SubstatePostAlloc, | ||
substate.NewSubstateEnv(etherBlock, statedb.SubstateBlockHashes), | ||
substate.NewSubstateMessage(&msg), | ||
substate.NewSubstateResult(receipt), | ||
substate.NewEnv( | ||
stypes.Address(etherBlock.Coinbase()), | ||
etherBlock.Difficulty(), | ||
etherBlock.GasLimit(), | ||
etherBlock.NumberU64(), | ||
etherBlock.Time(), | ||
etherBlock.BaseFee(), | ||
innerSubstate.HashGethToSubstate(statedb.SubstateBlockHashes)), | ||
substate.NewMessage( | ||
msg.Nonce(), | ||
msg.IsFake(), | ||
msg.GasPrice(), | ||
msg.Gas(), | ||
stypes.Address(msg.From()), | ||
&to, | ||
msg.Value(), | ||
msg.Data(), | ||
&dataHash, | ||
innerSubstate.AccessListGethToSubstate(msg.AccessList()), | ||
msg.GasFeeCap(), | ||
msg.GasTipCap()), | ||
substate.NewResult( | ||
receipt.Status, | ||
receipt.Bloom.Bytes(), | ||
innerSubstate.LogsGethToSubstate(receipt.Logs), | ||
stypes.Address(receipt.ContractAddress), | ||
receipt.GasUsed), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each New function takes long arguments. I want to suggest moving them to the |
||
blockNumber.Uint64(), | ||
txCounter, | ||
) | ||
substate.PutSubstate(block.NumberU64(), txCounter, recording) | ||
err = innerSubstate.PutSubstate(recording) | ||
if err != nil { | ||
return nil, nil, nil, fmt.Errorf("could not put substate %d [%v]: %w", i, tx.Hash().Hex(), err) | ||
} | ||
} | ||
txCounter++ | ||
receipts = append(receipts, receipt) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package substate | ||
|
||
import ( | ||
"github.com/syndtr/goleveldb/leveldb/opt" | ||
|
||
"github.com/Fantom-foundation/Substate/db" | ||
"github.com/Fantom-foundation/Substate/substate" | ||
) | ||
|
||
var ( | ||
staticSubstateDB db.SubstateDB | ||
RecordReplay bool = false | ||
) | ||
|
||
func NewSubstateDB(path string) error { | ||
var err error | ||
staticSubstateDB, err = db.NewSubstateDB(path, &opt.Options{ReadOnly: false}, nil, nil) | ||
return err | ||
} | ||
|
||
func CloseSubstateDB() error { | ||
return staticSubstateDB.Close() | ||
} | ||
|
||
func PutSubstate(ss *substate.Substate) error { | ||
return staticSubstateDB.PutSubstate(ss) | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see a full copy of a structure in the util function. Can you and @matejmlejnek double check on performance penalty when recording new substates? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is our package, please add unit tests. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package substate | ||
|
||
import ( | ||
stypes "github.com/Fantom-foundation/Substate/types" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
) | ||
|
||
//Here I put some utils to convert Geth types to Substate types | ||
|
||
func HashGethToSubstate(g map[uint64]common.Hash) map[uint64]stypes.Hash { | ||
res := make(map[uint64]stypes.Hash) | ||
for k, v := range g { | ||
res[k] = stypes.Hash(v) | ||
} | ||
return res | ||
} | ||
|
||
func AccessListGethToSubstate(al types.AccessList) stypes.AccessList { | ||
st := stypes.AccessList{} | ||
for _, tuple := range al { | ||
var keys []stypes.Hash | ||
for _, key := range tuple.StorageKeys { | ||
keys = append(keys, stypes.Hash(key)) | ||
} | ||
st = append(st, stypes.AccessTuple{Address: stypes.Address(tuple.Address), StorageKeys: keys}) | ||
} | ||
return st | ||
} | ||
|
||
func LogsGethToSubstate(logs []*types.Log) []*stypes.Log { | ||
var ls []*stypes.Log | ||
for _, log := range logs { | ||
l := new(stypes.Log) | ||
l.BlockHash = stypes.Hash(log.BlockHash) | ||
l.Data = log.Data | ||
l.Address = stypes.Address(log.Address) | ||
l.Index = log.Index | ||
l.BlockNumber = log.BlockNumber | ||
l.Removed = log.Removed | ||
l.TxHash = stypes.Hash(log.TxHash) | ||
l.TxIndex = log.TxIndex | ||
for _, topic := range log.Topics { | ||
l.Topics = append(l.Topics, stypes.Hash(topic)) | ||
} | ||
|
||
ls = append(ls, l) | ||
} | ||
return ls | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file seems to be modified by auto-formatting tool. I would suggest remove files which are unrelated to this change.