forked from xuperchain/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
173 lines (148 loc) · 3.97 KB
/
core.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
package abi
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/hyperledger/burrow/deploy/compile"
log "github.com/sirupsen/logrus"
)
type Variable struct {
Name string
Value string
}
func init() {
var err error
RevertAbi, err = ReadAbiSpec([]byte(`[{"name":"Error","type":"function","outputs":[{"type":"string"}],"inputs":[{"type":"string"}]}]`))
if err != nil {
panic(fmt.Sprintf("internal error: failed to build revert abi: %v", err))
}
}
var RevertAbi *AbiSpec
func ReadAbiFormulateCallFile(abiLocation, binPath, funcName string, args []string) ([]byte, error) {
abiSpecBytes, err := readAbi(binPath, abiLocation)
if err != nil {
return []byte{}, err
}
log.WithField("=>", string(abiSpecBytes)).Debug("ABI Specification (Formulate)")
log.WithFields(log.Fields{
"function": funcName,
"arguments": fmt.Sprintf("%v", args),
}).Debug("Packing Call via ABI")
return Packer(abiSpecBytes, funcName, args...)
}
func ReadAbiFormulateCall(abiSpecBytes []byte, funcName string, args []string) ([]byte, error) {
log.WithField("=>", string(abiSpecBytes)).Debug("ABI Specification (Formulate)")
log.WithFields(log.Fields{
"function": funcName,
"arguments": fmt.Sprintf("%v", args),
}).Debug("Packing Call via ABI")
return Packer(string(abiSpecBytes), funcName, args...)
}
func ReadAndDecodeContractReturn(abiLocation, binPath, funcName string, resultRaw []byte) ([]*Variable, error) {
abiSpecBytes, err := readAbi(binPath, abiLocation)
if err != nil {
return nil, err
}
log.WithField("=>", abiSpecBytes).Debug("ABI Specification (Decode)")
// Unpack the result
return Unpacker(abiSpecBytes, funcName, resultRaw)
}
//Convenience Packing Functions
func Packer(abiData, funcName string, args ...string) ([]byte, error) {
abiSpec, err := ReadAbiSpec([]byte(abiData))
if err != nil {
log.WithFields(log.Fields{
"abi": abiData,
"error": err.Error(),
}).Error("Failed to decode abi spec")
return nil, err
}
iArgs := make([]interface{}, len(args))
for i, s := range args {
iArgs[i] = interface{}(s)
}
packedBytes, err := abiSpec.Pack(funcName, iArgs...)
if err != nil {
log.WithFields(log.Fields{
"abi": abiData,
"error": err.Error(),
}).Error("Failed to encode abi spec")
return nil, err
}
return packedBytes, nil
}
func Unpacker(abiData, name string, data []byte) ([]*Variable, error) {
abiSpec, err := ReadAbiSpec([]byte(abiData))
if err != nil {
return nil, err
}
var args []Argument
if name == "" {
args = abiSpec.Constructor.Outputs
} else {
if _, ok := abiSpec.Functions[name]; ok {
args = abiSpec.Functions[name].Outputs
} else {
args = abiSpec.Fallback.Outputs
}
}
if args == nil {
return nil, fmt.Errorf("no such function")
}
vars := make([]*Variable, len(args))
if len(args) == 0 {
return nil, nil
}
vals := make([]interface{}, len(args))
for i, _ := range vals {
vals[i] = new(string)
}
err = Unpack(args, data, vals...)
if err != nil {
return nil, err
}
for i, a := range args {
if a.Name != "" {
vars[i] = &Variable{Name: a.Name, Value: *(vals[i].(*string))}
} else {
vars[i] = &Variable{Name: fmt.Sprintf("%d", i), Value: *(vals[i].(*string))}
}
}
return vars, nil
}
func readAbi(root, contract string) (string, error) {
p := path.Join(root, stripHex(contract))
if _, err := os.Stat(p); err != nil {
log.WithField("abifile", p).Debug("Tried, not found")
p = path.Join(root, stripHex(contract)+".bin")
if _, err = os.Stat(p); err != nil {
log.WithField("abifile", p).Debug("Tried, not found")
return "", fmt.Errorf("Abi doesn't exist for =>\t%s", p)
}
}
log.WithField("abifile", p).Debug("Found ABI")
b, err := ioutil.ReadFile(p)
if err != nil {
return "", err
}
sol := compile.SolidityOutputContract{}
err = json.Unmarshal(b, &sol)
if err != nil {
return "", err
}
return string(sol.Abi), nil
}
func stripHex(s string) string {
if len(s) > 1 {
if s[:2] == "0x" {
s = s[2:]
if len(s)%2 != 0 {
s = "0" + s
}
return s
}
}
return s
}