Skip to content

Commit

Permalink
stores: fix sqlite revert update failing
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Sep 19, 2024
1 parent cdf3592 commit 3665e5d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
46 changes: 46 additions & 0 deletions stores/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"go.sia.tech/core/types"
"go.sia.tech/coreutils/chain"
"go.sia.tech/coreutils/wallet"
"go.sia.tech/renterd/api"
"go.sia.tech/renterd/stores/sql"
)
Expand Down Expand Up @@ -208,4 +209,49 @@ func TestProcessChainUpdate(t *testing.T) {
if err := ss.ProcessChainUpdate(context.Background(), func(tx sql.ChainUpdateTx) error { return nil }); err != nil {
panic("oh no")
}

if err := ss.ProcessChainUpdate(context.Background(), func(tx sql.ChainUpdateTx) error {
index3 := types.ChainIndex{Height: 3}
index4 := types.ChainIndex{Height: 4}
created := []types.SiacoinElement{
{
StateElement: types.StateElement{},
SiacoinOutput: types.SiacoinOutput{},
MaturityHeight: 100,
},
}
events := []wallet.Event{
{
Type: wallet.EventTypeV2Transaction,
Data: wallet.EventV2Transaction{},
},
}

// create some elements
err := tx.WalletApplyIndex(index3, created, nil, events, time.Now())
if err != nil {
return err
}

// spend them
err = tx.WalletApplyIndex(index4, nil, created, events, time.Now())
if err != nil {
return err
}

// revert the spend
err = tx.WalletRevertIndex(index4, nil, created, time.Now())
if err != nil {
return err
}

// revert the creation
err = tx.WalletRevertIndex(index3, nil, created, time.Now())
if err != nil {
return err
}
return nil
}); err != nil {
t.Fatal("unexpected error", err)
}
}
2 changes: 1 addition & 1 deletion stores/sql/sqlite/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (c chainUpdateTx) WalletRevertIndex(index types.ChainIndex, removed, unspen
c.l.Debugw(fmt.Sprintf("recreate unspent output %v", e.ID), "height", index.Height, "block_id", index.ID)
if _, err := insertOutputStmt.Exec(c.ctx,
time.Now().UTC(),
e.ID,
ssql.Hash256(e.ID),
e.StateElement.LeafIndex,
ssql.MerkleProof{Hashes: e.StateElement.MerkleProof},
ssql.Currency(e.SiacoinOutput.Value),
Expand Down

0 comments on commit 3665e5d

Please sign in to comment.