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

Commit

Permalink
refactor: cast registry pcs unpacks properly (ethereum-optimism#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
therealbytes committed Sep 2, 2023
1 parent aaf2a70 commit c2e53fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
12 changes: 7 additions & 5 deletions concrete/precompiles/precompile_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ import (
//go:embed sol/abi/PrecompileRegistry.abi
var precompileRegistryABI string

type FrameworkMetadata = struct {
Name string `json:"name"`
Version string `json:"version"`
Source string `json:"source"`
}

var PrecompileRegistryMetadata = PrecompileMetadata{
Name: "PrecompileRegistry",
Version: "0.1.0",
Expand Down Expand Up @@ -70,11 +76,7 @@ func (p *getFramework) RequiredGas(input []byte) uint64 {

func (p *getFramework) Run(API api.API, input []byte) ([]byte, error) {
return p.CallRunWithArgs(func(API api.API, args []interface{}) ([]interface{}, error) {
metadata := struct {
Name string `json:"name"`
Version string `json:"version"`
Source string `json:"source"`
}{
metadata := FrameworkMetadata{
"Concrete",
"0.1.0",
"https://github.com/therealbytes/concrete-geth",
Expand Down
26 changes: 3 additions & 23 deletions concrete/precompiles/precompile_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ func TestPrecompileRegistry(t *testing.T) {
r.NoError(err)
_frameworkData, err := pc.ABI.Unpack("getFramework", output)
r.NoError(err)
frameworkData, ok := _frameworkData[0].(struct {
Name string `json:"name"`
Version string `json:"version"`
Source string `json:"source"`
})
frameworkData, ok := _frameworkData[0].(FrameworkMetadata)
r.True(ok)
r.NotEmpty(frameworkData.Name)
r.NotEmpty(frameworkData.Version)
Expand All @@ -69,15 +65,7 @@ func TestPrecompileRegistry(t *testing.T) {
r.NoError(err)
_precompileData, err := pc.ABI.Unpack("getPrecompile", output)
r.NoError(err)
precompileData, ok := _precompileData[0].(struct {
Addr common.Address `json:"addr"`
Name string `json:"name"`
Version string `json:"version"`
Author string `json:"author"`
Description string `json:"description"`
Source string `json:"source"`
ABI string `json:"ABI"`
})
precompileData, ok := _precompileData[0].(PrecompileMetadata)
r.True(ok)
r.EqualValues(expectedMetadata, precompileData)

Expand Down Expand Up @@ -111,15 +99,7 @@ func TestPrecompileRegistry(t *testing.T) {
r.NoError(err)
_pcs, err := pc.ABI.Unpack("getPrecompiles", output)
r.NoError(err)
pcs, ok := _pcs[0].([]struct {
Addr common.Address `json:"addr"`
Name string `json:"name"`
Version string `json:"version"`
Author string `json:"author"`
Description string `json:"description"`
Source string `json:"source"`
ABI string `json:"ABI"`
})
pcs, ok := _pcs[0].([]PrecompileMetadata)
r.True(ok)
r.Equal(3, len(pcs))
contains := false
Expand Down
18 changes: 9 additions & 9 deletions concrete/precompiles/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@ import (
"github.com/ethereum/go-ethereum/concrete/lib"
)

var (
precompiles = make(map[common.Address]api.Precompile)
precompiledAddresses = make([]common.Address, 0)
precompileMetadata = make([]PrecompileMetadata, 0)
metadataByAddress = make(map[common.Address]*PrecompileMetadata)
metadataByName = make(map[string]*PrecompileMetadata)
)

type PrecompileMetadata struct {
type PrecompileMetadata = struct {
Addr common.Address `json:"addr"`
Name string `json:"name"`
Version string `json:"version"`
Expand All @@ -43,6 +35,14 @@ type PrecompileMetadata struct {
ABI string `json:"ABI"`
}

var (
precompiles = make(map[common.Address]api.Precompile)
precompiledAddresses = make([]common.Address, 0)
precompileMetadata = make([]PrecompileMetadata, 0)
metadataByAddress = make(map[common.Address]*PrecompileMetadata)
metadataByName = make(map[string]*PrecompileMetadata)
)

func AddPrecompile(addr common.Address, p api.Precompile, args ...interface{}) error {
var metadata PrecompileMetadata

Expand Down

0 comments on commit c2e53fb

Please sign in to comment.