Skip to content

Commit

Permalink
Add sub-status entries for transaction progress
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Whitehead <[email protected]>
  • Loading branch information
matthew1001 committed Jan 4, 2023
1 parent 1bd9c85 commit b56335b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
40 changes: 40 additions & 0 deletions pkg/apitypes/managed_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ type ManagedTXUpdate struct {
MappedReason ffcapi.ErrorReason `json:"reason,omitempty"`
}

// TxSubStatus is an intermediate status a transaction may go through
type TxSubStatus string

const (
// TxSubStatusRetrievedGasPrice indicates the operation has had gas price calculated for it
TxSubStatusRetrievedGasPrice TxSubStatus = "RetrievedGasPrice"
// TxSubStatusRetrievingGasPrice indicates the operation is getting a gas price
TxSubStatusRetrievingGasPrice TxSubStatus = "RetrievingGasPrice"
// TxSubStatusSubmitting indicates that the transaction is about to be submitted
TxSubStatusSubmitting TxSubStatus = "Submitting"
// TxSubStatusSubmitted indicates that the transaction has been submitted to the JSON/RPC endpoint
TxSubStatusSubmitted TxSubStatus = "Submitted"
// TxSubStatusReceivedReceipt indicates that we have received a receipt for the transaction
TxSubStatusReceivedReceipt TxSubStatus = "ReceivedReceipt"
// TxSubStatusConfirmed indicates that we have met the required number of confirmations for the transaction
TxSubStatusConfirmed TxSubStatus = "Confirmed"
)

type TxSubStatusEntry struct {
Time *fftypes.FFTime `json:"time"`
LastOccurrence *fftypes.FFTime `json:"lastOccurrence"`
Status TxSubStatus `json:"subStatus"`
Count int `json:"count"`
}

// MsgString is assured to be the same, as long as the type/message is the same.
// Does not change if the count/times are different - so allows comparison.
func (mtu *ManagedTXUpdate) MsgString() string {
Expand Down Expand Up @@ -98,6 +123,7 @@ type ManagedTX struct {
Receipt *ffcapi.TransactionReceiptResponse `json:"receipt,omitempty"`
ErrorMessage string `json:"errorMessage,omitempty"`
History []*ManagedTXUpdate `json:"history"`
SubStatusHistory []*TxSubStatusEntry `json:"subStatusHistory"`
Confirmations []confirmations.BlockInfo `json:"confirmations,omitempty"`
}

Expand Down Expand Up @@ -125,3 +151,17 @@ type TransactionUpdateReply struct {
ProtocolID string `json:"protocolId"`
TransactionHash string `json:"transactionHash,omitempty"`
}

func (mtx *ManagedTX) AddSubStatus(subStatus TxSubStatus) {
newStatus := &TxSubStatusEntry{
Time: fftypes.Now(),
LastOccurrence: fftypes.Now(),
Count: 1,
Status: subStatus,
}
mtx.SubStatusHistory = append(mtx.SubStatusHistory, newStatus)

if len(mtx.SubStatusHistory) > 20 {
mtx.SubStatusHistory = mtx.SubStatusHistory[1:]
}
}
2 changes: 2 additions & 0 deletions pkg/fftm/policyloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ func (m *manager) trackSubmittedTransaction(ctx context.Context, pending *pendin
// Will be picked up on the next policy loop cycle - guaranteed to occur before Confirmed
m.mux.Lock()
pending.mtx.Receipt = receipt
pending.mtx.AddSubStatus(apitypes.TxSubStatusReceivedReceipt)
m.mux.Unlock()
log.L(m.ctx).Debugf("Receipt received for transaction %s at nonce %s / %d - hash: %s", pending.mtx.ID, pending.mtx.TransactionHeaders.From, pending.mtx.Nonce.Int64(), pending.mtx.TransactionHash)
m.markInflightUpdate()
Expand All @@ -397,6 +398,7 @@ func (m *manager) trackSubmittedTransaction(ctx context.Context, pending *pendin
m.mux.Lock()
pending.confirmed = true
pending.mtx.Confirmations = confirmations
pending.mtx.AddSubStatus(apitypes.TxSubStatusConfirmed)
m.mux.Unlock()
log.L(m.ctx).Debugf("Confirmed transaction %s at nonce %s / %d - hash: %s", pending.mtx.ID, pending.mtx.TransactionHeaders.From, pending.mtx.Nonce.Int64(), pending.mtx.TransactionHash)
m.markInflightUpdate()
Expand Down
4 changes: 4 additions & 0 deletions pkg/policyengines/simple/simple_policy_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,17 @@ func (p *simplePolicyEngine) Execute(ctx context.Context, cAPI ffcapi.API, mtx *
// Simple policy engine only submits once.
if mtx.FirstSubmit == nil {
// Only calculate gas price here in the simple policy engine
mtx.AddSubStatus(apitypes.TxSubStatusRetrievingGasPrice)
mtx.GasPrice, err = p.getGasPrice(ctx, cAPI)
if err != nil {
return policyengine.UpdateNo, "", err
}
mtx.AddSubStatus(apitypes.TxSubStatusRetrievedGasPrice)
// Submit the first time
if reason, err := p.submitTX(ctx, cAPI, mtx); err != nil {
return policyengine.UpdateYes, reason, err
}
mtx.AddSubStatus(apitypes.TxSubStatusSubmitted)
mtx.FirstSubmit = mtx.LastSubmit
return policyengine.UpdateYes, "", nil

Expand Down Expand Up @@ -193,6 +196,7 @@ func (p *simplePolicyEngine) Execute(ctx context.Context, cAPI ffcapi.API, mtx *
return policyengine.UpdateYes, reason, err
}
}
mtx.AddSubStatus(apitypes.TxSubStatusSubmitted)
return policyengine.UpdateYes, "", nil
}
return policyengine.UpdateNo, "", nil
Expand Down

0 comments on commit b56335b

Please sign in to comment.