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

core, state: introduce code reader interface #30816

Merged
merged 7 commits into from
Nov 29, 2024

Conversation

rjl493456442
Copy link
Member

@rjl493456442 rjl493456442 commented Nov 27, 2024

It's an alternative of #30808

This PR introduces a ContractCodeReader interface with functions defined:

// ContractCodeReader defines the interface for accessing contract code.
type ContractCodeReader interface {
	// Code retrieves a particular contract's code.
	//
	// - Returns nil code along with nil error if the requested contract code
	//   doesn't exist
	// - Returns an error only if an unexpected issue occurs
	Code(addr common.Address, codeHash common.Hash) ([]byte, error)

	// CodeSize retrieves a particular contracts code's size.
	//
	// - Returns zero code size along with nil error if the requested contract code
	//   doesn't exist
	// - Returns an error only if an unexpected issue occurs
	CodeSize(addr common.Address, codeHash common.Hash) (int, error)
}

This interface can be implemented in various ways. Although the codebase currently includes only one implementation, additional implementations could be created for different purposes and scenarios, such as a code reader designed for the Verkle tree approach or one that reads code from the witness.

Notably, this interface modifies the function’s semantics. If the contract code is not found, no error will be returned. An error should only be returned in the event of an unexpected issue, primarily for future implementations.


Besides, the original Reader interface is extended with ContractCodeReader, it gives us more flexibility to manipulate the reader with additional logics on top, e.g. Hooks.

// Reader defines the interface for accessing accounts, storage slots and contract
// code associated with a specific state.
type Reader interface {
	ContractCodeReader
	StateReader
}

Copy link
Contributor

@s1na s1na left a comment

Choose a reason for hiding this comment

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

I think this structure looks very nice. Just had a comment about performance of the multireader.

core/state/reader.go Outdated Show resolved Hide resolved
@MariusVanDerWijden
Copy link
Member

This might be a stupid suggestion, but what do you think about renaming the interface a little bit like this:

// ContractCodeReader defines the interface for accessing contract code.
type ContractCodeReader interface {
	// Code retrieves a particular contract's code.
	Code(addr common.Address, codeHash common.Hash) ([]byte, error)

	// CodeSize retrieves a particular contracts code's size.
	CodeSize(addr common.Address, codeHash common.Hash) (int, error)
}

Makes the name of the interface a bit longer, but the methods themselves a bit shorter and a user will still understand that this Reader is for contract code

@rjl493456442
Copy link
Member Author

@MariusVanDerWijden applied!

@rjl493456442
Copy link
Member Author

@s1na @MariusVanDerWijden I have updated the implementation, please take another look.

Copy link
Contributor

@s1na s1na left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@MariusVanDerWijden MariusVanDerWijden left a comment

Choose a reason for hiding this comment

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

LGTM

@rjl493456442 rjl493456442 added this to the 1.14.13 milestone Nov 29, 2024
if cached, ok := r.codeSizeCache.Get(codeHash); ok {
return cached, nil
}
code, err := r.Code(addr, codeHash)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't you add it to the code/codeSize caches too, before returning?

Copy link
Member Author

Choose a reason for hiding this comment

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

The codeSizeCache is filled in the r.Code(addr, codeHash) call.

It's a little bit weird, I can change it if you want.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh right, yeah never mind!

@fjl fjl merged commit 03c37cd into ethereum:master Nov 29, 2024
2 of 3 checks passed
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.

5 participants