Skip to content

Commit d4f8bb1

Browse files
committed
update contractFetchAddress in chains.json and enhance error logging in verify.go
1 parent 08b5c71 commit d4f8bb1

3 files changed

Lines changed: 37 additions & 67 deletions

File tree

compile.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -95,40 +95,6 @@ type SolcOutput struct {
9595
CompileTarget string
9696
}
9797

98-
func (o *SolcOutput) PickDeployedBytesCode(compileTarget, contractName string) string {
99-
if compileTarget == "" {
100-
for target := range o.Contracts {
101-
for name := range o.Contracts[target] {
102-
return o.Contracts[target][name].Evm.DeployedBytecode.Object
103-
}
104-
}
105-
}
106-
return o.Contracts[compileTarget][contractName].Evm.DeployedBytecode.Object
107-
}
108-
109-
func (o *SolcOutput) PickBytesCode(compileTarget, contractName string) string {
110-
if compileTarget == "" {
111-
for target := range o.Contracts {
112-
for name := range o.Contracts[target] {
113-
return o.Contracts[target][name].Evm.Bytecode.Object
114-
}
115-
}
116-
}
117-
return o.Contracts[compileTarget][contractName].Evm.Bytecode.Object
118-
}
119-
120-
func (o *SolcOutput) retryToFindCompileTarget() {
121-
for target := range o.Contracts {
122-
for contractName, source := range o.Contracts[target] {
123-
if source.Evm.Bytecode.Object != "" {
124-
o.CompileTarget = target
125-
o.ContractName = contractName
126-
return
127-
}
128-
}
129-
}
130-
}
131-
13298
type SolcContract struct {
13399
Abi []any `json:"abi"`
134100
Evm struct {
@@ -173,12 +139,6 @@ func (s *SolcMetadata) recompileContract(_ context.Context, version string) (*So
173139
if err = json.Unmarshal(stdoutBuf.Bytes(), &result); err != nil {
174140
return nil, err
175141
}
176-
if result.CompileTarget == "" || result.ContractName == "" {
177-
result.retryToFindCompileTarget()
178-
}
179-
if _, ok := result.Contracts[result.CompileTarget][result.ContractName]; !ok {
180-
result.retryToFindCompileTarget()
181-
}
182142
return &result, nil
183143
}
184144

revive.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ func (s *ReviveMetadata) recompileContract(_ context.Context, version string) (*
5656
if err = json.Unmarshal(stdoutBuf.Bytes(), &result); err != nil {
5757
return nil, err
5858
}
59-
if result.CompileTarget == "" || result.ContractName == "" {
60-
result.retryToFindCompileTarget()
61-
}
62-
if _, ok := result.Contracts[result.CompileTarget][result.ContractName]; !ok {
63-
result.retryToFindCompileTarget()
64-
}
6559
return &result, nil
6660
}
6761

verify.go

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,32 +92,48 @@ type Match struct {
9292
}
9393

9494
func (v *VerificationRequest) compareBytecodes(ctx context.Context, chainBytecode string, compiledOutput *SolcOutput) (*Match, error) {
95-
recompileDeployCodeWithLibraries := addLibraryAddresses(compiledOutput.PickDeployedBytesCode(compiledOutput.CompileTarget, compiledOutput.ContractName), chainBytecode).Replaced
96-
if util.TrimHex(recompileDeployCodeWithLibraries) == util.TrimHex(chainBytecode) {
97-
return &Match{Status: perfect}, nil
98-
}
99-
10095
trimmedChainBytecode := util.TrimHex(BytecodeWithoutMetadata(chainBytecode))
101-
trimmedWithLibraries := util.TrimHex(BytecodeWithoutMetadata(recompileDeployCodeWithLibraries))
102-
if trimmedChainBytecode == util.TrimHex(trimmedWithLibraries) {
103-
return &Match{Status: partial}, nil
104-
}
96+
trimmedRawChainBytecode := util.TrimHex(chainBytecode)
97+
createData := ""
98+
fetchedCreateData := false
99+
100+
for compileTarget, contracts := range compiledOutput.Contracts {
101+
for contractName, contract := range contracts {
102+
recompileDeployCodeWithLibraries := addLibraryAddresses(contract.Evm.DeployedBytecode.Object, chainBytecode).Replaced
103+
if util.TrimHex(recompileDeployCodeWithLibraries) == trimmedRawChainBytecode {
104+
compiledOutput.CompileTarget = compileTarget
105+
compiledOutput.ContractName = contractName
106+
return &Match{Status: perfect}, nil
107+
}
105108

106-
if len(trimmedChainBytecode) == len(trimmedWithLibraries) {
107-
createData, err := fetchCreateBytecode(ctx, v.Address, v.Chain)
108-
if err != nil {
109-
return &Match{Status: mismatch}, fmt.Errorf("fetch create bytecode failed: please retry later")
110-
}
109+
trimmedWithLibraries := util.TrimHex(BytecodeWithoutMetadata(recompileDeployCodeWithLibraries))
110+
if trimmedChainBytecode == trimmedWithLibraries {
111+
compiledOutput.CompileTarget = compileTarget
112+
compiledOutput.ContractName = contractName
113+
return &Match{Status: partial}, nil
114+
}
111115

112-
createData = util.TrimHex(createData)
113-
if len(createData) > 0 {
114-
recompileBytesCodeWithLibraries := addLibraryAddresses(compiledOutput.PickBytesCode(compiledOutput.CompileTarget, compiledOutput.ContractName), createData).Replaced
115-
encodedConstructorArgs := extractEncodedConstructorArgs(createData, recompileBytesCodeWithLibraries)
116-
if strings.HasPrefix(createData, BytecodeWithoutMetadata(recompileBytesCodeWithLibraries)) {
117-
return &Match{Status: perfect, ConstructorArgs: encodedConstructorArgs}, nil
116+
if len(trimmedChainBytecode) == len(trimmedWithLibraries) {
117+
if !fetchedCreateData {
118+
var err error
119+
createData, err = fetchCreateBytecode(ctx, v.Address, v.Chain)
120+
if err != nil {
121+
return &Match{Status: mismatch}, fmt.Errorf("fetch create bytecode failed: please retry later")
122+
}
123+
createData = util.TrimHex(createData)
124+
fetchedCreateData = true
125+
}
126+
if len(createData) > 0 {
127+
recompileBytesCodeWithLibraries := addLibraryAddresses(contract.Evm.Bytecode.Object, createData).Replaced
128+
encodedConstructorArgs := extractEncodedConstructorArgs(createData, recompileBytesCodeWithLibraries)
129+
if strings.HasPrefix(createData, BytecodeWithoutMetadata(recompileBytesCodeWithLibraries)) {
130+
compiledOutput.CompileTarget = compileTarget
131+
compiledOutput.ContractName = contractName
132+
return &Match{Status: perfect, ConstructorArgs: encodedConstructorArgs}, nil
133+
}
134+
}
118135
}
119136
}
120-
121137
}
122138
return &Match{Status: mismatch}, nil
123139
}

0 commit comments

Comments
 (0)