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

[Flow EVM] extend EVM precompiles with cadence arch #5233

Merged
merged 23 commits into from
Jan 23, 2024

Conversation

ramtinms
Copy link
Contributor

closes: #5199

@codecov-commenter
Copy link

codecov-commenter commented Jan 11, 2024

Codecov Report

Attention: 24 lines in your changes are missing coverage. Please review.

Comparison is base (690a250) 55.47% compared to head (a930a00) 55.42%.

Files Patch % Lines
fvm/evm/emulator/emulator.go 61.90% 8 Missing ⚠️
fvm/evm/precompiles/arch.go 72.72% 4 Missing and 2 partials ⚠️
fvm/evm/precompiles/precompile.go 80.64% 4 Missing and 2 partials ⚠️
fvm/evm/emulator/config.go 82.60% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5233      +/-   ##
==========================================
- Coverage   55.47%   55.42%   -0.06%     
==========================================
  Files         996      993       -3     
  Lines       95787    95514     -273     
==========================================
- Hits        53136    52935     -201     
+ Misses      38661    38602      -59     
+ Partials     3990     3977      -13     
Flag Coverage Δ
unittests 55.42% <81.95%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ramtinms ramtinms marked this pull request as ready for review January 11, 2024 19:34
@ramtinms ramtinms marked this pull request as draft January 11, 2024 20:55
Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

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

Nice!

fvm/evm/emulator/emulator.go Outdated Show resolved Hide resolved
fvm/evm/handler/addressAllocator.go Outdated Show resolved Hide resolved
fvm/evm/handler/addressAllocator.go Outdated Show resolved Hide resolved
fvm/evm/emulator/config.go Show resolved Hide resolved
fvm/evm/precompiles/arch.go Show resolved Hide resolved
// RequiredPrice calculates the contract gas use
func (p *precompile) RequiredGas(input []byte) uint64 {
if len(input) < 4 {
return 0
Copy link
Member

Choose a reason for hiding this comment

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

Why do shorter inputs not require any gas?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a small gas of 1 to be returned but inputs shorter than 4 mean we don't have a proper methodID extractable so we are going to error out on the run section.

fvm/evm/precompiles/signature.go Outdated Show resolved Hide resolved
fvm/evm/types/emulator.go Outdated Show resolved Hide resolved
@turbolent
Copy link
Member

@ramtinms Apologies, I hadn't realized this was still in draft when I reviewed it

@ramtinms
Copy link
Contributor Author

@turbolent you were too fast, I accidentally pressed ready to be reviewed and changed it back but you got it reviewed :D

@ramtinms ramtinms marked this pull request as ready for review January 17, 2024 18:46
@ramtinms ramtinms requested a review from turbolent January 17, 2024 18:46
// BlockContext holds the context needed for the emulator operations
type BlockContext struct {
BlockNumber uint64
DirectCallBaseGasUsage uint64
DirectCallGasPrice uint64
GasFeeCollector Address

// a set of extra precompiles to be injected
ExtraPrecompiles []Precompile
Copy link
Contributor

Choose a reason for hiding this comment

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

FlowPrecompiles?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm trying to keep the emulator not referencing any Flow protocol concepts, we might even add some precompiles in the future that is not Flow-related.

Copy link
Contributor

@sideninja sideninja left a comment

Choose a reason for hiding this comment

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

Left some comments, but generally looks good. Nice!

@ramtinms
Copy link
Contributor Author

@turbolent now this is ready, do you want to take another look?

Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

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

Great work! Just some minor suggestions

Comment on lines 15 to 23
)

var (
// prefixes:
// the first 12 bytes of addresses allocation
// leading zeros helps with storage and all zero is reserved for the EVM precompiles
FlowEVMPrecompileAddressPrefix = [...]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
FlowEVMCOAAddressPrefix = [...]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}
)
Copy link
Member

Choose a reason for hiding this comment

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

Given that the prefix is/must be 12 bytes, define a constant and use it here, as well in the related copy calls below:

Suggested change
)
var (
// prefixes:
// the first 12 bytes of addresses allocation
// leading zeros helps with storage and all zero is reserved for the EVM precompiles
FlowEVMPrecompileAddressPrefix = [...]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
FlowEVMCOAAddressPrefix = [...]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}
)
addressPrefixLen = 8
)
var (
// prefixes:
// the first 12 bytes of addresses allocation
// leading zeros helps with storage and all zero is reserved for the EVM precompiles
FlowEVMPrecompileAddressPrefix = [addressPrefixLen]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
FlowEVMCOAAddressPrefix = [addressPrefixLen]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}
)

return makePrefixedAddress(index, FlowEVMPrecompileAddressPrefix)
}

func makePrefixedAddress(index uint64, prefix [12]byte) types.Address {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func makePrefixedAddress(index uint64, prefix [12]byte) types.Address {
func makePrefixedAddress(index uint64, prefix [addressPrefixLen]byte) types.Address {

)

func TestMutiFunctionContract(t *testing.T) {
address := testutils.RandomAddress(t)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
address := testutils.RandomAddress(t)
t.Parallel()
address := testutils.RandomAddress(t)


// Run runs the precompiled contract
func (p *precompile) Run(input []byte) ([]byte, error) {
if len(input) < 4 {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if len(input) < 4 {
if len(input) < FunctionSelectorLength {


// RequiredGas calculates the contract gas use
func (p *precompile) RequiredGas(input []byte) uint64 {
if len(input) < 4 {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if len(input) < 4 {
if len(input) < FunctionSelectorLength {

)

func TestFunctionSelector(t *testing.T) {
expected := gethCrypto.Keccak256([]byte("test()"))[:4]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
expected := gethCrypto.Keccak256([]byte("test()"))[:4]
t.Parallel()
expected := gethCrypto.Keccak256([]byte("test()"))[:FunctionSelectorLength]

@ramtinms ramtinms enabled auto-merge January 23, 2024 09:19
@ramtinms ramtinms added this pull request to the merge queue Jan 23, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Jan 23, 2024
@ramtinms ramtinms added this pull request to the merge queue Jan 23, 2024
Merged via the queue into master with commit 3107bc9 Jan 23, 2024
51 checks passed
@ramtinms ramtinms deleted the ramtin/5199-add-precompile branch January 23, 2024 17:35
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.

[Flow EVM] Expose core FVM functions as a precompile contract
4 participants