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

create TypeID for Actions/Auths + delete reflection in Registry #297

Merged
merged 7 commits into from
Jul 26, 2023

Conversation

najeal
Copy link
Contributor

@najeal najeal commented Jul 26, 2023

Closes #228

This PR adds GetTypeID method to the Action & Auth interfaces.

Now we register directly an ID instead of a Type in Registry.
Using a TypeID tied to the Action/Auth struct makes the reflection mechanism "legacy". Therefore it has been cleaned.

@patrick-ogrady is it what you expected?

package actions

const (
burnAssetID uint8 = iota
Copy link
Contributor

Choose a reason for hiding this comment

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

Please list these out explicitly. iota will do the job but find it is much more delicate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there is duplicated id created manually, the registry will return the error 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

May be worth adding that as a comment just for other contributors to keep in mind.

@patrick-ogrady
Copy link
Contributor

patrick-ogrady commented Jul 26, 2023

@patrick-ogrady is it what you expected?

Reviewing now...this is exactly what I had in mind ❤️ . I didn't realize the function signatures would be so much cleaner, which is awesome.

@@ -334,22 +329,14 @@ func (t *Transaction) Payer() string {

func (t *Transaction) Marshal(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: make this 1 line

t.Action.Marshal(p)
p.PackByte(authByte)
p.PackByte(authID)
t.Auth.Marshal(p)
return p.Err()
}

func MarshalTxs(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: make this 1 line

@patrick-ogrady patrick-ogrady changed the title create TypeID for Actions/Auths + delete reflection in Registry #228 create TypeID for Actions/Auths + delete reflection in Registry Jul 26, 2023
@patrick-ogrady
Copy link
Contributor

Ran the CI to give you a read on what's remaining to change 👍

// LookupType returns the index, decoder function and the success of lookup of
// type [o] from Typeparser [p].
func (p *TypeParser[T, X, Y]) LookupType(o T) (uint8, func(*Packer, X) (T, error), Y, bool) {
index, ok := p.typeToIndex[fmt.Sprintf("%T", o)]
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

package actions

const (
// IDs explicitly listed. Registry will avoid usage of duplicated IDs by returning an error when occurs
Copy link
Contributor

Choose a reason for hiding this comment

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

nit (for all):

// Note: Registry will error during initialization if a duplicate ID is assigned. We explicitly assign IDs to avoid accidental remapping.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can also put this just above const

@patrick-ogrady
Copy link
Contributor

@najeal VERY close! just 1-2 small nits. Kicking off full CI rn.

@patrick-ogrady
Copy link
Contributor

Would be good future work to convert this to use TypeID:

switch action := tx.Action.(type) {
case *actions.CreateAsset:
c.metrics.createAsset.Inc()
case *actions.MintAsset:
c.metrics.mintAsset.Inc()
case *actions.BurnAsset:
c.metrics.burnAsset.Inc()
case *actions.ModifyAsset:
c.metrics.modifyAsset.Inc()
case *actions.Transfer:
c.metrics.transfer.Inc()
case *actions.CreateOrder:
c.metrics.createOrder.Inc()
actor := auth.GetActor(tx.Auth)
c.orderBook.Add(tx.ID(), actor, action)
case *actions.FillOrder:
c.metrics.fillOrder.Inc()
orderResult, err := actions.UnmarshalOrderResult(result.Output)
if err != nil {
// This should never happen
return err
}
if orderResult.Remaining == 0 {
c.orderBook.Remove(action.Order)
continue
}
c.orderBook.UpdateRemaining(action.Order, orderResult.Remaining)
case *actions.CloseOrder:
c.metrics.closeOrder.Inc()
c.orderBook.Remove(action.Order)
case *actions.ImportAsset:
c.metrics.importAsset.Inc()
case *actions.ExportAsset:
c.metrics.exportAsset.Inc()
}

@patrick-ogrady
Copy link
Contributor

Looks like full CI passed, as soon as nits are pushed should be good to merge.

@patrick-ogrady
Copy link
Contributor

patrick-ogrady commented Jul 26, 2023

(no need to update branch to main...will merge as soon as the sync tests pass)

@najeal
Copy link
Contributor Author

najeal commented Jul 26, 2023

Would be good future work to convert this to use TypeID:

switch action := tx.Action.(type) {
case *actions.CreateAsset:
c.metrics.createAsset.Inc()
case *actions.MintAsset:
c.metrics.mintAsset.Inc()
case *actions.BurnAsset:
c.metrics.burnAsset.Inc()
case *actions.ModifyAsset:
c.metrics.modifyAsset.Inc()
case *actions.Transfer:
c.metrics.transfer.Inc()
case *actions.CreateOrder:
c.metrics.createOrder.Inc()
actor := auth.GetActor(tx.Auth)
c.orderBook.Add(tx.ID(), actor, action)
case *actions.FillOrder:
c.metrics.fillOrder.Inc()
orderResult, err := actions.UnmarshalOrderResult(result.Output)
if err != nil {
// This should never happen
return err
}
if orderResult.Remaining == 0 {
c.orderBook.Remove(action.Order)
continue
}
c.orderBook.UpdateRemaining(action.Order, orderResult.Remaining)
case *actions.CloseOrder:
c.metrics.closeOrder.Inc()
c.orderBook.Remove(action.Order)
case *actions.ImportAsset:
c.metrics.importAsset.Inc()
case *actions.ExportAsset:
c.metrics.exportAsset.Inc()
}

You want the same switch but on the TypeID instead?

@patrick-ogrady
Copy link
Contributor

Would be good future work to convert this to use TypeID:

switch action := tx.Action.(type) {
case *actions.CreateAsset:
c.metrics.createAsset.Inc()
case *actions.MintAsset:
c.metrics.mintAsset.Inc()
case *actions.BurnAsset:
c.metrics.burnAsset.Inc()
case *actions.ModifyAsset:
c.metrics.modifyAsset.Inc()
case *actions.Transfer:
c.metrics.transfer.Inc()
case *actions.CreateOrder:
c.metrics.createOrder.Inc()
actor := auth.GetActor(tx.Auth)
c.orderBook.Add(tx.ID(), actor, action)
case *actions.FillOrder:
c.metrics.fillOrder.Inc()
orderResult, err := actions.UnmarshalOrderResult(result.Output)
if err != nil {
// This should never happen
return err
}
if orderResult.Remaining == 0 {
c.orderBook.Remove(action.Order)
continue
}
c.orderBook.UpdateRemaining(action.Order, orderResult.Remaining)
case *actions.CloseOrder:
c.metrics.closeOrder.Inc()
c.orderBook.Remove(action.Order)
case *actions.ImportAsset:
c.metrics.importAsset.Inc()
case *actions.ExportAsset:
c.metrics.exportAsset.Inc()
}

You want the same switch but on the TypeID instead?

Yeah, the idea is to remove all use of reflect, especially in the "hot path". Do you want to do that here or in a separate PR?

@najeal
Copy link
Contributor Author

najeal commented Jul 26, 2023

@patrick-ogrady I will do it in another PR. I will also pick another issue in the list.

@patrick-ogrady patrick-ogrady merged commit fe99c39 into ava-labs:main Jul 26, 2023
18 checks passed
@najeal najeal deleted the action-auth-id branch July 26, 2023 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Assign TypeID to Actions/Auths Instead of Using Reflect-Based Registry
2 participants