Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Oct 6, 2023
1 parent d86bd89 commit b226e07
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
2 changes: 1 addition & 1 deletion dependencies/caminoethvm
Submodule caminoethvm updated 1 files
+1 −1 caminogo
76 changes: 41 additions & 35 deletions services/indexes/pvm/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,7 @@ func (w *Writer) indexTransaction(ctx services.ConsumerCtx, blkID ids.ID, tx *tx
case *txs.FinishProposalsTx:
baseTx = castTx.BaseTx.BaseTx
typ = models.TransactionTypeFinishDACProposals
finishedProposalIDs := append(castTx.EarlyFinishedProposalIDs, castTx.ExpiredProposalIDs...) //nolint:gocritic
if err := w.FinishDACProposals(ctx, finishedProposalIDs, ctx.Time()); err != nil {
if err := w.FinishDACProposals(ctx, castTx, ctx.Time()); err != nil {
return err
}
default:
Expand Down Expand Up @@ -861,49 +860,56 @@ func (w *Writer) InsertDACProposal(
})
}

func (w *Writer) FinishDACProposals(ctx services.ConsumerCtx, proposalIDs []ids.ID, finishedAt time.Time) error {
proposalIDsStrs := make([]string, len(proposalIDs))
for i := range proposalIDs {
proposalIDsStrs[i] = proposalIDs[i].String()
func (w *Writer) FinishDACProposals(ctx services.ConsumerCtx, tx *txs.FinishProposalsTx, finishedAt time.Time) error {
// Finishing successful proposals
successfulProposalIDsStrs := make([]string, 0, len(tx.EarlyFinishedSuccessfulProposalIDs)+len(tx.ExpiredSuccessfulProposalIDs))
for _, proposalID := range tx.EarlyFinishedSuccessfulProposalIDs {
successfulProposalIDsStrs = append(successfulProposalIDsStrs, proposalID.String())
}
dbProposals, err := ctx.Persist().GetDACProposals(ctx.Ctx(), ctx.DB(), proposalIDsStrs)
if err != nil {
return err
for _, proposalID := range tx.ExpiredSuccessfulProposalIDs {
successfulProposalIDsStrs = append(successfulProposalIDsStrs, proposalID.String())
}

var failedProposalIDsStrs []string
for _, dbProposal := range dbProposals {
proposal := dacProposalWrapper{}
if _, err := txs.Codec.Unmarshal(dbProposal.SerializedBytes, &proposal); err != nil {
return err
}

if !proposal.IsSuccessful() {
failedProposalIDsStrs = append(failedProposalIDsStrs, dbProposal.ID)
continue
}

outcomeBytes, err := json.Marshal(proposal.Outcome())
if len(successfulProposalIDsStrs) > 0 {
successfulProposals, err := ctx.Persist().GetDACProposals(ctx.Ctx(), ctx.DB(), successfulProposalIDsStrs)
if err != nil {
return err
}
for _, dbProposal := range successfulProposals {
proposal := dacProposalWrapper{}
if _, err := txs.Codec.Unmarshal(dbProposal.SerializedBytes, &proposal); err != nil {
return err
}

if err := ctx.Persist().FinishDACProposalWithOutcome(
ctx.Ctx(),
ctx.DB(),
dbProposal.ID,
finishedAt,
models.ProposalStatusSuccess,
outcomeBytes,
); err != nil {
return err
outcomeBytes, err := json.Marshal(proposal.Outcome())
if err != nil {
return err
}

if err := ctx.Persist().FinishDACProposalWithOutcome(
ctx.Ctx(),
ctx.DB(),
dbProposal.ID,
finishedAt,
models.ProposalStatusSuccess,
outcomeBytes,
); err != nil {
return err
}
}
}

if len(failedProposalIDsStrs) > 0 {
return ctx.Persist().FinishDACProposals(ctx.Ctx(), ctx.DB(), failedProposalIDsStrs, finishedAt, models.ProposalStatusFailed)
// Finishing failed proposals
failedProposalIDsStrs := make([]string, 0, len(tx.EarlyFinishedFailedProposalIDs)+len(tx.ExpiredFailedProposalIDs))
for _, proposalID := range tx.EarlyFinishedFailedProposalIDs {
failedProposalIDsStrs = append(failedProposalIDsStrs, proposalID.String())
}
return nil
for _, proposalID := range tx.ExpiredFailedProposalIDs {
failedProposalIDsStrs = append(failedProposalIDsStrs, proposalID.String())
}
if len(failedProposalIDsStrs) == 0 {
return nil
}
return ctx.Persist().FinishDACProposals(ctx.Ctx(), ctx.DB(), failedProposalIDsStrs, finishedAt, models.ProposalStatusFailed)
}

func (w *Writer) InsertDACVote(
Expand Down

0 comments on commit b226e07

Please sign in to comment.