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

transition snapshots script to common framework (#13398)

parent b62e7740
package solc package solc
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/ethereum/go-ethereum/accounts/abi" "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 { type CompilerInput struct {
Language string `json:"language"` Language string `json:"language"`
Sources map[string]map[string]string `json:"sources"` Sources map[string]map[string]string `json:"sources"`
...@@ -39,7 +52,7 @@ type CompilerOutputContracts map[string]CompilerOutputContract ...@@ -39,7 +52,7 @@ type CompilerOutputContracts map[string]CompilerOutputContract
// CompilerOutputContract represents the solc compiler output for a contract. // CompilerOutputContract represents the solc compiler output for a contract.
// Ignoring some fields such as devdoc and userdoc. // Ignoring some fields such as devdoc and userdoc.
type CompilerOutputContract struct { type CompilerOutputContract struct {
Abi abi.ABI `json:"abi"` Abi AbiType `json:"abi"`
Evm CompilerOutputEvm `json:"evm"` Evm CompilerOutputEvm `json:"evm"`
Metadata string `json:"metadata"` Metadata string `json:"metadata"`
StorageLayout StorageLayout `json:"storageLayout"` StorageLayout StorageLayout `json:"storageLayout"`
...@@ -72,6 +85,14 @@ func (s *StorageLayout) GetStorageLayoutType(name string) (StorageLayoutType, er ...@@ -72,6 +85,14 @@ func (s *StorageLayout) GetStorageLayoutType(name string) (StorageLayoutType, er
return StorageLayoutType{}, fmt.Errorf("%s not found", name) 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 { type StorageLayoutEntry struct {
AstId uint `json:"astId"` AstId uint `json:"astId"`
Contract string `json:"contract"` Contract string `json:"contract"`
...@@ -241,7 +262,7 @@ type Expression struct { ...@@ -241,7 +262,7 @@ type Expression struct {
} }
type ForgeArtifact struct { type ForgeArtifact struct {
Abi abi.ABI `json:"abi"` Abi AbiType `json:"abi"`
Bytecode CompilerOutputBytecode `json:"bytecode"` Bytecode CompilerOutputBytecode `json:"bytecode"`
DeployedBytecode CompilerOutputBytecode `json:"deployedBytecode"` DeployedBytecode CompilerOutputBytecode `json:"deployedBytecode"`
MethodIdentifiers map[string]string `json:"methodIdentifiers"` MethodIdentifiers map[string]string `json:"methodIdentifiers"`
...@@ -266,7 +287,7 @@ type ForgeCompilerInfo struct { ...@@ -266,7 +287,7 @@ type ForgeCompilerInfo struct {
} }
type ForgeMetadataOutput struct { type ForgeMetadataOutput struct {
Abi abi.ABI `json:"abi"` Abi AbiType `json:"abi"`
DevDoc ForgeDocObject `json:"devdoc"` DevDoc ForgeDocObject `json:"devdoc"`
UserDoc ForgeDocObject `json:"userdoc"` UserDoc ForgeDocObject `json:"userdoc"`
} }
......
package common package common
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"path/filepath"
"runtime" "runtime"
"sync" "sync"
"sync/atomic" "sync/atomic"
...@@ -100,8 +100,7 @@ func FindFiles(includes, excludes []string) (map[string]string, error) { ...@@ -100,8 +100,7 @@ func FindFiles(includes, excludes []string) (map[string]string, error) {
return nil, fmt.Errorf("glob pattern error: %w", err) return nil, fmt.Errorf("glob pattern error: %w", err)
} }
for _, match := range matches { for _, match := range matches {
name := filepath.Base(match) included[match] = match
included[name] = match
} }
} }
...@@ -112,7 +111,7 @@ func FindFiles(includes, excludes []string) (map[string]string, error) { ...@@ -112,7 +111,7 @@ func FindFiles(includes, excludes []string) (map[string]string, error) {
return nil, fmt.Errorf("glob pattern error: %w", err) return nil, fmt.Errorf("glob pattern error: %w", err)
} }
for _, match := range matches { for _, match := range matches {
excluded[filepath.Base(match)] = struct{}{} excluded[match] = struct{}{}
} }
} }
...@@ -137,3 +136,22 @@ func ReadForgeArtifact(path string) (*solc.ForgeArtifact, error) { ...@@ -137,3 +136,22 @@ func ReadForgeArtifact(path string) (*solc.ForgeArtifact, error) {
return &artifact, nil 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) { ...@@ -41,7 +41,7 @@ func processFile(path string) (*common.Void, []error) {
func extractTestNames(artifact *solc.ForgeArtifact) []string { func extractTestNames(artifact *solc.ForgeArtifact) []string {
isTest := false isTest := false
for _, entry := range artifact.Abi.Methods { for _, entry := range artifact.Abi.Parsed.Methods {
if entry.Name == "IS_TEST" { if entry.Name == "IS_TEST" {
isTest = true isTest = true
break break
...@@ -52,7 +52,7 @@ func extractTestNames(artifact *solc.ForgeArtifact) []string { ...@@ -52,7 +52,7 @@ func extractTestNames(artifact *solc.ForgeArtifact) []string {
} }
names := []string{} names := []string{}
for _, entry := range artifact.Abi.Methods { for _, entry := range artifact.Abi.Parsed.Methods {
if !strings.HasPrefix(entry.Name, "test") { if !strings.HasPrefix(entry.Name, "test") {
continue continue
} }
......
...@@ -218,13 +218,15 @@ func TestExtractTestNames(t *testing.T) { ...@@ -218,13 +218,15 @@ func TestExtractTestNames(t *testing.T) {
{ {
name: "valid test contract", name: "valid test contract",
artifact: &solc.ForgeArtifact{ artifact: &solc.ForgeArtifact{
Abi: abi.ABI{ Abi: solc.AbiType{
Methods: map[string]abi.Method{ Parsed: abi.ABI{
"IS_TEST": {Name: "IS_TEST"}, Methods: map[string]abi.Method{
"test_something_succeeds": {Name: "test_something_succeeds"}, "IS_TEST": {Name: "IS_TEST"},
"test_other_fails": {Name: "test_other_fails"}, "test_something_succeeds": {Name: "test_something_succeeds"},
"not_a_test": {Name: "not_a_test"}, "test_other_fails": {Name: "test_other_fails"},
"testFuzz_something_works": {Name: "testFuzz_something_works"}, "not_a_test": {Name: "not_a_test"},
"testFuzz_something_works": {Name: "testFuzz_something_works"},
},
}, },
}, },
}, },
...@@ -237,10 +239,12 @@ func TestExtractTestNames(t *testing.T) { ...@@ -237,10 +239,12 @@ func TestExtractTestNames(t *testing.T) {
{ {
name: "non-test contract", name: "non-test contract",
artifact: &solc.ForgeArtifact{ artifact: &solc.ForgeArtifact{
Abi: abi.ABI{ Abi: solc.AbiType{
Methods: map[string]abi.Method{ Parsed: abi.ABI{
"test_something_succeeds": {Name: "test_something_succeeds"}, Methods: map[string]abi.Method{
"not_a_test": {Name: "not_a_test"}, "test_something_succeeds": {Name: "test_something_succeeds"},
"not_a_test": {Name: "not_a_test"},
},
}, },
}, },
}, },
...@@ -249,8 +253,10 @@ func TestExtractTestNames(t *testing.T) { ...@@ -249,8 +253,10 @@ func TestExtractTestNames(t *testing.T) {
{ {
name: "empty contract", name: "empty contract",
artifact: &solc.ForgeArtifact{ artifact: &solc.ForgeArtifact{
Abi: abi.ABI{ Abi: solc.AbiType{
Methods: map[string]abi.Method{}, Parsed: abi.ABI{
Methods: map[string]abi.Method{},
},
}, },
}, },
want: nil, want: nil,
...@@ -258,11 +264,13 @@ func TestExtractTestNames(t *testing.T) { ...@@ -258,11 +264,13 @@ func TestExtractTestNames(t *testing.T) {
{ {
name: "test contract with no test methods", name: "test contract with no test methods",
artifact: &solc.ForgeArtifact{ artifact: &solc.ForgeArtifact{
Abi: abi.ABI{ Abi: solc.AbiType{
Methods: map[string]abi.Method{ Parsed: abi.ABI{
"IS_TEST": {Name: "IS_TEST"}, Methods: map[string]abi.Method{
"not_a_test": {Name: "not_a_test"}, "IS_TEST": {Name: "IS_TEST"},
"another_method": {Name: "another_method"}, "not_a_test": {Name: "not_a_test"},
"another_method": {Name: "another_method"},
},
}, },
}, },
}, },
......
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