Skip to content

Commit

Permalink
Merge pull request #41 from SiaFoundation/nate/update-store-interface
Browse files Browse the repository at this point in the history
Update store interface
  • Loading branch information
n8maninger authored Mar 29, 2024
2 parents 3fc21ab + 4656bc2 commit b6e48b9
Show file tree
Hide file tree
Showing 5 changed files with 597 additions and 312 deletions.
7 changes: 5 additions & 2 deletions testutil/network.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package testutil

import (
"time"

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

Expand Down Expand Up @@ -37,8 +40,8 @@ func MineBlock(cm *chain.Manager, minerAddress types.Address) types.Block {
Transactions: cm.PoolTransactions(),
MinerPayouts: []types.SiacoinOutput{{Address: minerAddress, Value: state.BlockReward().Add(minerFees)}},
}
for b.ID().CmpWork(state.ChildTarget) < 0 {
b.Nonce += state.NonceFactor()
if !coreutils.FindBlockNonce(state, &b, 5*time.Second) {
panic("failed to find nonce")
}
return b
}
77 changes: 33 additions & 44 deletions testutil/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ type (
EphemeralWalletStore struct {
privateKey types.PrivateKey

mu sync.Mutex
uncommitted []*chain.ApplyUpdate

mu sync.Mutex
tip types.ChainIndex
utxos map[types.SiacoinOutputID]wallet.SiacoinElement
utxos map[types.SiacoinOutputID]types.SiacoinElement
events []wallet.Event
}

Expand Down Expand Up @@ -51,27 +49,27 @@ func (et *ephemeralWalletUpdateTxn) AddEvents(events []wallet.Event) error {
return nil
}

func (et *ephemeralWalletUpdateTxn) AddSiacoinElements(elements []wallet.SiacoinElement) error {
for _, se := range elements {
func (et *ephemeralWalletUpdateTxn) ApplyIndex(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))
}
delete(et.store.utxos, types.SiacoinOutputID(se.ID))
}
// add siacoin elements
for _, se := range created {
if _, ok := et.store.utxos[types.SiacoinOutputID(se.ID)]; ok {
return fmt.Errorf("siacoin element %q already exists", se.ID)
continue
}
et.store.utxos[types.SiacoinOutputID(se.ID)] = se
}
return nil
}

func (et *ephemeralWalletUpdateTxn) RemoveSiacoinElements(ids []types.SiacoinOutputID) error {
for _, id := range ids {
if _, ok := et.store.utxos[id]; !ok {
return fmt.Errorf("siacoin element %q does not exist", id)
}
delete(et.store.utxos, id)
}
// add events
et.store.events = append(et.store.events, events...)
return nil
}

func (et *ephemeralWalletUpdateTxn) RevertIndex(index types.ChainIndex) error {
func (et *ephemeralWalletUpdateTxn) RevertIndex(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 @@ -83,11 +81,23 @@ func (et *ephemeralWalletUpdateTxn) RevertIndex(index types.ChainIndex) error {
et.store.events = filtered

// remove any siacoin elements that were added in the reverted block
for id, se := range et.store.utxos {
if se.Index == index {
delete(et.store.utxos, id)
}
for _, se := range removed {
delete(et.store.utxos, types.SiacoinOutputID(se.ID))
}

// readd any siacoin elements that were spent in the reverted block
for _, se := range unspent {
et.store.utxos[types.SiacoinOutputID(se.ID)] = se
}
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
}

Expand Down Expand Up @@ -122,7 +132,7 @@ func (es *EphemeralWalletStore) WalletEventCount() (uint64, error) {
}

// UnspentSiacoinElements returns the wallet's unspent siacoin outputs.
func (es *EphemeralWalletStore) UnspentSiacoinElements() (utxos []wallet.SiacoinElement, _ error) {
func (es *EphemeralWalletStore) UnspentSiacoinElements() (utxos []types.SiacoinElement, _ error) {
es.mu.Lock()
defer es.mu.Unlock()

Expand All @@ -139,32 +149,11 @@ func (es *EphemeralWalletStore) Tip() (types.ChainIndex, error) {
return es.tip, nil
}

// ProcessChainApplyUpdate implements chain.Subscriber.
func (es *EphemeralWalletStore) ProcessChainApplyUpdate(cau chain.ApplyUpdate) error {
es.mu.Lock()
defer es.mu.Unlock()
address := types.StandardUnlockHash(es.privateKey.PublicKey())
if err := wallet.ApplyChainUpdates(&ephemeralWalletUpdateTxn{store: es}, address, []chain.ApplyUpdate{cau}); err != nil {
return err
}
es.tip = cau.State.Index
return nil
}

// ProcessChainRevertUpdate implements chain.Subscriber.
func (es *EphemeralWalletStore) ProcessChainRevertUpdate(cru chain.RevertUpdate) error {
es.mu.Lock()
defer es.mu.Unlock()

address := types.StandardUnlockHash(es.privateKey.PublicKey())
return wallet.RevertChainUpdate(&ephemeralWalletUpdateTxn{store: es}, address, cru)
}

// NewEphemeralWalletStore returns a new EphemeralWalletStore.
func NewEphemeralWalletStore(pk types.PrivateKey) *EphemeralWalletStore {
return &EphemeralWalletStore{
privateKey: pk,

utxos: make(map[types.SiacoinOutputID]wallet.SiacoinElement),
utxos: make(map[types.SiacoinOutputID]types.SiacoinElement),
}
}
Loading

0 comments on commit b6e48b9

Please sign in to comment.