Skip to content

Commit

Permalink
Merge branch 'main' into merkle_bench
Browse files Browse the repository at this point in the history
  • Loading branch information
iFrostizz authored Apr 15, 2024
2 parents df97603 + a0b6584 commit 2fa9847
Show file tree
Hide file tree
Showing 46 changed files with 698 additions and 371 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/hypersdk-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ on:
branches:
- main
pull_request:
types: [labeled,synchronize,reopened]

jobs:
hypersdk-unit-tests:
if: ${{ github.ref == 'refs/heads/main' || contains(github.event.pull_request.labels.*.name, 'run all ci') }}
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"x/programs/rust/wasmlanche-sdk",
"x/programs/rust/examples/token",
"x/programs/rust/examples/counter",
"x/programs/rust/wasmlanche-sdk/tests/test-crate",
]
resolver = "2"

Expand Down
34 changes: 17 additions & 17 deletions x/programs/cmd/simulator/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Program VM simulator (work in progress)

The `Simulator` is currently a work-in-progress.
It is a cli-tool built in Go, but meant to be used from the `wasmlanche-sdk` as a way to run automated tests on your HyperSDK wasm programs. For relatively up-to-date documentation, run `cargo doc --no-deps -p simulator --open`. Create a `Client`, create a `Plan`, then call `client.execute(plan)`, to execute the plan.

And please, if you see any inconsistencies in the `README.md`` here, [open a PR](https://github.com/ava-labs/hypersdk/edit/main/x/programs/cmd/simulator/README.md)!
It is a CLI-tool built in Go, but meant to be used from the `wasmlanche-sdk` as a way to run automated tests on your HyperSDK Wasm programs. For relatively up-to-date documentation, run `cargo doc --no-deps -p simulator --open`. Create a `Client`, create a `Plan`, then call `client.execute(plan)`, to execute the plan.

And please, if you see any inconsistencies in the `README.md` here, [open a PR](https://github.com/ava-labs/hypersdk/edit/main/x/programs/cmd/simulator/README.md)!

#### Note

Are we calling a Go based-cli from Rust? Yes. The Go-cli re-uses primitives from the HyperSDK, but we wanted to wrap that code in a Rust client to give a seamless experience testing.
Are we calling a Go-based CLI from Rust? Yes. The Go-CLI re-uses primitives from the HyperSDK, but we wanted to wrap that code in a Rust client to give a seamless experience testing.

## Cli Useage on its own
## CLI Usage on its own

The VM simulator provides a tool for testing and interacting with HyperSDK Wasm
`Programs`.
The VM simulator provides a tool for testing and interacting with HyperSDK Wasm Programs.

## Build

Expand All @@ -22,10 +22,10 @@ go build

## Getting Started

To try out out test token program its as easy as one command.
To try out the test token program, it's as easy as running one command from this working directory:

```sh
./simulator run ./cmd/testdata/basic_token.yaml
./bin/simulator run ./cmd/testdata/basic_token.yaml
```

```json
Expand All @@ -37,15 +37,15 @@ To try out out test token program its as easy as one command.
{"id":5,"result":{"response":[10000],"timestamp":1697835142}}
```

## Testing a HyperSDK Programs
## Testing a HyperSDK Program

To test a HyperSDK Program you will need to create a `Plan` file which can be
To test a HyperSDK Program, you will need to create a `Plan` file which can be
either `JSON` and `YAML`. Look at the example `./cmd/testdata/basic_token.yaml`
for hints on usage.

### Deploy a HyperSDK Program
### Deploying a HyperSDK Program

In this example we will create a new key `my_key` and deploy a new program
In this example we will create a new key `my_key` and deploy a new program.

```yaml
# new_program.yaml
Expand All @@ -68,24 +68,24 @@ steps:
value: ./my_program.wasm
```
Next we will run the simulation
Next we will run the simulation from this working directory:
```sh
$./simulator run ./new_program.yaml
./bin/simulator run cmd/my_program/new_program.yaml
```

```json
{"id":0,"result":{"msg":"created key my_key","timestamp":1697835142}}
{"id":2,"result":{"id":"2ut4fwdGE5FJG5w89CF3pVCjLrhiqCRZxB7ojtPnigh7QVU51i","timestamp":1697835142}}
```

Congratulations you have just deployed your first HyperSDK program! Lets make
sure to keep track of the transaction ID
Congratulations, you have just deployed your first HyperSDK program! Lets make
sure to keep track of the transaction ID.
`2ut4fwdGE5FJG5w89CF3pVCjLrhiqCRZxB7ojtPnigh7QVU51i`.

### Interact with a HyperSDK Program

Now that the program is on chain lets interact with it.
Now that the program is on chain, let's interact with it.

```yaml
# play_program.yaml
Expand Down
3 changes: 2 additions & 1 deletion x/programs/cmd/simulator/cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"crypto/rand"
"errors"
"fmt"
"github.com/ava-labs/hypersdk/x/programs/cmd/simulator/vm/actions"
"io"
"math"
"os"
"strconv"
"strings"
"time"

"github.com/ava-labs/hypersdk/x/programs/cmd/simulator/vm/actions"

"github.com/ava-labs/hypersdk/x/programs/program"

"github.com/spf13/cobra"
Expand Down
2 changes: 2 additions & 0 deletions x/programs/cmd/simulator/cmd/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func programExecuteFunc(
) (ids.ID, []int64, uint64, error) {
// simulate create program transaction
programTxID, err := generateRandomID()

if err != nil {
return ids.Empty, nil, 0, err
}
Expand All @@ -157,6 +158,7 @@ func programExecuteFunc(

// execute the action
success, _, resp, _, err := programExecuteAction.Execute(ctx, nil, db, 0, codec.EmptyAddress, programTxID, false)

if !success {
return ids.Empty, nil, 0, fmt.Errorf("program execution failed: %s", string(resp))
}
Expand Down
16 changes: 11 additions & 5 deletions x/programs/cmd/simulator/vm/actions/program_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package actions
import (
"context"
"fmt"

"github.com/near/borsh-go"

"github.com/ava-labs/hypersdk/crypto/ed25519"
Expand Down Expand Up @@ -61,7 +62,7 @@ func (t *ProgramExecute) Execute(
_ chain.Rules,
mu state.Mutable,
_ int64,
_ codec.Address,
actor codec.Address,
_ ids.ID,
_ bool,
) (success bool, computeUnits uint64, output []byte, warpMessage *warp.UnsignedMessage, err error) {
Expand All @@ -82,7 +83,6 @@ func (t *ProgramExecute) Execute(
if err != nil {
return false, 1, utils.ErrBytes(err), nil, nil
}
t.Params[0].Value = programID
programBytes, err := storage.GetProgram(ctx, mu, programID)
if err != nil {
return false, 1, utils.ErrBytes(err), nil, nil
Expand All @@ -107,13 +107,19 @@ func (t *ProgramExecute) Execute(
importsBuilder.Register("state", func() host.Import {
return pstate.New(logging.NoLog{}, mu)
})
callContext := program.Context{
ProgramID: programID,
// Actor: [32]byte(actor[1:]),
// OriginatingActor: [32]byte(actor[1:])
}

importsBuilder.Register("program", func() host.Import {
return importProgram.New(logging.NoLog{}, eng, mu, cfg)
return importProgram.New(logging.NoLog{}, eng, mu, cfg, &callContext)
})
imports := importsBuilder.Build()

t.rt = runtime.New(logging.NoLog{}, eng, imports, cfg)
err = t.rt.Initialize(ctx, programBytes, t.MaxUnits)
err = t.rt.Initialize(ctx, callContext, programBytes, t.MaxUnits)
if err != nil {
return false, 1, utils.ErrBytes(err), nil, nil
}
Expand All @@ -128,7 +134,7 @@ func (t *ProgramExecute) Execute(
return false, 1, utils.ErrBytes(err), nil, nil
}

resp, err := t.rt.Call(ctx, t.Function, params...)
resp, err := t.rt.Call(ctx, t.Function, callContext, params[1:]...)
if err != nil {
return false, 1, utils.ErrBytes(err), nil, nil
}
Expand Down
9 changes: 5 additions & 4 deletions x/programs/cmd/simulator/vm/storage/state_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package storage

import (
"context"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/state"
Expand All @@ -13,22 +14,22 @@ import (
type StateManager struct{}

func (m *StateManager) SponsorStateKeys(_ codec.Address) state.Keys {
//TODO implement me
// TODO implement me
panic("implement me")
}

func (m *StateManager) CanDeduct(_ context.Context, _ codec.Address, _ state.Immutable, _ uint64) error {
//TODO implement me
// TODO implement me
panic("implement me")
}

func (m *StateManager) Deduct(_ context.Context, _ codec.Address, _ state.Mutable, _ uint64) error {
//TODO implement me
// TODO implement me
panic("implement me")
}

func (m *StateManager) Refund(_ context.Context, _ codec.Address, _ state.Mutable, _ uint64) error {
//TODO implement me
// TODO implement me
panic("implement me")
}

Expand Down
Loading

0 comments on commit 2fa9847

Please sign in to comment.