Skip to content

Commit

Permalink
Support ABI V3 (#2)
Browse files Browse the repository at this point in the history
* update vm

* sending txs

* sending tx via local snap

* add options for local and remote

* hi action and better faucet

* hello read only action

* fall back to string

* use real hypersdk-client
  • Loading branch information
containerman17 authored Sep 17, 2024
1 parent b5987d5 commit e26c7d9
Show file tree
Hide file tree
Showing 58 changed files with 1,560 additions and 263 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.git
web_wallet/node_modules
vm/tests/e2e/e2e.test
tests/e2e/e2e.test
build
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
SERVE_DOMAIN=mysuperdomain.com
FAUCET_PRIVATE_KEY_HEX=""
PREFUND_ADDRESS=""
SERVE_DOMAIN=""
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
vm/tests/e2e/e2e.test
tests/e2e/e2e.test
build
6 changes: 3 additions & 3 deletions Dockerfile.devnet
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ COPY --from=avalanchego /avalanchego/build/avalanchego /root/.hypersdk/avalanche

ENV GOMODCACHE /go/pkg/mod

WORKDIR /
WORKDIR /app

COPY ./go.mod ./go.sum ./

COPY ./vm ./vm
COPY ./ ./

WORKDIR /vm
WORKDIR /app


ENTRYPOINT ["/bin/bash", "-c", "./scripts/stop.sh; ./scripts/run.sh && echo 'Devnet started' && tail -f /dev/null"]
6 changes: 4 additions & 2 deletions Dockerfile.faucet
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ FROM golang:1.22-bookworm AS faucet-builder
WORKDIR /build
COPY ./go.mod ./go.sum ./

COPY ./vm ./vm
COPY ./ ./

ENV GOMODCACHE /go/pkg/mod
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go build -o build/faucet ./vm/cmd/faucet
go build -o build/faucet ./cmd/faucet

#####
# Final layer with faucet, VM and avalanchego
#####
FROM debian:bookworm-slim

COPY --from=faucet-builder /build/build/faucet /faucet
ENV RPC_ENDPOINT=http://devnet:9650


ENTRYPOINT ["/faucet"]
84 changes: 84 additions & 0 deletions actions/hi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package actions

import (
"context"
"fmt"

"github.com/ava-labs/avalanchego/ids"

"github.com/ava-labs/hypersdk-starter/storage"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/state"
)

const (
HiComputeUnits = 1
MaxNameSize = 256
)

var (
ErrNameTooLarge = fmt.Errorf("name is too large")
_ chain.Action = (*Hi)(nil)
)

type Hi struct {
Name string `serialize:"true" json:"name"`
}

type HiResult struct {
Greeting string `serialize:"true" json:"greeting"`
Balance uint64 `serialize:"true" json:"balance"`
}

func (*Hi) GetTypeID() uint8 {
return 1
}

func (h *Hi) StateKeys(actor codec.Address, _ ids.ID) state.Keys {
return state.Keys{
string(storage.BalanceKey(actor)): state.Read,
}
}

func (*Hi) StateKeysMaxChunks() []uint16 {
return []uint16{storage.BalanceChunks}
}

func (h *Hi) Execute(
ctx context.Context,
_ chain.Rules,
mu state.Mutable,
_ int64,
actor codec.Address,
_ ids.ID,
) ([][]byte, error) {
if len(h.Name) > MaxNameSize {
return nil, ErrNameTooLarge
}

balance, err := storage.GetBalance(ctx, mu, actor)
if err != nil {
return nil, err
}

greeting := fmt.Sprintf("Hi, %s", h.Name)

bytes, err := codec.Marshal(HiResult{
Greeting: greeting,
Balance: balance,
})

return [][]byte{bytes}, err
}

func (*Hi) ComputeUnits(chain.Rules) uint64 {
return HiComputeUnits
}

func (*Hi) ValidRange(chain.Rules) (int64, int64) {
return -1, -1
}
52 changes: 24 additions & 28 deletions vm/actions/transfer.go → actions/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import (

"github.com/ava-labs/avalanchego/ids"

"github.com/ava-labs/hypersdk-starter/vm/storage"
"github.com/ava-labs/hypersdk-starter/storage"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/state"

mconsts "github.com/ava-labs/hypersdk-starter/vm/consts"
consts "github.com/ava-labs/hypersdk/consts"
)

const (
Expand All @@ -37,11 +34,16 @@ type Transfer struct {
Value uint64 `serialize:"true" json:"value"`

// Optional message to accompany transaction.
Memo codec.StringAsBytes `serialize:"true" json:"memo"`
Memo codec.Bytes `serialize:"true" json:"memo"`
}

type TransferResult struct {
SenderBalance uint64 `serialize:"true" json:"sender_balance"`
ReceiverBalance uint64 `serialize:"true" json:"receiver_balance"`
}

func (*Transfer) GetTypeID() uint8 {
return mconsts.TransferID
return 0
}

func (t *Transfer) StateKeys(actor codec.Address, _ ids.ID) state.Keys {
Expand Down Expand Up @@ -75,7 +77,22 @@ func (t *Transfer) Execute(
if err := storage.AddBalance(ctx, mu, t.To, t.Value, true); err != nil {
return nil, err
}
return nil, nil

senderBalance, err := storage.GetBalance(ctx, mu, actor)
if err != nil {
return nil, err
}
receiverBalance, err := storage.GetBalance(ctx, mu, t.To)
if err != nil {
return nil, err
}

bytes, err := codec.Marshal(TransferResult{
SenderBalance: senderBalance,
ReceiverBalance: receiverBalance,
})

return [][]byte{bytes}, err
}

func (*Transfer) ComputeUnits(chain.Rules) uint64 {
Expand All @@ -86,24 +103,3 @@ func (*Transfer) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}

// Implementing chain.Marshaler is optional but can be used to optimize performance when hitting TPS limits
var _ chain.Marshaler = (*Transfer)(nil)

func (t *Transfer) Size() int {
return codec.AddressLen + consts.Uint64Len + codec.BytesLen(t.Memo)
}

func (t *Transfer) Marshal(p *codec.Packer) {
p.PackAddress(t.To)
p.PackLong(t.Value)
p.PackBytes(t.Memo)
}

func UnmarshalTransfer(p *codec.Packer) (chain.Action, error) {
var transfer Transfer
p.UnpackAddress(&transfer.To)
transfer.Value = p.UnpackUint64(true)
p.UnpackBytes(MaxMemoSize, false, (*[]byte)(&transfer.Memo))
return &transfer, p.Err()
}
14 changes: 13 additions & 1 deletion vm/actions/transfer_test.go → actions/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/ava-labs/hypersdk-starter/vm/storage"
"github.com/ava-labs/hypersdk-starter/storage"
"github.com/ava-labs/hypersdk/chain/chaintest"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/codec/codectest"
Expand Down Expand Up @@ -79,6 +79,12 @@ func TestTransferAction(t *testing.T) {
require.NoError(t, err)
require.Equal(t, balance, uint64(1))
},
ExpectedOutputs: [][]byte{
codec.MustMarshal(TransferResult{
SenderBalance: 1,
ReceiverBalance: 1,
}),
},
},
{
Name: "OverflowBalance",
Expand Down Expand Up @@ -117,6 +123,12 @@ func TestTransferAction(t *testing.T) {
require.NoError(t, err)
require.Equal(t, senderBalance, uint64(0))
},
ExpectedOutputs: [][]byte{
codec.MustMarshal(TransferResult{
SenderBalance: 0,
ReceiverBalance: 1,
}),
},
},
}

Expand Down
Loading

0 comments on commit e26c7d9

Please sign in to comment.