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

add RegistryFactory and deprecate global action, auth and output registries in VMs #1624

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ 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) {
registryFactory, err := newRegistryFactory()
if err != nil {
return nil, err
}
options = append(options, With(), indexer.With()) // Add MorpheusVM API and Indexer
return vm.New(
consts.Version,
genesis.DefaultGenesisFactory{},
&storage.StateManager{},
ActionParser,
AuthParser,
registryFactory,
auth.Engines(),
options...,
)
Expand Down
3 changes: 3 additions & 0 deletions chain/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,6 @@
MaxUnits() (bandwidth uint64, compute uint64)
Address() codec.Address
}

// RegistryFactory is the factory function, provided to the VM initializer that provides the registries for teh actions, auth and output.

Check failure on line 291 in chain/dependencies.go

View workflow job for this annotation

GitHub Actions / hypersdk-lint

`teh` is a misspelling of `the` (misspell)
type RegistryFactory func() (actionRegistry ActionRegistry, authRegistry AuthRegistry, outputRegistry OutputRegistry)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to have a struct here?

type Registry struct {
    action ActionRegistry
    auth   AuthRegistry
    output OutputRegistry
}
Suggested change
type RegistryFactory func() (actionRegistry ActionRegistry, authRegistry AuthRegistry, outputRegistry OutputRegistry)
type RegistryFactory func() Registry

I'm not sure if we plan on extending this, but I think backwards-compatible changes would be easier with a struct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

43 changes: 22 additions & 21 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, error) {
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 nil, errs.Err
}
return func() (actionRegistry chain.ActionRegistry, authRegistry chain.AuthRegistry, outputRegistry chain.OutputRegistry) {
return actionParser, authParser, outputParser
}, nil
}

```

By “registry”, we mean the `ActionParser` and `AuthParser` which tell our VM
By “registry”, we mean the `actionParser`, `authParser` and `outputParser` 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 @@ -809,13 +808,15 @@ be instantiated.
```golang
// NewWithOptions returns a VM with the specified options
func New(options ...vm.Option) (*vm.VM, error) {
registryFactory, err := newRegistryFactory()
if err != nil {
return nil, err
}
return defaultvm.New(
consts.Version,
genesis.DefaultGenesisFactory{},
&storage.StateManager{},
ActionParser,
AuthParser,
OutputParser,
registryFactory,
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
38 changes: 18 additions & 20 deletions examples/morpheusvm/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,44 @@ 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, error) {
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 nil, errs.Err
}
return func() (actionRegistry chain.ActionRegistry, authRegistry chain.AuthRegistry, outputRegistry chain.OutputRegistry) {
return actionParser, authParser, outputParser
}, nil
}

// NewWithOptions returns a VM with the specified options
func New(options ...vm.Option) (*vm.VM, error) {
options = append(options, With()) // Add MorpheusVM API
registryFactory, err := newRegistryFactory()
if err != nil {
return nil, err
}
return defaultvm.New(
consts.Version,
genesis.DefaultGenesisFactory{},
&storage.StateManager{},
ActionParser,
AuthParser,
OutputParser,
registryFactory,
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
Loading
Loading