This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
compilers.go
271 lines (242 loc) · 6.7 KB
/
compilers.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package compile
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/hyperledger/burrow/crypto"
log "github.com/sirupsen/logrus"
)
type SolidityInput struct {
Language string `json:"language"`
Sources map[string]SolidityInputSource `json:"sources"`
Settings struct {
Libraries map[string]map[string]string `json:"libraries"`
Optimizer struct {
Enabled bool `json:"enabled"`
} `json:"optimizer"`
OutputSelection struct {
File struct {
OutputType []string `json:"*"`
} `json:"*"`
} `json:"outputSelection"`
} `json:"settings"`
}
type SolidityInputSource struct {
Content string `json:"content,omitempty"`
Urls []string `json:"urls,omitempty"`
}
type SolidityOutput struct {
Contracts map[string]map[string]SolidityContract
Errors []struct {
Component string
FormattedMessage string
Message string
Severity string
Type string
}
}
type SolidityContract struct {
Abi json.RawMessage
Evm struct {
Bytecode struct {
Object string
Opcodes string
LinkReferences json.RawMessage
}
}
Devdoc json.RawMessage
Userdoc json.RawMessage
Metadata string
}
type Response struct {
Objects []ResponseItem `json:"objects"`
Warning string `json:"warning"`
Version string `json:"version"`
Error string `json:"error"`
}
// Compile response object
type ResponseItem struct {
Filename string `json:"filename"`
Objectname string `json:"objectname"`
Contract SolidityContract `json:"binary"`
}
func LoadSolidityContract(file string) (*SolidityContract, error) {
codeB, err := ioutil.ReadFile(file)
if err != nil {
return &SolidityContract{}, err
}
contract := SolidityContract{}
err = json.Unmarshal(codeB, &contract)
if err != nil {
return &SolidityContract{}, err
}
return &contract, nil
}
func (contract *SolidityContract) Save(dir, file string) error {
str, err := json.Marshal(*contract)
if err != nil {
return err
}
// This will make the contract file appear atomically
// This is important since if we run concurrent jobs, one job could be compiling a solidity
// file while another reads the bin file. If write is incomplete, it will result in failures
f, err := ioutil.TempFile(dir, "bin.*.txt")
if err != nil {
return err
}
defer os.Remove(f.Name())
_, err = f.Write(str)
if err != nil {
return err
}
f.Close()
return os.Rename(f.Name(), filepath.Join(dir, file))
}
func (contract *SolidityContract) Link(libraries map[string]string) error {
bin := contract.Evm.Bytecode.Object
if !strings.Contains(bin, "_") {
return nil
}
var links map[string]map[string][]struct{ Start, Length int }
err := json.Unmarshal(contract.Evm.Bytecode.LinkReferences, &links)
if err != nil {
return err
}
for _, f := range links {
for name, relos := range f {
addr, ok := libraries[name]
if !ok {
return fmt.Errorf("library %s is not defined", name)
}
for _, relo := range relos {
if relo.Length != crypto.AddressLength {
return fmt.Errorf("linkReference should be %d bytes long, not %d", crypto.AddressLength, relo.Length)
}
if len(addr) != crypto.AddressHexLength {
return fmt.Errorf("address %s should be %d character long, not %d", addr, crypto.AddressHexLength, len(addr))
}
start := relo.Start * 2
end := relo.Start*2 + crypto.AddressHexLength
if bin[start+1] != '_' || bin[end-1] != '_' {
return fmt.Errorf("relocation dummy not found at %d in %s ", relo.Start, bin)
}
bin = bin[:start] + addr + bin[end:]
}
}
}
contract.Evm.Bytecode.Object = bin
return nil
}
func Compile(file string, optimize bool, libraries map[string]string) (*Response, error) {
input := SolidityInput{Language: "Solidity", Sources: make(map[string]SolidityInputSource)}
input.Sources[file] = SolidityInputSource{Urls: []string{file}}
input.Settings.Optimizer.Enabled = optimize
input.Settings.OutputSelection.File.OutputType = []string{"abi", "evm.bytecode.linkReferences", "metadata", "bin", "devdoc"}
input.Settings.Libraries = make(map[string]map[string]string)
input.Settings.Libraries[""] = make(map[string]string)
if libraries != nil {
for l, a := range libraries {
input.Settings.Libraries[""][l] = "0x" + a
}
}
command, err := json.Marshal(input)
if err != nil {
return nil, err
}
log.WithField("Command: ", string(command)).Debug("Command Input")
result, err := runSolidity(string(command))
if err != nil {
return nil, err
}
log.WithField("Command Result: ", result).Debug("Command Output")
output := SolidityOutput{}
err = json.Unmarshal([]byte(result), &output)
if err != nil {
return nil, err
}
respItemArray := make([]ResponseItem, 0)
for f, s := range output.Contracts {
for contract, item := range s {
respItem := ResponseItem{
Filename: f,
Objectname: objectName(contract),
Contract: item,
}
respItemArray = append(respItemArray, respItem)
}
}
warnings := ""
errors := ""
for _, msg := range output.Errors {
if msg.Type == "Warning" {
warnings += msg.FormattedMessage
} else {
errors += msg.FormattedMessage
}
}
for _, re := range respItemArray {
log.WithFields(log.Fields{
"name": re.Objectname,
"bin": re.Contract.Evm.Bytecode.Object,
"abi": string(re.Contract.Abi),
}).Debug("Response formulated")
}
resp := Response{
Objects: respItemArray,
Warning: warnings,
Error: errors,
}
return &resp, nil
}
func objectName(contract string) string {
if contract == "" {
return ""
}
parts := strings.Split(strings.TrimSpace(contract), ":")
return parts[len(parts)-1]
}
func runSolidity(jsonCmd string) (string, error) {
buf := bytes.NewBufferString(jsonCmd)
shellCmd := exec.Command("solc", "--standard-json", "--allow-paths", "/")
shellCmd.Stdin = buf
output, err := shellCmd.CombinedOutput()
s := string(output)
return s, err
}
func PrintResponse(resp Response, cli bool) {
if resp.Error != "" {
log.Warn(resp.Error)
} else {
for _, r := range resp.Objects {
message := log.WithFields((log.Fields{
"name": r.Objectname,
"bin": r.Contract.Evm.Bytecode,
"abi": string(r.Contract.Abi[:]),
"link": string(r.Contract.Evm.Bytecode.LinkReferences[:]),
}))
if cli {
message.Warn("Response")
} else {
message.Info("Response")
}
}
}
}
func extractObjectNames(script []byte) ([]string, error) {
regExpression, err := regexp.Compile("(contract|library) (.+?) (is)?(.+?)?({)")
if err != nil {
return nil, err
}
objectNamesList := regExpression.FindAllSubmatch(script, -1)
var objects []string
for _, objectNames := range objectNamesList {
objects = append(objects, string(objectNames[2]))
}
return objects, nil
}