Skip to content

Commit

Permalink
Merge pull request onflow#5933 from onflow/gregor/evm/event-address-hex
Browse files Browse the repository at this point in the history
[EVM] Event addresses hex-encoded
  • Loading branch information
sideninja authored May 17, 2024
2 parents 4f85bc2 + 5837ad1 commit a430211
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 42 deletions.
2 changes: 1 addition & 1 deletion engine/execution/state/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestBootstrapLedger(t *testing.T) {
}

func TestBootstrapLedger_ZeroTokenSupply(t *testing.T) {
expectedStateCommitmentBytes, _ := hex.DecodeString("1668d2976c830ef5b8d860d8bee72a0cdf4ddf01bfa63b4b2f2981a453752499")
expectedStateCommitmentBytes, _ := hex.DecodeString("d8a3773e2fa181ae0291249b0a4d4e1453c282fe5e159f75aa57045b6d618778,")
expectedStateCommitment, err := flow.ToStateCommitment(expectedStateCommitmentBytes)
require.NoError(t, err)

Expand Down
13 changes: 7 additions & 6 deletions fvm/evm/stdlib/contract.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ contract EVM {
)

access(all)
event CadenceOwnedAccountCreated(addressBytes: [UInt8; 20])
event CadenceOwnedAccountCreated(address: String)

/// FLOWTokensDeposited is emitted when FLOW tokens is bridged
/// into the EVM environment. Note that this event is not emitted
/// for transfer of flow tokens between two EVM addresses.
access(all)
event FLOWTokensDeposited(addressBytes: [UInt8; 20], amount: UFix64)
event FLOWTokensDeposited(address: String, amount: UFix64)

/// FLOWTokensWithdrawn is emitted when FLOW tokens are bridged
/// out of the EVM environment. Note that this event is not emitted
/// for transfer of flow tokens between two EVM addresses.
access(all)
event FLOWTokensWithdrawn(addressBytes: [UInt8; 20], amount: UFix64)
event FLOWTokensWithdrawn(address: String, amount: UFix64)

/// BridgeAccessorUpdated is emitted when the BridgeAccessor Capability
/// is updated in the stored BridgeRouter along with identifying
Expand Down Expand Up @@ -147,7 +147,7 @@ contract EVM {
from: <-from,
to: self.bytes
)
emit FLOWTokensDeposited(addressBytes: self.bytes, amount: amount)
emit FLOWTokensDeposited(address: self.toString(), amount: amount)
}

/// Serializes the address to a hex string without the 0x prefix
Expand Down Expand Up @@ -366,7 +366,7 @@ contract EVM {
from: self.addressBytes,
amount: balance.attoflow
) as! @FlowToken.Vault
emit FLOWTokensWithdrawn(addressBytes: self.addressBytes, amount: balance.inFLOW())
emit FLOWTokensWithdrawn(address: self.address().toString(), amount: balance.inFLOW())
return <-vault
}

Expand Down Expand Up @@ -465,7 +465,8 @@ contract EVM {
let acc <-create CadenceOwnedAccount()
let addr = InternalEVM.createCadenceOwnedAccount(uuid: acc.uuid)
acc.initAddress(addressBytes: addr)
emit CadenceOwnedAccountCreated(addressBytes: addr)

emit CadenceOwnedAccountCreated(address: acc.address().toString())
return <-acc
}

Expand Down
41 changes: 17 additions & 24 deletions fvm/evm/stdlib/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"encoding/hex"
"math/big"
"strings"
"testing"

"github.com/onflow/cadence"
Expand Down Expand Up @@ -182,6 +183,16 @@ func (t *testFlowAccount) Call(address types.Address, data types.Data, limit typ
return t.call(address, data, limit, balance)
}

func requireEqualEventAddress(t *testing.T, event cadence.Event, address types.Address) {
actual := cadence.SearchFieldByName(event, types.CadenceOwnedAccountCreatedTypeAddressFieldName)
strippedHex := strings.TrimPrefix(address.String(), "0x")
expected, err := cadence.NewString(strippedHex)
if err != nil {
require.NoError(t, err)
}
require.Equal(t, expected, actual)
}

func deployContracts(
t *testing.T,
rt runtime.Runtime,
Expand Down Expand Up @@ -3443,22 +3454,10 @@ func TestEVMCreateCadenceOwnedAccount(t *testing.T) {

// check cadence owned account created events
expectedCoaAddress := types.Address{3}
require.Equal(t,
expectedCoaAddress.ToCadenceValue(),
cadence.SearchFieldByName(
events[0],
types.CadenceOwnedAccountCreatedTypeAddressBytesFieldName,
),
)
requireEqualEventAddress(t, events[0], expectedCoaAddress)

expectedCoaAddress = types.Address{4}
require.Equal(t,
expectedCoaAddress.ToCadenceValue(),
cadence.SearchFieldByName(
events[1],
types.CadenceOwnedAccountCreatedTypeAddressBytesFieldName,
),
)
requireEqualEventAddress(t, events[1], expectedCoaAddress)
}

func TestCadenceOwnedAccountCall(t *testing.T) {
Expand Down Expand Up @@ -3828,11 +3827,8 @@ func TestCOADeposit(t *testing.T) {
tokenDepositEvent := events[3]
tokenDepositEventFields := cadence.FieldsMappedByName(tokenDepositEvent)

// check address
require.Equal(t,
expectedCoaAddress.ToCadenceValue(),
tokenDepositEventFields["addressBytes"],
)
requireEqualEventAddress(t, tokenDepositEvent, expectedCoaAddress)

// check amount
require.Equal(t,
expectedBalance,
Expand Down Expand Up @@ -4001,11 +3997,8 @@ func TestCadenceOwnedAccountWithdraw(t *testing.T) {
tokenWithdrawEvent := events[4]
tokenWithdrawEventFields := cadence.FieldsMappedByName(tokenWithdrawEvent)

// check address
require.Equal(t,
expectedCoaAddress.ToCadenceValue(),
tokenWithdrawEventFields["addressBytes"],
)
requireEqualEventAddress(t, tokenWithdrawEvent, expectedCoaAddress)

// check amount
require.Equal(t,
expectedWithdrawBalance,
Expand Down
17 changes: 9 additions & 8 deletions fvm/evm/types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"bytes"
"encoding/hex"
"fmt"

"github.com/onflow/cadence"
Expand Down Expand Up @@ -76,7 +77,7 @@ func NewAddressFromBytes(inp []byte) Address {
return Address(gethCommon.BytesToAddress(inp))
}

const CadenceOwnedAccountCreatedTypeAddressBytesFieldName = "addressBytes"
const CadenceOwnedAccountCreatedTypeAddressFieldName = "address"

func COAAddressFromFlowCOACreatedEvent(evmContractAddress flow.Address, event flow.Event) (Address, error) {
// check the type first
Expand All @@ -95,19 +96,19 @@ func COAAddressFromFlowCOACreatedEvent(evmContractAddress flow.Address, event fl
return Address{}, fmt.Errorf("event data is not a cadence event")
}

addressBytesValue := cadence.SearchFieldByName(
addressValue := cadence.SearchFieldByName(
cadenceEvent,
CadenceOwnedAccountCreatedTypeAddressBytesFieldName,
CadenceOwnedAccountCreatedTypeAddressFieldName,
)

addressBytesArray, ok := addressBytesValue.(cadence.Array)
addressString, ok := addressValue.(cadence.String)
if !ok {
return Address{}, fmt.Errorf("addressBytes is not an array")
return Address{}, fmt.Errorf("address is not a string")
}

addressBytes := make([]byte, AddressLength)
for i, v := range addressBytesArray.Values {
addressBytes[i] = byte(v.(cadence.UInt8))
addressBytes, err := hex.DecodeString(string(addressString))
if err != nil {
return Address{}, err
}

return NewAddressFromBytes(addressBytes), nil
Expand Down
6 changes: 3 additions & 3 deletions utils/unittest/execution_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const ServiceAccountPrivateKeySignAlgo = crypto.ECDSAP256
const ServiceAccountPrivateKeyHashAlgo = hash.SHA2_256

// Pre-calculated state commitment with root account with the above private key
const GenesisStateCommitmentHex = "96493d5fe1772a2c89df85337bd46d089e200cca91a6873f7b4bf872e27a6d7c"
const GenesisStateCommitmentHex = "31387e0426f017e4de41a189f2cec21e9c7e832950099838338a1a432d97c092"

var GenesisStateCommitment flow.StateCommitment

Expand Down Expand Up @@ -87,10 +87,10 @@ func genesisCommitHexByChainID(chainID flow.ChainID) string {
return GenesisStateCommitmentHex
}
if chainID == flow.Testnet {
return "57c1a8b33cf96d6c006ff86e9ab307ac09e91118440d7a1679cbcaf464233e5d"
return "ad91363b261d818269de9adcd8a47fb865ae7d447ea7607337d88d885d08bdd1"
}
if chainID == flow.Sandboxnet {
return "e1c08b17f9e5896f03fe28dd37ca396c19b26628161506924fbf785834646ea1"
}
return "586452c116b30e96fad18b95eaee32c9762a5324dc3ecc0e9948781f13736fdf"
return "47d511aefe4e464ac800c54170344244655ff853917abb0f2759f3222cfdab06"
}

0 comments on commit a430211

Please sign in to comment.