Skip to content
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

use libevm: params, core/vm, eth/tracers/* + some of core/types #662

Open
wants to merge 30 commits into
base: libevm
Choose a base branch
from

Conversation

darioush
Copy link
Collaborator

@darioush darioush commented Sep 26, 2024

Why this should be merged

Improved maintainability and reducing copies of upstream packages with modified code. This reduces our tech debt by entirely using the upstream vm module + jump tables.

As we have the libevm branch to test these changes, I would like to defer some of the following cleanups to future PRs:

  • using "core/vm" instead of "/vmerrs" where possible
  • removal of IsProhibited
  • renaming "ava-labs/go-ethereum" to "ava-labs/libevm"

How this works

Uses core/vm, and eth/tracer/* packages from https://github.com/ava-labs/libevm as a module that expands geth code without making destructive modifications instead of using code copied from upstream and modified in this repo.
Instead, the avalanche specific functionality such as precompiles are re-introduced through the hooks provided by libevm

How this was tested

CI, reprocessed historical blocks of fuji/mainnet (probably should run this again with additional switches using upstream fork names)

params/config.go Outdated Show resolved Hide resolved
@darioush darioush force-pushed the use-libevm branch 2 times, most recently from f127a63 to 0976d50 Compare October 11, 2024 21:10
@darioush darioush changed the base branch from master to libevm October 14, 2024 22:44
@darioush darioush marked this pull request as ready for review October 14, 2024 23:05
@darioush darioush requested review from ceyonur and a team as code owners October 14, 2024 23:05
@darioush darioush changed the title wip: use libevm use libevm: params, core/vm, eth/tracers/* + some of core/types Oct 14, 2024
Copy link
Contributor

@ARR4N ARR4N left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see what @ceyonur thinks too, but generally this LGTM. Only a few nits and questions as I see you've already merged my recommendations PR.

Alloc: types.GenesisAlloc{addr1: {Balance: genesisBalance}},
Config: params.WithExtra(
&params.ChainConfig{HomesteadBlock: new(big.Int)},
&params.ChainConfigExtra{},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a raw &params.ChainConfig{...} not work? params.GetExtra() guarantees that the extra won't be nil.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this works

core/evm.go Outdated
"github.com/ethereum/go-ethereum/log"
"github.com/holiman/uint256"
)

var _ vm.Hooks = hooks{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) If you move the init() here then you don't need the type check because it's implied.

}

func (s *StateDbAP1) GetCommittedState(addr common.Address, key common.Hash) common.Hash {
state.NormalizeStateKey(&key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(no action required) So good to see that wrapping with just this one method does the trick for this!

@@ -238,13 +233,7 @@ func (s *StateDB) Error() error {
// AddLog adds a log with the specified parameters to the statedb
// Note: blockNumber is a required argument because StateDB does not
// know the current block number.
func (s *StateDB) AddLog(addr common.Address, topics []common.Hash, data []byte, blockNumber uint64) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the change to StateDB.AddLog() just to move closer to upstream?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also needed so coreth/core/state.StateDB implements libevm/core/vm.StateDB
Before this PR importing coreth/core/types in the precompiles is not possible because of a circular dependency with from types -> params -> precompiles.

@@ -329,6 +332,18 @@ func (st *StateTransition) buyGas() error {
return nil
}

// TODO: Probably can be removed -- extremely unlikely someone has private keys
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove it in a separate PR next.

@@ -69,8 +69,9 @@ func (utx *UnsignedExportTx) InputUTXOs() set.Set[ids.ID] {
// Verify this transaction is well-formed
func (utx *UnsignedExportTx) Verify(
ctx *snow.Context,
rules params.Rules,
rules_ params.Rules,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(style) basicRules? That's the best word I can come up with, but I still think it's better than a suffix _ as that jumps out as very non-idiomatic Go.

Below as well.

Copy link
Collaborator Author

@darioush darioush Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree. I am switching to use using rules & rulesExtra in places where both the base rules and the extra rules are used.
I don't think "Extra" is very long but I am open to consider using like "rulesExt" if others prefer a shorter name.

@@ -62,8 +62,9 @@ func (utx *UnsignedImportTx) InputUTXOs() set.Set[ids.ID] {
// Verify this transaction is well-formed
func (utx *UnsignedImportTx) Verify(
ctx *snow.Context,
rules params.Rules,
rules_ params.Rules,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(style) As with the other rules_.

plugin/evm/vm.go Outdated
@@ -865,8 +867,9 @@ func (vm *VM) preBatchOnFinalizeAndAssemble(header *types.Header, state *state.S
// Note: snapshot is taken inside the loop because you cannot revert to the same snapshot more than
// once.
snapshot := state.Snapshot()
rules := vm.chainConfig.Rules(header.Number, header.Time)
if err := vm.verifyTx(tx, header.ParentHash, header.BaseFee, state, rules); err != nil {
rules_ := vm.chainConfig.Rules(header.Number, params.IsMergeTODO, header.Time)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(style) Here the variable can just be removed entirely.

rules := params.GetRulesExtra(
  vm.chainConfig.Rules(...),
)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately rules is used to pass to verifyTx. I think we should maintain a consistent style to pass params.Rules and params.ChainConfig around in the code then get the extra in the function if needed.

plugin/evm/vm.go Outdated
@@ -1013,7 +1016,8 @@ func (vm *VM) onExtraStateChange(block *types.Block, state *state.StateDB) (*big
batchContribution *big.Int = big.NewInt(0)
batchGasUsed *big.Int = big.NewInt(0)
header = block.Header()
rules = vm.chainConfig.Rules(header.Number, header.Time)
rules_ = vm.chainConfig.Rules(header.Number, params.IsMergeTODO, header.Time)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(style) As above without the intermediate rules_.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above (also used in verifyTx)

@@ -62,3 +62,7 @@ func GetPredicateResultBytes(extraData []byte) ([]byte, bool) {
}
return extraData[params.DynamicFeeExtraDataSize:], true
}

func SetPredicateResultBytes(extraData []byte, predicateResults []byte) []byte {
return append(extraData[:params.DynamicFeeExtraDataSize], predicateResults...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is extra data supposed to be that we know the prefix? Please add a comment describing the parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants