dependency graph calculation working

This commit is contained in:
Jared Wasinger 2024-11-20 20:48:43 +07:00
parent 46e6dd4fcd
commit cd86fca55c
2 changed files with 21 additions and 15 deletions

View file

@ -342,17 +342,23 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
if _, ok := isLib[name]; !ok { if _, ok := isLib[name]; !ok {
isLib[name] = struct{}{} isLib[name] = struct{}{}
} }
} }
} }
// Check if that type has already been identified as a library }
for i := 0; i < len(types); i++ {
_, ok := isLib[types[i]]
contracts[types[i]].Library = ok
}
// Check if that type has already been identified as a library
for i := 0; i < len(types); i++ {
_, ok := isLib[types[i]]
contracts[types[i]].Library = ok
}
// compute the full set of libraries that each contract depends on.
for _, contract := range contracts {
if contract.Library {
continue
}
// recursively traverse the library dependency graph // recursively traverse the library dependency graph
// of the contract, flattening it into a list. // of the contract, flattening it into a set.
// //
// For abigenv2, we do not generate contract deploy // For abigenv2, we do not generate contract deploy
// methods (which in v1 recursively deploy their // methods (which in v1 recursively deploy their
@ -368,25 +374,25 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
} }
libBin := contracts[contract.Type].InputBin libBin := contracts[contract.Type].InputBin
matches := re.FindAllStringSubmatch(libBin, -1) matches := re.FindAllStringSubmatch(libBin, -1)
var result map[string]struct{} result := make(map[string]struct{})
// 2) recurse, gathering nested library dependencies // 2) recurse, gathering nested library dependencies
for _, match := range matches { for _, match := range matches {
pattern := match[1] pattern := match[1]
result[pattern] = struct{}{} result[pattern] = struct{}{}
depContract := contracts[pattern] depContract := contracts[libs[pattern]]
for subPattern, _ := range findDeps(depContract) { for subPattern, _ := range findDeps(depContract) {
result[subPattern] = struct{}{} result[subPattern] = struct{}{}
} }
} }
return result return result
} }
// take the set of library patterns, convert it to a map of pattern -> type // take the set of library patterns, convert it to a map of type -> pattern
deps := findDeps(contracts[types[i]]) deps := findDeps(contract)
contracts[types[i]].AllLibraries = make(map[string]string) contract.AllLibraries = make(map[string]string)
for contractPattern, _ := range deps { for contractPattern, _ := range deps {
contractType := libs[contractPattern] contractType := libs[contractPattern]
contracts[types[i]].AllLibraries[contractType] = contractPattern contract.AllLibraries[contractType] = contractPattern
} }
} }

View file

@ -39,9 +39,9 @@ var (
{{range $contract := .Contracts}} {{range $contract := .Contracts}}
var {{$contract.Type}}LibraryDeps = map[string]*bind.MetaData{ var {{$contract.Type}}LibraryDeps = map[string]*bind.MetaData{
{{range $pattern, $name := .Libraries}} {{range $pattern, $name := .AllLibraries -}}
"{{$pattern}}": &{{$name}}MetaData, "{{$pattern}}": &{{$name}}MetaData,
{{end}} {{ end}}
} }
// {{.Type}}MetaData contains all meta data concerning the {{.Type}} contract. // {{.Type}}MetaData contains all meta data concerning the {{.Type}} contract.