Skip to content

Commit 896fc0d

Browse files
committed
Dump state to disk
1 parent d99216e commit 896fc0d

4 files changed

Lines changed: 29 additions & 2 deletions

File tree

appstate.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ package whatsmeow
99
import (
1010
"context"
1111
"encoding/hex"
12+
"encoding/json"
1213
"errors"
1314
"fmt"
15+
"os"
1416
"time"
1517

1618
"github.com/rs/zerolog"
19+
"go.mau.fi/util/exerrors"
1720
"go.mau.fi/util/exslices"
1821
"go.mau.fi/util/ptr"
1922

@@ -22,6 +25,7 @@ import (
2225
"go.mau.fi/whatsmeow/proto/waE2E"
2326
"go.mau.fi/whatsmeow/proto/waServerSync"
2427
"go.mau.fi/whatsmeow/store"
28+
"go.mau.fi/whatsmeow/store/sqlstore"
2529
"go.mau.fi/whatsmeow/types"
2630
"go.mau.fi/whatsmeow/types/events"
2731
)
@@ -39,6 +43,8 @@ func (cli *Client) FetchAppState(ctx context.Context, name appstate.WAPatchName,
3943
return nil
4044
}
4145

46+
var DumpAppStateToDisk = false
47+
4248
func (cli *Client) fetchAppState(ctx context.Context, name appstate.WAPatchName, fullSync, onlyIfNotSynced bool) ([]any, error) {
4349
if cli == nil {
4450
return nil, ErrClientIsNil
@@ -79,6 +85,17 @@ func (cli *Client) fetchAppState(ctx context.Context, name appstate.WAPatchName,
7985
} else if patches.Snapshot != nil && state != (appstate.HashState{}) {
8086
return nil, fmt.Errorf("unexpected non-empty input state (v%d) for %s when applying snapshot", state.Version, name)
8187
}
88+
if DumpAppStateToDisk {
89+
f := exerrors.Must(os.OpenFile(fmt.Sprintf("/tmp/appstate-%s-v%d.json", name, state.Version), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600))
90+
_ = json.NewEncoder(f).Encode(map[string]any{
91+
"name": name,
92+
"patches": patches.Patches,
93+
"snapshot": patches.Snapshot,
94+
"keys": exerrors.Must(cli.Store.AppStateKeys.GetAllAppStateSyncKeys(ctx)),
95+
"lids": exerrors.Must(cli.Store.LIDs.(*sqlstore.CachedLIDMap).GetAll(ctx)),
96+
})
97+
_ = f.Close()
98+
}
8299
wantSnapshot = false
83100
hasMore = patches.HasMorePatches
84101
state, err = cli.applyAppStatePatches(ctx, name, state, patches, fullSync, eventsToDispatchPtr)

store/sqlstore/lidmap.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"database/sql"
1313
"errors"
1414
"fmt"
15+
"maps"
1516
"slices"
1617
"strings"
1718
"sync"
@@ -59,6 +60,9 @@ const (
5960
func (s *CachedLIDMap) FillCache(ctx context.Context) error {
6061
s.lidCacheLock.Lock()
6162
defer s.lidCacheLock.Unlock()
63+
if s.cacheFilled {
64+
return nil
65+
}
6266
rows, err := s.db.Query(ctx, getAllLIDMappingsQuery)
6367
if err != nil {
6468
return err
@@ -68,6 +72,11 @@ func (s *CachedLIDMap) FillCache(ctx context.Context) error {
6872
return err
6973
}
7074

75+
func (s *CachedLIDMap) GetAll(ctx context.Context) (map[string]string, error) {
76+
err := s.FillCache(ctx)
77+
return maps.Clone(s.lidToPNCache), err
78+
}
79+
7180
func (s *CachedLIDMap) scanManyLids(rows dbutil.Rows, fn func(lid, pn string)) error {
7281
if fn == nil {
7382
fn = func(lid, pn string) {}

store/sqlstore/store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ const (
442442
SET key_data=excluded.key_data, timestamp=excluded.timestamp, fingerprint=excluded.fingerprint
443443
WHERE excluded.timestamp > whatsmeow_app_state_sync_keys.timestamp
444444
`
445-
getAllAppStateSyncKeysQuery = `SELECT key_data, timestamp, fingerprint FROM whatsmeow_app_state_sync_keys WHERE jid=$1 ORDER BY timestamp DESC`
445+
getAllAppStateSyncKeysQuery = `SELECT key_id, key_data, timestamp, fingerprint FROM whatsmeow_app_state_sync_keys WHERE jid=$1 ORDER BY timestamp DESC`
446446
getAppStateSyncKeyQuery = `SELECT key_data, timestamp, fingerprint FROM whatsmeow_app_state_sync_keys WHERE jid=$1 AND key_id=$2`
447447
getLatestAppStateSyncKeyIDQuery = `SELECT key_id FROM whatsmeow_app_state_sync_keys WHERE jid=$1 ORDER BY timestamp DESC LIMIT 1`
448448
)
@@ -460,7 +460,7 @@ func (s *SQLStore) GetAllAppStateSyncKeys(ctx context.Context) ([]*store.AppStat
460460
var out []*store.AppStateSyncKey
461461
for rows.Next() {
462462
var item store.AppStateSyncKey
463-
err = rows.Scan(&item.Data, &item.Timestamp, &item.Fingerprint)
463+
err = rows.Scan(&item.KeyID, &item.Data, &item.Timestamp, &item.Fingerprint)
464464
if err != nil {
465465
return nil, err
466466
}

store/store.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type SenderKeyStore interface {
5252
}
5353

5454
type AppStateSyncKey struct {
55+
KeyID []byte
5556
Data []byte
5657
Fingerprint []byte
5758
Timestamp int64

0 commit comments

Comments
 (0)