Skip to content

Commit

Permalink
Merge branch 'main' into morpheusvm-tutorial-update
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoVillar committed Nov 7, 2024
2 parents d8cf20e + 29e7ac4 commit d8e9376
Show file tree
Hide file tree
Showing 41 changed files with 4,329 additions and 239 deletions.
20 changes: 0 additions & 20 deletions .github/workflows/todo.yml

This file was deleted.

2 changes: 1 addition & 1 deletion abi/testdata/transfer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"to": "0x0102030405060708090a0b0c0d0e0f101112131400000000000000000000000000",
"to": "0x0102030405060708090a0b0c0d0e0f10111213140000000000000000000000000020db0e6c",
"value": 1000,
"memo": "aGk="
}
2 changes: 1 addition & 1 deletion abi/testdata/transferField.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"transfer": {
"to": "0x0102030405060708090a0b0c0d0e0f101112131400000000000000000000000000",
"to": "0x0102030405060708090a0b0c0d0e0f10111213140000000000000000000000000020db0e6c",
"value": 1000,
"memo": "aGk="
}
Expand Down
4 changes: 2 additions & 2 deletions abi/testdata/transfersArray.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"transfers": [
{
"to": "0x0102030405060708090a0b0c0d0e0f101112131400000000000000000000000000",
"to": "0x0102030405060708090a0b0c0d0e0f10111213140000000000000000000000000020db0e6c",
"value": 1000,
"memo": "aGk="
},
{
"to": "0x0102030405060708090a0b0c0d0e0f101112131400000000000000000000000000",
"to": "0x0102030405060708090a0b0c0d0e0f10111213140000000000000000000000000020db0e6c",
"value": 1000,
"memo": "aGk="
}
Expand Down
4 changes: 4 additions & 0 deletions api/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
)

type VM interface {
GetDataDir() string
GetGenesisBytes() []byte
Genesis() genesis.Genesis
ChainID() ids.ID
NetworkID() uint32
Expand All @@ -34,6 +36,8 @@ type VM interface {
verifySig bool,
txs []*chain.Transaction,
) (errs []error)
// LastAcceptedBlock provides the most recent block that the VM has accepted.
// The value returned is guaranteed to be non-nil.
LastAcceptedBlock() *chain.StatefulBlock
UnitPrices(context.Context) (fees.Dimensions, error)
CurrentValidators(
Expand Down
17 changes: 9 additions & 8 deletions api/indexer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package indexer
import (
"path/filepath"

"github.com/ava-labs/hypersdk/api"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/event"
"github.com/ava-labs/hypersdk/vm"
Expand Down Expand Up @@ -34,14 +35,14 @@ func With() vm.Option {
return vm.NewOption(Namespace, NewDefaultConfig(), OptionFunc)
}

func OptionFunc(v *vm.VM, config Config) error {
func OptionFunc(v api.VM, config Config) (vm.Opt, error) {
if !config.Enabled {
return nil
return vm.NewOpt(), nil
}
indexerPath := filepath.Join(v.DataDir, Namespace)
indexerPath := filepath.Join(v.GetDataDir(), Namespace)
indexer, err := NewIndexer(indexerPath, v, config.BlockWindow)
if err != nil {
return err
return nil, err
}

subscriptionFactory := &subscriptionFactory{
Expand All @@ -54,10 +55,10 @@ func OptionFunc(v *vm.VM, config Config) error {
indexer: indexer,
}

vm.WithBlockSubscriptions(subscriptionFactory)(v)
vm.WithVMAPIs(apiFactory)(v)

return nil
return vm.NewOpt(
vm.WithBlockSubscriptions(subscriptionFactory),
vm.WithVMAPIs(apiFactory),
), nil
}

type subscriptionFactory struct {
Expand Down
3 changes: 1 addition & 2 deletions api/jsonrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ func (cli *JSONRPCClient) GenerateTransactionManual(
}

// Build transaction
actionCodec, authCodec := parser.ActionCodec(), parser.AuthCodec()
unsignedTx := chain.NewTxData(base, actions)
tx, err := unsignedTx.Sign(authFactory, actionCodec, authCodec)
tx, err := unsignedTx.Sign(authFactory)
if err != nil {
return nil, nil, fmt.Errorf("%w: failed to sign transaction", err)
}
Expand Down
12 changes: 7 additions & 5 deletions api/jsonrpc/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package jsonrpc

import "github.com/ava-labs/hypersdk/vm"
import (
"github.com/ava-labs/hypersdk/api"
"github.com/ava-labs/hypersdk/vm"
)

const Namespace = "core"

Expand All @@ -18,11 +21,10 @@ func NewDefaultConfig() Config {
}

func With() vm.Option {
return vm.NewOption(Namespace, NewDefaultConfig(), func(v *vm.VM, config Config) error {
return vm.NewOption(Namespace, NewDefaultConfig(), func(_ api.VM, config Config) (vm.Opt, error) {
if !config.Enabled {
return nil
return vm.NewOpt(), nil
}
vm.WithVMAPIs(JSONRPCServerFactory{})(v)
return nil
return vm.WithVMAPIs(JSONRPCServerFactory{}), nil
})
}
12 changes: 7 additions & 5 deletions api/state/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package state

import "github.com/ava-labs/hypersdk/vm"
import (
"github.com/ava-labs/hypersdk/api"
"github.com/ava-labs/hypersdk/vm"
)

const Namespace = "corestate"

Expand All @@ -18,11 +21,10 @@ func NewDefaultConfig() Config {
}

func With() vm.Option {
return vm.NewOption(Namespace, NewDefaultConfig(), func(v *vm.VM, config Config) error {
return vm.NewOption(Namespace, NewDefaultConfig(), func(_ api.VM, config Config) (vm.Opt, error) {
if !config.Enabled {
return nil
return vm.NewOpt(), nil
}
vm.WithVMAPIs(JSONRPCStateServerFactory{})(v)
return nil
return vm.WithVMAPIs(JSONRPCStateServerFactory{}), nil
})
}
14 changes: 7 additions & 7 deletions api/ws/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func With() vm.Option {
return vm.NewOption(Namespace, NewDefaultConfig(), OptionFunc)
}

func OptionFunc(v *vm.VM, config Config) error {
func OptionFunc(v api.VM, config Config) (vm.Opt, error) {
if !config.Enabled {
return nil
return vm.NewOpt(), nil
}

actionCodec, authCodec := v.ActionCodec(), v.AuthCodec()
Expand All @@ -78,11 +78,11 @@ func OptionFunc(v *vm.VM, config Config) error {
},
}

vm.WithBlockSubscriptions(blockSubscription)(v)
vm.WithTxRemovedSubscriptions(txRemovedSubscription)(v)
vm.WithVMAPIs(webSocketFactory)(v)

return nil
return vm.NewOpt(
vm.WithBlockSubscriptions(blockSubscription),
vm.WithTxRemovedSubscriptions(txRemovedSubscription),
vm.WithVMAPIs(webSocketFactory),
), nil
}

func NewWebSocketServerFactory(server *pubsub.Server) *WebSocketServerFactory {
Expand Down
74 changes: 74 additions & 0 deletions chain/assembler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package chain

import (
"context"
"time"

"github.com/ava-labs/hypersdk/utils"
)

type Assembler struct {
vm VM
}

func (a *Assembler) AssembleBlock(
ctx context.Context,
parent *StatefulBlock,
timestamp int64,
blockHeight uint64,
txs []*Transaction,
) (*StatefulBlock, error) {
ctx, span := a.vm.Tracer().Start(ctx, "chain.AssembleBlock")
defer span.End()

parentView, err := parent.View(ctx, true)
if err != nil {
return nil, err
}
parentStateRoot, err := parentView.GetMerkleRoot(ctx)
if err != nil {
return nil, err
}

blk := &StatelessBlock{
Prnt: parent.ID(),
Tmstmp: timestamp,
Hght: blockHeight,
Txs: txs,
StateRoot: parentStateRoot,
}
for _, tx := range txs {
blk.authCounts[tx.Auth.GetTypeID()]++
}

blkBytes, err := blk.Marshal()
if err != nil {
return nil, err
}
b := &StatefulBlock{
StatelessBlock: blk,
t: time.UnixMilli(blk.Tmstmp),
bytes: blkBytes,
accepted: false,
vm: a.vm,
id: utils.ToID(blkBytes),
}
return b, b.populateTxs(ctx) // TODO: simplify since txs are guaranteed to already be de-duplicated here
}

func (a *Assembler) ExecuteBlock(
ctx context.Context,
b *StatefulBlock,
) (*ExecutedBlock, error) {
ctx, span := a.vm.Tracer().Start(ctx, "chain.ExecuteBlock")
defer span.End()

if err := b.Verify(ctx); err != nil {
return nil, err
}

return NewExecutedBlockFromStateful(b), nil
}
2 changes: 2 additions & 0 deletions chain/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func (b *Base) Marshal(p *codec.Packer) {
p.PackUint64(b.MaxFee)
}

// UnmarshalBase unmarshals a Base from packer.
// Caller can assume packer errors are returned from the function.
func UnmarshalBase(p *codec.Packer) (*Base, error) {
var base Base
base.Timestamp = p.UnpackInt64(true)
Expand Down
Loading

0 comments on commit d8e9376

Please sign in to comment.