-
Notifications
You must be signed in to change notification settings - Fork 22
/
tx_receipt.go
235 lines (187 loc) · 5.7 KB
/
tx_receipt.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
package packet
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"github.com/Venachain/Venachain/core/types"
precompile "github.com/Venachain/Venachain/cmd/ptransfer/client/precompiled"
"github.com/Venachain/Venachain/cmd/ptransfer/client/utils"
"github.com/Venachain/Venachain/common"
"github.com/Venachain/Venachain/common/hexutil"
"github.com/Venachain/Venachain/crypto"
"github.com/Venachain/Venachain/rlp"
)
var (
txReceiptSuccessCode = hexutil.EncodeUint64(types.ReceiptStatusSuccessful)
txReceiptFailureCode = hexutil.EncodeUint64(types.ReceiptStatusFailed)
)
const (
TxReceiptSuccessMsg = "Operation Succeeded"
TxReceiptFailureMsg = "Operation Failed"
)
func receiptStatusReturn(status string) (result string) {
switch status {
case txReceiptSuccessCode:
result = TxReceiptSuccessMsg
case txReceiptFailureCode:
result = TxReceiptFailureMsg
default:
result = "undefined status. Something wrong"
}
return
}
// Receipt, eth_getTransactionReceipt return data struct
type Receipt struct {
BlockHash string `json:"blockHash"` // hash of the block
BlockNumber string `json:"blockNumber"` // height of the block
ContractAddress string `json:"contractAddress"` // contract address of the contract deployment. otherwise null
CumulativeGasUsed string `json:"cumulativeGasUsed"` //
From string `json:"from"` // the account address used to send the transaction
GasUsed string `json:"gasUsed"` // gas used by executing the transaction
Root string `json:"root"`
To string `json:"to"` // the address the transaction is sent to
TransactionHash string `json:"transactionHash"` // the hash of the transaction
TransactionIndex string `json:"transactionIndex"` // the index of the transaction
Logs RecptLogs `json:"logs"`
Status string `json:"status"` // the execution status of the transaction, "0x1" for success
}
type Log struct {
Address string `json:"address"`
Topics []string `json:"topics"`
Data string `json:"data"`
}
type RecptLogs []*Log
// ParseSysContractResult parsed the rpc response to Receipt object
func ParseTxReceipt(response interface{}) (*Receipt, error) {
var receipt = &Receipt{}
temp, _ := json.Marshal(response)
err := json.Unmarshal(temp, receipt)
if err != nil {
// LogErr.Printf(ErrUnmarshalBytesFormat, "transaction receipt", err.Error())
errStr := fmt.Sprintf(utils.ErrUnmarshalBytesFormat, "transaction receipt", err.Error())
return nil, errors.New(errStr)
}
return receipt, nil
}
func getSysEventAbis(SysEventList []string) (abiBytesArr [][]byte) {
for _, data := range SysEventList {
p := precompile.List[data]
abiBytes, _ := precompile.Asset(p)
abiBytesArr = append(abiBytesArr, abiBytes)
}
return
}
type eventParsingFunc func(eLog *Log, abiBytes []byte) string
func WasmEventParsingPerLog(eLog *Log, abiBytes []byte) string {
var rlpList []interface{}
eventName, topicTypes := findWasmLogTopic(eLog.Topics[0], abiBytes)
if len(topicTypes) == 0 {
return ""
}
dataBytes, _ := hexutil.Decode(eLog.Data)
err := rlp.DecodeBytes(dataBytes, &rlpList)
if err != nil {
// todo: error handle
fmt.Printf("the error is %v\n", err)
}
result := fmt.Sprintf("Event %s: ", eventName)
result += parseReceiptLogData(rlpList, topicTypes)
return result
}
func EventParsing(logs RecptLogs, abiBytesArr [][]byte, fn eventParsingFunc) []string {
var res []string
for _, logData := range logs {
for _, data := range abiBytesArr {
result := fn(logData, data)
if result != "" {
res = append(res, result)
break
}
}
}
return res
}
func findWasmLogTopic(topic string, abiBytes []byte) (string, []string) {
abiFunc, err := ParseAbiFromJson(abiBytes)
if err != nil {
return "", nil
}
for _, data := range abiFunc {
if data.Type != "event" {
continue
}
if strings.EqualFold(wasmLogTopicEncode(data.Name), topic) {
topicTypes := make([]string, 0)
name := data.Name
for _, v := range data.Inputs {
topicTypes = append(topicTypes, v.Type)
}
return name, topicTypes
}
}
return "", nil
}
func parseReceiptLogData(data []interface{}, types []string) string {
var str string
for i, v := range data {
result := ConvertRlpBytesTo(v.([]uint8), types[i])
str += fmt.Sprintf("%v ", result)
}
return str
}
func wasmLogTopicEncode(name string) string {
return common.BytesToHash(crypto.Keccak256([]byte(name))).String()
}
func ConvertRlpBytesTo(input []byte, targetType string) interface{} {
v, ok := Bytes2X_CMD[targetType]
if !ok {
panic("unsupported type")
}
return reflect.ValueOf(v).Call([]reflect.Value{reflect.ValueOf(input)})[0].Interface()
}
var Bytes2X_CMD = map[string]interface{}{
"string": BytesToString,
// "uint8": RlpBytesToUint,
"uint16": RlpBytesToUint16,
"uint32": RlpBytesToUint32,
"uint64": RlpBytesToUint64,
// "uint8": RlpBytesToUint,
"int16": RlpBytesToUint16,
"int32": RlpBytesToUint32,
"int64": RlpBytesToUint64,
"bool": RlpBytesToBool,
}
func BytesToString(b []byte) string {
return string(b)
}
func RlpBytesToUint16(b []byte) uint16 {
b = common.LeftPadBytes(b, 32)
result := common.CallResAsUint32(b)
return uint16(result)
}
func RlpBytesToUint32(b []byte) uint32 {
b = common.LeftPadBytes(b, 32)
return common.CallResAsUint32(b)
}
func RlpBytesToUint64(b []byte) uint64 {
b = common.LeftPadBytes(b, 32)
return common.CallResAsUint64(b)
}
func RlpBytesToBool(b []byte) bool {
if bytes.Compare(b, []byte{1}) == 0 {
return true
}
return false
}
/*
func RlpBytesToUintV2(b []byte) interface{} {
var val interface{}
for _, v := range b {
val = val << 8
val |= uint(v)
}
return val
}*/