Skip to content

Commit

Permalink
Merge pull request #73 from SiaFoundation/nate/update-core
Browse files Browse the repository at this point in the history
Refactor SingleAddressWallet store
  • Loading branch information
n8maninger authored Jul 25, 2024
2 parents bf92f53 + 11cc9d2 commit 531b658
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 62 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.8

require (
go.etcd.io/bbolt v1.3.10
go.sia.tech/core v0.4.1
go.sia.tech/core v0.4.2-0.20240723013228-2b1c3d890e25
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.25.0
lukechampine.com/frand v1.4.2
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.etcd.io/bbolt v1.3.10 h1:+BqfJTcCzTItrop8mq/lbzL8wSGtj94UO/3U31shqG0=
go.etcd.io/bbolt v1.3.10/go.mod h1:bK3UQLPJZly7IlNmV7uVHJDxfe5aK9Ll93e/74Y9oEQ=
go.sia.tech/core v0.4.0 h1:TlbVuiw1nk7wAybSvuZozRixnI4lpmcK0MVIlpJ9ApA=
go.sia.tech/core v0.4.0/go.mod h1:6dN3J2GDX+f8H2p82MJ7V4BFdnmgoHAiovfmBD/F1Hg=
go.sia.tech/core v0.4.1 h1:yawkyvr7mHYKWXa8RsHAPriLtJdvDQzqXgq4/hHqjHQ=
go.sia.tech/core v0.4.1/go.mod h1:6dN3J2GDX+f8H2p82MJ7V4BFdnmgoHAiovfmBD/F1Hg=
go.sia.tech/core v0.4.2-0.20240723013228-2b1c3d890e25 h1:VEldn/1zMNPdwv8wPimDk6V4D0UXUTU+CufSwQ0Cxug=
go.sia.tech/core v0.4.2-0.20240723013228-2b1c3d890e25/go.mod h1:6dN3J2GDX+f8H2p82MJ7V4BFdnmgoHAiovfmBD/F1Hg=
go.sia.tech/mux v1.2.0 h1:ofa1Us9mdymBbGMY2XH/lSpY8itFsKIo/Aq8zwe+GHU=
go.sia.tech/mux v1.2.0/go.mod h1:Yyo6wZelOYTyvrHmJZ6aQfRoer3o4xyKQ4NmQLJrBSo=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
Expand Down
17 changes: 7 additions & 10 deletions testutil/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"

"go.sia.tech/core/types"
"go.sia.tech/coreutils/chain"
"go.sia.tech/coreutils/wallet"
)

Expand Down Expand Up @@ -35,7 +34,7 @@ func (et *ephemeralWalletUpdateTxn) WalletStateElements() (elements []types.Stat
return
}

func (et *ephemeralWalletUpdateTxn) UpdateStateElements(elements []types.StateElement) error {
func (et *ephemeralWalletUpdateTxn) UpdateWalletStateElements(elements []types.StateElement) error {
for _, se := range elements {
utxo := et.store.utxos[types.SiacoinOutputID(se.ID)]
utxo.StateElement = se
Expand All @@ -44,7 +43,7 @@ func (et *ephemeralWalletUpdateTxn) UpdateStateElements(elements []types.StateEl
return nil
}

func (et *ephemeralWalletUpdateTxn) ApplyIndex(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) 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 @@ -61,10 +60,11 @@ func (et *ephemeralWalletUpdateTxn) ApplyIndex(index types.ChainIndex, created,

// add events
et.store.events = append(et.store.events, events...)
et.store.tip = index
return nil
}

func (et *ephemeralWalletUpdateTxn) RevertIndex(index types.ChainIndex, removed, unspent []types.SiacoinElement) error {
func (et *ephemeralWalletUpdateTxn) WalletRevertIndex(index types.ChainIndex, removed, unspent []types.SiacoinElement) error {
// remove any events that were added in the reverted block
filtered := et.store.events[:0]
for i := range et.store.events {
Expand All @@ -84,16 +84,13 @@ func (et *ephemeralWalletUpdateTxn) RevertIndex(index types.ChainIndex, removed,
for _, se := range unspent {
et.store.utxos[types.SiacoinOutputID(se.ID)] = se
}
et.store.tip = index
return nil
}

// UpdateChainState applies and reverts chain updates to the wallet.
func (es *EphemeralWalletStore) UpdateChainState(reverted []chain.RevertUpdate, applied []chain.ApplyUpdate) error {
if err := wallet.UpdateChainState(&ephemeralWalletUpdateTxn{store: es}, types.StandardUnlockHash(es.privateKey.PublicKey()), applied, reverted); err != nil {
return fmt.Errorf("failed to update chain state: %w", err)
}
es.tip = applied[len(applied)-1].State.Index
return nil
func (es *EphemeralWalletStore) UpdateChainState(fn func(ux wallet.UpdateTx) error) error {
return fn(&ephemeralWalletUpdateTxn{store: es})
}

// WalletEvents returns the wallet's events.
Expand Down
27 changes: 13 additions & 14 deletions wallet/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ type (
// WalletStateElements returns all state elements related to the wallet. It is used
// to update the proofs of all state elements affected by the update.
WalletStateElements() ([]types.StateElement, error)
// UpdateStateElements updates the proofs of all state elements affected by the
// UpdateWalletStateElements updates the proofs of all state elements affected by the
// update.
UpdateStateElements([]types.StateElement) error
UpdateWalletStateElements([]types.StateElement) error

// ApplyIndex is called with the chain index that is being applied.
// WalletApplyIndex is called with the chain index that is being applied.
// Any transactions and siacoin elements that were created by the index
// should be added and any siacoin elements that were spent should be
// removed.
ApplyIndex(index types.ChainIndex, created, spent []types.SiacoinElement, events []Event) error
// RevertIndex is called with the chain index that is being reverted.
WalletApplyIndex(index types.ChainIndex, created, spent []types.SiacoinElement, events []Event) error
// WalletRevertIndex is called with the chain index that is being reverted.
// Any transactions that were added by the index should be removed
//
// removed contains the siacoin elements that were created by the index
Expand All @@ -41,7 +41,7 @@ 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.
RevertIndex(index types.ChainIndex, removed, unspent []types.SiacoinElement) error
WalletRevertIndex(index types.ChainIndex, removed, unspent []types.SiacoinElement) error
}
)

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

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

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

Expand All @@ -352,30 +352,29 @@ func revertChainUpdate(tx UpdateTx, revertedIndex types.ChainIndex, address type
cru.UpdateElementProof(&stateElements[i])
}

if err := tx.UpdateStateElements(stateElements); err != nil {
if err := tx.UpdateWalletStateElements(stateElements); err != nil {
return fmt.Errorf("failed to update state elements: %w", err)
}
return nil
}

// UpdateChainState atomically applies and reverts chain updates to a single
// wallet store.
func UpdateChainState(tx UpdateTx, address types.Address, applied []chain.ApplyUpdate, reverted []chain.RevertUpdate) error {
func (sw *SingleAddressWallet) UpdateChainState(tx UpdateTx, reverted []chain.RevertUpdate, applied []chain.ApplyUpdate) error {
for _, cru := range reverted {
revertedIndex := types.ChainIndex{
ID: cru.Block.ID(),
Height: cru.State.Index.Height + 1,
}
if err := revertChainUpdate(tx, revertedIndex, address, cru); err != nil {
if err := revertChainUpdate(tx, revertedIndex, sw.addr, cru); err != nil {
return err
}
}

for _, cau := range applied {
if err := applyChainState(tx, address, cau); err != nil {
if err := applyChainState(tx, sw.addr, cau); err != nil {
return fmt.Errorf("failed to apply chain update %q: %w", cau.State.Index, err)
}
}

return nil
}
4 changes: 0 additions & 4 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"go.sia.tech/core/consensus"
"go.sia.tech/core/types"
"go.sia.tech/coreutils/chain"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -63,9 +62,6 @@ type (
// WalletEventCount returns the total number of events relevant to the
// wallet.
WalletEventCount() (uint64, error)

// UpdateChainState applies and reverts chain updates to the wallet.
UpdateChainState([]chain.RevertUpdate, []chain.ApplyUpdate) error
}

// A SingleAddressWallet is a hot wallet that manages the outputs controlled
Expand Down
Loading

0 comments on commit 531b658

Please sign in to comment.