Commit 26f508cc authored by Michael Amadi's avatar Michael Amadi Committed by GitHub

transition snapshots script to common framework (#13398)

parent b62e7740
package solc
import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
)
type AbiType struct {
Parsed abi.ABI
Raw interface{}
}
func (a *AbiType) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &a.Raw); err != nil {
return err
}
return json.Unmarshal(data, &a.Parsed)
}
type CompilerInput struct {
Language string `json:"language"`
Sources map[string]map[string]string `json:"sources"`
......@@ -39,7 +52,7 @@ type CompilerOutputContracts map[string]CompilerOutputContract
// CompilerOutputContract represents the solc compiler output for a contract.
// Ignoring some fields such as devdoc and userdoc.
type CompilerOutputContract struct {
Abi abi.ABI `json:"abi"`
Abi AbiType `json:"abi"`
Evm CompilerOutputEvm `json:"evm"`
Metadata string `json:"metadata"`
StorageLayout StorageLayout `json:"storageLayout"`
......@@ -72,6 +85,14 @@ func (s *StorageLayout) GetStorageLayoutType(name string) (StorageLayoutType, er
return StorageLayoutType{}, fmt.Errorf("%s not found", name)
}
type AbiSpecStorageLayoutEntry struct {
Bytes uint `json:"bytes,string"`
Label string `json:"label"`
Offset uint `json:"offset"`
Slot uint `json:"slot,string"`
Type string `json:"type"`
}
type StorageLayoutEntry struct {
AstId uint `json:"astId"`
Contract string `json:"contract"`
......@@ -241,7 +262,7 @@ type Expression struct {
}
type ForgeArtifact struct {
Abi abi.ABI `json:"abi"`
Abi AbiType `json:"abi"`
Bytecode CompilerOutputBytecode `json:"bytecode"`
DeployedBytecode CompilerOutputBytecode `json:"deployedBytecode"`
MethodIdentifiers map[string]string `json:"methodIdentifiers"`
......@@ -266,7 +287,7 @@ type ForgeCompilerInfo struct {
}
type ForgeMetadataOutput struct {
Abi abi.ABI `json:"abi"`
Abi AbiType `json:"abi"`
DevDoc ForgeDocObject `json:"devdoc"`
UserDoc ForgeDocObject `json:"userdoc"`
}
......
package common
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"sync"
"sync/atomic"
......@@ -100,8 +100,7 @@ func FindFiles(includes, excludes []string) (map[string]string, error) {
return nil, fmt.Errorf("glob pattern error: %w", err)
}
for _, match := range matches {
name := filepath.Base(match)
included[name] = match
included[match] = match
}
}
......@@ -112,7 +111,7 @@ func FindFiles(includes, excludes []string) (map[string]string, error) {
return nil, fmt.Errorf("glob pattern error: %w", err)
}
for _, match := range matches {
excluded[filepath.Base(match)] = struct{}{}
excluded[match] = struct{}{}
}
}
......@@ -137,3 +136,22 @@ func ReadForgeArtifact(path string) (*solc.ForgeArtifact, error) {
return &artifact, nil
}
func WriteJSON(data interface{}, path string) error {
var out bytes.Buffer
enc := json.NewEncoder(&out)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
err := enc.Encode(data)
if err != nil {
return fmt.Errorf("failed to encode data: %w", err)
}
jsonData := out.Bytes()
if len(jsonData) > 0 && jsonData[len(jsonData)-1] == '\n' { // strip newline
jsonData = jsonData[:len(jsonData)-1]
}
if err := os.WriteFile(path, jsonData, 0644); err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}
......@@ -41,7 +41,7 @@ func processFile(path string) (*common.Void, []error) {
func extractTestNames(artifact *solc.ForgeArtifact) []string {
isTest := false
for _, entry := range artifact.Abi.Methods {
for _, entry := range artifact.Abi.Parsed.Methods {
if entry.Name == "IS_TEST" {
isTest = true
break
......@@ -52,7 +52,7 @@ func extractTestNames(artifact *solc.ForgeArtifact) []string {
}
names := []string{}
for _, entry := range artifact.Abi.Methods {
for _, entry := range artifact.Abi.Parsed.Methods {
if !strings.HasPrefix(entry.Name, "test") {
continue
}
......
......@@ -218,7 +218,8 @@ func TestExtractTestNames(t *testing.T) {
{
name: "valid test contract",
artifact: &solc.ForgeArtifact{
Abi: abi.ABI{
Abi: solc.AbiType{
Parsed: abi.ABI{
Methods: map[string]abi.Method{
"IS_TEST": {Name: "IS_TEST"},
"test_something_succeeds": {Name: "test_something_succeeds"},
......@@ -228,6 +229,7 @@ func TestExtractTestNames(t *testing.T) {
},
},
},
},
want: []string{
"test_something_succeeds",
"test_other_fails",
......@@ -237,28 +239,33 @@ func TestExtractTestNames(t *testing.T) {
{
name: "non-test contract",
artifact: &solc.ForgeArtifact{
Abi: abi.ABI{
Abi: solc.AbiType{
Parsed: abi.ABI{
Methods: map[string]abi.Method{
"test_something_succeeds": {Name: "test_something_succeeds"},
"not_a_test": {Name: "not_a_test"},
},
},
},
},
want: nil,
},
{
name: "empty contract",
artifact: &solc.ForgeArtifact{
Abi: abi.ABI{
Abi: solc.AbiType{
Parsed: abi.ABI{
Methods: map[string]abi.Method{},
},
},
},
want: nil,
},
{
name: "test contract with no test methods",
artifact: &solc.ForgeArtifact{
Abi: abi.ABI{
Abi: solc.AbiType{
Parsed: abi.ABI{
Methods: map[string]abi.Method{
"IS_TEST": {Name: "IS_TEST"},
"not_a_test": {Name: "not_a_test"},
......@@ -266,6 +273,7 @@ func TestExtractTestNames(t *testing.T) {
},
},
},
},
want: []string{},
},
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment