/
blockchain.go
180 lines (137 loc) · 3.65 KB
/
blockchain.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
package v8
/*
#include "v8/vm.h"
*/
import "C"
import (
"encoding/json"
"errors"
"github.com/iost-official/go-iost/vm/host"
)
var (
// ErrGetSandbox error when GetSandbox failed.
ErrGetSandbox = errors.New("get sandbox failed")
)
func newCStr(str string) C.CStr {
cstr := C.CStr{}
cstr.data = C.CString(str)
cstr.size = C.int(len(str))
return cstr
}
func (cstr *C.CStr) SetString(str string) {
cstr.data = C.CString(str)
cstr.size = C.int(len(str))
}
func (cstr C.CStr) GoString() string {
return C.GoStringN(cstr.data, cstr.size)
}
//export goBlockInfo
func goBlockInfo(cSbx C.SandboxPtr, info *C.CStr, gasUsed *C.size_t) *C.char {
sbx, ok := GetSandbox(cSbx)
if !ok {
return C.CString(ErrGetSandbox.Error())
}
blkInfo, cost := sbx.host.BlockInfo()
*gasUsed = C.size_t(cost.CPU)
info.SetString(string(blkInfo))
return nil
}
//export goTxInfo
func goTxInfo(cSbx C.SandboxPtr, info *C.CStr, gasUsed *C.size_t) *C.char {
sbx, ok := GetSandbox(cSbx)
if !ok {
return C.CString(ErrGetSandbox.Error())
}
txInfo, cost := sbx.host.TxInfo()
*gasUsed = C.size_t(cost.CPU)
info.SetString(string(txInfo))
return nil
}
//export goContextInfo
func goContextInfo(cSbx C.SandboxPtr, info *C.CStr, gasUsed *C.size_t) *C.char {
sbx, ok := GetSandbox(cSbx)
if !ok {
return C.CString(ErrGetSandbox.Error())
}
ctxInfo, cost := sbx.host.ContextInfo()
*gasUsed = C.size_t(cost.CPU)
info.SetString(string(ctxInfo))
return nil
}
//export goCall
func goCall(cSbx C.SandboxPtr, contract, api, args C.CStr, result *C.CStr, gasUsed *C.size_t) *C.char {
sbx, ok := GetSandbox(cSbx)
if !ok {
return C.CString(ErrGetSandbox.Error())
}
contractStr := contract.GoString()
apiStr := api.GoString()
argsStr := args.GoString()
callRs, cost, err := sbx.host.Call(contractStr, apiStr, argsStr)
*gasUsed = C.size_t(cost.CPU)
if err != nil {
return C.CString(err.Error())
}
rsStr, err := json.Marshal(callRs)
if err != nil {
return C.CString(host.ErrInvalidData.Error())
}
result.SetString(string(rsStr))
return nil
}
//export goCallWithAuth
func goCallWithAuth(cSbx C.SandboxPtr, contract, api, args C.CStr, result *C.CStr, gasUsed *C.size_t) *C.char {
sbx, ok := GetSandbox(cSbx)
if !ok {
return C.CString(ErrGetSandbox.Error())
}
contractStr := contract.GoString()
apiStr := api.GoString()
argsStr := args.GoString()
callRs, cost, err := sbx.host.CallWithAuth(contractStr, apiStr, argsStr)
*gasUsed = C.size_t(cost.CPU)
if err != nil {
return C.CString(err.Error())
}
rsStr, err := json.Marshal(callRs)
if err != nil {
return C.CString(host.ErrInvalidData.Error())
}
result.SetString(string(rsStr))
return nil
}
//export goRequireAuth
func goRequireAuth(cSbx C.SandboxPtr, ID, permission C.CStr, ok *C.bool, gasUsed *C.size_t) *C.char {
sbx, sbOk := GetSandbox(cSbx)
if !sbOk {
return C.CString(ErrGetSandbox.Error())
}
pubKeyStr := ID.GoString()
permissionStr := permission.GoString()
callOk, RequireAuthCost := sbx.host.RequireAuth(pubKeyStr, permissionStr)
*ok = C.bool(callOk)
*gasUsed = C.size_t(RequireAuthCost.CPU)
return nil
}
//export goReceipt
func goReceipt(cSbx C.SandboxPtr, content C.CStr, gasUsed *C.size_t) *C.char {
sbx, sbOk := GetSandbox(cSbx)
if !sbOk {
return C.CString(ErrGetSandbox.Error())
}
contentStr := content.GoString()
cost := sbx.host.Receipt(contentStr)
*gasUsed = C.size_t(cost.CPU)
return nil
}
//export goEvent
func goEvent(cSbx C.SandboxPtr, content C.CStr, gasUsed *C.size_t) *C.char {
sbx, sbOk := GetSandbox(cSbx)
if !sbOk {
return C.CString(ErrGetSandbox.Error())
}
contentStr := content.GoString()
cost := sbx.host.PostEvent(contentStr)
*gasUsed = C.size_t(cost.CPU)
return nil
}