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

In-Memory Snapshot Implementation #204

Merged

Conversation

jordanschalm
Copy link
Member

@jordanschalm jordanschalm commented Dec 3, 2020

This PR defines an in-memory implementation of protocol.Snapshot and a canonical encoding structure for Snapshot.

Changes

  • Adds EncodableSnapshot and sub-structures, which define the structure of encoding a snapshot
  • Adds inmem.Snapshot, a memory-backed implementation of protocol.Snapshot that uses EncodableSnapshot as its underlying data structure
  • Adds conversion function inmem.FromSnapshot which converts any implementation of protocol.Snapshot to a memory-backed snapshot + smoke tests

@jordanschalm jordanschalm self-assigned this Dec 3, 2020
@jordanschalm jordanschalm changed the title Serializable In-Memory Snapshot Implementation In-Memory Snapshot Implementation Dec 3, 2020
These were effectively already using similar memory-backed
implementations, this removes the duplication
Base automatically changed from jordan/4889-qc-in-snapshot to feature/serializable-snapshots December 4, 2020 01:52
Copy link
Member

@zhangchiqing zhangchiqing left a comment

Choose a reason for hiding this comment

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

Looks good to me

@jordanschalm jordanschalm removed the request for review from AlexHentschel December 8, 2020 18:22
}

func (u *Epochs) Next() protocol.Epoch {
return NewEpoch(u.err)
Copy link
Contributor

Choose a reason for hiding this comment

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

can this recursively go through? (e.g., ..Next().Next().Next()), if so, does this make sense to prefix the error each time we create a new epoch?

Copy link
Member Author

@jordanschalm jordanschalm Dec 9, 2020

Choose a reason for hiding this comment

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

No there's only one layer of querying. From a block reference (ie state.AtBlockID(id)) we can query only the previous/current/next epochs from that reference. Next returns an Epoch object which doesn't have a Next method.

return nil, err
}
snap.Identities, err = from.Identities(filter.Any)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add some prefix to this error letting them get nested: return nil, fmt.Errorf("could not retrieve identities: %w")

Copy link
Contributor

@yhassanzadeh13 yhassanzadeh13 Dec 9, 2020

Choose a reason for hiding this comment

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

Same suggestion for the rest of the errors (unless you want to preserve their type later on).

)

// should be able to convert from protocol.Snapshot
func TestFromSnapshot(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

better to start godoc with name of the function // TestFromSnapshot ....

t.Run("staking phase", func(t *testing.T) {
expected := state.AtHeight(1)
actual, err := inmem.FromSnapshot(expected)
require.Nil(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

you may use require.NoError here as well.

"github.com/onflow/flow-go/model/flow"
)

// Snapshot is the encoding format for protocol.Snapshot
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Snapshot is the encoding format for protocol.Snapshot
// EncodableSnapshot is the encoding format for protocol.Snapshot

Next *EncodableEpoch
}

// Epoch is the encoding format for protocol.Epoch
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Epoch is the encoding format for protocol.Epoch
// EncodableEpoch is the encoding format for protocol.Epoch

DKG *EncodableDKG
}

// DKG is the encoding format for protocol.DKG
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// DKG is the encoding format for protocol.DKG
// EncodableDKG is the encoding format for protocol.DKG

Participants map[flow.Identifier]flow.DKGParticipant
}

// Cluster is the encoding format for protocol.Cluster
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Cluster is the encoding format for protocol.Cluster
// EncodableCluster is the encoding format for protocol.Cluster

enc EncodableSnapshot
}

func (s *Snapshot) Head() (*flow.Header, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these methods are not required to be pointer receiver. They do not change affect s and safer to be by value.

Copy link
Contributor

@yhassanzadeh13 yhassanzadeh13 left a comment

Choose a reason for hiding this comment

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

Clean implementation 👍 left a few comments basically related to the linting

@jordanschalm
Copy link
Member Author

Thanks @yhassanzadeh13, applied those suggestions

@jordanschalm jordanschalm merged commit 1e3c5c3 into feature/serializable-snapshots Dec 9, 2020
@jordanschalm jordanschalm deleted the jordan/4889-serializable-snapshot branch December 9, 2020 23:10
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.

3 participants