Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Refactor/pc demux 2 (ethereum-optimism#15)
Browse files Browse the repository at this point in the history
* refactor: rename vars and method in pc w/ abi

* refactor: make pc demux use method id
  • Loading branch information
therealbytes committed Sep 2, 2023
1 parent ff78231 commit 8e48b3d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
36 changes: 19 additions & 17 deletions concrete/lib/precompile_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package lib

import (
"errors"
"math/big"

cc_api "github.com/ethereum/go-ethereum/concrete/api"
)
Expand Down Expand Up @@ -46,28 +45,32 @@ func (pc *BlankPrecompile) Run(api cc_api.API, input []byte) ([]byte, error) {

var _ cc_api.Precompile = &BlankPrecompile{}

type PrecompileDemux map[uint]cc_api.Precompile
type PrecompileDemux map[string]cc_api.Precompile

func (d PrecompileDemux) getSelect(input []byte) uint {
return uint(new(big.Int).SetBytes(GetData(input, 0, 32)).Uint64())
func (d PrecompileDemux) getSelection(input []byte) (cc_api.Precompile, []byte, error) {
sel := input[:4]
input = input[4:]
pc, ok := d[string(sel)]
if !ok {
return nil, nil, errors.New("invalid select value")
}
return pc, input, nil
}

func (d PrecompileDemux) MutatesStorage(input []byte) bool {
sel := d.getSelect(input)
pc, ok := d[sel]
if !ok {
pc, input, err := d.getSelection(input)
if err != nil {
return false
}
return pc.MutatesStorage(input[32:])
return pc.MutatesStorage(input)
}

func (d PrecompileDemux) RequiredGas(input []byte) uint64 {
sel := d.getSelect(input)
pc, ok := d[sel]
if !ok {
pc, input, err := d.getSelection(input)
if err != nil {
return 0
}
return pc.RequiredGas(input[32:])
return pc.RequiredGas(input)
}

func (d PrecompileDemux) Finalise(api cc_api.API) error {
Expand All @@ -89,12 +92,11 @@ func (d PrecompileDemux) Commit(api cc_api.API) error {
}

func (d PrecompileDemux) Run(api cc_api.API, input []byte) ([]byte, error) {
sel := d.getSelect(input)
pc, ok := d[sel]
if !ok {
return nil, errors.New("invalid select value")
pc, input, err := d.getSelection(input)
if err != nil {
return nil, err
}
return pc.Run(api, input[32:])
return pc.Run(api, input)
}

var _ cc_api.Precompile = &PrecompileDemux{}
28 changes: 14 additions & 14 deletions concrete/lib/precompile_utils_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,54 +69,54 @@ func (p *BlankMethodPrecompile) CallRunWithArgs(run func(concrete cc_api.API, ar
var _ MethodPrecompile = &BlankMethodPrecompile{}

type PrecompileWithABI struct {
ABI abi.ABI
Implementations map[string]cc_api.Precompile
ABI abi.ABI
Methods map[string]cc_api.Precompile
}

func NewPrecompileWithABI(contractABI abi.ABI, implementations map[string]MethodPrecompile) *PrecompileWithABI {
func NewPrecompileWithABI(contractABI abi.ABI, methods map[string]MethodPrecompile) *PrecompileWithABI {
p := &PrecompileWithABI{
ABI: contractABI,
Implementations: make(map[string]cc_api.Precompile),
ABI: contractABI,
Methods: make(map[string]cc_api.Precompile),
}
for name, method := range contractABI.Methods {
impl, ok := implementations[name]
impl, ok := methods[name]
if !ok {
panic("missing implementation for " + name)
}
impl.Init(method)
p.Implementations[string(method.ID)] = impl
p.Methods[string(method.ID)] = impl
}
return p
}

func (p *PrecompileWithABI) getImplementation(input []byte) (cc_api.Precompile, []byte, error) {
func (p *PrecompileWithABI) getMethod(input []byte) (cc_api.Precompile, []byte, error) {
id := input[:4]
input = input[4:]
impl, ok := p.Implementations[string(id)]
impl, ok := p.Methods[string(id)]
if !ok {
return nil, nil, errors.New("invalid method ID")
}
return impl, input, nil
}

func (p *PrecompileWithABI) MutatesStorage(input []byte) bool {
pc, input, err := p.getImplementation(input)
pc, input, err := p.getMethod(input)
if err != nil {
return false
}
return pc.MutatesStorage(input)
}

func (p *PrecompileWithABI) RequiredGas(input []byte) uint64 {
pc, input, err := p.getImplementation(input)
pc, input, err := p.getMethod(input)
if err != nil {
return 0
}
return pc.RequiredGas(input)
}

func (p *PrecompileWithABI) Finalise(api cc_api.API) error {
for _, pc := range p.Implementations {
for _, pc := range p.Methods {
if err := pc.Finalise(api); err != nil {
return err
}
Expand All @@ -125,7 +125,7 @@ func (p *PrecompileWithABI) Finalise(api cc_api.API) error {
}

func (p *PrecompileWithABI) Commit(api cc_api.API) error {
for _, pc := range p.Implementations {
for _, pc := range p.Methods {
if err := pc.Commit(api); err != nil {
return err
}
Expand All @@ -134,7 +134,7 @@ func (p *PrecompileWithABI) Commit(api cc_api.API) error {
}

func (p *PrecompileWithABI) Run(api cc_api.API, input []byte) ([]byte, error) {
pc, input, err := p.getImplementation(input)
pc, input, err := p.getMethod(input)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8e48b3d

Please sign in to comment.