Skip to content

Commit

Permalink
rebase and revert owner replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Nov 20, 2024
1 parent 42d3acf commit 2b69774
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions app/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func (app *App) AddCosmosEventsToEVMReceiptIfApplicable(ctx sdk.Context, tx sdk.
return
}
logs := []*ethtypes.Log{}
ownerReplacements := map[uint]common.Hash{}
// Note: txs with a very large number of WASM events may run out of gas due to
// additional gas consumption from EVM receipt generation and event translation
wasmToEvmEventGasLimit := app.EvmKeeper.GetDeliverTxHookWasmGasLimit(ctx.WithGasMeter(sdk.NewInfiniteGasMeter(1, 1)))
Expand All @@ -61,13 +60,10 @@ func (app *App) AddCosmosEventsToEVMReceiptIfApplicable(ctx sdk.Context, tx sdk.
// check if there is a ERC721 pointer to contract Addr
pointerAddr, _, exists = app.EvmKeeper.GetERC721CW721Pointer(wasmToEvmEventCtx, contractAddr)
if exists {
log, realOwner, eligible := app.translateCW721Event(wasmToEvmEventCtx, wasmEvent, pointerAddr, contractAddr, response)
log, eligible := app.translateCW721Event(wasmToEvmEventCtx, wasmEvent, pointerAddr, contractAddr, response)
if eligible {
log.Index = uint(len(logs))
logs = append(logs, log)
if (realOwner != common.Hash{}) {
ownerReplacements[log.Index] = realOwner
}
}
continue
}
Expand All @@ -81,22 +77,15 @@ func (app *App) AddCosmosEventsToEVMReceiptIfApplicable(ctx sdk.Context, tx sdk.
}
var bloom ethtypes.Bloom
if r, err := app.EvmKeeper.GetTransientReceipt(wasmToEvmEventCtx, txHash); err == nil && r != nil {
existingLogCnt := len(r.Logs)
r.Logs = append(r.Logs, utils.Map(logs, evmkeeper.ConvertSyntheticEthLog)...)
for i, l := range r.Logs {
l.Index = uint32(i)
}
bloom = ethtypes.CreateBloom(ethtypes.Receipts{&ethtypes.Receipt{Logs: evmkeeper.GetLogsForTx(r)}})
r.LogsBloom = bloom[:]
for i, o := range ownerReplacements {
r.Logs[existingLogCnt+int(i)].Topics[1] = o.Hex()
}
_ = app.EvmKeeper.SetTransientReceipt(wasmToEvmEventCtx, txHash, r)
} else {
bloom = ethtypes.CreateBloom(ethtypes.Receipts{&ethtypes.Receipt{Logs: logs}})
for i, o := range ownerReplacements {
logs[int(i)].Topics[1] = o
}
receipt := &evmtypes.Receipt{
TxType: ShellEVMTxType,
TxHashHex: txHash.Hex(),
Expand Down Expand Up @@ -184,17 +173,17 @@ func (app *App) translateCW20Event(ctx sdk.Context, wasmEvent abci.Event, pointe
return nil, false
}

func (app *App) translateCW721Event(ctx sdk.Context, wasmEvent abci.Event, pointerAddr common.Address, contractAddr string, response sdk.DeliverTxHookInput) (*ethtypes.Log, common.Hash, bool) {
func (app *App) translateCW721Event(ctx sdk.Context, wasmEvent abci.Event, pointerAddr common.Address, contractAddr string, response sdk.DeliverTxHookInput) (*ethtypes.Log, bool) {
action, found := GetAttributeValue(wasmEvent, "action")
if !found {
return nil, common.Hash{}, false
return nil, false
}
var topics []common.Hash
switch action {
case "transfer_nft", "send_nft", "burn":
tokenID := GetTokenIDAttribute(wasmEvent)
if tokenID == nil {
return nil, common.Hash{}, false
return nil, false
}
sender := common.Hash{}
// unfortunately CW721 transfer events differ from ERC721 transfer events
Expand Down Expand Up @@ -226,19 +215,19 @@ func (app *App) translateCW721Event(ctx sdk.Context, wasmEvent abci.Event, point
}
topics = []common.Hash{
ERC721TransferTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
sender,
app.GetEvmAddressAttribute(ctx, wasmEvent, "recipient"),
common.BigToHash(tokenID),
}
return &ethtypes.Log{
Address: pointerAddr,
Topics: topics,
Data: EmptyHash.Bytes(),
}, sender, true
}, true
case "mint":
tokenID := GetTokenIDAttribute(wasmEvent)
if tokenID == nil {
return nil, common.Hash{}, false
return nil, false
}
topics = []common.Hash{
ERC721TransferTopic,
Expand All @@ -250,11 +239,11 @@ func (app *App) translateCW721Event(ctx sdk.Context, wasmEvent abci.Event, point
Address: pointerAddr,
Topics: topics,
Data: EmptyHash.Bytes(),
}, common.Hash{}, true
}, true
case "approve":
tokenID := GetTokenIDAttribute(wasmEvent)
if tokenID == nil {
return nil, common.Hash{}, false
return nil, false
}
topics = []common.Hash{
ERC721ApprovalTopic,
Expand All @@ -266,11 +255,11 @@ func (app *App) translateCW721Event(ctx sdk.Context, wasmEvent abci.Event, point
Address: pointerAddr,
Topics: topics,
Data: EmptyHash.Bytes(),
}, common.Hash{}, true
}, true
case "revoke":
tokenID := GetTokenIDAttribute(wasmEvent)
if tokenID == nil {
return nil, common.Hash{}, false
return nil, false
}
topics = []common.Hash{
ERC721ApprovalTopic,
Expand All @@ -282,7 +271,7 @@ func (app *App) translateCW721Event(ctx sdk.Context, wasmEvent abci.Event, point
Address: pointerAddr,
Topics: topics,
Data: EmptyHash.Bytes(),
}, common.Hash{}, true
}, true
case "approve_all":
topics = []common.Hash{
ERC721ApproveAllTopic,
Expand All @@ -293,7 +282,7 @@ func (app *App) translateCW721Event(ctx sdk.Context, wasmEvent abci.Event, point
Address: pointerAddr,
Topics: topics,
Data: TrueHash.Bytes(),
}, common.Hash{}, true
}, true
case "revoke_all":
topics = []common.Hash{
ERC721ApproveAllTopic,
Expand All @@ -304,9 +293,9 @@ func (app *App) translateCW721Event(ctx sdk.Context, wasmEvent abci.Event, point
Address: pointerAddr,
Topics: topics,
Data: EmptyHash.Bytes(),
}, common.Hash{}, true
}, true
}
return nil, common.Hash{}, false
return nil, false
}

func (app *App) GetEvmAddressAttribute(ctx sdk.Context, event abci.Event, attribute string) common.Hash {
Expand Down

0 comments on commit 2b69774

Please sign in to comment.