Skip to content

Commit 6a6be4b

Browse files
author
Yutong Pei
authored
panic on negative amount transactionlog, add addlog (iotexproject#2352)
1 parent be6a2c5 commit 6a6be4b

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

action/protocol/execution/evm/evm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func ExecuteContract(
205205
return nil, nil, errors.Wrap(err, "failed to commit contracts to underlying db")
206206
}
207207
stateDB.clear()
208-
receipt.Logs = stateDB.Logs()
208+
receipt.AddLogs(stateDB.Logs()...)
209209
log.S().Debugf("Receipt: %+v, %v", receipt, err)
210210
return retval, receipt, nil
211211
}

action/protocol/rewarding/protocol.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -375,20 +375,14 @@ func (p *Protocol) createReceipt(
375375
gasConsumed uint64,
376376
logs ...*action.Log,
377377
) *action.Receipt {
378-
// remove possible nil log
379-
nlogs := make([]*action.Log, 0, len(logs))
380-
for _, l := range logs {
381-
if l != nil {
382-
nlogs = append(nlogs, l)
383-
}
384-
}
385378
// TODO: need to review the fields
386-
return &action.Receipt{
379+
r := &action.Receipt{
387380
Status: status,
388381
BlockHeight: blkHeight,
389382
ActionHash: actHash,
390383
GasConsumed: gasConsumed,
391384
ContractAddress: p.addr.String(),
392-
Logs: nlogs,
393385
}
386+
r.AddLogs(logs...)
387+
return r
394388
}

action/protocol/staking/protocol.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,6 @@ func (p *Protocol) settleAction(
454454
GasConsumed: actionCtx.IntrinsicGas,
455455
ContractAddress: p.addr.String(),
456456
}
457-
if len(logs) != 0 {
458-
r.Logs = logs
459-
}
457+
r.AddLogs(logs...)
460458
return &r, nil
461459
}

action/receipt.go

+12
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ func (receipt *Receipt) Hash() hash.Hash256 {
119119
return hash.Hash256b(data)
120120
}
121121

122+
// AddLogs add log to receipt and filter out nil log.
123+
func (receipt *Receipt) AddLogs(logs ...*Log) {
124+
if receipt.Logs == nil {
125+
receipt.Logs = make([]*Log, 0, len(logs))
126+
}
127+
for _, l := range logs {
128+
if l != nil {
129+
receipt.Logs = append(receipt.Logs, l)
130+
}
131+
}
132+
}
133+
122134
// ConvertToLogPb converts a Log to protobuf's Log
123135
func (log *Log) ConvertToLogPb() *iotextypes.Log {
124136
l := &iotextypes.Log{}

blockchain/block/systemlog.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
"github.com/golang/protobuf/proto"
1111
"github.com/iotexproject/go-pkgs/hash"
1212
"github.com/iotexproject/iotex-proto/golang/iotextypes"
13+
"go.uber.org/zap"
1314

1415
"github.com/iotexproject/iotex-core/action"
16+
"github.com/iotexproject/iotex-core/pkg/log"
1517
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
1618
)
1719

@@ -154,15 +156,18 @@ func ReceiptTransactionLog(r *action.Receipt) *TransactionLog {
154156
}
155157

156158
// LogTokenTxRecord generates token transaction record from log
157-
func LogTokenTxRecord(log *action.Log) *TokenTxRecord {
158-
if log == nil || !log.IsTransactionLog() {
159+
func LogTokenTxRecord(l *action.Log) *TokenTxRecord {
160+
if l == nil || !l.IsTransactionLog() {
159161
return nil
160162
}
163+
if l.TransactionData.Amount.Sign() < 0 {
164+
log.L().Panic("Negative amount transaction log.", zap.Any("TransactionLog", l.TransactionData))
165+
}
161166

162167
return &TokenTxRecord{
163-
sender: log.TransactionData.Sender,
164-
recipient: log.TransactionData.Recipient,
165-
amount: log.TransactionData.Amount.String(),
166-
typ: log.TransactionData.Type,
168+
sender: l.TransactionData.Sender,
169+
recipient: l.TransactionData.Recipient,
170+
amount: l.TransactionData.Amount.String(),
171+
typ: l.TransactionData.Type,
167172
}
168173
}

0 commit comments

Comments
 (0)