Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: WebAssembly API #2760

Draft
wants to merge 49 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
2b7c365
add support for dictionary types
turbolent Aug 31, 2023
9fdfb4d
update wasmtime
turbolent Sep 1, 2023
cbc1ece
improve encodeArgs helper
turbolent Sep 1, 2023
a780497
add the start of a WebAssembly contract
turbolent Sep 1, 2023
188b162
Merge branch 'feature/stable-cadence' into bastian/webassembly
turbolent Sep 1, 2023
02a0122
add comment explaining the program
turbolent Sep 22, 2023
8d9cbb6
convert type
turbolent Sep 22, 2023
254cb3d
Merge branch 'feature/stable-cadence' into bastian/webassembly
turbolent Sep 22, 2023
eaaf1ea
Merge branch 'bastian/allow-type-injection' into bastian/webassembly
turbolent Sep 22, 2023
d4265f6
inject WebAssembly standard library type into environment
turbolent Sep 22, 2023
384eef2
gate WebAssembly behind feture flag
turbolent Sep 23, 2023
4240bf9
move wasmtime-based WebAssembly module to runtime, so it can be reused
turbolent Sep 23, 2023
57b8d6a
rename
turbolent Sep 23, 2023
8d30d3a
extract constructor for wasmtime-based WebAssembly module
turbolent Sep 23, 2023
606c3a2
add metering using fuel mechanism
turbolent Sep 23, 2023
a6e7b9a
fix return type conversion
turbolent Sep 23, 2023
f54b831
Merge branch 'bastian/sync-stable-cadence-10' into bastian/webassembly
turbolent Oct 5, 2023
f8270bd
Merge branch 'bastian/sync-stable-cadence-10' into bastian/webassembly
turbolent Oct 5, 2023
692aa2a
re-generate
turbolent Oct 5, 2023
eb98a3d
Merge branch 'feature/stable-cadence' into bastian/webassembly
turbolent Oct 5, 2023
b4c634b
update comment to reflect current functionality
turbolent Mar 3, 2024
cbfcbd3
Merge branch 'master' into bastian/webassembly
turbolent Jun 25, 2024
b67d23f
update to wasmtime v22
turbolent Jun 25, 2024
e470d46
intgrate WebAssembly standard library values and types into existing …
turbolent Jun 25, 2024
ead17b8
lint
turbolent Jun 25, 2024
e3c752f
update copyright
turbolent Jun 25, 2024
f197db0
improve config
turbolent Jun 25, 2024
23795cb
improve metering: always meter, even on failure
turbolent Jun 25, 2024
f6b6acf
disabling SIMD is broken
turbolent Jun 25, 2024
8bcbf8c
fix check
turbolent Jun 25, 2024
12b56a5
limit store contents
turbolent Jun 25, 2024
9089bc6
add comments
turbolent Jun 26, 2024
6b6c8fb
update wasmtime
turbolent Jun 26, 2024
e55b81e
improve wasmtime setup
turbolent Jun 26, 2024
8bd4f7f
update wasmtime
turbolent Jul 12, 2024
d737d3d
add tests
turbolent Jul 12, 2024
efa0800
convert wasmtime trap to generic stdlib trap error
turbolent Jul 12, 2024
d9a6321
make trap error a user error
turbolent Jul 12, 2024
12817cb
improve error for invalid non-function export
turbolent Jul 12, 2024
ab88756
improve error for WebAssembly module compilation failure
turbolent Jul 12, 2024
a3fe557
convert wasmtime call trap into generic WebAssembly trap error
turbolent Jul 12, 2024
9b4c462
GetFuel/SetFuel should never error
turbolent Jul 12, 2024
755d0d3
add runtime interface method to get remaining computation for given kind
turbolent Jul 12, 2024
ef5d371
use new computation remaining for fuel
turbolent Jul 12, 2024
2d42183
Merge branch 'master' into bastian/webassembly
turbolent Jul 13, 2024
e6c5f1b
Merge branch 'master' into bastian/webassembly
turbolent Oct 16, 2024
d3cca43
fix merge: implement new runtime interface functions
turbolent Oct 16, 2024
c0a9bf1
update to wasmtime v25
turbolent Oct 16, 2024
91b3e19
Merge branch 'master' into bastian/webassembly
turbolent Nov 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/bits-and-blooms/bitset v1.5.0
github.com/bytecodealliance/wasmtime-go/v7 v7.0.0
github.com/bytecodealliance/wasmtime-go/v12 v12.0.0
github.com/c-bata/go-prompt v0.2.6
github.com/dave/dst v0.27.2
github.com/fxamacker/cbor/v2 v2.4.1-0.20230228173756-c0c9f774e40c
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/SaveTheRbtz/mph v0.1.2 h1:5l3W496Up+7BNOVJQnJhzcGBh+wWfxWdmPUAkx3WmaM
github.com/SaveTheRbtz/mph v0.1.2/go.mod h1:V4+WtKQPe2+dEA5os1WnGsEB0NR9qgqqgIiSt73+sT4=
github.com/bits-and-blooms/bitset v1.5.0 h1:NpE8frKRLGHIcEzkR+gZhiioW1+WbYV6fKwD6ZIpQT8=
github.com/bits-and-blooms/bitset v1.5.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/bytecodealliance/wasmtime-go/v7 v7.0.0 h1:/rBNjgFju2HCZnkPb1eL+W4GBwP8DMbaQu7i+GR9DH4=
github.com/bytecodealliance/wasmtime-go/v7 v7.0.0/go.mod h1:bu6fic7trDt20w+LMooX7j3fsOwv4/ln6j8gAdP6vmA=
github.com/bytecodealliance/wasmtime-go/v12 v12.0.0 h1:Wga02UaZXYF3p0LIeL5xFp09/RI7UhjfT2uB0mLrwlw=
github.com/bytecodealliance/wasmtime-go/v12 v12.0.0/go.mod h1:a3PRoftJxxUzkQvgjC6sv7pKyJJK0ZsFVmH+eeEKQC4=
github.com/c-bata/go-prompt v0.2.6 h1:POP+nrHE+DfLYx370bedwNhsqmpCUynWPxuHi0C5vZI=
github.com/c-bata/go-prompt v0.2.6/go.mod h1:/LMAke8wD2FsNu9EXNdHxNLbd9MedkPnCdfpU9wwHfY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
8 changes: 4 additions & 4 deletions runtime/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ func TestRuntimeAuthAccountKeysAdd(t *testing.T) {
err := rt.ExecuteTransaction(
Script{
Source: []byte(code),
Arguments: encodeArgs([]cadence.Value{pubKey}),
Arguments: encodeArgs(pubKey),
},
Context{
Location: nextTransactionLocation(),
Expand Down Expand Up @@ -1048,7 +1048,7 @@ func addPublicKeyValidation(runtimeInterface *TestRuntimeInterface, returnError
}
}

func encodeArgs(argValues []cadence.Value) [][]byte {
func encodeArgs(argValues ...cadence.Value) [][]byte {
args := make([][]byte, len(argValues))
for i, arg := range argValues {
var err error
Expand All @@ -1071,7 +1071,7 @@ func (test accountKeyTestCase) executeTransaction(
runtimeInterface *TestRuntimeInterface,
location Location,
) error {
args := encodeArgs(test.args)
args := encodeArgs(test.args...)

err := runtime.ExecuteTransaction(
Script{
Expand All @@ -1091,7 +1091,7 @@ func (test accountKeyTestCase) executeScript(
runtimeInterface *TestRuntimeInterface,
) (cadence.Value, error) {

args := encodeArgs(test.args)
args := encodeArgs(test.args...)

value, err := runtime.ExecuteScript(
Script{
Expand Down
4 changes: 4 additions & 0 deletions runtime/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ func (*StandardLibraryHandler) BLSAggregateSignatures(_ [][]byte) ([]byte, error
return nil, goerrors.New("crypto functionality is not available in this environment")
}

func (*StandardLibraryHandler) CompileWebAssembly(_ []byte) (stdlib.WebAssemblyModule, error) {
return nil, goerrors.New("WebAssembly functionality is not available in this environment")
}

func (h *StandardLibraryHandler) NewOnEventEmittedHandler() interpreter.OnEventEmittedFunc {
return func(
inter *interpreter.Interpreter,
Expand Down
4 changes: 4 additions & 0 deletions runtime/common/computationkind.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,8 @@ const (
// RLP
ComputationKindSTDLIBRLPDecodeString
ComputationKindSTDLIBRLPDecodeList
_
_
// WebAssembly
ComputationKindWebAssemblyFuel
)
4 changes: 4 additions & 0 deletions runtime/common/computationkind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ type Config struct {
CoverageReport *CoverageReport
// AttachmentsEnabled specifies if attachments are enabled
AttachmentsEnabled bool
// WebAssemblyEnabled specifies if the WebAssembly API is enabled
WebAssemblyEnabled bool
}
4 changes: 2 additions & 2 deletions runtime/contract_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,10 @@ func TestRuntimeInvalidContractRedeploy(t *testing.T) {
err := runtime.ExecuteTransaction(
Script{
Source: tx,
Arguments: encodeArgs([]cadence.Value{
Arguments: encodeArgs(
cadence.String(foo1),
cadence.String(foo2),
}),
),
},
Context{
Interface: runtimeInterface,
Expand Down
10 changes: 5 additions & 5 deletions runtime/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func TestRuntimeSignatureAlgorithmImport(t *testing.T) {
value, err := runtime.ExecuteScript(
Script{
Source: []byte(script),
Arguments: encodeArgs([]cadence.Value{
Arguments: encodeArgs(
cadence.NewEnum([]cadence.Value{
cadence.UInt8(algo.RawValue()),
}).WithType(&cadence.EnumType{
Expand All @@ -362,7 +362,7 @@ func TestRuntimeSignatureAlgorithmImport(t *testing.T) {
},
},
}),
}),
),
},
Context{
Interface: runtimeInterface,
Expand Down Expand Up @@ -428,7 +428,7 @@ func TestRuntimeHashAlgorithmImport(t *testing.T) {
value, err := runtime.ExecuteScript(
Script{
Source: []byte(script),
Arguments: encodeArgs([]cadence.Value{
Arguments: encodeArgs(
cadence.NewEnum([]cadence.Value{
cadence.UInt8(algo.RawValue()),
}).WithType(&cadence.EnumType{
Expand All @@ -441,7 +441,7 @@ func TestRuntimeHashAlgorithmImport(t *testing.T) {
},
},
}),
}),
),
},
Context{
Interface: runtimeInterface,
Expand Down Expand Up @@ -802,7 +802,7 @@ func TestRuntimeTraversingMerkleProof(t *testing.T) {
_, err := runtime.ExecuteScript(
Script{
Source: script,
Arguments: encodeArgs([]cadence.Value{rootHash, address, accountProof}),
Arguments: encodeArgs(rootHash, address, accountProof),
},
Context{
Interface: runtimeInterface,
Expand Down
10 changes: 10 additions & 0 deletions runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ func newInterpreterEnvironment(config Config) *interpreterEnvironment {
}
env.InterpreterConfig = env.newInterpreterConfig()
env.CheckerConfig = env.newCheckerConfig()

if config.WebAssemblyEnabled {
env.DeclareValue(stdlib.NewWebAssemblyContract(nil, env))
env.DeclareType(stdlib.WebAssemblyContractType)
}

return env
}

Expand Down Expand Up @@ -694,6 +700,10 @@ func (e *interpreterEnvironment) Hash(data []byte, tag string, algorithm sema.Ha
return e.runtimeInterface.Hash(data, tag, algorithm)
}

func (e *interpreterEnvironment) CompileWebAssembly(bytes []byte) (stdlib.WebAssemblyModule, error) {
return e.runtimeInterface.CompileWebAssembly(bytes)
}

func (e *interpreterEnvironment) DecodeArgument(argument []byte, argumentType cadence.Type) (cadence.Value, error) {
return e.runtimeInterface.DecodeArgument(argument, argumentType)
}
Expand Down
12 changes: 6 additions & 6 deletions runtime/ft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,10 @@ func BenchmarkRuntimeFungibleTokenTransfer(b *testing.B) {
err = runtime.ExecuteTransaction(
Script{
Source: []byte(realMintFlowTokenTransaction),
Arguments: encodeArgs([]cadence.Value{
Arguments: encodeArgs(
cadence.Address(senderAddress),
mintAmount,
}),
),
},
Context{
Interface: runtimeInterface,
Expand All @@ -647,10 +647,10 @@ func BenchmarkRuntimeFungibleTokenTransfer(b *testing.B) {
err = runtime.ExecuteTransaction(
Script{
Source: []byte(realFlowTokenTransferTransaction),
Arguments: encodeArgs([]cadence.Value{
Arguments: encodeArgs(
sendAmount,
cadence.Address(receiverAddress),
}),
),
},
Context{
Interface: runtimeInterface,
Expand All @@ -677,9 +677,9 @@ func BenchmarkRuntimeFungibleTokenTransfer(b *testing.B) {
result, err := runtime.ExecuteScript(
Script{
Source: []byte(realFlowTokenBalanceScript),
Arguments: encodeArgs([]cadence.Value{
Arguments: encodeArgs(
cadence.Address(address),
}),
),
},
Context{
Interface: runtimeInterface,
Expand Down
8 changes: 4 additions & 4 deletions runtime/imported_values_memory_metering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestRuntimeImportedValueMemoryMetering(t *testing.T) {
_, err := runtime.ExecuteScript(
Script{
Source: script,
Arguments: encodeArgs(args),
Arguments: encodeArgs(args...),
},
Context{
Interface: runtimeInterface,
Expand Down Expand Up @@ -510,9 +510,9 @@ func TestRuntimeImportedValueMemoryMeteringForSimpleTypes(t *testing.T) {
_, err := runtime.ExecuteScript(
Script{
Source: script,
Arguments: encodeArgs([]cadence.Value{
Arguments: encodeArgs(
test.TypeInstance,
}),
),
},
Context{
Interface: runtimeInterface,
Expand Down Expand Up @@ -585,7 +585,7 @@ func TestRuntimeScriptDecodedLocationMetering(t *testing.T) {
_, err := runtime.ExecuteScript(
Script{
Source: script,
Arguments: encodeArgs([]cadence.Value{value}),
Arguments: encodeArgs(value),
},
Context{
Interface: runtimeInterface,
Expand Down
2 changes: 2 additions & 0 deletions runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/stdlib"
)

type Interface interface {
Expand Down Expand Up @@ -143,6 +144,7 @@ type Interface interface {
)
// GenerateAccountID generates a new, *non-zero*, unique ID for the given account.
GenerateAccountID(address common.Address) (uint64, error)
CompileWebAssembly(bytes []byte) (stdlib.WebAssemblyModule, error)
}

type MeterInterface interface {
Expand Down
8 changes: 4 additions & 4 deletions runtime/rlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ func TestRuntimeRLPDecodeString(t *testing.T) {
result, err := runtime.ExecuteScript(
Script{
Source: script,
Arguments: encodeArgs([]cadence.Value{
Arguments: encodeArgs(
cadence.Array{
ArrayType: &cadence.VariableSizedArrayType{
ElementType: cadence.UInt8Type,
},
Values: test.input,
},
}),
),
},
Context{
Interface: runtimeInterface,
Expand Down Expand Up @@ -291,14 +291,14 @@ func TestRuntimeRLPDecodeList(t *testing.T) {
result, err := runtime.ExecuteScript(
Script{
Source: script,
Arguments: encodeArgs([]cadence.Value{
Arguments: encodeArgs(
cadence.Array{
ArrayType: &cadence.VariableSizedArrayType{
ElementType: cadence.UInt8Type,
},
Values: test.input,
},
}),
),
},
Context{
Interface: runtimeInterface,
Expand Down
Loading
Loading