Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tsachiherman committed Oct 2, 2024
1 parent 79f363e commit e6d2c33
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 101 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ Services are created by adding an [Option](./vm/option.go) to the VM. They can b
```golang
// NewWithOptions returns a VM with the specified options
func New(options ...vm.Option) (*vm.VM, error) {
options = append(options, With(), indexer.With()) // Add MorpheusVM API and Indexer
return vm.New(
return defaultvm.New(
consts.Version,
genesis.DefaultGenesisFactory{},
&storage.StateManager{},
ActionParser,
AuthParser,
newRegistryFactory(),
auth.Engines(),
options...,
)
Expand Down
2 changes: 2 additions & 0 deletions chain/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,5 @@ type AuthFactory interface {
MaxUnits() (bandwidth uint64, compute uint64)
Address() codec.Address
}

type RegistryFactory func() (actionRegistry ActionRegistry, authRegistry AuthRegistry, outputRegistry OutputRegistry)
37 changes: 17 additions & 20 deletions docs/tutorials/morpheusvm/morpheusvm.md
Original file line number Diff line number Diff line change
Expand Up @@ -771,36 +771,35 @@ import (
"github.com/ava-labs/hypersdk/examples/tutorial/actions"
)

var (
ActionParser *codec.TypeParser[chain.Action]
AuthParser *codec.TypeParser[chain.Auth]
OutputParser *codec.TypeParser[codec.Typed]
)

// Setup types
func init() {
ActionParser = codec.NewTypeParser[chain.Action]()
AuthParser = codec.NewTypeParser[chain.Auth]()
OutputParser = codec.NewTypeParser[codec.Typed]()
func newRegistryFactory() chain.RegistryFactory {
actionParser := codec.NewTypeParser[chain.Action]()
authParser := codec.NewTypeParser[chain.Auth]()
outputParser := codec.NewTypeParser[codec.Typed]()

errs := &wrappers.Errs{}
errs.Add(
ActionParser.Register(&actions.Transfer{}, actions.UnmarshalTransfer),
// When registering new actions, ALWAYS make sure to append at the end.
// Pass nil as second argument if manual marshalling isn't needed (if in doubt, you probably don't)
actionParser.Register(&actions.Transfer{}, actions.UnmarshalTransfer),

AuthParser.Register(&auth.ED25519{}, auth.UnmarshalED25519),
AuthParser.Register(&auth.SECP256R1{}, auth.UnmarshalSECP256R1),
AuthParser.Register(&auth.BLS{}, auth.UnmarshalBLS),
// When registering new auth, ALWAYS make sure to append at the end.
authParser.Register(&auth.ED25519{}, auth.UnmarshalED25519),
authParser.Register(&auth.SECP256R1{}, auth.UnmarshalSECP256R1),
authParser.Register(&auth.BLS{}, auth.UnmarshalBLS),

OutputParser.Register(&actions.TransferResult{}, nil),
outputParser.Register(&actions.TransferResult{}, nil),
)
if errs.Errored() {
panic(errs.Err)
}
return func() (actionRegistry chain.ActionRegistry, authRegistry chain.AuthRegistry, outputRegistry chain.OutputRegistry) {
return actionParser, authParser, outputParser
}
}

```

By “registry”, we mean the `ActionParser` and `AuthParser` which tell our VM
By “registry”, we mean the `actionParser` and `authParser` which tell our VM
which actions and cryptographic functions that it’ll support.

Finally, we create a `New()` function that allows for the VM we’ve worked on to
Expand All @@ -813,9 +812,7 @@ func New(options ...vm.Option) (*vm.VM, error) {
consts.Version,
genesis.DefaultGenesisFactory{},
&storage.StateManager{},
ActionParser,
AuthParser,
OutputParser,
newRegistryFactory(),
auth.Engines(),
options...,
)
Expand Down
18 changes: 11 additions & 7 deletions docs/tutorials/morpheusvm/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,30 +254,34 @@ var _ chain.Parser = (*Parser)(nil)

type Parser struct {
genesis *genesis.DefaultGenesis
registryFactory chain.RegistryFactory
}

func (p *Parser) Rules(_ int64) chain.Rules {
return p.genesis.Rules
}

func (*Parser) ActionRegistry() chain.ActionRegistry {
return ActionParser
func (p *Parser) ActionRegistry() chain.ActionRegistry {
actionRegistry, _, _ := p.registryFactory()
return actionRegistry
}

func (*Parser) OutputRegistry() chain.OutputRegistry {
return OutputParser
func (*Parser) AuthRegistry() chain.AuthRegistry {
_, authRegistry, _ := p.registryFactory()
return authRegistry
}

func (*Parser) AuthRegistry() chain.AuthRegistry {
return AuthParser
func (*Parser) OutputRegistry() chain.OutputRegistry {
_, _, outputRegistry := p.registryFactory()
return outputRegistry
}

func (*Parser) StateManager() chain.StateManager {
return &storage.StateManager{}
}

func NewParser(genesis *genesis.DefaultGenesis) chain.Parser {
return &Parser{genesis: genesis}
return &Parser{genesis: genesis, registryFactory: newRegistryFactory()}
}

// Used as a lambda function for creating ExternalSubscriberServer parser
Expand Down
5 changes: 4 additions & 1 deletion examples/morpheusvm/tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
genesisBytes, err := json.Marshal(gen)
require.NoError(err)

expectedABI, err := abi.NewABI(vm.ActionParser.GetRegisteredTypes(), vm.OutputParser.GetRegisteredTypes())
parser, err := vm.CreateParser(genesisBytes)
require.NoError(err)

expectedABI, err := abi.NewABI((*parser.ActionRegistry()).GetRegisteredTypes(), (*parser.OutputRegistry()).GetRegisteredTypes())
require.NoError(err)

// Import HyperSDK e2e test coverage and inject MorpheusVM name
Expand Down
4 changes: 3 additions & 1 deletion examples/morpheusvm/tests/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ func confirmTx(ctx context.Context, require *require.Assertions, uri string, txI
transferOutputBytes := []byte(txRes.Outputs[0])
require.Equal(consts.TransferID, transferOutputBytes[0])
reader := codec.NewReader(transferOutputBytes, len(transferOutputBytes))
transferOutputTyped, err := vm.OutputParser.Unmarshal(reader)
parser, err := lcli.Parser(context.Background())
require.NoError(err)
transferOutputTyped, err := (*parser.OutputRegistry()).Unmarshal(reader)
require.NoError(err)
transferOutput, ok := transferOutputTyped.(*actions.TransferResult)
require.True(ok)
Expand Down
20 changes: 12 additions & 8 deletions examples/morpheusvm/vm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,35 @@ func (cli *JSONRPCClient) Parser(ctx context.Context) (chain.Parser, error) {
var _ chain.Parser = (*Parser)(nil)

type Parser struct {
genesis *genesis.DefaultGenesis
genesis *genesis.DefaultGenesis
registryFactory chain.RegistryFactory
}

func (p *Parser) Rules(_ int64) chain.Rules {
return p.genesis.Rules
}

func (*Parser) ActionRegistry() chain.ActionRegistry {
return ActionParser
func (p *Parser) ActionRegistry() chain.ActionRegistry {
actionRegistry, _, _ := p.registryFactory()
return actionRegistry
}

func (*Parser) OutputRegistry() chain.OutputRegistry {
return OutputParser
func (p *Parser) AuthRegistry() chain.AuthRegistry {
_, authRegistry, _ := p.registryFactory()
return authRegistry
}

func (*Parser) AuthRegistry() chain.AuthRegistry {
return AuthParser
func (p *Parser) OutputRegistry() chain.OutputRegistry {
_, _, outputRegistry := p.registryFactory()
return outputRegistry
}

func (*Parser) StateManager() chain.StateManager {
return &storage.StateManager{}
}

func NewParser(genesis *genesis.DefaultGenesis) chain.Parser {
return &Parser{genesis: genesis}
return &Parser{genesis: genesis, registryFactory: newRegistryFactory()}
}

// Used as a lambda function for creating ExternalSubscriberServer parser
Expand Down
32 changes: 13 additions & 19 deletions examples/morpheusvm/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,30 @@ import (
"github.com/ava-labs/hypersdk/vm/defaultvm"
)

var (
ActionParser *codec.TypeParser[chain.Action]
AuthParser *codec.TypeParser[chain.Auth]
OutputParser *codec.TypeParser[codec.Typed]
)

// Setup types
func init() {
ActionParser = codec.NewTypeParser[chain.Action]()
AuthParser = codec.NewTypeParser[chain.Auth]()
OutputParser = codec.NewTypeParser[codec.Typed]()
func newRegistryFactory() chain.RegistryFactory {
actionParser := codec.NewTypeParser[chain.Action]()
authParser := codec.NewTypeParser[chain.Auth]()
outputParser := codec.NewTypeParser[codec.Typed]()

errs := &wrappers.Errs{}
errs.Add(
// When registering new actions, ALWAYS make sure to append at the end.
// Pass nil as second argument if manual marshalling isn't needed (if in doubt, you probably don't)
ActionParser.Register(&actions.Transfer{}, actions.UnmarshalTransfer),
actionParser.Register(&actions.Transfer{}, actions.UnmarshalTransfer),

// When registering new auth, ALWAYS make sure to append at the end.
AuthParser.Register(&auth.ED25519{}, auth.UnmarshalED25519),
AuthParser.Register(&auth.SECP256R1{}, auth.UnmarshalSECP256R1),
AuthParser.Register(&auth.BLS{}, auth.UnmarshalBLS),
authParser.Register(&auth.ED25519{}, auth.UnmarshalED25519),
authParser.Register(&auth.SECP256R1{}, auth.UnmarshalSECP256R1),
authParser.Register(&auth.BLS{}, auth.UnmarshalBLS),

OutputParser.Register(&actions.TransferResult{}, nil),
outputParser.Register(&actions.TransferResult{}, nil),
)
if errs.Errored() {
panic(errs.Err)
}
return func() (actionRegistry chain.ActionRegistry, authRegistry chain.AuthRegistry, outputRegistry chain.OutputRegistry) {
return actionParser, authParser, outputParser
}
}

// NewWithOptions returns a VM with the specified options
Expand All @@ -54,9 +50,7 @@ func New(options ...vm.Option) (*vm.VM, error) {
consts.Version,
genesis.DefaultGenesisFactory{},
&storage.StateManager{},
ActionParser,
AuthParser,
OutputParser,
newRegistryFactory(),
auth.Engines(),
options...,
)
Expand Down
5 changes: 4 additions & 1 deletion examples/vmwithcontracts/tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
genesisBytes, err := json.Marshal(gen)
require.NoError(err)

expectedABI, err := abi.NewABI(vm.ActionParser.GetRegisteredTypes(), vm.OutputParser.GetRegisteredTypes())
parser, err := vm.CreateParser(genesisBytes)
require.NoError(err)

expectedABI, err := abi.NewABI((*parser.ActionRegistry()).GetRegisteredTypes(), (*parser.OutputRegistry()).GetRegisteredTypes())
require.NoError(err)

// Import HyperSDK e2e test coverage and inject VMWithContracts name
Expand Down
20 changes: 12 additions & 8 deletions examples/vmwithcontracts/vm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,35 @@ func (cli *JSONRPCClient) Parser(ctx context.Context) (chain.Parser, error) {
var _ chain.Parser = (*Parser)(nil)

type Parser struct {
genesis *genesis.DefaultGenesis
genesis *genesis.DefaultGenesis
registryFactory chain.RegistryFactory
}

func (p *Parser) Rules(_ int64) chain.Rules {
return p.genesis.Rules
}

func (*Parser) ActionRegistry() chain.ActionRegistry {
return ActionParser
func (p *Parser) ActionRegistry() chain.ActionRegistry {
actionRegistry, _, _ := p.registryFactory()
return actionRegistry
}

func (*Parser) OutputRegistry() chain.OutputRegistry {
return OutputParser
func (p *Parser) AuthRegistry() chain.AuthRegistry {
_, authRegistry, _ := p.registryFactory()
return authRegistry
}

func (*Parser) AuthRegistry() chain.AuthRegistry {
return AuthParser
func (p *Parser) OutputRegistry() chain.OutputRegistry {
_, _, outputRegistry := p.registryFactory()
return outputRegistry
}

func (*Parser) StateManager() chain.StateManager {
return &storage.StateManager{}
}

func NewParser(genesis *genesis.DefaultGenesis) chain.Parser {
return &Parser{genesis: genesis}
return &Parser{genesis: genesis, registryFactory: newRegistryFactory()}
}

// Used as a lambda function for creating ExternalSubscriberServer parser
Expand Down
41 changes: 18 additions & 23 deletions examples/vmwithcontracts/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,36 @@ import (
"github.com/ava-labs/hypersdk/x/contracts/runtime"
)

var (
ActionParser *codec.TypeParser[chain.Action]
AuthParser *codec.TypeParser[chain.Auth]
OutputParser *codec.TypeParser[codec.Typed]
wasmRuntime *runtime.WasmRuntime
)
var wasmRuntime *runtime.WasmRuntime

// Setup types
func init() {
ActionParser = codec.NewTypeParser[chain.Action]()
AuthParser = codec.NewTypeParser[chain.Auth]()
OutputParser = codec.NewTypeParser[codec.Typed]()
func newRegistryFactory() chain.RegistryFactory {
actionParser := codec.NewTypeParser[chain.Action]()
authParser := codec.NewTypeParser[chain.Auth]()
outputParser := codec.NewTypeParser[codec.Typed]()

errs := &wrappers.Errs{}
errs.Add(
// When registering new actions, ALWAYS make sure to append at the end.
// Pass nil as second argument if manual marshalling isn't needed (if in doubt, you probably don't)
ActionParser.Register(&actions.Transfer{}, actions.UnmarshalTransfer),
ActionParser.Register(&actions.Call{}, actions.UnmarshalCallContract(wasmRuntime)),
ActionParser.Register(&actions.Publish{}, actions.UnmarshalPublishContract),
ActionParser.Register(&actions.Deploy{}, actions.UnmarshalDeployContract),
actionParser.Register(&actions.Transfer{}, actions.UnmarshalTransfer),
actionParser.Register(&actions.Call{}, actions.UnmarshalCallContract(wasmRuntime)),
actionParser.Register(&actions.Publish{}, actions.UnmarshalPublishContract),
actionParser.Register(&actions.Deploy{}, actions.UnmarshalDeployContract),

// When registering new auth, ALWAYS make sure to append at the end.
AuthParser.Register(&auth.ED25519{}, auth.UnmarshalED25519),
AuthParser.Register(&auth.SECP256R1{}, auth.UnmarshalSECP256R1),
AuthParser.Register(&auth.BLS{}, auth.UnmarshalBLS),
authParser.Register(&auth.ED25519{}, auth.UnmarshalED25519),
authParser.Register(&auth.SECP256R1{}, auth.UnmarshalSECP256R1),
authParser.Register(&auth.BLS{}, auth.UnmarshalBLS),

OutputParser.Register(&actions.Result{}, nil),
OutputParser.Register(&actions.AddressOutput{}, nil),
outputParser.Register(&actions.Result{}, nil),
outputParser.Register(&actions.AddressOutput{}, nil),
)
if errs.Errored() {
panic(errs.Err)
}
return func() (actionRegistry chain.ActionRegistry, authRegistry chain.AuthRegistry, outputRegistry chain.OutputRegistry) {
return actionParser, authParser, outputParser
}
}

// New returns a VM with the indexer, websocket, rpc, and external subscriber apis enabled.
Expand All @@ -78,9 +75,7 @@ func NewWithOptions(options ...vm.Option) (*vm.VM, error) {
consts.Version,
genesis.DefaultGenesisFactory{},
&storage.StateManager{},
ActionParser,
AuthParser,
OutputParser,
newRegistryFactory(),
auth.Engines(),
opts...,
)
Expand Down
8 changes: 2 additions & 6 deletions vm/defaultvm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func New(
v *version.Semantic,
genesisFactory genesis.GenesisAndRuleFactory,
stateManager chain.StateManager,
actionRegistry chain.ActionRegistry,
authRegistry chain.AuthRegistry,
outputRegistry chain.OutputRegistry,
registryFactory chain.RegistryFactory,
authEngine map[uint8]vm.AuthEngine,
options ...vm.Option,
) (*vm.VM, error) {
Expand All @@ -46,9 +44,7 @@ func New(
v,
genesisFactory,
stateManager,
actionRegistry,
authRegistry,
outputRegistry,
registryFactory,
authEngine,
options...,
)
Expand Down
Loading

0 comments on commit e6d2c33

Please sign in to comment.