Skip to content

Commit ace6db5

Browse files
CoderZhiYutong Pei
and
Yutong Pei
authored
split transaction log and action log (iotexproject#2354)
* split transaction log and action log Co-authored-by: Yutong Pei <[email protected]>
1 parent 4a324c8 commit ace6db5

25 files changed

+318
-385
lines changed

action/protocol/account/protocol.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Protocol struct {
3232
}
3333

3434
// DepositGas deposits gas to some pool
35-
type DepositGas func(ctx context.Context, sm protocol.StateManager, amount *big.Int) (*action.Log, error)
35+
type DepositGas func(ctx context.Context, sm protocol.StateManager, amount *big.Int) (*action.TransactionLog, error)
3636

3737
// NewProtocol instantiates the protocol of account
3838
func NewProtocol(depositGas DepositGas) *Protocol {

action/protocol/account/transfer.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro
5151
)
5252
}
5353

54-
var depositLog *action.Log
54+
var depositLog *action.TransactionLog
5555
hu := config.NewHeightUpgrade(&bcCtx.Genesis)
5656
if hu.IsPre(config.Pacific, blkCtx.BlockHeight) {
5757
// charge sender gas
@@ -93,7 +93,7 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro
9393
GasConsumed: actionCtx.IntrinsicGas,
9494
ContractAddress: p.addr.String(),
9595
}
96-
receipt.AddLogs(depositLog)
96+
receipt.AddTransactionLogs(depositLog)
9797
return receipt, nil
9898
}
9999

@@ -136,13 +136,11 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro
136136
GasConsumed: actionCtx.IntrinsicGas,
137137
ContractAddress: p.addr.String(),
138138
}
139-
receipt.AddLogs(&action.Log{
140-
TransactionData: &action.TransactionLog{
141-
Type: iotextypes.TransactionLogType_NATIVE_TRANSFER,
142-
Sender: actionCtx.Caller.String(),
143-
Recipient: tsf.Recipient(),
144-
Amount: tsf.Amount(),
145-
},
139+
receipt.AddTransactionLogs(&action.TransactionLog{
140+
Type: iotextypes.TransactionLogType_NATIVE_TRANSFER,
141+
Sender: actionCtx.Caller.String(),
142+
Recipient: tsf.Recipient(),
143+
Amount: tsf.Amount(),
146144
}, depositLog)
147145

148146
return receipt, nil

action/protocol/execution/evm/evm.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func ExecuteContract(
185185
}
186186

187187
receipt.Status = statusCode
188-
var burnLog *action.Log
188+
var burnLog *action.TransactionLog
189189
if hu.IsPost(config.Pacific, blkCtx.BlockHeight) {
190190
// Refund all deposit and, actual gas fee will be subtracted when depositing gas fee to the rewarding protocol
191191
stateDB.AddBalance(ps.context.Origin, big.NewInt(0).Mul(big.NewInt(0).SetUint64(depositGas), ps.context.GasPrice))
@@ -195,20 +195,15 @@ func ExecuteContract(
195195
stateDB.AddBalance(ps.context.Origin, remainingValue)
196196
}
197197
if depositGas-remainingGas > 0 {
198-
burnLog = &action.Log{
199-
Address: contractAddress,
200-
BlockHeight: blkCtx.BlockHeight,
201-
ActionHash: execution.Hash(),
202-
TransactionData: &action.TransactionLog{
203-
Type: iotextypes.TransactionLogType_GAS_FEE,
204-
Sender: actionCtx.Caller.String(),
205-
Recipient: "", // burned
206-
Amount: new(big.Int).Mul(new(big.Int).SetUint64(depositGas-remainingGas), ps.context.GasPrice),
207-
},
198+
burnLog = &action.TransactionLog{
199+
Type: iotextypes.TransactionLogType_GAS_FEE,
200+
Sender: actionCtx.Caller.String(),
201+
Recipient: "", // burned
202+
Amount: new(big.Int).Mul(new(big.Int).SetUint64(depositGas-remainingGas), ps.context.GasPrice),
208203
}
209204
}
210205
}
211-
var depositLog *action.Log
206+
var depositLog *action.TransactionLog
212207
if depositGas-remainingGas > 0 {
213208
gasValue := new(big.Int).Mul(new(big.Int).SetUint64(depositGas-remainingGas), ps.context.GasPrice)
214209
depositLog, err = depositGasFunc(ctx, sm, gasValue)
@@ -221,8 +216,7 @@ func ExecuteContract(
221216
return nil, nil, errors.Wrap(err, "failed to commit contracts to underlying db")
222217
}
223218
stateDB.clear()
224-
receipt.AddLogs(stateDB.Logs()...)
225-
receipt.AddLogs(depositLog, burnLog)
219+
receipt.AddLogs(stateDB.Logs()...).AddTransactionLogs(stateDB.TransactionLogs()...).AddTransactionLogs(depositLog, burnLog)
226220
log.S().Debugf("Receipt: %+v, %v", receipt, err)
227221
return retval, receipt, nil
228222
}
@@ -377,7 +371,7 @@ func SimulateExecution(
377371
sm,
378372
ex,
379373
getBlockHash,
380-
func(context.Context, protocol.StateManager, *big.Int) (*action.Log, error) {
374+
func(context.Context, protocol.StateManager, *big.Int) (*action.TransactionLog, error) {
381375
return nil, nil
382376
},
383377
)

action/protocol/execution/evm/evm_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestExecuteContractFailure(t *testing.T) {
6060
func(uint64) (hash.Hash256, error) {
6161
return hash.ZeroHash256, nil
6262
},
63-
func(context.Context, protocol.StateManager, *big.Int) (*action.Log, error) {
63+
func(context.Context, protocol.StateManager, *big.Int) (*action.TransactionLog, error) {
6464
return nil, nil
6565
})
6666
require.Nil(t, retval)

action/protocol/execution/evm/evmstatedbadapter.go

+24-15
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ type (
4545
GetBlockHash func(uint64) (hash.Hash256, error)
4646

4747
// DepositGas deposits gas
48-
DepositGas func(context.Context, protocol.StateManager, *big.Int) (*action.Log, error)
48+
DepositGas func(context.Context, protocol.StateManager, *big.Int) (*action.TransactionLog, error)
4949

5050
// StateDBAdapter represents the state db adapter for evm to access iotx blockchain
5151
StateDBAdapter struct {
5252
sm protocol.StateManager
5353
logs []*action.Log
54+
transactionLogs []*action.TransactionLog
5455
err error
5556
blockHeight uint64
5657
executionHash hash.Hash256
@@ -425,33 +426,41 @@ func (stateDB *StateDBAdapter) AddLog(evmLog *types.Log) {
425426
copy(topic[:], evmTopic.Bytes())
426427
topics = append(topics, topic)
427428
}
428-
log := &action.Log{
429+
if topics[0] == inContractTransfer {
430+
if len(topics) != 3 {
431+
panic("Invalid in contract transfer topics")
432+
}
433+
from, _ := address.FromBytes(topics[1][12:])
434+
to, _ := address.FromBytes(topics[2][12:])
435+
stateDB.transactionLogs = append(stateDB.transactionLogs, &action.TransactionLog{
436+
Type: iotextypes.TransactionLogType_IN_CONTRACT_TRANSFER,
437+
Sender: from.String(),
438+
Recipient: to.String(),
439+
Amount: new(big.Int).SetBytes(evmLog.Data),
440+
})
441+
return
442+
}
443+
444+
stateDB.logs = append(stateDB.logs, &action.Log{
429445
Address: addr.String(),
430446
Topics: topics,
431447
Data: evmLog.Data,
432448
BlockHeight: stateDB.blockHeight,
433449
ActionHash: stateDB.executionHash,
434450
NotFixTopicCopyBug: stateDB.notFixTopicCopyBug,
435-
}
436-
437-
if len(topics) >= 3 && topics[0] == inContractTransfer {
438-
from, _ := address.FromBytes(log.Topics[1][12:])
439-
to, _ := address.FromBytes(log.Topics[2][12:])
440-
log.TransactionData = &action.TransactionLog{
441-
Type: iotextypes.TransactionLogType_IN_CONTRACT_TRANSFER,
442-
Sender: from.String(),
443-
Recipient: to.String(),
444-
Amount: new(big.Int).SetBytes(log.Data),
445-
}
446-
}
447-
stateDB.logs = append(stateDB.logs, log)
451+
})
448452
}
449453

450454
// Logs returns the logs
451455
func (stateDB *StateDBAdapter) Logs() []*action.Log {
452456
return stateDB.logs
453457
}
454458

459+
// TransactionLogs returns the transaction logs
460+
func (stateDB *StateDBAdapter) TransactionLogs() []*action.TransactionLog {
461+
return stateDB.transactionLogs
462+
}
463+
455464
// AddPreimage adds the preimage of a hash
456465
func (stateDB *StateDBAdapter) AddPreimage(hash common.Hash, preimage []byte) {
457466
if _, ok := stateDB.preimages[hash]; !ok {

action/protocol/execution/protocol_test.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,7 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
490490
)
491491
}
492492
if receipt.Status == uint64(iotextypes.ReceiptStatus_Success) {
493-
numLog := 0
494-
for _, l := range receipt.Logs {
495-
if !l.IsTransactionLog() {
496-
numLog++
497-
}
498-
}
499-
r.Equal(len(exec.ExpectedLogs), numLog, i)
493+
r.Equal(len(exec.ExpectedLogs), len(receipt.Logs()), i)
500494
// TODO: check value of logs
501495
}
502496
}

action/protocol/poll/consortium.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (cc *consortiumCommittee) CreateGenesisStates(ctx context.Context, sm proto
129129
func(height uint64) (hash.Hash256, error) {
130130
return hash.ZeroHash256, nil
131131
},
132-
func(ctx context.Context, sm protocol.StateManager, amount *big.Int) (*action.Log, error) {
132+
func(ctx context.Context, sm protocol.StateManager, amount *big.Int) (*action.TransactionLog, error) {
133133
return nil, nil
134134
},
135135
)

action/protocol/poll/staking_committee.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (sc *stakingCommittee) CreateGenesisStates(ctx context.Context, sm protocol
140140
func(height uint64) (hash.Hash256, error) {
141141
return hash.ZeroHash256, nil
142142
},
143-
func(ctx context.Context, sm protocol.StateManager, amount *big.Int) (*action.Log, error) {
143+
func(ctx context.Context, sm protocol.StateManager, amount *big.Int) (*action.TransactionLog, error) {
144144
return nil, nil
145145
},
146146
)

action/protocol/rewarding/fund.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ func (p *Protocol) Deposit(
6464
sm protocol.StateManager,
6565
amount *big.Int,
6666
transactionLogType iotextypes.TransactionLogType,
67-
) (*action.Log, error) {
67+
) (*action.TransactionLog, error) {
6868
actionCtx := protocol.MustGetActionCtx(ctx)
69-
blkCtx := protocol.MustGetBlockCtx(ctx)
7069
if err := p.assertAmount(amount); err != nil {
7170
return nil, err
7271
}
@@ -92,16 +91,11 @@ func (p *Protocol) Deposit(
9291
if err := p.putState(ctx, sm, fundKey, &f); err != nil {
9392
return nil, err
9493
}
95-
return &action.Log{
96-
Address: p.addr.String(),
97-
BlockHeight: blkCtx.BlockHeight,
98-
ActionHash: actionCtx.ActionHash,
99-
TransactionData: &action.TransactionLog{
100-
Type: transactionLogType,
101-
Sender: actionCtx.Caller.String(),
102-
Recipient: address.RewardingPoolAddr,
103-
Amount: amount,
104-
},
94+
return &action.TransactionLog{
95+
Type: transactionLogType,
96+
Sender: actionCtx.Caller.String(),
97+
Recipient: address.RewardingPoolAddr,
98+
Amount: amount,
10599
}, nil
106100
}
107101

@@ -147,7 +141,7 @@ func (p *Protocol) assertEnoughBalance(
147141
}
148142

149143
// DepositGas deposits gas into the rewarding fund
150-
func DepositGas(ctx context.Context, sm protocol.StateManager, amount *big.Int) (*action.Log, error) {
144+
func DepositGas(ctx context.Context, sm protocol.StateManager, amount *big.Int) (*action.TransactionLog, error) {
151145
// If the gas fee is 0, return immediately
152146
if amount.Cmp(big.NewInt(0)) == 0 {
153147
return nil, nil

action/protocol/rewarding/fund_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ func TestProtocol_Fund(t *testing.T) {
3131
rlog, err := p.Deposit(ctx, sm, big.NewInt(5), iotextypes.TransactionLogType_DEPOSIT_TO_REWARDING_FUND)
3232
require.NoError(t, err)
3333
require.NotNil(t, rlog)
34-
require.True(t, rlog.IsTransactionLog())
35-
require.Equal(t, big.NewInt(5).String(), rlog.TransactionData.Amount.String())
36-
require.Equal(t, actionCtx.Caller.String(), rlog.TransactionData.Sender)
37-
require.Equal(t, address.RewardingPoolAddr, rlog.TransactionData.Recipient)
34+
require.Equal(t, big.NewInt(5).String(), rlog.Amount.String())
35+
require.Equal(t, actionCtx.Caller.String(), rlog.Sender)
36+
require.Equal(t, address.RewardingPoolAddr, rlog.Recipient)
3837

3938
totalBalance, _, err := p.TotalBalance(ctx, sm)
4039
require.NoError(t, err)

action/protocol/rewarding/protocol.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -162,38 +162,38 @@ func (p *Protocol) Handle(
162162
rlog, err := p.Deposit(ctx, sm, act.Amount(), iotextypes.TransactionLogType_DEPOSIT_TO_REWARDING_FUND)
163163
if err != nil {
164164
log.L().Debug("Error when handling rewarding action", zap.Error(err))
165-
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si)
165+
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si, nil)
166166
}
167-
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, rlog)
167+
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, nil, rlog)
168168
case *action.ClaimFromRewardingFund:
169169
si := sm.Snapshot()
170170
rlog, err := p.Claim(ctx, sm, act.Amount())
171171
if err != nil {
172172
log.L().Debug("Error when handling rewarding action", zap.Error(err))
173-
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si)
173+
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si, nil)
174174
}
175-
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, rlog)
175+
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, nil, rlog)
176176
case *action.GrantReward:
177177
switch act.RewardType() {
178178
case action.BlockReward:
179179
si := sm.Snapshot()
180180
rewardLog, err := p.GrantBlockReward(ctx, sm)
181181
if err != nil {
182182
log.L().Debug("Error when handling rewarding action", zap.Error(err))
183-
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si)
183+
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si, nil)
184184
}
185185
if rewardLog == nil {
186-
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si)
186+
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, nil)
187187
}
188-
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, rewardLog)
188+
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, []*action.Log{rewardLog})
189189
case action.EpochReward:
190190
si := sm.Snapshot()
191191
rewardLogs, err := p.GrantEpochReward(ctx, sm)
192192
if err != nil {
193193
log.L().Debug("Error when handling rewarding action", zap.Error(err))
194-
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si)
194+
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si, nil)
195195
}
196-
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, rewardLogs...)
196+
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, rewardLogs)
197197
}
198198
}
199199
return nil, nil
@@ -337,7 +337,8 @@ func (p *Protocol) settleAction(
337337
sm protocol.StateManager,
338338
status uint64,
339339
si int,
340-
logs ...*action.Log,
340+
logs []*action.Log,
341+
tLogs ...*action.TransactionLog,
341342
) (*action.Receipt, error) {
342343
actionCtx := protocol.MustGetActionCtx(ctx)
343344
blkCtx := protocol.MustGetBlockCtx(ctx)
@@ -352,12 +353,12 @@ func (p *Protocol) settleAction(
352353
return nil, err
353354
}
354355
if depositLog != nil {
355-
logs = append(logs, depositLog)
356+
tLogs = append(tLogs, depositLog)
356357
}
357358
if err := p.increaseNonce(sm, actionCtx.Caller, actionCtx.Nonce); err != nil {
358359
return nil, err
359360
}
360-
return p.createReceipt(status, blkCtx.BlockHeight, actionCtx.ActionHash, actionCtx.IntrinsicGas, logs...), nil
361+
return p.createReceipt(status, blkCtx.BlockHeight, actionCtx.ActionHash, actionCtx.IntrinsicGas, logs, tLogs...), nil
361362
}
362363

363364
func (p *Protocol) increaseNonce(sm protocol.StateManager, addr address.Address, nonce uint64) error {
@@ -377,16 +378,15 @@ func (p *Protocol) createReceipt(
377378
blkHeight uint64,
378379
actHash hash.Hash256,
379380
gasConsumed uint64,
380-
logs ...*action.Log,
381+
logs []*action.Log,
382+
tLogs ...*action.TransactionLog,
381383
) *action.Receipt {
382384
// TODO: need to review the fields
383-
r := &action.Receipt{
385+
return (&action.Receipt{
384386
Status: status,
385387
BlockHeight: blkHeight,
386388
ActionHash: actHash,
387389
GasConsumed: gasConsumed,
388390
ContractAddress: p.addr.String(),
389-
}
390-
r.AddLogs(logs...)
391-
return r
391+
}).AddLogs(logs...).AddTransactionLogs(tLogs...)
392392
}

action/protocol/rewarding/protocol_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func TestProtocol_Handle(t *testing.T) {
371371
receipt, err = p.Handle(ctx, se2.Action(), sm)
372372
require.NoError(t, err)
373373
assert.Equal(t, uint64(iotextypes.ReceiptStatus_Success), receipt.Status)
374-
assert.Equal(t, 1, len(receipt.Logs))
374+
assert.Equal(t, 1, len(receipt.Logs()))
375375
// Grant the block reward again should fail
376376
receipt, err = p.Handle(ctx, se2.Action(), sm)
377377
require.NoError(t, err)

0 commit comments

Comments
 (0)