This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
/
project.go
188 lines (164 loc) · 4.99 KB
/
project.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2018 karma.run AG. All rights reserved.
package extract // import "github.com/karmarun/karma.link/ast/extract"
import (
"encoding/hex"
"fmt"
"github.com/karmarun/karma.link/ast"
"github.com/karmarun/karma.link/types"
"strings"
)
// Project extracts an entire smart contract project's combined type information and structure.
// It will remove the longest shared path prefix among all source units (solidity files).
// E.g. files "a/x/b.sol" and "a/c.sol" will be referenced as "x/b.sol" and "c.sol", respectively.
// TODO: Windows support: normalize paths to forward slashes without drive letters, etc.
func Project(combined ast.Combined) (types.Project, error) {
lpp := longestPathPrefix{}
for _, path := range combined.SourceList {
lpp.Observe(path)
}
typeMap, sourceUnits := make(types.Map, 128), make(map[string]ast.SourceUnit, len(combined.Sources))
for path, source := range combined.Sources {
path = lpp.RemovePrefix(path)
unserialized, e := ast.UnserializeJSON(source.AST)
if e != nil {
return types.Project{}, e
}
sourceUnit := unserialized.(ast.SourceUnit)
sourceUnits[path] = sourceUnit
ts, e := Types(path, sourceUnit)
if e != nil {
return types.Project{}, e
}
for id, typ := range ts {
typeMap[id] = typ
}
}
{ // resolve all type references
var resolve func(t types.Type) types.Type
resolve = func(t types.Type) types.Type {
if ref, ok := t.(types.Reference); ok {
return typeMap.Deref(ref).Map(resolve)
}
return t
}
for ref, typ := range typeMap {
typeMap[ref] = typ.Map(resolve)
}
}
contractMap := make(map[int]*types.Contract, len(sourceUnits)*2)
for path, sourceUnit := range sourceUnits {
contractDefinitions := ContractDefinitions(sourceUnit)
for _, contractDefinition := range contractDefinitions {
functions, e := ContractAPI(contractDefinition, typeMap)
if e != nil {
return types.Project{}, e
}
api := make(map[string]types.Function, len(functions))
for _, function := range functions {
api[string(function.SoliditySignature())] = function
}
bin := []byte(nil)
if compiled, ok := combined.Contracts[lpp.PrependPrefix(path)+`:`+contractDefinition.Name]; ok {
bs, e := hex.DecodeString(compiled.Binary)
if e != nil {
return types.Project{}, fmt.Errorf(`invalid binary in contract: %s`, path)
}
bin = bs
}
contractMap[contractDefinition.Header().Id] = &types.Contract{
File: path,
Name: contractDefinition.Name,
Parents: make([]*types.Contract, 0, len(contractDefinition.LinearizedBaseContracts)-1), // NOTE: filled below
Types: make(map[string]types.Type, 16), // idem
NatSpec: contractDefinition.Documentation,
Kind: contractDefinition.ContractKind,
API: api,
Definition: contractDefinition,
Binary: bin,
}
}
}
project := types.Project{
Path: "",
Files: make(map[string]map[string]*types.Contract, len(contractMap)),
}
project.Path, _ = lpp.Prefix()
for _, contract := range contractMap {
// NOTE: first element of contract.Definition.LinearizedBaseContracts is own ID
for _, parentId := range contract.Definition.LinearizedBaseContracts[1:] {
parent, ok := contractMap[parentId]
if !ok {
return types.Project{}, fmt.Errorf(`missing contract parent definition`)
}
contract.Parents = append(contract.Parents, parent)
}
for _, typ := range typeMap {
if named, ok := typ.(types.Named); ok && strings.HasPrefix(named.Name, (contract.File+":"+contract.Name)) {
typeName := named.Name[strings.LastIndex(named.Name, `.`)+1:]
contract.Types[typeName] = named
}
}
contracts := project.Files[contract.File]
if contracts == nil {
contracts = make(map[string]*types.Contract, 8)
}
contracts[contract.Name] = contract
project.Files[contract.File] = contracts
}
return project, nil
}
type longestPathPrefix struct {
init bool
prfx string
}
func (lpp *longestPathPrefix) Observe(path string) {
if lpp.init {
lpp.prfx = trimToLastSeparator(longestCommonPrefix(path, lpp.prfx))
} else {
lpp.init, lpp.prfx = true, trimToLastSeparator(path)
}
}
func (lpp *longestPathPrefix) RemovePrefix(path string) string {
l := len(lpp.prfx)
if path[:l] != lpp.prfx {
panic("")
}
return path[l:]
}
func (lpp *longestPathPrefix) PrependPrefix(path string) string {
if len(path) > 0 && path[0] == '/' {
return lpp.prfx + path[1:]
}
return lpp.prfx + path
}
func (lpp *longestPathPrefix) Prefix() (string, bool) {
return lpp.prfx, lpp.init
}
// TODO: Windows support with \ and drive letters with colons.
func trimToLastSeparator(s string) string {
for len(s) > 0 && s[len(s)-1] != '/' {
s = s[:len(s)-1]
}
return s
}
func longestCommonPrefix(a, b string) string {
if a == "" || b == "" {
return ""
}
for i, l := 0, minInt(len(a), len(b)); i < l; i++ {
if a[i] == b[i] {
continue
}
return a[:i]
}
if len(a) < len(b) {
return a
}
return b
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}