-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathcontract.go
109 lines (90 loc) · 3.66 KB
/
contract.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package contracts
import (
"slices"
"strings"
"github.com/crytic/medusa-geth/accounts/abi"
"github.com/crytic/medusa/compilation/types"
)
// Contracts describes an array of contracts
type Contracts []*Contract
// MatchBytecode takes init and/or runtime bytecode and attempts to match it to a contract definition in the
// current list of contracts. It returns the contract definition if found. Otherwise, it returns nil.
func (c Contracts) MatchBytecode(initBytecode []byte, runtimeBytecode []byte) *Contract {
// Loop through all our contract definitions to find a match.
for i := 0; i < len(c); i++ {
// If we have a match, register the deployed contract.
if c[i].CompiledContract().IsMatch(initBytecode, runtimeBytecode) {
return c[i]
}
}
// If we found no definition, return nil.
return nil
}
// Contract describes a compiled smart contract.
type Contract struct {
// name represents the name of the contract.
name string
// sourcePath represents the key used to index the source file in the compilation it was derived from.
sourcePath string
// compiledContract describes the compiled contract data.
compiledContract *types.CompiledContract
// compilation describes the compilation which contains the compiledContract.
compilation *types.Compilation
// PropertyTestMethods are the methods that are property tests.
PropertyTestMethods []abi.Method
// OptimizationTestMethods are the methods that are optimization tests.
OptimizationTestMethods []abi.Method
// AssertionTestMethods are ALL other methods that are not property or optimization tests by default.
// If configured, the methods will be targeted or excluded based on the targetFunctionSignatures
// and excludedFunctionSignatures, respectively.
AssertionTestMethods []abi.Method
}
// NewContract returns a new Contract instance with the provided information.
func NewContract(name string, sourcePath string, compiledContract *types.CompiledContract, compilation *types.Compilation) *Contract {
return &Contract{
name: name,
sourcePath: sourcePath,
compiledContract: compiledContract,
compilation: compilation,
}
}
// WithTargetedAssertionMethods filters the assertion test methods to those in the target list.
func (c *Contract) WithTargetedAssertionMethods(target []string) *Contract {
var candidateMethods []abi.Method
for _, method := range c.AssertionTestMethods {
canonicalSig := strings.Join([]string{c.name, method.Sig}, ".")
if slices.Contains(target, canonicalSig) {
candidateMethods = append(candidateMethods, method)
}
}
c.AssertionTestMethods = candidateMethods
return c
}
// WithExcludedAssertionMethods filters the assertion test methods to all methods not in excluded list.
func (c *Contract) WithExcludedAssertionMethods(excludedMethods []string) *Contract {
var candidateMethods []abi.Method
for _, method := range c.AssertionTestMethods {
canonicalSig := strings.Join([]string{c.name, method.Sig}, ".")
if !slices.Contains(excludedMethods, canonicalSig) {
candidateMethods = append(candidateMethods, method)
}
}
c.AssertionTestMethods = candidateMethods
return c
}
// Name returns the name of the contract.
func (c *Contract) Name() string {
return c.name
}
// SourcePath returns the path of the source file containing the contract.
func (c *Contract) SourcePath() string {
return c.sourcePath
}
// CompiledContract returns the compiled contract information including source mappings, byte code, and ABI.
func (c *Contract) CompiledContract() *types.CompiledContract {
return c.compiledContract
}
// Compilation returns the compilation which contains the CompiledContract.
func (c *Contract) Compilation() *types.Compilation {
return c.compilation
}