Skip to content

Immediate staking pool snapshot creation for new proposals #758

@thismars

Description

@thismars

Current Behavior

Currently, when a new proposal is created, its staking pool snapshot is only created during periodic operations that run every 5 minutes. This leads to:

  1. Data delay: Users must wait up to 5 minutes to see the initial snapshot data
  2. Data accuracy issues: The first snapshot might not reflect the exact staking state at proposal creation
  3. Poor user experience: Incomplete proposal information immediately after creation

Expected Behavior

Staking pool snapshots should be:

  1. Created immediately when a new proposal is created
  2. Continue to be updated every 5 minutes for ongoing monitoring

Technical Details

Current Implementation

// In modules/gov/handle_msg.go
func (m *Module) handleSubmitProposalEvent(tx *juno.Transaction, proposer string, events sdk.StringEvents) error {
    // ... proposal creation logic ...
    err = m.db.SaveProposals([]types.Proposal{proposalObj})
    if err != nil {
        return fmt.Errorf("error while saving proposal: %s", err)
    }
    // No immediate snapshot creation
    return m.handleDepositEvent(tx, proposer, events)
}

Proposed Implementation

// New optimized function in modules/gov/utils_proposal.go
func (m *Module) UpdateSingleProposalStakingPoolSnapshot(height int64, proposalID uint64) error {
    pool, err := m.stakingModule.GetStakingPoolSnapshot(height)
    if err != nil {
        return fmt.Errorf("error while getting staking pool: %s", err)
    }
    return m.db.SaveProposalStakingPoolSnapshot(
        types.NewProposalStakingPoolSnapshot(proposalID, pool),
    )
}

// Modified handleSubmitProposalEvent
func (m *Module) handleSubmitProposalEvent(tx *juno.Transaction, proposer string, events sdk.StringEvents) error {
    // ... proposal creation logic ...
    err = m.db.SaveProposals([]types.Proposal{proposalObj})
    if err != nil {
        return fmt.Errorf("error while saving proposal: %s", err)
    }

    // Create snapshot immediately
    err = m.UpdateSingleProposalStakingPoolSnapshot(int64(tx.Height), proposal.Id)
    if err != nil {
        log.Error().Err(err).
            Uint64("proposal_id", proposal.Id).
            Uint64("height", tx.Height).
            Msg("failed to update staking pool snapshot for new proposal")
    }

    return m.handleDepositEvent(tx, proposer, events)
}

Benefits

  1. Immediate Data Availability: Users can see complete proposal information right after creation
  2. Data Accuracy: Snapshot reflects exact staking state at proposal creation time
  3. Resource Efficiency: Only updates the necessary proposal instead of all proposals
  4. Better Error Tracking: Proposal-specific error logging
  5. Maintains Existing Functionality: Keeps periodic updates as a safety net

Implementation Notes

  1. The change is minimally invasive and follows existing patterns
  2. Error handling is non-blocking to prevent proposal creation failure
  3. Maintains backward compatibility
  4. No database schema changes required

Test result

FirmaChain#5 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions