Skip to content

Commit

Permalink
sync: Add birth state.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed May 29, 2024
1 parent 7419307 commit 1734cf9
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 105 deletions.
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(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(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(ctx) {
addr, err := w.CurrentAddress(udb.DefaultAccountNum)
if err != nil {
return errCResponse("w.CurrentAddress error: %v", err)
Expand Down
42 changes: 30 additions & 12 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(ctx)
_, targetHeight := w.MainWallet().MainChainTip(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(ctx) && !w.rescanning {
ssc = SSCComplete
w.syncStatusCode = ssc
}
Expand Down Expand Up @@ -228,3 +220,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(ctx)
if err != nil {
return errCResponse("wallet.BirthState error: %v", err)
}

bsRes := &BirthdayState{
Hash: bs.Hash,
Height: bs.Height,
Time: bs.Time,
SetFromHeight: bs.SetFromHeight,
SetFromTime: bs.SetFromTime,
}
b, err := json.Marshal(bsRes)
if err != nil {
return errCResponse("unable to 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
12 changes: 11 additions & 1 deletion cgo/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import "C"
import (
"encoding/json"
"fmt"
"time"

wallettypes "decred.org/dcrwallet/v3/rpc/jsonrpc/types"
wallettypes "decred.org/dcrwallet/v4/rpc/jsonrpc/types"
"github.com/decred/dcrd/chaincfg/chainhash"
)

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

type BirthdayState struct {
Hash chainhash.Hash `json:"hash"`
Height uint32 `json:"height"`
Time time.Time `json:"time"`
SetFromHeight bool `json:"setfromheight"`
SetFromTime bool `json:"setfromtime"`
}
44 changes: 23 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,51 @@ module github.com/decred/libwallet

require (
decred.org/dcrdex v0.0.0-20240415203145-79c4689fd164
decred.org/dcrwallet/v3 v3.1.0
decred.org/dcrwallet/v4 v4.1.0
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
github.com/decred/dcrd/addrmgr/v2 v2.0.2
github.com/decred/dcrd/addrmgr/v2 v2.0.4
github.com/decred/dcrd/chaincfg/chainhash v1.0.4
github.com/decred/dcrd/chaincfg/v3 v3.2.0
github.com/decred/dcrd/connmgr/v3 v3.1.1
github.com/decred/dcrd/dcrutil/v4 v4.0.1
github.com/decred/dcrd/hdkeychain/v3 v3.1.1
github.com/decred/dcrd/txscript/v4 v4.1.0
github.com/decred/dcrd/wire v1.6.0
github.com/decred/dcrd/chaincfg/v3 v3.2.1
github.com/decred/dcrd/connmgr/v3 v3.1.2
github.com/decred/dcrd/dcrutil/v4 v4.0.2
github.com/decred/dcrd/hdkeychain/v3 v3.1.2
github.com/decred/dcrd/txscript/v4 v4.1.1
github.com/decred/dcrd/wire v1.7.0
github.com/decred/slog v1.2.0
github.com/jrick/logrotate v1.0.0
github.com/kevinburke/nacl v0.0.0-20210405173606-cd9060f5f776
golang.org/x/crypto v0.7.0
golang.org/x/crypto v0.23.0
)

require (
decred.org/cspp/v2 v2.1.0 // indirect
decred.org/cspp/v2 v2.2.0 // indirect
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 // indirect
github.com/companyzero/sntrup4591761 v0.0.0-20220309191932-9e0f3af2f07a // indirect
github.com/dchest/siphash v1.2.3 // indirect
github.com/decred/base58 v1.0.5 // indirect
github.com/decred/dcrd/blockchain/stake/v5 v5.0.0 // indirect
github.com/decred/dcrd/blockchain/standalone/v2 v2.2.0 // indirect
github.com/decred/dcrd/blockchain/stake/v5 v5.0.1 // indirect
github.com/decred/dcrd/blockchain/standalone/v2 v2.2.1 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect
github.com/decred/dcrd/crypto/ripemd160 v1.0.2 // indirect
github.com/decred/dcrd/database/v3 v3.0.1 // indirect
github.com/decred/dcrd/database/v3 v3.0.2 // indirect
github.com/decred/dcrd/dcrec v1.0.1 // indirect
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.3 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/decred/dcrd/dcrjson/v4 v4.0.1 // indirect
github.com/decred/dcrd/gcs/v4 v4.0.0 // indirect
github.com/decred/dcrd/rpc/jsonrpc/types/v4 v4.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/decred/dcrd/dcrjson/v4 v4.1.0 // indirect
github.com/decred/dcrd/gcs/v4 v4.1.0 // indirect
github.com/decred/dcrd/mixing v0.2.0 // indirect
github.com/decred/dcrd/rpc/jsonrpc/types/v4 v4.3.0 // indirect
github.com/decred/go-socks v1.1.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/jrick/bitset v1.0.0 // indirect
github.com/jrick/wsrpc/v2 v2.3.5 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/stretchr/testify v1.8.2 // indirect
go.etcd.io/bbolt v1.3.8 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.12.0 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
lukechampine.com/blake3 v1.3.0 // indirect
)

go 1.19
Loading

0 comments on commit 1734cf9

Please sign in to comment.