Skip to content

Commit

Permalink
stores: update error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan authored and ChrisSchinnerl committed Sep 26, 2024
1 parent fac7b60 commit 04339fa
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
15 changes: 15 additions & 0 deletions stores/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,19 @@ func TestProcessChainUpdate(t *testing.T) {
}); !errors.Is(err, sql.ErrOutputNotFound) {
t.Fatal("expected ErrOutputNotFound", err)
}

// assert we can't apply an index and pass events with mismatching index
if err := ss.ProcessChainUpdate(context.Background(), func(tx sql.ChainUpdateTx) error {
return tx.WalletApplyIndex(types.ChainIndex{Height: 5}, nil, nil, []wallet.Event{
{
ID: types.Hash256{1},
Index: types.ChainIndex{Height: 6},
Type: wallet.EventTypeV2Transaction,
Data: wallet.EventV2Transaction{},
Timestamp: now,
},
}, now)
}); !errors.Is(err, sql.ErrIndexMissmatch) {
t.Fatal("expected ErrIndexMissmatch", err)
}
}
1 change: 1 addition & 0 deletions stores/sql/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

var (
ErrIndexMissmatch = errors.New("index missmatch")
ErrOutputNotFound = errors.New("output not found")
)

Expand Down
14 changes: 11 additions & 3 deletions stores/sql/mysql/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c chainUpdateTx) WalletApplyIndex(index types.ChainIndex, created, spent [
if res, err := deleteSpentStmt.Exec(c.ctx, ssql.Hash256(e.ID)); err != nil {
return fmt.Errorf("failed to delete spent output: %w", err)
} else if n, err := res.RowsAffected(); err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
return fmt.Errorf("failed to delete spent output: %w", err)
} else if n != 1 {
return fmt.Errorf("failed to delete spent output: %w", ssql.ErrOutputNotFound)
}
Expand Down Expand Up @@ -86,6 +86,14 @@ func (c chainUpdateTx) WalletApplyIndex(index types.ChainIndex, created, spent [

// insert new events
for _, e := range events {
if e.Index != index {
return fmt.Errorf("%w, event index %v != applied index %v", ssql.ErrIndexMissmatch, e.Index, index)
} else if e.ID == (types.Hash256{}) {
return fmt.Errorf("event id is required")
} else if e.Timestamp.IsZero() {
return fmt.Errorf("event timestamp is required")
}

c.l.Debugw(fmt.Sprintf("create event %v", e.ID), "height", index.Height, "block_id", index.ID)
data, err := json.Marshal(e.Data)
if err != nil {
Expand Down Expand Up @@ -132,7 +140,7 @@ func (c chainUpdateTx) WalletRevertIndex(index types.ChainIndex, removed, unspen
if res, err := deleteRemovedStmt.Exec(c.ctx, ssql.Hash256(e.ID)); err != nil {
return fmt.Errorf("failed to delete removed output: %w", err)
} else if n, err := res.RowsAffected(); err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
return fmt.Errorf("failed to delete removed output: %w", err)
} else if n != 1 {
return fmt.Errorf("failed to delete removed output: %w", ssql.ErrOutputNotFound)
}
Expand Down Expand Up @@ -169,7 +177,7 @@ func (c chainUpdateTx) WalletRevertIndex(index types.ChainIndex, removed, unspen
if err != nil {
return fmt.Errorf("failed to delete events: %w", err)
} else if n, err := res.RowsAffected(); err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
return fmt.Errorf("failed to delete events: %w", err)
} else if n > 0 {
c.l.Debugw(fmt.Sprintf("removed %d events", n), "height", index.Height, "block_id", index.ID)
}
Expand Down
10 changes: 6 additions & 4 deletions stores/sql/sqlite/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c chainUpdateTx) WalletApplyIndex(index types.ChainIndex, created, spent [
if res, err := deleteSpentStmt.Exec(c.ctx, ssql.Hash256(e.ID)); err != nil {
return fmt.Errorf("failed to delete spent output: %w", err)
} else if n, err := res.RowsAffected(); err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
return fmt.Errorf("failed to delete spent output: %w", err)
} else if n != 1 {
return fmt.Errorf("failed to delete spent output: %w", ssql.ErrOutputNotFound)
}
Expand Down Expand Up @@ -90,9 +90,11 @@ func (c chainUpdateTx) WalletApplyIndex(index types.ChainIndex, created, spent [
// insert new events
for _, e := range events {
if e.Index != index {
return fmt.Errorf("event index %v doesn't match index being applied %v", e.Index, index)
return fmt.Errorf("%w, event index %v != applied index %v", ssql.ErrIndexMissmatch, e.Index, index)
} else if e.ID == (types.Hash256{}) {
return fmt.Errorf("event id is required")
} else if e.Timestamp.IsZero() {
return fmt.Errorf("event timestamp is required")
}

c.l.Debugw(fmt.Sprintf("create event %v", e.ID), "height", index.Height, "block_id", index.ID)
Expand Down Expand Up @@ -141,7 +143,7 @@ func (c chainUpdateTx) WalletRevertIndex(index types.ChainIndex, removed, unspen
if res, err := deleteRemovedStmt.Exec(c.ctx, ssql.Hash256(e.ID)); err != nil {
return fmt.Errorf("failed to delete removed output: %w", err)
} else if n, err := res.RowsAffected(); err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
return fmt.Errorf("failed to delete removed output: %w", err)
} else if n != 1 {
return fmt.Errorf("failed to delete removed output: %w", ssql.ErrOutputNotFound)
}
Expand Down Expand Up @@ -178,7 +180,7 @@ func (c chainUpdateTx) WalletRevertIndex(index types.ChainIndex, removed, unspen
if err != nil {
return fmt.Errorf("failed to delete events: %w", err)
} else if n, err := res.RowsAffected(); err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
return fmt.Errorf("failed to delete events: %w", err)
} else if n > 0 {
c.l.Debugw(fmt.Sprintf("removed %d events", n), "height", index.Height, "block_id", index.ID)
}
Expand Down

0 comments on commit 04339fa

Please sign in to comment.