Skip to content

Commit

Permalink
Merge pull request onflow#5515 from onflow/bastian/5514-fix-coa-withdraw
Browse files Browse the repository at this point in the history
[EVM] Fix withdraw: set UUID FlowToken.Vault
  • Loading branch information
turbolent authored Mar 6, 2024
2 parents 61ee9b4 + 755da70 commit f2e22ea
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 6 deletions.
5 changes: 5 additions & 0 deletions fvm/evm/backends/wrappedEnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ func (we *WrappedEnvironment) Invoke(
return val, handleEnvironmentError(err)
}

func (we *WrappedEnvironment) GenerateUUID() (uint64, error) {
uuid, err := we.env.GenerateUUID()
return uuid, handleEnvironmentError(err)
}

func handleEnvironmentError(err error) error {
if err == nil {
return nil
Expand Down
6 changes: 6 additions & 0 deletions fvm/evm/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ func (h *ContractHandler) executeAndHandleCall(
return res, h.blockStore.CommitBlockProposal()
}

func (h *ContractHandler) GenerateResourceUUID() uint64 {
uuid, err := h.backend.GenerateUUID()
panicOnAnyError(err)
return uuid
}

type Account struct {
isAuthorized bool
address types.Address
Expand Down
6 changes: 6 additions & 0 deletions fvm/evm/stdlib/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,12 @@ func newInternalEVMTypeWithdrawFunction(
return uint64(ufix)
}),
},
{
Name: sema.ResourceUUIDFieldName,
Value: interpreter.NewUInt64Value(gauge, func() uint64 {
return handler.GenerateResourceUUID()
}),
},
},
common.ZeroAddress,
)
Expand Down
35 changes: 29 additions & 6 deletions fvm/evm/stdlib/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import (
)

type testContractHandler struct {
flowTokenAddress common.Address
evmContractAddress common.Address
deployCOA func(uint64) types.Address
accountByAddress func(types.Address, bool) types.Account
lastExecutedBlock func() *types.Block
run func(tx []byte, coinbase types.Address) *types.ResultSummary
flowTokenAddress common.Address
evmContractAddress common.Address
deployCOA func(uint64) types.Address
accountByAddress func(types.Address, bool) types.Account
lastExecutedBlock func() *types.Block
run func(tx []byte, coinbase types.Address) *types.ResultSummary
generateResourceUUID func() uint64
}

var _ types.ContractHandler = &testContractHandler{}
Expand Down Expand Up @@ -73,6 +74,13 @@ func (t *testContractHandler) Run(tx []byte, coinbase types.Address) *types.Resu
return t.run(tx, coinbase)
}

func (t *testContractHandler) GenerateResourceUUID() uint64 {
if t.generateResourceUUID == nil {
panic("unexpected GenerateResourceUUID")
}
return t.generateResourceUUID()
}

type testFlowAccount struct {
address types.Address
balance func() types.Balance
Expand Down Expand Up @@ -3380,6 +3388,8 @@ func TestCadenceOwnedAccountWithdraw(t *testing.T) {

contractsAddress := flow.BytesToAddress([]byte{0x1})

var nextUUID uint64 = 1

handler := &testContractHandler{
flowTokenAddress: common.Address(contractsAddress),
accountByAddress: func(fromAddress types.Address, isAuthorized bool) types.Account {
Expand All @@ -3405,6 +3415,11 @@ func TestCadenceOwnedAccountWithdraw(t *testing.T) {
},
}
},
generateResourceUUID: func() uint64 {
uuid := nextUUID
nextUUID++
return uuid
},
}

transactionEnvironment := newEVMTransactionEnvironment(handler, contractsAddress)
Expand All @@ -3429,6 +3444,8 @@ func TestCadenceOwnedAccountWithdraw(t *testing.T) {
let vault2 <- cadenceOwnedAccount.withdraw(balance: EVM.Balance(attoflow: 1230000000000000000))
let balance = vault2.balance
log(vault2.uuid)
destroy cadenceOwnedAccount
destroy vault2
Expand All @@ -3438,6 +3455,7 @@ func TestCadenceOwnedAccountWithdraw(t *testing.T) {

accountCodes := map[common.Location][]byte{}
var events []cadence.Event
var logs []string

runtimeInterface := &TestRuntimeInterface{
Storage: NewTestLedger(nil, nil),
Expand All @@ -3460,6 +3478,9 @@ func TestCadenceOwnedAccountWithdraw(t *testing.T) {
OnDecodeArgument: func(b []byte, t cadence.Type) (cadence.Value, error) {
return json.Decode(nil, b)
},
OnProgramLog: func(s string) {
logs = append(logs, s)
},
}

nextTransactionLocation := NewTransactionLocationGenerator()
Expand Down Expand Up @@ -3494,6 +3515,8 @@ func TestCadenceOwnedAccountWithdraw(t *testing.T) {
assert.True(t, deposited)
assert.True(t, withdrew)
assert.Equal(t, expectedWithdrawBalance, result)

assert.Equal(t, []string{"1"}, logs)
}

func TestCadenceOwnedAccountDeploy(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions fvm/evm/testutils/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ type TestBackend struct {
*TestBlockInfo
*TestRandomGenerator
*TestContractFunctionInvoker
*testUUIDGenerator
}

var _ types.Backend = &TestBackend{}
Expand Down Expand Up @@ -463,3 +464,16 @@ func (t *TestContractFunctionInvoker) Invoke(
}
return t.InvokeFunc(spec, arguments)
}

type testUUIDGenerator struct {
generateUUID func() (uint64, error)
}

var _ environment.UUIDGenerator = &testUUIDGenerator{}

func (t *testUUIDGenerator) GenerateUUID() (uint64, error) {
if t.generateUUID == nil {
panic("generateUUID method is not set")
}
return t.generateUUID()
}
1 change: 1 addition & 0 deletions fvm/evm/types/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ type Backend interface {
environment.BlockInfo
environment.RandomGenerator
environment.ContractFunctionInvoker
environment.UUIDGenerator
}
3 changes: 3 additions & 0 deletions fvm/evm/types/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type ContractHandler interface {

// EVMContractAddress returns the address where EVM is deployed
EVMContractAddress() common.Address

// GenerateResourceUUID generates a new UUID for a resource
GenerateResourceUUID() uint64
}

// AddressAllocator allocates addresses, used by the handler
Expand Down

0 comments on commit f2e22ea

Please sign in to comment.