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

Precompile dispatch table #88

Closed
wants to merge 10 commits into from
Closed

Precompile dispatch table #88

wants to merge 10 commits into from

Conversation

ferranbt
Copy link
Collaborator

📝 Summary

This PR introduces a concept developed as part of #70, the dispatcher. The dispatcher is an object that stores all the list of MEVM precompiles and can invoke each one of them on runtime without needing the generated code for encoding/decoding.

Writing a new precompile

Now, writing a precompile is two steps.

First, write a struct that fulfills the SuavePrecompile interface:

type addPrecompile struct {}

func (a *addPrecompile) Address() common.Address {
   return common.Address("0x00...")
}

func (a *addPrecompile) Name() string {
   return "add"
}

func (a *addPrecompile) RequiredGas(input []byte) uint64 {
  return 100
}

func (a *addPrecompile) Do(ctx *SuaveContext, a, b uint64) (uint64, error) {
   return a + b
}

Methods:

  • Address: Returns the address assigned to this precompile.
  • Name: Returns the name of the precompile. This will be the name used on the Suave.sol library.
  • RequiredGas: The gas required by the precompile.
  • Do: A function with the typed inputs/outputs for the precompile. It must have as a first argument SuaveContext and as last output an error.

Second, the precompile gets registered in the dispatcher:

func init() {
   dispatcher.MustRegister(&addPrecompile{})
}

Suave library code generation

With the introduction of the dynamic runtime, the Go code generation is not required anymore.

Besides, now, it is the Go precompiles and the dispatcher the new source of truth about the precompiles. Thus, the yaml specification is not required anymore.

The code generation is still used to generate Suave.sol but it uses the methods registered in the dispatcher.

Benefits

  • The dispatcher could be easily repurposed to server also the suave-as-a-contract model.
  • It solves the full lifecycle of writing the precompile (encoding, routing, gas requirement). With the current model, though encoding/decoding is solved, we need extra wiring to fix the rest.

📚 References


  • I have seen and agree to CONTRIBUTING.md

@ferranbt
Copy link
Collaborator Author

ferranbt commented Nov 7, 2023

Closed in favour of #94

@ferranbt ferranbt closed this Nov 7, 2023
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.

1 participant