-
Notifications
You must be signed in to change notification settings - Fork 128
/
explorer.go
541 lines (456 loc) · 17.9 KB
/
explorer.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// Copyright (c) 2018, The Decred developers
// Copyright (c) 2017, The dcrdata developers
// See LICENSE for details.
// package explorer handles the block explorer subsystem for generating the
// explorer pages.
package explorer
import (
"fmt"
"math"
"net/http"
"os"
"os/signal"
"reflect"
"runtime"
"sync"
"time"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/dcrjson"
"github.com/decred/dcrd/dcrutil"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrdata/blockdata"
"github.com/decred/dcrdata/db/dbtypes"
"github.com/decred/dcrdata/txhelpers"
humanize "github.com/dustin/go-humanize"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/rs/cors"
)
const (
maxExplorerRows = 400
minExplorerRows = 20
defaultAddressRows int64 = 20
MaxAddressRows int64 = 1000
MaxUnconfirmedPossible int64 = 1000
)
// explorerDataSourceLite implements an interface for collecting data for the
// explorer pages
type explorerDataSourceLite interface {
GetExplorerBlock(hash string) *BlockInfo
GetExplorerBlocks(start int, end int) []*BlockBasic
GetBlockHeight(hash string) (int64, error)
GetBlockHash(idx int64) (string, error)
GetExplorerTx(txid string) *TxInfo
GetExplorerAddress(address string, count, offset int64) *AddressInfo
DecodeRawTransaction(txhex string) (*dcrjson.TxRawResult, error)
SendRawTransaction(txhex string) (string, error)
GetHeight() int
GetChainParams() *chaincfg.Params
UnconfirmedTxnsForAddress(address string) (*txhelpers.AddressOutpoints, int64, error)
GetMempool() []MempoolTx
TxHeight(txid string) (height int64)
BlockSubsidy(height int64, voters uint16) *dcrjson.GetBlockSubsidyResult
GetSqliteChartsData() (map[string]*dbtypes.ChartsData, error)
}
// explorerDataSource implements extra data retrieval functions that require a
// faster solution than RPC, or additional functionality.
type explorerDataSource interface {
BlockHeight(hash string) (int64, error)
SpendingTransaction(fundingTx string, vout uint32) (string, uint32, int8, error)
SpendingTransactions(fundingTxID string) ([]string, []uint32, []uint32, error)
PoolStatusForTicket(txid string) (dbtypes.TicketSpendType, dbtypes.TicketPoolStatus, error)
AddressHistory(address string, N, offset int64, txnType dbtypes.AddrTxnType) ([]*dbtypes.AddressRow, *AddressBalance, error)
DevBalance() (*AddressBalance, error)
FillAddressTransactions(addrInfo *AddressInfo) error
BlockMissedVotes(blockHash string) ([]string, error)
AgendaVotes(agendaID string, chartType int) (*dbtypes.AgendaVoteChoices, error)
GetPgChartsData() (map[string]*dbtypes.ChartsData, error)
GetTicketsPriceByHeight() (*dbtypes.ChartsData, error)
SideChainBlocks() ([]*dbtypes.BlockStatus, error)
//SideChainTips() []*dbtypes.BlockStatus
BlockStatus(hash string) (dbtypes.BlockStatus, error)
GetOldestTxBlockTime(addr string) (int64, error)
TicketPoolVisualization(interval dbtypes.ChartGrouping) ([]*dbtypes.PoolTicketsData, *dbtypes.PoolTicketsData, error)
}
// cacheChartsData holds the prepopulated data that is used to draw the charts
var cacheChartsData = new(ChartDataCounter)
// GetChartTypeData is a thread-safe way to access the chart type data.
func GetChartTypeData(chartType string) (data *dbtypes.ChartsData, ok bool) {
cacheChartsData.RLock()
defer cacheChartsData.RUnlock()
data, ok = cacheChartsData.Data[chartType]
return
}
// TicketStatusText generates the text to display on the explorer's transaction
// page for the "POOL STATUS" field.
func TicketStatusText(s dbtypes.TicketSpendType, p dbtypes.TicketPoolStatus) string {
switch p {
case dbtypes.PoolStatusLive:
return "In Live Ticket Pool"
case dbtypes.PoolStatusVoted:
return "Voted"
case dbtypes.PoolStatusExpired:
switch s {
case dbtypes.TicketUnspent:
return "Expired, Unrevoked"
case dbtypes.TicketRevoked:
return "Expired, Revoked"
default:
return "invalid ticket state"
}
case dbtypes.PoolStatusMissed:
switch s {
case dbtypes.TicketUnspent:
return "Missed, Unrevoked"
case dbtypes.TicketRevoked:
return "Missed, Reevoked"
default:
return "invalid ticket state"
}
default:
return "Immature"
}
}
type explorerUI struct {
Mux *chi.Mux
blockData explorerDataSourceLite
explorerSource explorerDataSource
liteMode bool
devPrefetch bool
templates templates
wsHub *WebsocketHub
NewBlockDataMtx sync.RWMutex
NewBlockData *BlockBasic
ExtraInfo *HomeInfo
MempoolData *MempoolInfo
ChainParams *chaincfg.Params
Version string
NetName string
}
func (exp *explorerUI) reloadTemplates() error {
return exp.templates.reloadTemplates()
}
// See reloadsig*.go for an exported method
func (exp *explorerUI) reloadTemplatesSig(sig os.Signal) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, sig)
go func() {
for {
sigr := <-sigChan
log.Infof("Received %s", sig)
if sigr == sig {
if err := exp.reloadTemplates(); err != nil {
log.Error(err)
continue
}
log.Infof("Explorer UI html templates reparsed.")
}
}
}()
}
// StopWebsocketHub stops the websocket hub
func (exp *explorerUI) StopWebsocketHub() {
if exp == nil {
return
}
log.Info("Stopping websocket hub.")
exp.wsHub.Stop()
}
// New returns an initialized instance of explorerUI
func New(dataSource explorerDataSourceLite, primaryDataSource explorerDataSource,
useRealIP bool, appVersion string, devPrefetch bool) *explorerUI {
exp := new(explorerUI)
exp.Mux = chi.NewRouter()
exp.blockData = dataSource
exp.explorerSource = primaryDataSource
exp.MempoolData = new(MempoolInfo)
exp.Version = appVersion
exp.devPrefetch = devPrefetch
// explorerDataSource is an interface that could have a value of pointer
// type, and if either is nil this means lite mode.
if exp.explorerSource == nil || reflect.ValueOf(exp.explorerSource).IsNil() {
log.Debugf("Primary data source not available. Operating explorer in lite mode.")
exp.liteMode = true
}
if useRealIP {
exp.Mux.Use(middleware.RealIP)
}
params := exp.blockData.GetChainParams()
exp.ChainParams = params
exp.NetName = netName(exp.ChainParams)
// Development subsidy address of the current network
devSubsidyAddress, err := dbtypes.DevSubsidyAddress(params)
if err != nil {
log.Warnf("explorer.New: %v", err)
}
log.Debugf("Organization address: %s", devSubsidyAddress)
// Set default static values for ExtraInfo
exp.ExtraInfo = &HomeInfo{
DevAddress: devSubsidyAddress,
Params: ChainParams{
WindowSize: exp.ChainParams.StakeDiffWindowSize,
RewardWindowSize: exp.ChainParams.SubsidyReductionInterval,
BlockTime: exp.ChainParams.TargetTimePerBlock.Nanoseconds(),
MeanVotingBlocks: txhelpers.CalcMeanVotingBlocks(params),
},
PoolInfo: TicketPoolInfo{
Target: exp.ChainParams.TicketPoolSize * exp.ChainParams.TicketsPerBlock,
},
}
log.Infof("Mean Voting Blocks calculated: %d", exp.ExtraInfo.Params.MeanVotingBlocks)
noTemplateError := func(err error) *explorerUI {
log.Errorf("Unable to create new html template: %v", err)
return nil
}
tmpls := []string{"home", "explorer", "mempool", "block", "tx", "address",
"rawtx", "status", "parameters", "agenda", "agendas", "charts", "sidechains",
"ticketpool"}
tempDefaults := []string{"extras"}
exp.templates = newTemplates("views", tempDefaults, makeTemplateFuncMap(exp.ChainParams))
for _, name := range tmpls {
if err := exp.templates.addTemplate(name); err != nil {
return noTemplateError(err)
}
}
if !exp.liteMode {
exp.prePopulateChartsData()
}
exp.addRoutes()
exp.wsHub = NewWebsocketHub()
go exp.wsHub.run()
return exp
}
// Height returns the height of the current block data.
func (exp *explorerUI) Height() int64 {
exp.NewBlockDataMtx.RLock()
defer exp.NewBlockDataMtx.RUnlock()
return exp.NewBlockData.Height
}
// prePopulateChartsData should run in the background the first time the system
// is initialized and when new blocks are added.
func (exp *explorerUI) prePopulateChartsData() {
if exp.liteMode {
log.Warnf("Charts are not supported in lite mode!")
return
}
log.Info("Pre-populating the charts data. This may take a minute...")
var err error
pgData, err := exp.explorerSource.GetPgChartsData()
if err != nil {
log.Errorf("Invalid PG data found: %v", err)
return
}
sqliteData, err := exp.blockData.GetSqliteChartsData()
if err != nil {
log.Errorf("Invalid SQLite data found: %v", err)
return
}
for k, v := range sqliteData {
pgData[k] = v
}
cacheChartsData.Lock()
cacheChartsData.Data = pgData
cacheChartsData.Unlock()
log.Info("Done Pre-populating the charts data")
}
func (exp *explorerUI) Store(blockData *blockdata.BlockData, _ *wire.MsgBlock) error {
bData := blockData.ToBlockExplorerSummary()
// Update the charts data after every five blocks or if no charts data
// exists yet.
if !exp.liteMode && bData.Height%5 == 0 || len(cacheChartsData.Data) == 0 {
go exp.prePopulateChartsData()
}
// Lock for explorerUI's NewBlockData and ExtraInfo
exp.NewBlockDataMtx.Lock()
newBlockData := &BlockBasic{
Height: int64(bData.Height),
Hash: blockData.Header.Hash,
Voters: bData.Voters,
FreshStake: bData.FreshStake,
Size: int32(bData.Size),
Transactions: bData.TxLen,
BlockTime: bData.Time,
FormattedTime: bData.FormattedTime,
FormattedBytes: humanize.Bytes(uint64(bData.Size)),
Revocations: uint32(bData.Revocations),
}
exp.NewBlockData = newBlockData
bdHeight := newBlockData.Height
stakePerc := blockData.PoolInfo.Value / dcrutil.Amount(blockData.ExtraInfo.CoinSupply).ToCoin()
// Update all ExtraInfo with latest data
exp.ExtraInfo.CoinSupply = blockData.ExtraInfo.CoinSupply
exp.ExtraInfo.StakeDiff = blockData.CurrentStakeDiff.CurrentStakeDifficulty
exp.ExtraInfo.NextExpectedStakeDiff = blockData.EstStakeDiff.Expected
exp.ExtraInfo.NextExpectedBoundsMin = blockData.EstStakeDiff.Min
exp.ExtraInfo.NextExpectedBoundsMax = blockData.EstStakeDiff.Max
exp.ExtraInfo.IdxBlockInWindow = blockData.IdxBlockInWindow
exp.ExtraInfo.IdxInRewardWindow = int(bdHeight % exp.ChainParams.SubsidyReductionInterval)
exp.ExtraInfo.Difficulty = blockData.Header.Difficulty
exp.ExtraInfo.NBlockSubsidy.Dev = blockData.ExtraInfo.NextBlockSubsidy.Developer
exp.ExtraInfo.NBlockSubsidy.PoS = blockData.ExtraInfo.NextBlockSubsidy.PoS
exp.ExtraInfo.NBlockSubsidy.PoW = blockData.ExtraInfo.NextBlockSubsidy.PoW
exp.ExtraInfo.NBlockSubsidy.Total = blockData.ExtraInfo.NextBlockSubsidy.Total
exp.ExtraInfo.PoolInfo.Size = blockData.PoolInfo.Size
exp.ExtraInfo.PoolInfo.Value = blockData.PoolInfo.Value
exp.ExtraInfo.PoolInfo.ValAvg = blockData.PoolInfo.ValAvg
exp.ExtraInfo.PoolInfo.Percentage = stakePerc * 100
exp.ExtraInfo.PoolInfo.PercentTarget = 100 * float64(blockData.PoolInfo.Size) /
float64(exp.ChainParams.TicketPoolSize*exp.ChainParams.TicketsPerBlock)
posSubsPerVote := dcrutil.Amount(blockData.ExtraInfo.NextBlockSubsidy.PoS).ToCoin() /
float64(exp.ChainParams.TicketsPerBlock)
exp.ExtraInfo.TicketReward = 100 * posSubsPerVote /
blockData.CurrentStakeDiff.CurrentStakeDifficulty
// The actual reward of a ticket needs to also take into consideration the
// ticket maturity (time from ticket purchase until its eligible to vote)
// and coinbase maturity (time after vote until funds distributed to ticket
// holder are available to use).
avgSSTxToSSGenMaturity := exp.ExtraInfo.Params.MeanVotingBlocks +
int64(exp.ChainParams.TicketMaturity) +
int64(exp.ChainParams.CoinbaseMaturity)
exp.ExtraInfo.RewardPeriod = fmt.Sprintf("%.2f days", float64(avgSSTxToSSGenMaturity)*
exp.ChainParams.TargetTimePerBlock.Hours()/24)
exp.ExtraInfo.ASR, _ = exp.simulateASR(1000, false, stakePerc,
dcrutil.Amount(blockData.ExtraInfo.CoinSupply).ToCoin(),
float64(bdHeight),
blockData.CurrentStakeDiff.CurrentStakeDifficulty)
exp.NewBlockDataMtx.Unlock()
if !exp.liteMode && exp.devPrefetch {
go exp.updateDevFundBalance()
}
// Signal to the websocket hub that a new block was received, but do not
// block Store(), and do not hang forever in a goroutine waiting to send.
go func() {
select {
case exp.wsHub.HubRelay <- sigNewBlock:
case <-time.After(time.Second * 10):
log.Errorf("sigNewBlock send failed: Timeout waiting for WebsocketHub.")
}
}()
log.Debugf("Got new block %d for the explorer.", bdHeight)
return nil
}
func (exp *explorerUI) updateDevFundBalance() {
if exp.liteMode {
log.Warnf("Full balances not supported in lite mode.")
return
}
// yield processor to other goroutines
runtime.Gosched()
devBalance, err := exp.explorerSource.DevBalance()
if err == nil && devBalance != nil {
exp.NewBlockDataMtx.Lock()
exp.ExtraInfo.DevFund = devBalance.TotalUnspent
exp.NewBlockDataMtx.Unlock()
} else {
log.Errorf("explorerUI.updateDevFundBalance failed: %v", err)
}
}
func (exp *explorerUI) addRoutes() {
exp.Mux.Use(middleware.Logger)
exp.Mux.Use(middleware.Recoverer)
corsMW := cors.Default()
exp.Mux.Use(corsMW.Handler)
redirect := func(url string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
x := chi.URLParam(r, "x")
if x != "" {
x = "/" + x
}
http.Redirect(w, r, "/"+url+x, http.StatusPermanentRedirect)
}
}
exp.Mux.Get("/", redirect("blocks"))
exp.Mux.Get("/block/{x}", redirect("block"))
exp.Mux.Get("/tx/{x}", redirect("tx"))
exp.Mux.Get("/address/{x}", redirect("address"))
exp.Mux.Get("/decodetx", redirect("decodetx"))
}
// Simulate ticket purchase and re-investment over a full year for a given
// starting amount of DCR and calculation parameters. Generate a TEXT table of
// the simulation results that can optionally be used for future expansion of
// dcrdata functionality.
func (exp *explorerUI) simulateASR(StartingDCRBalance float64, IntegerTicketQty bool,
CurrentStakePercent float64, ActualCoinbase float64, CurrentBlockNum float64,
ActualTicketPrice float64) (ASR float64, ReturnTable string) {
// Calculations are only useful on mainnet. Short circuit calculations if
// on any other version of chain params.
if exp.ChainParams.Name != "mainnet" {
return 0, ""
}
BlocksPerDay := 86400 / exp.ChainParams.TargetTimePerBlock.Seconds()
BlocksPerYear := 365 * BlocksPerDay
TicketsPurchased := float64(0)
StakeRewardAtBlock := func(blocknum float64) float64 {
// Option 1: RPC Call
Subsidy := exp.blockData.BlockSubsidy(int64(blocknum), 1)
return dcrutil.Amount(Subsidy.PoS).ToCoin()
// Option 2: Calculation
// epoch := math.Floor(blocknum / float64(exp.ChainParams.SubsidyReductionInterval))
// RewardProportionPerVote := float64(exp.ChainParams.StakeRewardProportion) / (10 * float64(exp.ChainParams.TicketsPerBlock))
// return float64(RewardProportionPerVote) * dcrutil.Amount(exp.ChainParams.BaseSubsidy).ToCoin() *
// math.Pow(float64(exp.ChainParams.MulSubsidy)/float64(exp.ChainParams.DivSubsidy), epoch)
}
MaxCoinSupplyAtBlock := func(blocknum float64) float64 {
// 4th order poly best fit curve to Decred mainnet emissions plot.
// Curve fit was done with 0 Y intercept and Pre-Mine added after.
return (-9E-19*math.Pow(blocknum, 4) +
7E-12*math.Pow(blocknum, 3) -
2E-05*math.Pow(blocknum, 2) +
29.757*blocknum + 76963 +
1680000) // Premine 1.68M
}
CoinAdjustmentFactor := ActualCoinbase / MaxCoinSupplyAtBlock(CurrentBlockNum)
TheoreticalTicketPrice := func(blocknum float64) float64 {
ProjectedCoinsCirculating := MaxCoinSupplyAtBlock(blocknum) * CoinAdjustmentFactor * CurrentStakePercent
TicketPoolSize := (float64(exp.ExtraInfo.Params.MeanVotingBlocks) + float64(exp.ChainParams.TicketMaturity) +
float64(exp.ChainParams.CoinbaseMaturity)) * float64(exp.ChainParams.TicketsPerBlock)
return ProjectedCoinsCirculating / TicketPoolSize
}
TicketAdjustmentFactor := ActualTicketPrice / TheoreticalTicketPrice(CurrentBlockNum)
// Prepare for simulation
simblock := CurrentBlockNum
TicketPrice := ActualTicketPrice
DCRBalance := StartingDCRBalance
ReturnTable += fmt.Sprintf("\n\nBLOCKNUM DCR TICKETS TKT_PRICE TKT_REWRD ACTION\n")
ReturnTable += fmt.Sprintf("%8d %9.2f %8.1f %9.2f %9.2f INIT\n",
int64(simblock), DCRBalance, TicketsPurchased,
TicketPrice, StakeRewardAtBlock(simblock))
for simblock < (BlocksPerYear + CurrentBlockNum) {
// Simulate a Purchase on simblock
TicketPrice = TheoreticalTicketPrice(simblock) * TicketAdjustmentFactor
if IntegerTicketQty {
// Use this to simulate integer qtys of tickets up to max funds
TicketsPurchased = math.Floor(DCRBalance / TicketPrice)
} else {
// Use this to simulate ALL funds used to buy tickets - even fractional tickets
// which is actually not possible
TicketsPurchased = (DCRBalance / TicketPrice)
}
DCRBalance -= (TicketPrice * TicketsPurchased)
ReturnTable += fmt.Sprintf("%8d %9.2f %8.1f %9.2f %9.2f BUY\n",
int64(simblock), DCRBalance, TicketsPurchased,
TicketPrice, StakeRewardAtBlock(simblock))
// Move forward to average vote
simblock += (float64(exp.ChainParams.TicketMaturity) + float64(exp.ExtraInfo.Params.MeanVotingBlocks))
ReturnTable += fmt.Sprintf("%8d %9.2f %8.1f %9.2f %9.2f VOTE\n",
int64(simblock), DCRBalance, TicketsPurchased,
(TheoreticalTicketPrice(simblock) * TicketAdjustmentFactor), StakeRewardAtBlock(simblock))
// Simulate return of funds
DCRBalance += (TicketPrice * TicketsPurchased)
// Simulate reward
DCRBalance += (StakeRewardAtBlock(simblock) * TicketsPurchased)
TicketsPurchased = 0
// Move forward to coinbase maturity
simblock += float64(exp.ChainParams.CoinbaseMaturity)
ReturnTable += fmt.Sprintf("%8d %9.2f %8.1f %9.2f %9.2f REWARD\n",
int64(simblock), DCRBalance, TicketsPurchased,
(TheoreticalTicketPrice(simblock) * TicketAdjustmentFactor), StakeRewardAtBlock(simblock))
// Need to receive funds before we can use them again so add 1 block
simblock++
}
// Scale down to exactly 365 days
SimulationReward := ((DCRBalance - StartingDCRBalance) / StartingDCRBalance) * 100
ASR = (BlocksPerYear / (simblock - CurrentBlockNum)) * SimulationReward
ReturnTable += fmt.Sprintf("ASR over 365 Days is %.2f.\n", ASR)
return
}