Skip to content

Commit 0d46322

Browse files
authored
Merge pull request #4745 from onflow/bastian/flex-poc
2 parents 2e429b4 + 2170337 commit 0d46322

File tree

2 files changed

+69
-54
lines changed

2 files changed

+69
-54
lines changed

fvm/flex/handler.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package flex
33
import (
44
"github.com/ethereum/go-ethereum/common"
55
"github.com/onflow/atree"
6+
67
env "github.com/onflow/flow-go/fvm/flex/environment"
78
"github.com/onflow/flow-go/fvm/flex/models"
89
"github.com/onflow/flow-go/fvm/flex/storage"
@@ -21,20 +22,20 @@ func (h *FOAHandler) Address() *models.FlexAddress {
2122

2223
// Deposit deposits the token from the given vault into the Flex main vault
2324
// and update the FOA balance with the new amount
24-
func (h *FOAHandler) Deposit(models.FlowTokenVault) {
25+
func (h *FOAHandler) Deposit(models.FLOWTokenVault) {
2526
panic("not implemented")
2627
}
2728

2829
// Withdraw deducts the balance from the FOA account and
2930
// withdraw and return flow token from the Flex main vault.
30-
func (h *FOAHandler) Withdraw(models.Balance) models.FlowTokenVault {
31+
func (h *FOAHandler) Withdraw(models.Balance) models.FLOWTokenVault {
3132
panic("not implemented")
3233
}
3334

3435
// Deploy deploys a contract to the Flex environment
3536
// the new deployed contract would be at the returned address and
3637
// the contract data is not controlled by the FOA accounts
37-
func (h *FOAHandler) Deploy(code models.Code, gaslimit models.Gaslimit, balance models.Balance) models.FlexAddress {
38+
func (h *FOAHandler) Deploy(code models.Code, gaslimit models.GasLimit, balance models.Balance) models.FlexAddress {
3839
config := env.NewFlexConfig(
3940
env.WithBlockNumber(env.BlockNumberForEVMRules))
4041
env, err := env.NewEnvironment(config, h.fch.db)
@@ -56,7 +57,7 @@ func (h *FOAHandler) Deploy(code models.Code, gaslimit models.Gaslimit, balance
5657
// given it doesn't goes beyond what Flow transaction allows.
5758
// the balance would be deducted from the OFA account and would be transferred to the target address
5859
// contract data is not controlled by the FOA accounts
59-
func (h *FOAHandler) Call(to models.FlexAddress, data models.Data, gaslimit models.Gaslimit, balance models.Balance) models.Data {
60+
func (h *FOAHandler) Call(to models.FlexAddress, data models.Data, gaslimit models.GasLimit, balance models.Balance) models.Data {
6061
config := env.NewFlexConfig(
6162
env.WithBlockNumber(env.BlockNumberForEVMRules))
6263
env, err := env.NewEnvironment(config, h.fch.db)

fvm/flex/models/models.go

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,57 @@ package models
33
import (
44
"math/big"
55

6-
"github.com/ethereum/go-ethereum/common"
6+
gethCommon "github.com/ethereum/go-ethereum/common"
77
"github.com/onflow/cadence"
88
)
99

10-
// Flex is an account inside FVM with special access to the underlying infrasturcture which
11-
// allows to run a virtual evm-based blockchain on top of FVM. There are two ways to interact with this
12-
// blockchain (flex env), first by passing signed transactions (EOA accounts) through Flex.Run method and each transaction
13-
// would be considered as a block, updating the internal merkle tree of the Flex env and emitting a new root hash.
14-
// The second way is through a new form of accounts called (FOA) which acts as a resource that any account on Flow environment
15-
// could own and it has a collision-proof allocated flex address and any one who owns the resource, would be able to
16-
// interact with the Flex environment on behalf of the flex address.
17-
18-
// The Flex enviornment shares the same native token as Flow, there is no new tokens minted and tokens could be bridged between
19-
// FOA resources and Flow accounts. Then it could be circulated to any address space in the Flex (EOA, smart contracts), etc.
20-
21-
// Flex addresses are evm-compatible addresses
22-
type FlexAddress common.Address
23-
24-
func (fa FlexAddress) ToCommon() common.Address {
25-
return common.Address(fa)
10+
// Flex is an account inside FVM with special access to the underlying infrastructure
11+
// which allows to run a virtual EVM-based blockchain inside FVM.
12+
//
13+
// There are two ways to interact with this environment:
14+
//
15+
// First, passing a signed transaction (EOA account) to the `Flex.run` Cadence function
16+
// creates a new block, updates the internal merkle tree, and emits a new root hash.
17+
//
18+
// The Second way is through a new form of account called Flow-owned account (FOA),
19+
// which is represented and controlled through a resource, owned by a Flow account.
20+
// The FOA has a collision-proof allocated address.
21+
/// The owner of the FOA resource can interact with the Flex environment on behalf of the Flex address.
22+
//
23+
// The Flex environment shares the same native token as Flow, there are no new tokens minted.
24+
// Other ERC-20 fungible tokens can be bridged between FOA resources and Flow accounts.
25+
26+
// FlexAddress is an EVM-compatible address
27+
type FlexAddress gethCommon.Address
28+
29+
func (fa FlexAddress) ToCommon() gethCommon.Address {
30+
return gethCommon.Address(fa)
2631
}
2732

2833
func NewFlexAddressFromString(str string) FlexAddress {
29-
return FlexAddress(common.BytesToAddress([]byte(str)))
34+
return FlexAddress(gethCommon.BytesToAddress([]byte(str)))
3035
}
3136

32-
// FlexBlock captures block info such as height and state
37+
// FlexBlock represents an EVM block.
38+
// It captures block info such as height and state
3339
type FlexBlock interface {
34-
// returns the height of this block (autoincrement number)
40+
// Height returns the height of this EVM block (auto-incremented number)
3541
Height() uint64
36-
// returns the root hash of the state after executing this block
37-
StateRoot() common.Hash
38-
// returns the root hash of the events emited during execution of this block
39-
EventRoot() common.Hash
42+
// StateRoot returns the EVM root hash of the state after executing this EVM block
43+
StateRoot() gethCommon.Hash
44+
// EventRoot returns the EVM root hash of the events emitted during execution of this EVM block
45+
EventRoot() gethCommon.Hash
4046
}
4147

42-
// Balance of a flex account
48+
// Balance represents the balance of a Flex account
4349
// a separate type has been considered here to prevent
4450
// accidental dev mistakes when dealing with the conversion
4551
type Balance interface {
4652
InAttoFlow() *big.Int
4753
InFlow() cadence.UFix64
4854
}
4955

50-
type Gaslimit uint64
56+
type GasLimit uint64
5157

5258
type Code []byte
5359

@@ -59,51 +65,59 @@ type FlexAccount interface {
5965
Balance() Balance
6066
}
6167

62-
type FlowTokenVault interface {
68+
// FLOWTokenVault represents a FLOW Vault
69+
type FLOWTokenVault interface {
6370
Balance() Balance
64-
Withdraw(Balance) FlowTokenVault
65-
Deposit(FlowTokenVault)
71+
Withdraw(Balance) FLOWTokenVault
72+
Deposit(FLOWTokenVault)
6673
}
6774

68-
// FlowOwnedAccount is a new type of accounts on the Flex environment
69-
// that instead of being managed by public key inside the Flex, it would be managed
70-
// as a resource inside the FVM accounts. in other words, the FVM account who holds
71-
// a owns the FOA resource, could bridge native token from and to flex account associated with the FOA
72-
// and deploys contracts or calls method on contracts without the need to sign a transaction.
75+
// FlowOwnedAccount is a new type of account in the Flex environment,
76+
// that instead of being managed by public key inside the Flex,
77+
// is managed by a resource owned by a Flow account.
78+
//
79+
// In other words, the FVM account who owns the FOA resource
80+
// can bridge native tokens to and from the Flex account associated with the FOA,
81+
// deploy contracts to the Flex environment,
82+
// or call methods on contracts without the need to sign a transaction.
7383
type FlowOwnedAccount interface {
7484
// Address returns the flex address associated with the FOA account
7585
Address() *FlexAddress
7686

7787
// Deposit deposits the token from the given vault into the Flex main vault
7888
// and update the FOA balance with the new amount
79-
Deposit(FlowTokenVault)
89+
// TODO: move to FlexAccount
90+
Deposit(FLOWTokenVault)
8091

8192
// Withdraw deducts the balance from the FOA account and
8293
// withdraw and return flow token from the Flex main vault.
83-
Withdraw(Balance) FlowTokenVault
94+
Withdraw(Balance) FLOWTokenVault
8495

8596
// Deploy deploys a contract to the Flex environment
8697
// the new deployed contract would be at the returned address and
8798
// the contract data is not controlled by the FOA accounts
88-
Deploy(Code, Gaslimit, Balance) FlexAddress
89-
90-
// Call calls a smart contract function with the given data
91-
// it would limit the gas used according to the limit provided
92-
// given it doesn't goes beyond what Flow transaction allows.
93-
// the balance would be deducted from the OFA account and would be transferred to the target address
94-
// contract data is not controlled by the FOA accounts
95-
Call(FlexAddress, Data, Gaslimit, Balance) Data
99+
Deploy(Code, GasLimit, Balance) FlexAddress
100+
101+
// Call calls a smart contract function with the given data.
102+
// The gas usage is limited by the given gas limit,
103+
// and the Flow transaction's computation limit.
104+
// The fees are deducted from the FOA
105+
// and are transferred to the target address.
106+
// TODO: clarify
107+
// Contract data is not controlled by the FOA account
108+
Call(FlexAddress, Data, GasLimit, Balance) Data
96109
}
97110

98-
// Flex contract handles operations on the flex environment
111+
// FlexContractHandler handles operations on the Flex environment
99112
type FlexContractHandler interface {
100-
// constructs a new flow owned account
113+
// NewFlowOwnedAccount constructs a new FOA
101114
NewFlowOwnedAccount() FlowOwnedAccount
102115

103-
// returns the last executed block info
116+
// LastExecutedBlock returns information about the last executed block
104117
LastExecutedBlock() FlexBlock
105118

106-
// runs a transaction in the Flex environment and collect
107-
// the flex gas fees under coinbase account, if tx is success full it returns true
108-
Run(bytes []byte, coinbase FlexAddress) bool
119+
// Run runs a transaction in the Flex environment,
120+
// collects the gas fees, and transfers the gas fees to the given coinbase account.
121+
// Returns true if the transaction was successfully executed
122+
Run(tx []byte, coinbase FlexAddress) bool
109123
}

0 commit comments

Comments
 (0)