forked from Onyx-Protocol/Onyx
/
reset.go
76 lines (68 loc) · 1.62 KB
/
reset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Package coreunsafe contains Core logic that is unsafe for
// production.
//
// It is used in the Developer Edition and in command-line
// utilities but shouldn't be used in production.
package coreunsafe
import (
"context"
"expvar"
"chain/database/pg"
"chain/errors"
)
func isProduction() bool {
bt := expvar.Get("buildtag")
return bt != nil && bt.String() != `"dev"`
}
// ResetBlockchain deletes all blockchain data, resulting in an
// unconfigured core. It does not delete access tokens or mockhsm
// keys.
func ResetBlockchain(ctx context.Context, db pg.DB) error {
if isProduction() {
// Shouldn't ever happen; This package shouldn't even be
// included in a production binary.
panic("reset called on production")
}
const q = `
TRUNCATE
account_control_programs,
account_utxos,
accounts,
annotated_accounts,
annotated_assets,
annotated_outputs,
annotated_txs,
asset_tags,
assets,
blocks,
config,
generator_pending_block,
leader,
pool_txs,
query_blocks,
reservations,
signed_blocks,
signers,
snapshots,
submitted_txs,
txfeeds
RESTART IDENTITY;
`
_, err := db.Exec(ctx, q)
return errors.Wrap(err)
}
// ResetEverything deletes all of a Core's data.
func ResetEverything(ctx context.Context, db pg.DB) error {
if isProduction() {
// Shouldn't ever happen; This package shouldn't even be
// included in a production binary.
panic("reset called on production")
}
err := ResetBlockchain(ctx, db)
if err != nil {
return errors.Wrap(err)
}
const q = `TRUNCATE mockhsm, access_tokens RESTART IDENTITY;`
_, err = db.Exec(ctx, q)
return errors.Wrap(err)
}