Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block timestamp to SingleAddressStore #75

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions testutil/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"slices"
"sort"
"sync"
"time"

"go.sia.tech/core/types"
"go.sia.tech/coreutils/wallet"
Expand Down Expand Up @@ -43,7 +44,7 @@ func (et *ephemeralWalletUpdateTxn) UpdateWalletStateElements(elements []types.S
return nil
}

func (et *ephemeralWalletUpdateTxn) WalletApplyIndex(index types.ChainIndex, created, spent []types.SiacoinElement, events []wallet.Event) error {
func (et *ephemeralWalletUpdateTxn) WalletApplyIndex(index types.ChainIndex, created, spent []types.SiacoinElement, events []wallet.Event, timestamp time.Time) error {
for _, se := range spent {
if _, ok := et.store.utxos[types.SiacoinOutputID(se.ID)]; !ok {
panic(fmt.Sprintf("siacoin element %q does not exist", se.ID))
Expand All @@ -64,7 +65,7 @@ func (et *ephemeralWalletUpdateTxn) WalletApplyIndex(index types.ChainIndex, cre
return nil
}

func (et *ephemeralWalletUpdateTxn) WalletRevertIndex(index types.ChainIndex, removed, unspent []types.SiacoinElement) error {
func (et *ephemeralWalletUpdateTxn) WalletRevertIndex(index types.ChainIndex, removed, unspent []types.SiacoinElement, timestamp time.Time) error {
// remove any events that were added in the reverted block
filtered := et.store.events[:0]
for i := range et.store.events {
Expand Down
13 changes: 9 additions & 4 deletions wallet/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wallet

import (
"fmt"
"time"

"go.sia.tech/core/types"
"go.sia.tech/coreutils/chain"
Expand Down Expand Up @@ -31,7 +32,9 @@ type (
// Any transactions and siacoin elements that were created by the index
// should be added and any siacoin elements that were spent should be
// removed.
WalletApplyIndex(index types.ChainIndex, created, spent []types.SiacoinElement, events []Event) error
//
// timestamp is the timestamp of the block being applied.
WalletApplyIndex(index types.ChainIndex, created, spent []types.SiacoinElement, events []Event, timestamp time.Time) error
// WalletRevertIndex is called with the chain index that is being reverted.
// Any transactions that were added by the index should be removed
//
Expand All @@ -41,7 +44,9 @@ type (
// unspent contains the siacoin elements that were spent and should be
// recreated. They are not necessarily created by the index and should
// not be associated with it.
WalletRevertIndex(index types.ChainIndex, removed, unspent []types.SiacoinElement) error
//
// timestamp is the timestamp of the block being reverted
WalletRevertIndex(index types.ChainIndex, removed, unspent []types.SiacoinElement, timestamp time.Time) error
}
)

Expand Down Expand Up @@ -315,7 +320,7 @@ func applyChainState(tx UpdateTx, address types.Address, cau chain.ApplyUpdate)
cau.UpdateElementProof(&stateElements[i])
}

if err := tx.WalletApplyIndex(cau.State.Index, createdUTXOs, spentUTXOs, appliedEvents(cau, address)); err != nil {
if err := tx.WalletApplyIndex(cau.State.Index, createdUTXOs, spentUTXOs, appliedEvents(cau, address), cau.Block.Timestamp); err != nil {
return fmt.Errorf("failed to apply index: %w", err)
} else if err := tx.UpdateWalletStateElements(stateElements); err != nil {
return fmt.Errorf("failed to update state elements: %w", err)
Expand All @@ -338,7 +343,7 @@ func revertChainUpdate(tx UpdateTx, revertedIndex types.ChainIndex, address type
})

// remove any existing events that were added in the reverted block
if err := tx.WalletRevertIndex(revertedIndex, removedUTXOs, unspentUTXOs); err != nil {
if err := tx.WalletRevertIndex(revertedIndex, removedUTXOs, unspentUTXOs, cru.Block.Timestamp); err != nil {
return fmt.Errorf("failed to revert block: %w", err)
}

Expand Down
Loading