-
Notifications
You must be signed in to change notification settings - Fork 123
Reservations add protocol version #920
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,29 @@ package reservation | |
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/lightninglabs/lndclient" | ||
| "github.com/lightninglabs/loop/fsm" | ||
| "github.com/lightninglabs/loop/swapserverrpc" | ||
| ) | ||
|
|
||
| type ProtocolVersion uint32 | ||
|
|
||
| // String returns the string representation of the protocol version. | ||
| func (v ProtocolVersion) String() string { | ||
| return fmt.Sprintf("ProtocolVersion(%d)", v) | ||
| } | ||
|
|
||
| const ( | ||
| // CurrentProtocolVersion is the current protocol version. | ||
| CurrentProtocolVersion ProtocolVersion = ProtocolVersionServerInitiated | ||
|
|
||
| // ProtocolVersionServerInitiated is the protocol version where the | ||
| // server initiates the reservation. | ||
| ProtocolVersionServerInitiated ProtocolVersion = 0 | ||
| ) | ||
|
|
||
| const ( | ||
| // defaultObserverSize is the size of the fsm observer channel. | ||
| defaultObserverSize = 15 | ||
|
|
@@ -45,7 +62,8 @@ type FSM struct { | |
| // NewFSM creates a new reservation FSM. | ||
| func NewFSM(cfg *Config) *FSM { | ||
| reservation := &Reservation{ | ||
| State: fsm.EmptyState, | ||
| State: fsm.EmptyState, | ||
| ProtocolVersion: CurrentProtocolVersion, | ||
| } | ||
|
|
||
| return NewFSMFromReservation(cfg, reservation) | ||
|
|
@@ -59,10 +77,19 @@ func NewFSMFromReservation(cfg *Config, reservation *Reservation) *FSM { | |
| reservation: reservation, | ||
| } | ||
|
|
||
| var states fsm.States | ||
| switch reservation.ProtocolVersion { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to log here which version of the state machine is running. Perhaps also consider adding a
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, will do that for the static state machine as well. |
||
| case ProtocolVersionServerInitiated: | ||
| states = reservationFsm.GetServerInitiatedReservationStates() | ||
sputn1ck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| default: | ||
| states = make(fsm.States) | ||
| } | ||
|
|
||
| reservationFsm.StateMachine = fsm.NewStateMachineWithState( | ||
| reservationFsm.GetReservationStates(), reservation.State, | ||
| defaultObserverSize, | ||
| states, reservation.State, defaultObserverSize, | ||
| ) | ||
|
|
||
| reservationFsm.ActionEntryFunc = reservationFsm.updateReservation | ||
|
|
||
| return reservationFsm | ||
|
|
@@ -133,9 +160,9 @@ var ( | |
| OnUnlocked = fsm.EventType("OnUnlocked") | ||
| ) | ||
|
|
||
| // GetReservationStates returns the statemap that defines the reservation | ||
| // state machine. | ||
| func (f *FSM) GetReservationStates() fsm.States { | ||
| // GetServerInitiatedReservationStates returns the statemap that defines the | ||
| // reservation state machine, where the server initiates the reservation. | ||
| func (f *FSM) GetServerInitiatedReservationStates() fsm.States { | ||
| return fsm.States{ | ||
| fsm.EmptyState: fsm.State{ | ||
| Transitions: fsm.Transitions{ | ||
|
|
@@ -234,22 +261,25 @@ func (r *FSM) updateReservation(ctx context.Context, | |
|
|
||
| func (r *FSM) Infof(format string, args ...interface{}) { | ||
| log.Infof( | ||
| "Reservation %x: "+format, | ||
| append([]interface{}{r.reservation.ID}, args...)..., | ||
| "Reservation %v %x: "+format, | ||
| append([]interface{}{r.reservation.ProtocolVersion, r.reservation.ID}, | ||
| args...)..., | ||
| ) | ||
| } | ||
|
|
||
| func (r *FSM) Debugf(format string, args ...interface{}) { | ||
| log.Debugf( | ||
| "Reservation %x: "+format, | ||
| append([]interface{}{r.reservation.ID}, args...)..., | ||
| "Reservation %v %x: "+format, | ||
| append([]interface{}{r.reservation.ProtocolVersion, r.reservation.ID}, | ||
| args...)..., | ||
| ) | ||
| } | ||
|
|
||
| func (r *FSM) Errorf(format string, args ...interface{}) { | ||
| log.Errorf( | ||
| "Reservation %x: "+format, | ||
| append([]interface{}{r.reservation.ID}, args...)..., | ||
| "Reservation %v %x: "+format, | ||
| append([]interface{}{r.reservation.ProtocolVersion, r.reservation.ID}, | ||
| args...)..., | ||
| ) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| -- protocol_version is used to determine the version of the reservation protocol | ||
| -- that was used to create the reservation. | ||
| ALTER TABLE reservations DROP COLUMN protocol_Version; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| -- protocol_version is used to determine the version of the reservation protocol | ||
| -- that was used to create the reservation. | ||
| ALTER TABLE reservations ADD COLUMN protocol_Version INTEGER NOT NULL DEFAULT 0; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could have a
CurrentProtocolVersionfunction that returns the last one, so we don't need to pass it in when creating a new reservation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no current as we will still use both simultaneously
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed offline, so the reason to not have is because there will be two current versions, one for the client requested one for the server requested.