Skip to content

Commit

Permalink
Merge pull request #2995 from iotaledger/revert-2976-revert-2971-feat…
Browse files Browse the repository at this point in the history
…/show-evm-l1-deposits

Revert "Revert "feat: show deposits from L1 to EVM""
  • Loading branch information
jorgemmsilva authored Oct 25, 2023
2 parents 8ba2a2c + 60cbcfe commit 88d4165
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/testutil/testdbhash/TestStorageContract.hex
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x5c2f4cc322a2c1f4850d1d2c31cbbdd3fbfa92bd64b1a3f3d1e7f608c74ab8b8
0xf7c89a451c83d8d59b4ec7d6904a8421166463bd4b28feeffe3362313b3ee6d4
15 changes: 15 additions & 0 deletions packages/vm/core/accounts/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/iotaledger/wasp/packages/util"
"github.com/iotaledger/wasp/packages/vm"
"github.com/iotaledger/wasp/packages/vm/core/errors/coreerrors"
"github.com/iotaledger/wasp/packages/vm/core/evm"
"github.com/iotaledger/wasp/packages/vm/gas"
)

Expand Down Expand Up @@ -70,7 +71,21 @@ func deposit(ctx isc.Sandbox) dict.Dict {
func transferAllowanceTo(ctx isc.Sandbox) dict.Dict {
ctx.Log().Debugf("accounts.transferAllowanceTo.begin -- %s", ctx.AllowanceAvailable())
targetAccount := ctx.Params().MustGetAgentID(ParamAgentID)
allowance := ctx.AllowanceAvailable().Clone()
ctx.TransferAllowedFunds(targetAccount)
if targetAccount.Kind() == isc.AgentIDKindEthereumAddress {
evmAcc := targetAccount.(*isc.EthereumAddressAgentID).EthAddress().Bytes()
// issue an "custom" etherum tx so the funds appear on the explorer
ctx.Call(
evm.Contract.Hname(),
evm.FuncNewL1Deposit.Hname(),
dict.Dict{
evm.FieldAddress: evmAcc,
evm.FieldAssets: allowance.Bytes(),
},
nil,
)
}
ctx.Log().Debugf("accounts.transferAllowanceTo.success: target: %s\n%s", targetAccount, ctx.AllowanceAvailable())
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/core/evm/emulator/blockchaindb.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (bc *BlockchainDB) GetPendingHeader(timestamp uint64) *types.Header {
}
}

func (bc *BlockchainDB) getPendingCumulativeGasUsed() uint64 {
func (bc *BlockchainDB) GetPendingCumulativeGasUsed() uint64 {
blockNumber := bc.GetPendingBlockNumber()
receiptArray := bc.getReceiptArray(blockNumber)
n := receiptArray.Len()
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/core/evm/emulator/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (e *EVMEmulator) SendTransaction(
if result != nil {
gasUsed = result.UsedGas
}
cumulativeGasUsed := e.BlockchainDB().getPendingCumulativeGasUsed() + gasUsed
cumulativeGasUsed := e.BlockchainDB().GetPendingCumulativeGasUsed() + gasUsed

receipt = &types.Receipt{
Type: tx.Type(),
Expand Down
31 changes: 31 additions & 0 deletions packages/vm/core/evm/evmimpl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var Processor = evm.Contract.Processor(nil,
evm.FuncRegisterERC20ExternalNativeToken.WithHandler(registerERC20ExternalNativeToken),
evm.FuncRegisterERC721NFTCollection.WithHandler(restricted(registerERC721NFTCollection)),

evm.FuncNewL1Deposit.WithHandler(newL1Deposit),

// views
evm.FuncGetERC20ExternalNativeTokenAddress.WithHandler(viewERC20ExternalNativeTokenAddress),
evm.FuncGetChainID.WithHandler(getChainID),
Expand Down Expand Up @@ -435,3 +437,32 @@ func getEVMGasRatio(ctx isc.SandboxBase) util.Ratio32 {
gasRatioViewRes := ctx.CallView(governance.Contract.Hname(), governance.ViewGetEVMGasRatio.Hname(), nil)
return codec.MustDecodeRatio32(gasRatioViewRes.Get(governance.ParamEVMGasRatio), gas.DefaultEVMGasRatio)
}

func newL1Deposit(ctx isc.Sandbox) dict.Dict {
// can only be called from the accounts contract
ctx.RequireCaller(isc.NewContractAgentID(ctx.ChainID(), accounts.Contract.Hname()))
params := ctx.Params()
addr := common.BytesToAddress(params.MustGetBytes(evm.FieldAddress))
assets, err := isc.AssetsFromBytes(params.MustGetBytes(evm.FieldAssets))
ctx.RequireNoError(err, "unable to parse assets from params")

// create a fake tx so that the deposit is visible by the EVM
value := util.BaseTokensDecimalsToEthereumDecimals(assets.BaseTokens, newEmulatorContext(ctx).BaseTokensDecimals())
nonce := uint64(0)
tx := types.NewTransaction(nonce, addr, value, 0, util.Big0, assets.Bytes())

// create a fake receipt
receipt := &types.Receipt{
Type: types.LegacyTxType,
CumulativeGasUsed: createBlockchainDB(ctx.State(), ctx.ChainInfo()).GetPendingCumulativeGasUsed(),
GasUsed: 0,
Logs: make([]*types.Log, 0),
}
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})

ctx.Privileged().OnWriteReceipt(func(evmPartition kv.KVStore) {
createBlockchainDB(evmPartition, ctx.ChainInfo()).AddTransaction(tx, receipt)
})

return nil
}
3 changes: 3 additions & 0 deletions packages/vm/core/evm/evmnames/evmnames.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ const (
FuncGetERC20ExternalNativeTokenAddress = "getERC20ExternalNativeTokenAddress"
FuncRegisterERC721NFTCollection = "registerERC721NFTCollection"

FuncNewL1Deposit = "newL1Deposit"

FieldTransaction = "t"
FieldCallMsg = "c"
FieldChainID = "chid"
FieldAddress = "a"
FieldAssets = "s"
FieldKey = "k"
FieldAgentID = "i"
FieldTransactionIndex = "ti"
Expand Down
20 changes: 20 additions & 0 deletions packages/vm/core/evm/evmtest/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2041,3 +2041,23 @@ func TestEmitEventAndRevert(t *testing.T) {
require.ErrorContains(t, err, "execution reverted")
require.Empty(t, res.EVMReceipt.Logs)
}

func TestL1DepositEVM(t *testing.T) {
env := InitEVM(t)
// ensure that after a deposit to an EVM account, there is a tx/receipt for it to be auditable on the EVM side
_, ethAddr := env.Chain.NewEthereumAccountWithL2Funds()
bal, err := env.Chain.EVM().Balance(ethAddr, nil)
require.NoError(t, err)

// previous block must only have 1 tx, that corresponds to the deposit to ethAddr
block, err := env.Chain.EVM().BlockByNumber(big.NewInt(int64(env.getBlockNumber())))
require.NoError(t, err)
blockTxs := block.Transactions()
require.Len(t, blockTxs, 1)
tx := blockTxs[0]
require.True(t, ethAddr == *tx.To())
require.Zero(t, tx.Value().Cmp(bal))

rec := env.Chain.EVM().TransactionReceipt(tx.Hash())
require.NotNil(t, rec)
}
3 changes: 3 additions & 0 deletions packages/vm/core/evm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ var (
FuncRegisterERC20ExternalNativeToken = coreutil.Func(evmnames.FuncRegisterERC20ExternalNativeToken)
FuncGetERC20ExternalNativeTokenAddress = coreutil.ViewFunc(evmnames.FuncGetERC20ExternalNativeTokenAddress)
FuncRegisterERC721NFTCollection = coreutil.Func(evmnames.FuncRegisterERC721NFTCollection)

FuncNewL1Deposit = coreutil.Func(evmnames.FuncNewL1Deposit)
)

const (
FieldTransaction = evmnames.FieldTransaction
FieldCallMsg = evmnames.FieldCallMsg
FieldChainID = evmnames.FieldChainID
FieldAddress = evmnames.FieldAddress
FieldAssets = evmnames.FieldAssets
FieldKey = evmnames.FieldKey
FieldAgentID = evmnames.FieldAgentID
FieldTransactionIndex = evmnames.FieldTransactionIndex
Expand Down

0 comments on commit 88d4165

Please sign in to comment.