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

feat(cosmos): contextual and declarative VM action field defaults #8703

Merged
merged 2 commits into from
Jan 6, 2024

Conversation

michaelfig
Copy link
Member

@michaelfig michaelfig commented Dec 31, 2023

closes: #8676

Description

Combine Cosmos SDK context and type tags with reflection to reduce the boilerplate needed to create "actions" to be sent to the SwingSet VM.

The pioneering work was done by @gibson042 in #8676, I just tried to make it more thorough and use it throughout golang/cosmos.

Security Considerations

n/a

Scaling Considerations

n/a

Documentation Considerations

Agoric-Cosmos internals-only, should be documented in comments to give clues, but need not be published elsewhere.

Testing Considerations

Unit tests of vm.WithActionContext, otherwise reliant on existing tests for basic functionality.

Upgrade Considerations

n/a

@michaelfig michaelfig added agd Agoric (Golang) Daemon agoric-cosmos labels Dec 31, 2023
@michaelfig michaelfig self-assigned this Dec 31, 2023
@michaelfig michaelfig force-pushed the mfig-vm-action-defaults branch from ff87a1c to 6511df8 Compare December 31, 2023 05:32
@michaelfig michaelfig marked this pull request as ready for review December 31, 2023 16:20
@michaelfig michaelfig force-pushed the mfig-vm-action-defaults branch 3 times, most recently from 53c04f3 to 405e2d5 Compare December 31, 2023 19:28
@michaelfig michaelfig changed the title Declarative VM action defaults feat(cosmos): contextual and declarative VM action field default values Dec 31, 2023
@michaelfig michaelfig changed the title feat(cosmos): contextual and declarative VM action field default values feat(cosmos): contextual and declarative VM action field defaults Dec 31, 2023
Copy link
Contributor

@JimLarson JimLarson left a comment

Choose a reason for hiding this comment

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

Approved, but please see strong suggestions in comments.

Comment on lines 53 to 104
if !field.IsZero() {
continue
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Cute, but consider an explicit continue for each case in the switch.


// WithActionContext interprets `default:"..."` tags and adds other fields from
// the SDK context to the action.
func WithActionContext(ctx sdk.Context, action Jsonable) Jsonable {
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider renaming to PopulateAction(). The "with...context" triggers me to think of the various "with" methods for sdk.Context.

Comment on lines 17 to 19
Type string `json:"type" default:"BEGIN_BLOCK"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Here and below, consider embedding a common ActionHeader structure as in Richard's version. Also, make some reference to the understanding that certain fields will be automagically populated. Having the common header lets you put that reference in just one place.

@mhofman mhofman self-requested a review January 4, 2024 21:56
@michaelfig michaelfig force-pushed the mfig-vm-action-defaults branch from 405e2d5 to 040fda4 Compare January 4, 2024 23:10
@michaelfig michaelfig added the automerge:no-update (expert!) Automatically merge without updates label Jan 4, 2024
@mhofman mhofman removed the automerge:no-update (expert!) Automatically merge without updates label Jan 4, 2024
@mhofman
Copy link
Member

mhofman commented Jan 4, 2024

Removing the automerge label, was reviewing

Copy link
Member

@mhofman mhofman left a comment

Choose a reason for hiding this comment

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

This is a nice consolidation. I am a little nervous about the init action change, but it's likely fine. Please consider addressing my nits before re-enabling automerge.

Comment on lines -903 to -906
var blockTime int64 = 0
if bootstrap || app.upgradePlan != nil {
blockTime = ctx.BlockTime().Unix()
}
Copy link
Member

Choose a reason for hiding this comment

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

I am uncomfortable with providing a blockTime and blockHeight for the init action sent during simple restart scenarios since there is so much non determinism in this case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you explain more about "so much non-determinism"? Are we trying to paper over a Cosmos bug, or is there some subtlety that the cosmic-swingset side of things needs to understand?

The AG_COSMOS_INIT handler is already very careful in its assumptions, and does not look at blockTime/blockHeight unless isBootstrap or upgradePlan. I don't think it's beneficial to override the Golang propagation of these values when we may need them someday in the handler.

If it's really Golang that should change, I would prefer changing the construction of the ctx sdk.Context passed into app.SwingSetKeeper.BlockingSend, not in special-casing the ActionHeader population. That to me falls into the "papering over a Cosmos bug" category, and would necessitate a bunch of justifying commentary so that the reader would be left with an (uncomfortable) understanding of the bug.

Copy link
Member

Choose a reason for hiding this comment

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

There is no bug. It's just how we use the init "blocking send", and that a node can be started either after consensus stop, or from per validator action. I was simply concerned about including consensus state in actions happening outside of consensus. But you're right, there isn't really a problem as long as no consensus decision is taken from that data.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. I will add a comment describing that to the init blocking send call.

Comment on lines 33 to 46
// Default the Type field to the default tag.
if len(defaultType) != 0 && len(ah.Type) == 0 {
ah.Type = defaultType
}
Copy link
Member

Choose a reason for hiding this comment

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

Is there a case where defaultType would not be set?

Should we assert that Type is never empty?

Copy link
Member Author

Choose a reason for hiding this comment

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

The semantics I'm aiming for is "never fail, just set defaults when they're available". It's reasonable to make Type a mandatory non-empty field in PushAction and BlockingSend, though. I'll do that.

// Populate the embedded *ActionHeader struct.
if fieldType.Anonymous && fieldType.Type == actionHeaderType {
ahp := field.Addr().Interface().(*ActionHeader)
setActionHeaderDefaults(ctx, defaultTag, ahp)
Copy link
Member

Choose a reason for hiding this comment

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

It feels weird to use the default tag an an embedded header like this. I know it might be more verbose, but I think a defaultType tag would be more appropriate.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, I can do that.

@JimLarson
Copy link
Contributor

Note that the non-action bridge messages will change format when we switch to JSON-RPC as part of the split brain effort.

@michaelfig michaelfig force-pushed the mfig-vm-action-defaults branch from 74c27e8 to 33e6c1a Compare January 5, 2024 03:35
@michaelfig michaelfig added the automerge:no-update (expert!) Automatically merge without updates label Jan 5, 2024
@michaelfig michaelfig force-pushed the mfig-vm-action-defaults branch from 33e6c1a to 021fd0f Compare January 6, 2024 00:28
@mergify mergify bot merged commit 75ef233 into master Jan 6, 2024
67 checks passed
@mergify mergify bot deleted the mfig-vm-action-defaults branch January 6, 2024 00:58
mhofman pushed a commit that referenced this pull request Jan 30, 2024
feat(cosmos): contextual and declarative VM action field defaults
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agd Agoric (Golang) Daemon agoric-cosmos automerge:no-update (expert!) Automatically merge without updates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants