Skip to content

Commit 0640820

Browse files
authored
Add TransactionLog to capture all asset-transferring tx (iotexproject#2341)
1 parent aff3522 commit 0640820

File tree

19 files changed

+257
-312
lines changed

19 files changed

+257
-312
lines changed

action/protocol/execution/evm/evmstatedbadapter.go

+1
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ func (stateDB *StateDBAdapter) AddLog(evmLog *types.Log) {
431431
BlockHeight: stateDB.blockHeight,
432432
ActionHash: stateDB.executionHash,
433433
NotFixTopicCopyBug: stateDB.notFixTopicCopyBug,
434+
HasAssetTransfer: len(topics) > 0 && topics[0] == action.InContractTransfer,
434435
}
435436
stateDB.logs = append(stateDB.logs, log)
436437
}

action/protocol/execution/protocol_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
492492
if receipt.Status == uint64(iotextypes.ReceiptStatus_Success) {
493493
numLog := 0
494494
for _, l := range receipt.Logs {
495-
if !l.IsImplicitTransfer() {
495+
if !l.IsTransactionLog() {
496496
numLog++
497497
}
498498
}

action/protocol/staking/handlers.go

+25-20
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ func (p *Protocol) handleCreateStake(ctx context.Context, act *action.CreateStak
129129
action.StakingBucketPoolTopic,
130130
hash.BytesToHash256(byteutil.Uint64ToBytesBigEndian(bucket.Index)),
131131
},
132-
Data: act.Amount().Bytes(),
133-
BlockHeight: blkCtx.BlockHeight,
134-
ActionHash: actionCtx.ActionHash,
135-
Recipient: address.StakingBucketPoolAddr,
132+
Data: act.Amount().Bytes(),
133+
BlockHeight: blkCtx.BlockHeight,
134+
ActionHash: actionCtx.ActionHash,
135+
Recipient: address.StakingBucketPoolAddr,
136+
HasAssetTransfer: true,
136137
}
137138
return log, &cLog, nil
138139
}
@@ -274,10 +275,11 @@ func (p *Protocol) handleWithdrawStake(ctx context.Context, act *action.Withdraw
274275
hash.BytesToHash256(actionCtx.Caller.Bytes()),
275276
hash.BytesToHash256(byteutil.Uint64ToBytesBigEndian(bucket.Index)),
276277
},
277-
Data: bucket.StakedAmount.Bytes(),
278-
BlockHeight: blkCtx.BlockHeight,
279-
ActionHash: actionCtx.ActionHash,
280-
Sender: address.StakingBucketPoolAddr,
278+
Data: bucket.StakedAmount.Bytes(),
279+
BlockHeight: blkCtx.BlockHeight,
280+
ActionHash: actionCtx.ActionHash,
281+
Sender: address.StakingBucketPoolAddr,
282+
HasAssetTransfer: true,
281283
}
282284
return log, &amountLog, nil
283285
}
@@ -516,10 +518,11 @@ func (p *Protocol) handleDepositToStake(ctx context.Context, act *action.Deposit
516518
action.StakingBucketPoolTopic,
517519
hash.BytesToHash256(byteutil.Uint64ToBytesBigEndian(bucket.Index)),
518520
},
519-
Data: act.Amount().Bytes(),
520-
BlockHeight: blkCtx.BlockHeight,
521-
ActionHash: actionCtx.ActionHash,
522-
Recipient: address.StakingBucketPoolAddr,
521+
Data: act.Amount().Bytes(),
522+
BlockHeight: blkCtx.BlockHeight,
523+
ActionHash: actionCtx.ActionHash,
524+
Recipient: address.StakingBucketPoolAddr,
525+
HasAssetTransfer: true,
523526
}
524527
return log, &dLog, nil
525528
}
@@ -696,10 +699,11 @@ func (p *Protocol) handleCandidateRegister(ctx context.Context, act *action.Cand
696699
action.StakingBucketPoolTopic,
697700
hash.BytesToHash256(byteutil.Uint64ToBytesBigEndian(bucket.Index)),
698701
},
699-
Data: act.Amount().Bytes(),
700-
BlockHeight: blkCtx.BlockHeight,
701-
ActionHash: actCtx.ActionHash,
702-
Recipient: address.StakingBucketPoolAddr,
702+
Data: act.Amount().Bytes(),
703+
BlockHeight: blkCtx.BlockHeight,
704+
ActionHash: actCtx.ActionHash,
705+
Recipient: address.StakingBucketPoolAddr,
706+
HasAssetTransfer: true,
703707
}
704708

705709
// generate candidate register log
@@ -711,10 +715,11 @@ func (p *Protocol) handleCandidateRegister(ctx context.Context, act *action.Cand
711715
action.RewardingPoolTopic,
712716
hash.BytesToHash256(byteutil.Uint64ToBytesBigEndian(bucket.Index)),
713717
},
714-
Data: registrationFee.Bytes(),
715-
BlockHeight: blkCtx.BlockHeight,
716-
ActionHash: actCtx.ActionHash,
717-
Recipient: address.RewardingPoolAddr,
718+
Data: registrationFee.Bytes(),
719+
BlockHeight: blkCtx.BlockHeight,
720+
ActionHash: actCtx.ActionHash,
721+
Recipient: address.RewardingPoolAddr,
722+
HasAssetTransfer: true,
718723
}
719724
return log, &cLog, &rLog, nil
720725
}

action/protocol/staking/handlers_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func TestProtocol_HandleCreateStake(t *testing.T) {
198198
// check the special create bucket log
199199
require.Equal(2, len(r.Logs))
200200
cLog := r.Logs[1]
201-
require.True(cLog.IsCreateBucket())
201+
require.True(cLog.IsTransactionLog())
202202
require.EqualValues(0, byteutil.BytesToUint64BigEndian(cLog.Topics[3][24:]))
203203
from, _ := address.FromBytes(cLog.Topics[1][12:])
204204
require.True(address.Equal(stakerAddr, from))
@@ -554,7 +554,7 @@ func TestProtocol_HandleCandidateRegister(t *testing.T) {
554554
// check the special create bucket and candidate register log
555555
require.Equal(3, len(r.Logs))
556556
cLog := r.Logs[1]
557-
require.True(cLog.IsCandidateSelfStake())
557+
require.True(cLog.IsTransactionLog())
558558
require.EqualValues(0, byteutil.BytesToUint64BigEndian(cLog.Topics[3][24:]))
559559
from, _ := address.FromBytes(cLog.Topics[1][12:])
560560
require.True(address.Equal(test.caller, from))
@@ -563,7 +563,7 @@ func TestProtocol_HandleCandidateRegister(t *testing.T) {
563563
amount := new(big.Int).SetBytes(r.Logs[1].Data)
564564
require.Equal(test.amountStr, amount.String())
565565
cLog = r.Logs[2]
566-
require.True(cLog.IsCandidateRegister())
566+
require.True(cLog.IsTransactionLog())
567567
require.EqualValues(0, byteutil.BytesToUint64BigEndian(cLog.Topics[3][24:]))
568568
from, _ = address.FromBytes(cLog.Topics[1][12:])
569569
require.True(address.Equal(test.caller, from))
@@ -1318,7 +1318,7 @@ func TestProtocol_HandleWithdrawStake(t *testing.T) {
13181318
// check the special withdraw bucket log
13191319
require.Equal(2, len(r.Logs))
13201320
wLog := r.Logs[1]
1321-
require.True(wLog.IsWithdrawBucket())
1321+
require.True(wLog.IsTransactionLog())
13221322
require.Equal(test.withdrawIndex, byteutil.BytesToUint64BigEndian(wLog.Topics[3][24:]))
13231323
from, _ := address.FromBytes(wLog.Topics[1][12:])
13241324
require.True(address.Equal(p.addr, from))
@@ -2486,7 +2486,7 @@ func TestProtocol_HandleDepositToStake(t *testing.T) {
24862486
// check the special deposit bucket log
24872487
require.Equal(2, len(r.Logs))
24882488
dLog := r.Logs[1]
2489-
require.True(dLog.IsDepositBucket())
2489+
require.True(dLog.IsTransactionLog())
24902490
require.EqualValues(0, byteutil.BytesToUint64BigEndian(dLog.Topics[3][24:]))
24912491
from, _ := address.FromBytes(dLog.Topics[1][12:])
24922492
require.True(address.Equal(test.caller, from))

action/receipt.go

+13-79
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ import (
1717
)
1818

1919
var (
20-
// InContractTransfer is topic for implicit transfer log of evm transfer
20+
// InContractTransfer is topic for transaction log of evm transfer
2121
// 32 bytes with all zeros
22-
InContractTransfer = hash.BytesToHash256([]byte{byte(iotextypes.ImplicitTransferLogType_IN_CONTRACT_TRANSFER)})
22+
InContractTransfer = hash.BytesToHash256([]byte{byte(iotextypes.TransactionLogType_IN_CONTRACT_TRANSFER)})
2323

2424
// BucketWithdrawAmount is topic for bucket withdraw
25-
BucketWithdrawAmount = hash.BytesToHash256([]byte{byte(iotextypes.ImplicitTransferLogType_BUCKET_WITHDRAW_AMOUNT)})
25+
BucketWithdrawAmount = hash.BytesToHash256([]byte{byte(iotextypes.TransactionLogType_BUCKET_WITHDRAW_AMOUNT)})
2626

2727
// BucketCreateAmount is topic for bucket create
28-
BucketCreateAmount = hash.BytesToHash256([]byte{byte(iotextypes.ImplicitTransferLogType_BUCKET_CREATE_AMOUNT)})
28+
BucketCreateAmount = hash.BytesToHash256([]byte{byte(iotextypes.TransactionLogType_BUCKET_CREATE_AMOUNT)})
2929

3030
// BucketDepositAmount is topic for bucket deposit
31-
BucketDepositAmount = hash.BytesToHash256([]byte{byte(iotextypes.ImplicitTransferLogType_BUCKET_DEPOSIT_AMOUNT)})
31+
BucketDepositAmount = hash.BytesToHash256([]byte{byte(iotextypes.TransactionLogType_BUCKET_DEPOSIT_AMOUNT)})
3232

3333
// CandidateSelfStake is topic for candidate self-stake
34-
CandidateSelfStake = hash.BytesToHash256([]byte{byte(iotextypes.ImplicitTransferLogType_CANDIDATE_SELF_STAKE)})
34+
CandidateSelfStake = hash.BytesToHash256([]byte{byte(iotextypes.TransactionLogType_CANDIDATE_SELF_STAKE)})
3535

3636
// CandidateRegistrationFee is topic for candidate register
37-
CandidateRegistrationFee = hash.BytesToHash256([]byte{byte(iotextypes.ImplicitTransferLogType_CANDIDATE_REGISTRATION_FEE)})
37+
CandidateRegistrationFee = hash.BytesToHash256([]byte{byte(iotextypes.TransactionLogType_CANDIDATE_REGISTRATION_FEE)})
3838

3939
// StakingBucketPoolTopic is topic for staking bucket pool
4040
StakingBucketPoolTopic = hash.BytesToHash256(address.StakingProtocolAddrHash[:])
@@ -66,6 +66,7 @@ type (
6666
ActionHash hash.Hash256
6767
Index uint
6868
NotFixTopicCopyBug bool
69+
HasAssetTransfer bool
6970
Sender string
7071
Recipient string
7172
}
@@ -81,8 +82,8 @@ func (receipt *Receipt) ConvertToReceiptPb() *iotextypes.Receipt {
8182
r.ContractAddress = receipt.ContractAddress
8283
r.Logs = []*iotextypes.Log{}
8384
for _, l := range receipt.Logs {
84-
// exclude implict transfer log when calculating receipts' hash or storing logs
85-
if !l.IsImplicitTransfer() {
85+
// exclude transaction log when calculating receipts' hash or storing logs
86+
if !l.IsTransactionLog() {
8687
r.Logs = append(r.Logs, l.ConvertToLogPb())
8788
}
8889
}
@@ -178,74 +179,7 @@ func (log *Log) Deserialize(buf []byte) error {
178179
return nil
179180
}
180181

181-
// IsImplicitTransfer checks whether a log is implicit transfer log
182-
func (log *Log) IsImplicitTransfer() bool {
183-
return log.IsEvmTransfer() || log.IsCreateBucket() || log.IsDepositBucket() ||
184-
log.IsWithdrawBucket() || log.IsCandidateRegister() || log.IsCandidateSelfStake()
185-
}
186-
187-
func (log *Log) isStakingImplicitLog(topic hash.Hash256) bool {
188-
if len(log.Topics) == 0 {
189-
return false
190-
}
191-
192-
addr, _ := address.FromBytes(address.StakingProtocolAddrHash[:])
193-
if log.Address != addr.String() {
194-
return false
195-
}
196-
197-
if log.Topics[0] != topic {
198-
return false
199-
}
200-
201-
if len(log.Topics) < 4 {
202-
return false
203-
}
204-
205-
switch {
206-
case topic == BucketCreateAmount || topic == BucketDepositAmount || topic == CandidateSelfStake:
207-
// amount goes into staking bucket pool
208-
return log.Topics[2] == StakingBucketPoolTopic
209-
case topic == BucketWithdrawAmount:
210-
// amount comes out of staking bucket pool
211-
return log.Topics[1] == StakingBucketPoolTopic
212-
case topic == CandidateRegistrationFee:
213-
// amount goes into rewarding pool
214-
return log.Topics[2] == RewardingPoolTopic
215-
default:
216-
return false
217-
}
218-
}
219-
220-
// IsEvmTransfer checks evm transfer log
221-
func (log *Log) IsEvmTransfer() bool {
222-
if log == nil || len(log.Topics) == 0 {
223-
return false
224-
}
225-
return log.Topics[0] == InContractTransfer
226-
}
227-
228-
// IsWithdrawBucket checks withdraw bucket log
229-
func (log *Log) IsWithdrawBucket() bool {
230-
return log.isStakingImplicitLog(BucketWithdrawAmount)
231-
}
232-
233-
// IsCreateBucket checks create bucket log
234-
func (log *Log) IsCreateBucket() bool {
235-
return log.isStakingImplicitLog(BucketCreateAmount)
236-
}
237-
238-
// IsDepositBucket checks deposit bucket log
239-
func (log *Log) IsDepositBucket() bool {
240-
return log.isStakingImplicitLog(BucketDepositAmount)
241-
}
242-
243-
// IsCandidateRegister checks candidate register log
244-
func (log *Log) IsCandidateRegister() bool {
245-
return log.isStakingImplicitLog(CandidateRegistrationFee)
246-
}
247-
248-
// IsCandidateSelfStake checks candidate self-stake log
249-
func (log *Log) IsCandidateSelfStake() bool {
250-
return log.isStakingImplicitLog(CandidateSelfStake)
182+
// IsTransactionLog checks whether a log is transaction log
183+
func (log *Log) IsTransactionLog() bool {
184+
return log.HasAssetTransfer
251185
}

0 commit comments

Comments
 (0)