Skip to content

Commit

Permalink
rename registry -> codec
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Kim <[email protected]>
  • Loading branch information
joshua-kim committed Oct 2, 2024
1 parent 84437e9 commit 244eb9a
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 144 deletions.
6 changes: 3 additions & 3 deletions api/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type VM interface {
SubnetID() ids.ID
Tracer() trace.Tracer
Logger() logging.Logger
ActionRegistry() *codec.TypeParser[chain.Action]
OutputRegistry() *codec.TypeParser[codec.Typed]
AuthRegistry() *codec.TypeParser[chain.Auth]
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
12 changes: 6 additions & 6 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 @@ -151,8 +151,8 @@ type GetABIReply struct {
}

func (j *JSONRPCServer) GetABI(_ *http.Request, _ *GetABIArgs, reply *GetABIReply) error {
actionRegistry, outputRegistry := j.vm.ActionRegistry(), j.vm.OutputRegistry()
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 @@ -178,8 +178,8 @@ func (j *JSONRPCServer) Execute(
ctx, span := j.vm.Tracer().Start(req.Context(), "JSONRPCServer.ExecuteAction")
defer span.End()

actionRegistry := j.vm.ActionRegistry()
action, err := actionRegistry.Unmarshal(codec.NewReader(args.Action, len(args.Action)))
actionCodec := j.vm.ActionCodec()
action, err := actionCodec.Unmarshal(codec.NewReader(args.Action, len(args.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 *codec.TypeParser[chain.Action]
authRegistry *codec.TypeParser[chain.Auth]
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 *codec.TypeParser[chain.Action],
authRegistry *codec.TypeParser[chain.Auth],
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 @@ -113,11 +113,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
6 changes: 3 additions & 3 deletions chain/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (

type Parser interface {
Rules(int64) Rules
ActionRegistry() *codec.TypeParser[Action]
OutputRegistry() *codec.TypeParser[codec.Typed]
AuthRegistry() *codec.TypeParser[Auth]
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 @@ -75,8 +75,8 @@ func (t *Transaction) Digest() ([]byte, error) {

func (t *Transaction) Sign(
factory AuthFactory,
actionRegistry *codec.TypeParser[Action],
authRegistry *codec.TypeParser[Auth],
actionCodec *codec.TypeParser[Action],
authCodec *codec.TypeParser[Auth],
) (*Transaction, error) {
msg, err := t.Digest()
if err != nil {
Expand All @@ -99,7 +99,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)
}

func (t *Transaction) Bytes() []byte { return t.bytes }
Expand Down Expand Up @@ -416,15 +416,15 @@ func MarshalTxs(txs []*Transaction) ([]byte, error) {
func UnmarshalTxs(
raw []byte,
initialCapacity int,
actionRegistry *codec.TypeParser[Action],
authRegistry *codec.TypeParser[Auth],
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 @@ -440,20 +440,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 @@ -483,15 +483,15 @@ func UnmarshalTx(

func unmarshalActions(
p *codec.Packer,
actionRegistry *codec.TypeParser[Action],
actionCodec *codec.TypeParser[Action],
) ([]Action, error) {
actionCount := p.UnpackByte()
if actionCount == 0 {
return nil, fmt.Errorf("%w: no actions", ErrInvalidObject)
}
actions := []Action{}
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,17 +105,17 @@ 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)

signedTx, err := tx.Sign(factory, actionRegistry, authRegistry)
signedTx, err := tx.Sign(factory, actionCodec, authCodec)
require.NoError(err)

require.Equal(len(signedTx.Actions), len(tx.Actions))
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 @@ -266,15 +266,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() *codec.TypeParser[chain.Action] {
func (*Parser) ActionCodec() *codec.TypeParser[chain.Action] {
return ActionParser
}

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

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

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

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions internal/gossiper/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type VM interface {
IsValidator(context.Context, ids.NodeID) (bool, error)
Logger() logging.Logger
PreferredBlock(context.Context) (*chain.StatefulBlock, error)
ActionRegistry() *codec.TypeParser[chain.Action]
AuthRegistry() *codec.TypeParser[chain.Auth]
ActionCodec() *codec.TypeParser[chain.Action]
AuthCodec() *codec.TypeParser[chain.Auth]
NodeID() ids.NodeID
Rules(int64) chain.Rules
Submit(ctx context.Context, verify bool, txs []*chain.Transaction) []error
Expand Down
4 changes: 2 additions & 2 deletions internal/gossiper/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (g *Manual) Force(ctx context.Context) error {
}

func (g *Manual) HandleAppGossip(ctx context.Context, nodeID ids.NodeID, msg []byte) error {
actionRegistry, authRegistry := g.vm.ActionRegistry(), g.vm.AuthRegistry()
_, txs, err := chain.UnmarshalTxs(msg, initialCapacity, actionRegistry, authRegistry)
actionCodec, authCodec := g.vm.ActionCodec(), g.vm.AuthCodec()
_, txs, err := chain.UnmarshalTxs(msg, initialCapacity, actionCodec, authCodec)
if err != nil {
g.vm.Logger().Warn(
"AppGossip provided invalid txs",
Expand Down
4 changes: 2 additions & 2 deletions internal/gossiper/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ func (g *Proposer) Force(ctx context.Context) error {
}

func (g *Proposer) HandleAppGossip(ctx context.Context, nodeID ids.NodeID, msg []byte) error {
actionRegistry, authRegistry := g.vm.ActionRegistry(), g.vm.AuthRegistry()
authCounts, txs, err := chain.UnmarshalTxs(msg, initialCapacity, actionRegistry, authRegistry)
actionCodec, authCodec := g.vm.ActionCodec(), g.vm.AuthCodec()
authCounts, txs, err := chain.UnmarshalTxs(msg, initialCapacity, actionCodec, authCodec)
if err != nil {
g.vm.Logger().Warn(
"received invalid txs",
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ var _ = ginkgo.Describe("[HyperSDK APIs]", func() {
ginkgo.It("GetABI", func() {
ginkgo.By("Gets ABI")

actionRegistry, outputRegistry := instances[0].vm.ActionRegistry(), instances[0].vm.OutputRegistry()
expectedABI, err := abi.NewABI(actionRegistry.GetRegisteredTypes(), (*outputRegistry).GetRegisteredTypes())
actionCodec, outputCodec := instances[0].vm.ActionCodec(), instances[0].vm.OutputCodec()
expectedABI, err := abi.NewABI(actionCodec.GetRegisteredTypes(), (*outputCodec).GetRegisteredTypes())
require.NoError(err)

workload.GetABI(ctx, require, uris, expectedABI)
Expand Down
12 changes: 6 additions & 6 deletions vm/defaultvm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func New(
v *version.Semantic,
genesisFactory genesis.GenesisAndRuleFactory,
stateManager chain.StateManager,
actionRegistry *codec.TypeParser[chain.Action],
authRegistry *codec.TypeParser[chain.Auth],
outputRegistry *codec.TypeParser[codec.Typed],
actionCodec *codec.TypeParser[chain.Action],
authCodec *codec.TypeParser[chain.Auth],
outputCodec *codec.TypeParser[codec.Typed],
authEngine map[uint8]vm.AuthEngine,
options ...vm.Option,
) (*vm.VM, error) {
Expand All @@ -47,9 +47,9 @@ func New(
v,
genesisFactory,
stateManager,
actionRegistry,
authRegistry,
outputRegistry,
actionCodec,
authCodec,
outputCodec,
authEngine,
options...,
)
Expand Down
Loading

0 comments on commit 244eb9a

Please sign in to comment.