Skip to content

Commit 2e429b4

Browse files
committed
.
1 parent 4790bf0 commit 2e429b4

File tree

6 files changed

+96
-18
lines changed

6 files changed

+96
-18
lines changed

fvm/flex/environment/environment.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ func (fe *Environment) Deploy(
222222
// back to the FVM environment. This method should only be used for FOA's
223223
// accounts where resource ownership has been verified
224224
func (fe *Environment) Call(
225-
from *common.Address,
226-
to *common.Address,
225+
from common.Address,
226+
to common.Address,
227227
data []byte,
228228
gasLimit uint64,
229229
value *big.Int,
@@ -233,7 +233,7 @@ func (fe *Environment) Call(
233233
}
234234
// TODO: verify that the authorizer has the resource to interact with this contract (higher level check)
235235

236-
msg := directCallMessage(from, to, value, data, gasLimit)
236+
msg := directCallMessage(&from, &to, value, data, gasLimit)
237237

238238
return fe.run(msg)
239239
}

fvm/flex/environment/environment_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ func TestContractInteraction(t *testing.T) {
160160
store, err := abi.Pack("store", num)
161161
require.NoError(t, err)
162162

163-
err = env.Call(&testAccount,
164-
&contractAddr,
163+
err = env.Call(testAccount,
164+
contractAddr,
165165
store,
166166
1_000_000,
167167
// this should be zero because the contract doesn't have receiver
@@ -175,8 +175,8 @@ func TestContractInteraction(t *testing.T) {
175175
retrieve, err := abi.Pack("retrieve")
176176
require.NoError(t, err)
177177

178-
err = env.Call(&testAccount,
179-
&contractAddr,
178+
err = env.Call(testAccount,
179+
contractAddr,
180180
retrieve,
181181
1_000_000,
182182
// this should be zero because the contract doesn't have receiver

fvm/flex/flex_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func encodeArgs(argValues []cadence.Value) [][]byte {
4646
func Test(t *testing.T) {
4747

4848
RunWithTempLedger(t, func(led atree.Ledger) {
49-
handler := NewBaseFlexContractHandler(led)
49+
handler := NewFlexContractHandler(led)
5050
env := runtime.NewBaseInterpreterEnvironment(runtime.Config{})
5151
flex := fstdlib.NewFlexStandardLibraryValue(nil, handler)
5252
env.Declare(*flex)

fvm/flex/handler.go

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,99 @@ import (
88
"github.com/onflow/flow-go/fvm/flex/storage"
99
)
1010

11-
type BaseFlexContractHandler struct {
11+
type FOAHandler struct {
12+
fch FlexContractHandler
13+
address *models.FlexAddress
14+
// TODO inject gas meter
15+
}
16+
17+
// Address returns the flex address associated with the FOA account
18+
func (h *FOAHandler) Address() *models.FlexAddress {
19+
return h.address
20+
}
21+
22+
// Deposit deposits the token from the given vault into the Flex main vault
23+
// and update the FOA balance with the new amount
24+
func (h *FOAHandler) Deposit(models.FlowTokenVault) {
25+
panic("not implemented")
26+
}
27+
28+
// Withdraw deducts the balance from the FOA account and
29+
// withdraw and return flow token from the Flex main vault.
30+
func (h *FOAHandler) Withdraw(models.Balance) models.FlowTokenVault {
31+
panic("not implemented")
32+
}
33+
34+
// Deploy deploys a contract to the Flex environment
35+
// the new deployed contract would be at the returned address and
36+
// 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+
config := env.NewFlexConfig(
39+
env.WithBlockNumber(env.BlockNumberForEVMRules))
40+
env, err := env.NewEnvironment(config, h.fch.db)
41+
// TODO improve this
42+
if err != nil {
43+
panic(err)
44+
}
45+
// TODO check gas limit against what has been left on the transaction side
46+
env.Deploy(h.address.ToCommon(), code, uint64(gaslimit), balance.InAttoFlow())
47+
if env.Result.Failed {
48+
// TODO panic with a handlable error
49+
panic("deploy failed")
50+
}
51+
return models.FlexAddress(env.Result.DeployedContractAddress)
52+
}
53+
54+
// Call calls a smart contract function with the given data
55+
// it would limit the gas used according to the limit provided
56+
// given it doesn't goes beyond what Flow transaction allows.
57+
// the balance would be deducted from the OFA account and would be transferred to the target address
58+
// 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+
config := env.NewFlexConfig(
61+
env.WithBlockNumber(env.BlockNumberForEVMRules))
62+
env, err := env.NewEnvironment(config, h.fch.db)
63+
// TODO improve this
64+
if err != nil {
65+
panic(err)
66+
}
67+
// TODO check gas limit against what has been left on the transaction side
68+
env.Call(h.address.ToCommon(), to.ToCommon(), data, uint64(gaslimit), balance.InAttoFlow())
69+
if env.Result.Failed {
70+
// TODO panic with a handlable error
71+
panic("call failed")
72+
}
73+
return env.Result.RetValue
74+
}
75+
76+
var _ models.FlowOwnedAccount = &FOAHandler{}
77+
78+
type FlexContractHandler struct {
1279
db *storage.Database
80+
// TODO inject what captures how much gas has been used
1381
}
1482

15-
var _ models.FlexContractHandler = &BaseFlexContractHandler{}
83+
var _ models.FlexContractHandler = &FlexContractHandler{}
1684

17-
func NewBaseFlexContractHandler(ledger atree.Ledger) *BaseFlexContractHandler {
18-
return &BaseFlexContractHandler{
85+
func NewFlexContractHandler(ledger atree.Ledger) *FlexContractHandler {
86+
return &FlexContractHandler{
1987
db: storage.NewDatabase(ledger),
2088
}
2189
}
2290

23-
func (h BaseFlexContractHandler) NewFlowOwnedAccount() models.FlowOwnedAccount {
91+
func (h FlexContractHandler) NewFlowOwnedAccount() models.FlowOwnedAccount {
92+
// allocate a new address
93+
// TODO check for collission
94+
// from 20 bytes, the first 12 could be zero, the next 8 could be the output of a uuid generator (resourceID?)
95+
// does this leads to trie depth issue?
2496
panic("not implemented yet")
2597
}
2698

27-
func (h BaseFlexContractHandler) LastExecutedBlock() models.FlexBlock {
99+
func (h FlexContractHandler) LastExecutedBlock() models.FlexBlock {
28100
panic("not implemented yet")
29101
}
30102

31-
func (h BaseFlexContractHandler) Run(tx []byte, coinbase models.FlexAddress) bool {
103+
func (h FlexContractHandler) Run(tx []byte, coinbase models.FlexAddress) bool {
32104
config := env.NewFlexConfig(
33105
env.WithCoinbase(common.Address(coinbase)),
34106
env.WithBlockNumber(env.BlockNumberForEVMRules))

fvm/flex/models/models.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package models
22

33
import (
4+
"math/big"
5+
46
"github.com/ethereum/go-ethereum/common"
57
"github.com/onflow/cadence"
68
)
@@ -19,6 +21,10 @@ import (
1921
// Flex addresses are evm-compatible addresses
2022
type FlexAddress common.Address
2123

24+
func (fa FlexAddress) ToCommon() common.Address {
25+
return common.Address(fa)
26+
}
27+
2228
func NewFlexAddressFromString(str string) FlexAddress {
2329
return FlexAddress(common.BytesToAddress([]byte(str)))
2430
}
@@ -37,15 +43,15 @@ type FlexBlock interface {
3743
// a separate type has been considered here to prevent
3844
// accidental dev mistakes when dealing with the conversion
3945
type Balance interface {
40-
InAttoFlow() uint64
46+
InAttoFlow() *big.Int
4147
InFlow() cadence.UFix64
4248
}
4349

4450
type Gaslimit uint64
4551

4652
type Code []byte
4753

48-
type Data []byte
54+
type Data []byte // TODO add functionality to convert this into values
4955

5056
type FlexAccount interface {
5157
Address() FlexAddress

fvm/flex/stdlib/flex.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func init() {
101101
}
102102

103103
var flexContractStaticType interpreter.StaticType = interpreter.NewCompositeStaticType(
104-
nil, // TODO deal with memory gage
104+
nil, // TODO deal with memory gauge
105105
FlexType.Location,
106106
FlexType.QualifiedIdentifier(),
107107
FlexType.ID(),

0 commit comments

Comments
 (0)