Skip to content

Commit

Permalink
Remove type alias for Action + Auth + Output Registry (#1547)
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Kim <[email protected]>
  • Loading branch information
joshua-kim authored Oct 9, 2024
1 parent b5407c9 commit b934dbd
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 113 deletions.
7 changes: 4 additions & 3 deletions api/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ava-labs/avalanchego/utils/logging"

"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/fees"
"github.com/ava-labs/hypersdk/genesis"
"github.com/ava-labs/hypersdk/state"
Expand All @@ -24,9 +25,9 @@ type VM interface {
SubnetID() ids.ID
Tracer() trace.Tracer
Logger() logging.Logger
ActionRegistry() chain.ActionRegistry
OutputRegistry() chain.OutputRegistry
AuthRegistry() chain.AuthRegistry
ActionCodec() *codec.TypeParser[chain.Action]
OutputCodec() *codec.TypeParser[codec.Typed]
AuthCodec() *codec.TypeParser[chain.Auth]
Rules(t int64) chain.Rules
Submit(
ctx context.Context,
Expand Down
4 changes: 2 additions & 2 deletions api/jsonrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ func (cli *JSONRPCClient) GenerateTransactionManual(
}

// Build transaction
actionRegistry, authRegistry := parser.ActionRegistry(), parser.AuthRegistry()
actionCodec, authCodec := parser.ActionCodec(), parser.AuthCodec()
tx := chain.NewTx(base, actions)
tx, err := tx.Sign(authFactory, actionRegistry, authRegistry)
tx, err := tx.Sign(authFactory, actionCodec, authCodec)
if err != nil {
return nil, nil, fmt.Errorf("%w: failed to sign transaction", err)
}
Expand Down
13 changes: 6 additions & 7 deletions api/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func (j *JSONRPCServer) SubmitTx(
ctx, span := j.vm.Tracer().Start(req.Context(), "JSONRPCServer.SubmitTx")
defer span.End()

actionRegistry, authRegistry := j.vm.ActionRegistry(), j.vm.AuthRegistry()
actionCodec, authCodec := j.vm.ActionCodec(), j.vm.AuthCodec()
rtx := codec.NewReader(args.Tx, consts.NetworkSizeLimit) // will likely be much smaller than this
tx, err := chain.UnmarshalTx(rtx, actionRegistry, authRegistry)
tx, err := chain.UnmarshalTx(rtx, actionCodec, authCodec)
if err != nil {
return fmt.Errorf("%w: unable to unmarshal on public service", err)
}
Expand Down Expand Up @@ -146,9 +146,8 @@ type GetABIReply struct {
}

func (j *JSONRPCServer) GetABI(_ *http.Request, _ *GetABIArgs, reply *GetABIReply) error {
actionRegistry, outputRegistry := j.vm.ActionRegistry(), j.vm.OutputRegistry()
// Must dereference aliased type to call GetRegisteredTypes
vmABI, err := abi.NewABI((*actionRegistry).GetRegisteredTypes(), (*outputRegistry).GetRegisteredTypes())
actionCodec, outputCodec := j.vm.ActionCodec(), j.vm.OutputCodec()
vmABI, err := abi.NewABI(actionCodec.GetRegisteredTypes(), outputCodec.GetRegisteredTypes())
if err != nil {
return err
}
Expand All @@ -174,10 +173,10 @@ func (j *JSONRPCServer) Execute(
ctx, span := j.vm.Tracer().Start(req.Context(), "JSONRPCServer.ExecuteAction")
defer span.End()

actionRegistry := j.vm.ActionRegistry()
actionCodec := j.vm.ActionCodec()
actions := make([]chain.Action, 0)
for _, action := range args.Actions {
action, err := (*actionRegistry).Unmarshal(codec.NewReader(action, len(action)))
action, err := actionCodec.Unmarshal(codec.NewReader(action, len(action)))
if err != nil {
return fmt.Errorf("failed to unmashal action: %w", err)
}
Expand Down
26 changes: 13 additions & 13 deletions api/ws/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func OptionFunc(v *vm.VM, config Config) error {
return nil
}

actionRegistry, authRegistry := v.ActionRegistry(), v.AuthRegistry()
actionCodec, authCodec := v.ActionCodec(), v.AuthCodec()
server, handler := NewWebSocketServer(
v,
v.Logger(),
v.Tracer(),
actionRegistry,
authRegistry,
actionCodec,
authCodec,
config.MaxPendingMessages,
)

Expand Down Expand Up @@ -103,11 +103,11 @@ func (w WebSocketServerFactory) New(api.VM) (api.Handler, error) {
}

type WebSocketServer struct {
vm api.VM
logger logging.Logger
tracer trace.Tracer
actionRegistry chain.ActionRegistry
authRegistry chain.AuthRegistry
vm api.VM
logger logging.Logger
tracer trace.Tracer
actionCodec *codec.TypeParser[chain.Action]
authCodec *codec.TypeParser[chain.Auth]

s *pubsub.Server

Expand All @@ -122,16 +122,16 @@ func NewWebSocketServer(
vm api.VM,
log logging.Logger,
tracer trace.Tracer,
actionRegistry chain.ActionRegistry,
authRegistry chain.AuthRegistry,
actionCodec *codec.TypeParser[chain.Action],
authCodec *codec.TypeParser[chain.Auth],
maxPendingMessages int,
) (*WebSocketServer, *pubsub.Server) {
w := &WebSocketServer{
vm: vm,
logger: log,
tracer: tracer,
actionRegistry: actionRegistry,
authRegistry: authRegistry,
actionCodec: actionCodec,
authCodec: authCodec,
blockListeners: pubsub.NewConnections(),
txListeners: map[ids.ID]*pubsub.Connections{},
expiringTxs: emap.NewEMap[*chain.Transaction](),
Expand Down Expand Up @@ -253,7 +253,7 @@ func (w *WebSocketServer) MessageCallback() pubsub.Callback {
msgBytes = msgBytes[1:]
// Unmarshal TX
p := codec.NewReader(msgBytes, consts.NetworkSizeLimit) // will likely be much smaller
tx, err := chain.UnmarshalTx(p, w.actionRegistry, w.authRegistry)
tx, err := chain.UnmarshalTx(p, w.actionCodec, w.authCodec)
if err != nil {
w.logger.Error("failed to unmarshal tx",
zap.Int("len", len(msgBytes)),
Expand Down
4 changes: 2 additions & 2 deletions chain/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ func UnmarshalBlock(raw []byte, parser Parser) (*StatelessBlock, error) {

// Parse transactions
txCount := p.UnpackInt(false) // can produce empty blocks
actionRegistry, authRegistry := parser.ActionRegistry(), parser.AuthRegistry()
actionCodec, authCodec := parser.ActionCodec(), parser.AuthCodec()
b.Txs = []*Transaction{} // don't preallocate all to avoid DoS
b.authCounts = map[uint8]int{}
for i := uint32(0); i < txCount; i++ {
tx, err := UnmarshalTx(p, actionRegistry, authRegistry)
tx, err := UnmarshalTx(p, actionCodec, authCodec)
if err != nil {
return nil, err
}
Expand Down
42 changes: 21 additions & 21 deletions chain/chaintest/test_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,47 @@ import (
)

type Parser struct {
rules genesis.RuleFactory
actionRegistry chain.ActionRegistry
authRegistry chain.AuthRegistry
outputRegistry chain.OutputRegistry
rules genesis.RuleFactory
actionCodec *codec.TypeParser[chain.Action]
authCodec *codec.TypeParser[chain.Auth]
outputCodec *codec.TypeParser[codec.Typed]
}

func NewParser(
ruleFactory genesis.RuleFactory,
actionRegistry chain.ActionRegistry,
authRegistry chain.AuthRegistry,
outputRegistry chain.OutputRegistry,
actionCodec *codec.TypeParser[chain.Action],
authCodec *codec.TypeParser[chain.Auth],
outputCodec *codec.TypeParser[codec.Typed],
) *Parser {
return &Parser{
rules: ruleFactory,
actionRegistry: actionRegistry,
authRegistry: authRegistry,
outputRegistry: outputRegistry,
rules: ruleFactory,
actionCodec: actionCodec,
authCodec: authCodec,
outputCodec: outputCodec,
}
}

func NewEmptyParser() *Parser {
return &Parser{
rules: &genesis.ImmutableRuleFactory{Rules: genesis.NewDefaultRules()},
actionRegistry: codec.NewTypeParser[chain.Action](),
authRegistry: codec.NewTypeParser[chain.Auth](),
outputRegistry: codec.NewTypeParser[codec.Typed](),
rules: &genesis.ImmutableRuleFactory{Rules: genesis.NewDefaultRules()},
actionCodec: codec.NewTypeParser[chain.Action](),
authCodec: codec.NewTypeParser[chain.Auth](),
outputCodec: codec.NewTypeParser[codec.Typed](),
}
}

func (p *Parser) Rules(t int64) chain.Rules {
return p.rules.GetRules(t)
}

func (p *Parser) ActionRegistry() chain.ActionRegistry {
return p.actionRegistry
func (p *Parser) ActionCodec() *codec.TypeParser[chain.Action] {
return p.actionCodec
}

func (p *Parser) AuthRegistry() chain.AuthRegistry {
return p.authRegistry
func (p *Parser) AuthCodec() *codec.TypeParser[chain.Auth] {
return p.authCodec
}

func (p *Parser) OutputRegistry() chain.OutputRegistry {
return p.outputRegistry
func (p *Parser) OutputCodec() *codec.TypeParser[codec.Typed] {
return p.outputCodec
}
12 changes: 3 additions & 9 deletions chain/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,11 @@ import (
"github.com/ava-labs/hypersdk/state"
)

type (
ActionRegistry *codec.TypeParser[Action]
OutputRegistry *codec.TypeParser[codec.Typed]
AuthRegistry *codec.TypeParser[Auth]
)

type Parser interface {
Rules(int64) Rules
ActionRegistry() ActionRegistry
OutputRegistry() OutputRegistry
AuthRegistry() AuthRegistry
ActionCodec() *codec.TypeParser[Action]
OutputCodec() *codec.TypeParser[codec.Typed]
AuthCodec() *codec.TypeParser[Auth]
}

type Metrics interface {
Expand Down
24 changes: 12 additions & 12 deletions chain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (t *Transaction) UnsignedBytes() ([]byte, error) {
// the original and a signature provided by the authFactory
func (t *Transaction) Sign(
factory AuthFactory,
actionRegistry ActionRegistry,
authRegistry AuthRegistry,
actionCodec *codec.TypeParser[Action],
authCodec *codec.TypeParser[Auth],
) (*Transaction, error) {
msg, err := t.UnsignedBytes()
if err != nil {
Expand Down Expand Up @@ -102,7 +102,7 @@ func (t *Transaction) Sign(
return nil, err
}
p = codec.NewReader(p.Bytes(), consts.MaxInt)
return UnmarshalTx(p, actionRegistry, authRegistry)
return UnmarshalTx(p, actionCodec, authCodec)
}

// Verify that the transaction was signed correctly.
Expand Down Expand Up @@ -451,15 +451,15 @@ func MarshalTxs(txs []*Transaction) ([]byte, error) {
func UnmarshalTxs(
raw []byte,
initialCapacity int,
actionRegistry ActionRegistry,
authRegistry AuthRegistry,
actionCodec *codec.TypeParser[Action],
authCodec *codec.TypeParser[Auth],
) (map[uint8]int, []*Transaction, error) {
p := codec.NewReader(raw, consts.NetworkSizeLimit)
txCount := p.UnpackInt(true)
authCounts := map[uint8]int{}
txs := make([]*Transaction, 0, initialCapacity) // DoS to set size to txCount
for i := uint32(0); i < txCount; i++ {
tx, err := UnmarshalTx(p, actionRegistry, authRegistry)
tx, err := UnmarshalTx(p, actionCodec, authCodec)
if err != nil {
return nil, nil, err
}
Expand All @@ -475,20 +475,20 @@ func UnmarshalTxs(

func UnmarshalTx(
p *codec.Packer,
actionRegistry *codec.TypeParser[Action],
authRegistry *codec.TypeParser[Auth],
actionCodec *codec.TypeParser[Action],
authCodec *codec.TypeParser[Auth],
) (*Transaction, error) {
start := p.Offset()
base, err := UnmarshalBase(p)
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal base", err)
}
actions, err := UnmarshalActions(p, actionRegistry)
actions, err := UnmarshalActions(p, actionCodec)
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal actions", err)
}
digest := p.Offset()
auth, err := authRegistry.Unmarshal(p)
auth, err := authCodec.Unmarshal(p)
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal auth", err)
}
Expand Down Expand Up @@ -518,15 +518,15 @@ func UnmarshalTx(

func UnmarshalActions(
p *codec.Packer,
actionRegistry *codec.TypeParser[Action],
actionCodec *codec.TypeParser[Action],
) (Actions, error) {
actionCount := p.UnpackByte()
if actionCount == 0 {
return nil, fmt.Errorf("%w: no actions", ErrInvalidObject)
}
actions := Actions{}
for i := uint8(0); i < actionCount; i++ {
action, err := actionRegistry.Unmarshal(p)
action, err := actionCodec.Unmarshal(p)
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal action", err)
}
Expand Down
12 changes: 6 additions & 6 deletions chain/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ func TestMarshalUnmarshal(t *testing.T) {
require.NoError(err)
factory := auth.NewED25519Factory(priv)

actionRegistry := codec.NewTypeParser[chain.Action]()
authRegistry := codec.NewTypeParser[chain.Auth]()
actionCodec := codec.NewTypeParser[chain.Action]()
authCodec := codec.NewTypeParser[chain.Auth]()

err = authRegistry.Register(&auth.ED25519{}, auth.UnmarshalED25519)
err = authCodec.Register(&auth.ED25519{}, auth.UnmarshalED25519)
require.NoError(err)
err = actionRegistry.Register(&mockTransferAction{}, unmarshalTransfer)
err = actionCodec.Register(&mockTransferAction{}, unmarshalTransfer)
require.NoError(err)
err = actionRegistry.Register(&action2{}, unmarshalAction2)
err = actionCodec.Register(&action2{}, unmarshalAction2)
require.NoError(err)

txBeforeSign := chain.Transaction{
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestMarshalUnmarshal(t *testing.T) {
}

require.Nil(tx.Auth)
signedTx, err := tx.Sign(factory, actionRegistry, authRegistry)
signedTx, err := tx.Sign(factory, actionCodec, authCodec)
require.NoError(err)
require.Equal(txBeforeSign, tx)
require.NotNil(signedTx.Auth)
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/morpheusvm/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,15 @@ func (p *Parser) Rules(_ int64) chain.Rules {
return p.genesis.Rules
}

func (*Parser) ActionRegistry() chain.ActionRegistry {
func (*Parser) ActionCodec() chain.ActionCodec {
return ActionParser
}

func (*Parser) OutputRegistry() chain.OutputRegistry {
func (*Parser) OutputCodec() chain.OutputCodec {
return OutputParser
}

func (*Parser) AuthRegistry() chain.AuthRegistry {
func (*Parser) AuthCodec() chain.AuthCodec {
return AuthParser
}

Expand Down
6 changes: 3 additions & 3 deletions examples/morpheusvm/vm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ func (p *Parser) Rules(_ int64) chain.Rules {
return p.genesis.Rules
}

func (*Parser) ActionRegistry() chain.ActionRegistry {
func (*Parser) ActionCodec() *codec.TypeParser[chain.Action] {
return ActionParser
}

func (*Parser) OutputRegistry() chain.OutputRegistry {
func (*Parser) OutputCodec() *codec.TypeParser[codec.Typed] {
return OutputParser
}

func (*Parser) AuthRegistry() chain.AuthRegistry {
func (*Parser) AuthCodec() *codec.TypeParser[chain.Auth] {
return AuthParser
}

Expand Down
Loading

0 comments on commit b934dbd

Please sign in to comment.