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

sync: Add birth state. #5

Merged
merged 1 commit into from
Jun 19, 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
2 changes: 1 addition & 1 deletion asset/dcr/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dcr

import (
"decred.org/dcrwallet/v3/wallet"
"decred.org/dcrwallet/v4/wallet"
"github.com/decred/dcrd/chaincfg/v3"
)

Expand Down
14 changes: 12 additions & 2 deletions asset/dcr/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"path/filepath"
"time"

"decred.org/dcrwallet/v3/wallet"
_ "decred.org/dcrwallet/v3/wallet/drivers/bdb"
"decred.org/dcrwallet/v4/wallet"
_ "decred.org/dcrwallet/v4/wallet/drivers/bdb"
"decred.org/dcrwallet/v4/wallet/udb"
"github.com/decred/dcrd/hdkeychain/v3"
"github.com/decred/libwallet/asset"
)
Expand Down Expand Up @@ -100,6 +101,15 @@ func CreateWallet(ctx context.Context, params asset.CreateWalletParams, recovery
return nil, fmt.Errorf("wallet.Open error: %w", err)
}

birthState := &udb.BirthdayState{
Time: birthday.Add(time.Hour * -24),
SetFromTime: true,
}

if err := w.SetBirthState(ctx, birthState); err != nil {
return nil, fmt.Errorf("wallet.SetBirthState error: %w", err)
}

// Upgrade the coin type if this is not a wallet recovery. If it's a
// recovery, extend the internal and external address indices.
if recovery == nil {
Expand Down
10 changes: 5 additions & 5 deletions asset/dcr/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"fmt"
"sync/atomic"

"decred.org/dcrwallet/v3/chain"
"decred.org/dcrwallet/v3/p2p"
"decred.org/dcrwallet/v3/spv"
"decred.org/dcrwallet/v3/wallet"
"decred.org/dcrwallet/v3/wallet/udb"
"decred.org/dcrwallet/v4/chain"
"decred.org/dcrwallet/v4/p2p"
"decred.org/dcrwallet/v4/spv"
"decred.org/dcrwallet/v4/wallet"
"decred.org/dcrwallet/v4/wallet/udb"
"github.com/decred/dcrd/connmgr/v3"
"github.com/decred/libwallet/assetlog"
"github.com/decred/slog"
Expand Down
15 changes: 8 additions & 7 deletions asset/dcr/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"net"
"time"

"decred.org/dcrwallet/v3/p2p"
"decred.org/dcrwallet/v3/spv"
dcrwallet "decred.org/dcrwallet/v3/wallet"
"decred.org/dcrwallet/v4/p2p"
"decred.org/dcrwallet/v4/spv"
dcrwallet "decred.org/dcrwallet/v4/wallet"
"github.com/decred/dcrd/addrmgr/v2"
)

Expand Down Expand Up @@ -66,18 +66,19 @@ func (w *Wallet) StartSync(ctx context.Context, ntfns *spv.Notifications, connec

// IsSyncing returns true if the wallet is catching up to the mainchain's best
// block.
func (w *Wallet) IsSyncing() bool {
if w.IsSynced() {
func (w *Wallet) IsSyncing(ctx context.Context) bool {
if w.IsSynced(ctx) {
return false
}
return w.IsSyncingOrSynced()
}

// IsSynced returns true if the wallet has synced up to the best block on the
// mainchain.
func (w *Wallet) IsSynced() bool {
func (w *Wallet) IsSynced(ctx context.Context) bool {
if w.syncer != nil {
return w.syncer.Synced()
synced, _ := w.syncer.Synced(ctx)
return synced
}
return false
}
Expand Down
8 changes: 4 additions & 4 deletions asset/dcr/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"math"
"math/rand"

wallettypes "decred.org/dcrwallet/v3/rpc/jsonrpc/types"
"decred.org/dcrwallet/v3/wallet"
"decred.org/dcrwallet/v3/wallet/txauthor"
"decred.org/dcrwallet/v3/wallet/txsizes"
wallettypes "decred.org/dcrwallet/v4/rpc/jsonrpc/types"
"decred.org/dcrwallet/v4/wallet"
"decred.org/dcrwallet/v4/wallet/txauthor"
"decred.org/dcrwallet/v4/wallet/txsizes"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrutil/v4"
"github.com/decred/dcrd/txscript/v4"
Expand Down
4 changes: 2 additions & 2 deletions asset/dcr/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"os"

"decred.org/dcrwallet/v3/wallet"
"decred.org/dcrwallet/v3/wallet/udb"
"decred.org/dcrwallet/v4/wallet"
"decred.org/dcrwallet/v4/wallet/udb"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/libwallet/asset"
)
Expand Down
4 changes: 2 additions & 2 deletions asset/dcr/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"path/filepath"

"decred.org/dcrwallet/v3/spv"
"decred.org/dcrwallet/v3/wallet"
"decred.org/dcrwallet/v4/spv"
"decred.org/dcrwallet/v4/wallet"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/libwallet/asset"
"github.com/decred/slog"
Expand Down
8 changes: 4 additions & 4 deletions cgo/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/base64"
"encoding/json"

"decred.org/dcrwallet/v3/wallet/udb"
"decred.org/dcrwallet/v4/wallet/udb"
"github.com/decred/dcrd/txscript/v4/stdaddr"
)

Expand All @@ -17,7 +17,7 @@ func currentReceiveAddress(cName *C.char) *C.char {
}

// Don't return an address if not synced!
if !w.IsSynced() {
if !w.IsSynced(w.ctx) {
return errCResponseWithCode(ErrCodeNotSynced, "currentReceiveAddress requested on an unsynced wallet")
}

Expand All @@ -37,7 +37,7 @@ func newExternalAddress(cName *C.char) *C.char {
}

// Don't return an address if not synced!
if !w.IsSynced() {
if !w.IsSynced(w.ctx) {
return errCResponseWithCode(ErrCodeNotSynced, "newExternalAddress requested on an unsynced wallet")
}

Expand Down Expand Up @@ -95,7 +95,7 @@ func addresses(cName *C.char) *C.char {
}

// w.AddressesByAccount does not include the current address.
if w.IsSynced() {
if w.IsSynced(w.ctx) {
addr, err := w.CurrentAddress(udb.DefaultAccountNum)
if err != nil {
return errCResponse("w.CurrentAddress error: %v", err)
Expand Down
10 changes: 5 additions & 5 deletions cgo/cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
)

var (
ctx context.Context
cancelCtx context.CancelFunc
wg sync.WaitGroup
mainCtx context.Context
cancelMainCtx context.CancelFunc
wg sync.WaitGroup

logBackend *parentLogger
logMtx sync.RWMutex
Expand Down Expand Up @@ -58,7 +58,7 @@ func initialize(cLogDir *C.char) *C.char {
log.SetLevel(slog.LevelTrace)
logMtx.Unlock()

ctx, cancelCtx = context.WithCancel(context.Background())
mainCtx, cancelMainCtx = context.WithCancel(context.Background())

initialized = true
return successCResponse("libwallet cgo initialized")
Expand All @@ -82,7 +82,7 @@ func shutdown() *C.char {
wallets = make(map[string]*wallet)

// Stop all remaining background processes and wait for them to stop.
cancelCtx()
cancelMainCtx()
wg.Wait()

// Close the logger backend as the last step.
Expand Down
44 changes: 31 additions & 13 deletions cgo/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strconv"
"strings"

"decred.org/dcrwallet/v3/spv"
dcrwallet "decred.org/dcrwallet/v3/wallet"
"decred.org/dcrwallet/v4/spv"
dcrwallet "decred.org/dcrwallet/v4/wallet"
)

//export syncWallet
Expand Down Expand Up @@ -129,22 +129,14 @@ func syncWalletStatus(cName *C.char) *C.char {
var ssc, cfh, hh, rh, np = w.syncStatusCode, w.cfiltersHeight, w.headersHeight, w.rescanHeight, w.numPeers
w.syncStatusMtx.RUnlock()

nb, err := w.NetworkBackend()
if err != nil {
return errCResponse("unable to get network backend: %v", err)
}
spvSyncer, is := nb.(*spv.Syncer)
if !is {
return errCResponse("backend is not an spv syncer")
}
targetHeight := spvSyncer.EstimateMainChainTip(w.ctx)
_, targetHeight := w.MainWallet().MainChainTip(w.ctx)

// Sometimes it appears we miss a notification during start up. This is
// a bandaid to put us as synced in that case.
//
// TODO: Figure out why we would miss a notification.
w.syncStatusMtx.Lock()
if ssc != SSCComplete && w.IsSynced() && !w.rescanning {
if ssc != SSCComplete && w.IsSynced(w.ctx) && !w.rescanning {
ssc = SSCComplete
w.syncStatusCode = ssc
}
Expand Down Expand Up @@ -182,7 +174,7 @@ func rescanFromHeight(cName, cHeight *C.char) *C.char {
if !exists {
return errCResponse("wallet with name %q does not exist", name)
}
if !w.IsSynced() {
if !w.IsSynced(w.ctx) {
return errCResponseWithCode(ErrCodeNotSynced, "rescanFromHeight requested on an unsynced wallet")
}
w.syncStatusMtx.Lock()
Expand Down Expand Up @@ -227,3 +219,29 @@ func rescanFromHeight(cName, cHeight *C.char) *C.char {
}()
return successCResponse("rescan from height %d for wallet %q started", height, name)
}

//export birthState
func birthState(cName *C.char) *C.char {
w, ok := loadedWallet(cName)
if !ok {
return errCResponse("wallet with name %q is not loaded", goString(cName))
}

bs, err := w.MainWallet().BirthState(w.ctx)
if err != nil {
return errCResponse("wallet.BirthState error: %v", err)
}

bsRes := &BirthdayState{
Hash: bs.Hash.String(),
Height: bs.Height,
Time: bs.Time,
SetFromHeight: bs.SetFromHeight,
SetFromTime: bs.SetFromTime,
}
b, err := json.Marshal(bsRes)
if err != nil {
return errCResponse("unable to marshal birth state result: %v", err)
}
return successCResponse(string(b))
}
2 changes: 1 addition & 1 deletion cgo/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"math"
"strconv"

dcrwallet "decred.org/dcrwallet/v3/wallet"
dcrwallet "decred.org/dcrwallet/v4/wallet"
"github.com/decred/dcrd/txscript/v4/stdaddr"
"github.com/decred/libwallet/asset/dcr"
)
Expand Down
11 changes: 10 additions & 1 deletion cgo/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import "C"
import (
"encoding/json"
"fmt"
"time"

wallettypes "decred.org/dcrwallet/v3/rpc/jsonrpc/types"
wallettypes "decred.org/dcrwallet/v4/rpc/jsonrpc/types"
)

const (
Expand Down Expand Up @@ -137,3 +138,11 @@ type ListTransactionRes struct {
Time int64 `json:"time"`
TxID string `json:"txid"`
}

type BirthdayState struct {
Hash string `json:"hash"`
Height uint32 `json:"height"`
Time time.Time `json:"time"`
SetFromHeight bool `json:"setfromheight"`
SetFromTime bool `json:"setfromtime"`
}
6 changes: 3 additions & 3 deletions cgo/walletloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func createWallet(cName, cDataDir, cNet, cPass, cMnemonic *C.char) *C.char {
Birthday: birthday,
}
}
walletCtx, cancel := context.WithCancel(ctx)
walletCtx, cancel := context.WithCancel(mainCtx)

w, err := dcr.CreateWallet(walletCtx, params, recoveryConfig)
if err != nil {
Expand Down Expand Up @@ -117,7 +117,7 @@ func createWatchOnlyWallet(cName, cDataDir, cNet, cPub *C.char) *C.char {
},
}

walletCtx, cancel := context.WithCancel(ctx)
walletCtx, cancel := context.WithCancel(mainCtx)

w, err := dcr.CreateWatchOnlyWallet(walletCtx, goString(cPub), params)
if err != nil {
Expand Down Expand Up @@ -161,7 +161,7 @@ func loadWallet(cName, cDataDir, cNet *C.char) *C.char {
Logger: logger,
}

walletCtx, cancel := context.WithCancel(ctx)
walletCtx, cancel := context.WithCancel(mainCtx)

w, err := dcr.LoadWallet(walletCtx, params)
if err != nil {
Expand Down
Loading
Loading