-
Notifications
You must be signed in to change notification settings - Fork 320
/
receipt.go
214 lines (188 loc) · 6.15 KB
/
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
// Copyright (c) 2020 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package action
import (
"math/big"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/iotexproject/iotex-core/pkg/log"
)
var (
// StakingBucketPoolTopic is topic for staking bucket pool
StakingBucketPoolTopic = hash.BytesToHash256(address.StakingProtocolAddrHash[:])
// RewardingPoolTopic is topic for rewarding pool
RewardingPoolTopic = hash.BytesToHash256(address.RewardingProtocolAddrHash[:])
)
type (
// Topics are data items of a transaction, such as send/recipient address
Topics []hash.Hash256
// Receipt represents the result of a contract
Receipt struct {
Status uint64
BlockHeight uint64
ActionHash hash.Hash256
GasConsumed uint64
ContractAddress string
logs []*Log
transactionLogs []*TransactionLog
executionRevertMsg string
}
// Log stores an evm contract event
Log struct {
Address string
Topics Topics
Data []byte
BlockHeight uint64
ActionHash hash.Hash256
Index uint
NotFixTopicCopyBug bool
}
// TransactionLog stores a transaction event
TransactionLog struct {
Type iotextypes.TransactionLogType
Amount *big.Int
Sender string
Recipient string
}
)
// ConvertToReceiptPb converts a Receipt to protobuf's Receipt
func (receipt *Receipt) ConvertToReceiptPb() *iotextypes.Receipt {
r := &iotextypes.Receipt{}
r.Status = receipt.Status
r.BlkHeight = receipt.BlockHeight
r.ActHash = receipt.ActionHash[:]
r.GasConsumed = receipt.GasConsumed
r.ContractAddress = receipt.ContractAddress
r.Logs = []*iotextypes.Log{}
for _, l := range receipt.logs {
r.Logs = append(r.Logs, l.ConvertToLogPb())
}
if receipt.executionRevertMsg != "" {
r.ExecutionRevertMsg = receipt.executionRevertMsg
}
return r
}
// ConvertFromReceiptPb converts a protobuf's Receipt to Receipt
func (receipt *Receipt) ConvertFromReceiptPb(pbReceipt *iotextypes.Receipt) {
receipt.Status = pbReceipt.GetStatus()
receipt.BlockHeight = pbReceipt.GetBlkHeight()
copy(receipt.ActionHash[:], pbReceipt.GetActHash())
receipt.GasConsumed = pbReceipt.GetGasConsumed()
receipt.ContractAddress = pbReceipt.GetContractAddress()
logs := pbReceipt.GetLogs()
receipt.logs = make([]*Log, len(logs))
for i, log := range logs {
receipt.logs[i] = &Log{}
receipt.logs[i].ConvertFromLogPb(log)
}
receipt.executionRevertMsg = pbReceipt.GetExecutionRevertMsg()
}
// Serialize returns a serialized byte stream for the Receipt
func (receipt *Receipt) Serialize() ([]byte, error) {
return proto.Marshal(receipt.ConvertToReceiptPb())
}
// Deserialize parse the byte stream into Receipt
func (receipt *Receipt) Deserialize(buf []byte) error {
pbReceipt := &iotextypes.Receipt{}
if err := proto.Unmarshal(buf, pbReceipt); err != nil {
return err
}
receipt.ConvertFromReceiptPb(pbReceipt)
return nil
}
// Hash returns the hash of receipt
func (receipt *Receipt) Hash() hash.Hash256 {
data, err := receipt.Serialize()
if err != nil {
log.L().Panic("Error when serializing a receipt")
}
return hash.Hash256b(data)
}
// Logs returns the list of logs stored in receipt
func (receipt *Receipt) Logs() []*Log {
return receipt.logs
}
// AddLogs add log to receipt and filter out nil log.
func (receipt *Receipt) AddLogs(logs ...*Log) *Receipt {
for _, l := range logs {
if l != nil {
receipt.logs = append(receipt.logs, l)
}
}
return receipt
}
// TransactionLogs returns the list of transaction logs stored in receipt
func (receipt *Receipt) TransactionLogs() []*TransactionLog {
return receipt.transactionLogs
}
// AddTransactionLogs add transaction logs to receipt and filter out nil log.
func (receipt *Receipt) AddTransactionLogs(logs ...*TransactionLog) *Receipt {
for _, l := range logs {
if l != nil {
receipt.transactionLogs = append(receipt.transactionLogs, l)
}
}
return receipt
}
// ExecutionRevertMsg returns the list of execution revert error logs stored in receipt.
func (receipt *Receipt) ExecutionRevertMsg() string {
return receipt.executionRevertMsg
}
// SetExecutionRevertMsg sets executionerrorlogs to receipt.
func (receipt *Receipt) SetExecutionRevertMsg(revertReason string) *Receipt {
if receipt.executionRevertMsg == "" && revertReason != "" {
receipt.executionRevertMsg = revertReason
}
return receipt
}
// ConvertToLogPb converts a Log to protobuf's Log
func (log *Log) ConvertToLogPb() *iotextypes.Log {
l := &iotextypes.Log{}
l.ContractAddress = log.Address
l.Topics = [][]byte{}
for _, topic := range log.Topics {
if log.NotFixTopicCopyBug {
l.Topics = append(l.Topics, topic[:])
} else {
data := make([]byte, len(topic))
copy(data, topic[:])
l.Topics = append(l.Topics, data)
}
}
l.Data = log.Data
l.BlkHeight = log.BlockHeight
l.ActHash = log.ActionHash[:]
l.Index = uint32(log.Index)
return l
}
// ConvertFromLogPb converts a protobuf's LogPb to Log
func (log *Log) ConvertFromLogPb(pbLog *iotextypes.Log) {
log.Address = pbLog.GetContractAddress()
pbLogs := pbLog.GetTopics()
log.Topics = make([]hash.Hash256, len(pbLogs))
for i, topic := range pbLogs {
copy(log.Topics[i][:], topic)
}
log.Data = pbLog.GetData()
log.BlockHeight = pbLog.GetBlkHeight()
copy(log.ActionHash[:], pbLog.GetActHash())
log.Index = uint(pbLog.GetIndex())
}
// Serialize returns a serialized byte stream for the Log
func (log *Log) Serialize() ([]byte, error) {
return proto.Marshal(log.ConvertToLogPb())
}
// Deserialize parse the byte stream into Log
func (log *Log) Deserialize(buf []byte) error {
pbLog := &iotextypes.Log{}
if err := proto.Unmarshal(buf, pbLog); err != nil {
return err
}
log.ConvertFromLogPb(pbLog)
return nil
}