Skip to content

Commit

Permalink
Merge pull request #161 from SiaFoundation/nate/use-coreutils-wallet-…
Browse files Browse the repository at this point in the history
…types

Use coreutils wallet types
  • Loading branch information
n8maninger authored Aug 9, 2024
2 parents e56716f + ff6bc37 commit e549203
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 236 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ toolchain go1.22.3

require (
github.com/mattn/go-sqlite3 v1.14.22
go.sia.tech/core v0.4.2
go.sia.tech/coreutils v0.2.2
go.sia.tech/core v0.4.3
go.sia.tech/coreutils v0.2.5
go.sia.tech/jape v0.12.0
go.sia.tech/web/walletd v0.22.3
go.uber.org/zap v1.27.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ 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.2 h1:5VCRuRJAOy0cWwG32IGB0BXQAviXgKRfNXOiU0zSViM=
go.sia.tech/core v0.4.2/go.mod h1:cGfGNcyAq1k4oIOsrNpJV/Z/p+20/IMS6vIaofE8nr8=
go.sia.tech/coreutils v0.2.2 h1:WWbuk4lEd5SVB/LoBsPG6+heZN2olZT4yOkRZd3pvpg=
go.sia.tech/coreutils v0.2.2/go.mod h1:0D0NLh0c0pBUNKPoO/rDtyyRapB5j4/gfATNyQO67Rs=
go.sia.tech/core v0.4.3 h1:XEX7v6X8eJh4zyOkSHYi6FsyD+N/OEKw/NIigaaWPAU=
go.sia.tech/core v0.4.3/go.mod h1:cGfGNcyAq1k4oIOsrNpJV/Z/p+20/IMS6vIaofE8nr8=
go.sia.tech/coreutils v0.2.5 h1:oMnBGMBRfxhLzTH1ZDBg0Ep0QLE2GE1lND9yfzOzenA=
go.sia.tech/coreutils v0.2.5/go.mod h1:Pg9eE3xL25couNL/vYrtCWP5uXkVvC+SUcMVh1/E7+I=
go.sia.tech/jape v0.12.0 h1:13fBi7c5X8zxTQ05Cd9ZsIfRJgdvGoZqbEzH861z7BU=
go.sia.tech/jape v0.12.0/go.mod h1:wU+h6Wh5olDjkPXjF0tbZ1GDgoZ6VTi4naFw91yyWC4=
go.sia.tech/mux v1.2.0 h1:ofa1Us9mdymBbGMY2XH/lSpY8itFsKIo/Aq8zwe+GHU=
Expand Down
2 changes: 1 addition & 1 deletion persist/sqlite/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *Store) AddressEvents(address types.Address, offset, limit int) (events
if err != nil {
return fmt.Errorf("failed to scan event: %w", err)
}

event.Relevant = []types.Address{address}
events = append(events, event)
}
return rows.Err()
Expand Down
134 changes: 0 additions & 134 deletions wallet/events.go

This file was deleted.

74 changes: 53 additions & 21 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,44 @@ import (

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

// event types indicate the source of an event. Events can
// either be created by sending Siacoins between addresses or they can be
// created by consensus (e.g. a miner payout, a siafund claim, or a contract).
const (
EventTypeMinerPayout = wallet.EventTypeMinerPayout
EventTypeFoundationSubsidy = wallet.EventTypeFoundationSubsidy
EventTypeSiafundClaim = wallet.EventTypeSiafundClaim

EventTypeV1Transaction = wallet.EventTypeV1Transaction
EventTypeV1ContractResolution = wallet.EventTypeV1ContractResolution

EventTypeV2Transaction = wallet.EventTypeV2Transaction
EventTypeV2ContractResolution = wallet.EventTypeV2ContractResolution
)

type (
// An EventPayout represents a miner payout, siafund claim, or foundation
// subsidy.
EventPayout = wallet.EventPayout
// An EventV1Transaction pairs a v1 transaction with its spent siacoin and
// siafund elements.
EventV1Transaction = wallet.EventV1Transaction
// An EventV1ContractResolution represents a file contract payout from a v1
// contract.
EventV1ContractResolution = wallet.EventV1ContractResolution
// EventV2Transaction is a transaction event that includes the transaction
EventV2Transaction = wallet.EventV2Transaction
// An EventV2ContractResolution represents a file contract payout from a v2
// contract.
EventV2ContractResolution = wallet.EventV2ContractResolution

// EventData is the data associated with an event.
EventData = wallet.EventData
// An Event is a record of a consensus event that affects the wallet.
Event = wallet.Event
)

type (
Expand Down Expand Up @@ -93,7 +131,7 @@ func SignTransaction(cs consensus.State, txn *types.Transaction, sigIndex int, k

// AppliedEvents extracts a list of relevant events from a chain update.
func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant func(types.Address) bool) (events []Event) {
addEvent := func(id types.Hash256, maturityHeight uint64, eventType string, v EventData, relevant []types.Address) {
addEvent := func(id types.Hash256, maturityHeight uint64, eventType string, v wallet.EventData, relevant []types.Address) {
// dedup relevant addresses
seen := make(map[types.Address]bool)
unique := relevant[:0]
Expand Down Expand Up @@ -135,8 +173,6 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f
// collect all elements
sces := make(map[types.SiacoinOutputID]types.SiacoinElement)
sfes := make(map[types.SiafundOutputID]types.SiafundElement)
fces := make(map[types.FileContractID]types.FileContractElement)
v2fces := make(map[types.FileContractID]types.V2FileContractElement)
cu.ForEachSiacoinElement(func(sce types.SiacoinElement, _, _ bool) {
sce.MerkleProof = nil
sces[types.SiacoinOutputID(sce.ID)] = sce
Expand All @@ -145,19 +181,11 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f
sfe.MerkleProof = nil
sfes[types.SiafundOutputID(sfe.ID)] = sfe
})
cu.ForEachFileContractElement(func(fce types.FileContractElement, _ bool, rev *types.FileContractElement, resolved, valid bool) {
fce.MerkleProof = nil
fces[types.FileContractID(fce.ID)] = fce
})
cu.ForEachV2FileContractElement(func(fce types.V2FileContractElement, _ bool, rev *types.V2FileContractElement, res types.V2FileContractResolutionType) {
fce.MerkleProof = nil
v2fces[types.FileContractID(fce.ID)] = fce
})

// handle v1 transactions
for _, txn := range b.Transactions {
addresses := make(map[types.Address]bool)
e := &EventV1Transaction{
e := &wallet.EventV1Transaction{
Transaction: txn,
SpentSiacoinElements: make([]types.SiacoinElement, 0, len(txn.SiacoinInputs)),
SpentSiafundElements: make([]types.SiafundElement, 0, len(txn.SiafundInputs)),
Expand Down Expand Up @@ -193,7 +221,7 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f

sce, ok := sces[sfi.ParentID.ClaimOutputID()]
if ok && relevant(sce.SiacoinOutput.Address) {
addEvent(sce.ID, sce.MaturityHeight, EventTypeSiafundClaim, EventPayout{
addEvent(sce.ID, sce.MaturityHeight, EventTypeSiafundClaim, wallet.EventPayout{
SiacoinElement: sce,
}, []types.Address{sfi.ClaimAddress})
}
Expand Down Expand Up @@ -240,7 +268,7 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f

sce, ok := sces[types.SiafundOutputID(sfi.Parent.ID).V2ClaimOutputID()]
if ok && relevant(sfi.ClaimAddress) {
addEvent(sce.ID, sce.MaturityHeight, EventTypeSiafundClaim, EventPayout{
addEvent(sce.ID, sce.MaturityHeight, EventTypeSiafundClaim, wallet.EventPayout{
SiacoinElement: sce,
}, []types.Address{sfi.ClaimAddress})
}
Expand All @@ -257,7 +285,7 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f
continue
}

ev := EventV2Transaction(txn)
ev := wallet.EventV2Transaction(txn)
relevant := make([]types.Address, 0, len(addresses))
for addr := range addresses {
relevant = append(relevant, addr)
Expand All @@ -271,6 +299,8 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f
return
}

fce.MerkleProof = nil

if valid {
for i := range fce.FileContract.ValidProofOutputs {
address := fce.FileContract.ValidProofOutputs[i].Address
Expand All @@ -279,7 +309,7 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f
}

element := sces[types.FileContractID(fce.ID).ValidOutputID(i)]
addEvent(element.ID, element.MaturityHeight, EventTypeV1ContractResolution, EventV1ContractResolution{
addEvent(element.ID, element.MaturityHeight, EventTypeV1ContractResolution, wallet.EventV1ContractResolution{
Parent: fce,
SiacoinElement: element,
Missed: false,
Expand All @@ -293,7 +323,7 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f
}

element := sces[types.FileContractID(fce.ID).MissedOutputID(i)]
addEvent(element.ID, element.MaturityHeight, EventTypeV1ContractResolution, EventV1ContractResolution{
addEvent(element.ID, element.MaturityHeight, EventTypeV1ContractResolution, wallet.EventV1ContractResolution{
Parent: fce,
SiacoinElement: element,
Missed: true,
Expand All @@ -307,14 +337,16 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f
return
}

fce.MerkleProof = nil

var missed bool
if _, ok := res.(*types.V2FileContractExpiration); ok {
missed = true
}

if relevant(fce.V2FileContract.HostOutput.Address) {
element := sces[types.FileContractID(fce.ID).V2HostOutputID()]
addEvent(element.ID, element.MaturityHeight, EventTypeV2ContractResolution, EventV2ContractResolution{
addEvent(element.ID, element.MaturityHeight, EventTypeV2ContractResolution, wallet.EventV2ContractResolution{
Resolution: types.V2FileContractResolution{
Parent: fce,
Resolution: res,
Expand All @@ -326,7 +358,7 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f

if relevant(fce.V2FileContract.RenterOutput.Address) {
element := sces[types.FileContractID(fce.ID).V2RenterOutputID()]
addEvent(element.ID, element.MaturityHeight, EventTypeV2ContractResolution, EventV2ContractResolution{
addEvent(element.ID, element.MaturityHeight, EventTypeV2ContractResolution, wallet.EventV2ContractResolution{
Resolution: types.V2FileContractResolution{
Parent: fce,
Resolution: res,
Expand All @@ -341,7 +373,7 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f
for i := range b.MinerPayouts {
if relevant(b.MinerPayouts[i].Address) {
element := sces[cs.Index.ID.MinerOutputID(i)]
addEvent(element.ID, element.MaturityHeight, EventTypeMinerPayout, EventPayout{
addEvent(element.ID, element.MaturityHeight, EventTypeMinerPayout, wallet.EventPayout{
SiacoinElement: element,
}, []types.Address{b.MinerPayouts[i].Address})
}
Expand All @@ -351,7 +383,7 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f
if relevant(cs.FoundationPrimaryAddress) {
element, ok := sces[cs.Index.ID.FoundationOutputID()]
if ok {
addEvent(element.ID, element.MaturityHeight, EventTypeFoundationSubsidy, EventPayout{
addEvent(element.ID, element.MaturityHeight, EventTypeFoundationSubsidy, wallet.EventPayout{
SiacoinElement: element,
}, []types.Address{element.SiacoinOutput.Address})
}
Expand Down
Loading

0 comments on commit e549203

Please sign in to comment.