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

multi: new treasury #1824

Merged
merged 2 commits into from May 10, 2021
Merged

multi: new treasury #1824

merged 2 commits into from May 10, 2021

Conversation

buck54321
Copy link
Member

@chappjc's #1821 plus some work on pagination. This is just a minimal implementation so we definitely have something that works for tonight. Work on charts and caches will follow, but can also be moved to separate PRs. I'd like to maybe get at least one chart up before tonight though. We'll see. Running out of time.

Comment on lines 57 to 65
SelectTreasuryBalance = `SELECT
COUNT(CASE WHEN tx_type=4 THEN 1 END) as tadds,
SUM(CASE WHEN tx_type=4 THEN value ELSE 0 END) AS spent,
COUNT(CASE WHEN tx_type=5 THEN 1 END) as tspends,
SUM(CASE WHEN tx_type=5 THEN -value ELSE 0 END) AS spent,
COUNT(CASE WHEN tx_type=6 THEN 1 END) as tbases,
SUM(CASE WHEN tx_type=6 THEN value ELSE 0 END) AS spent
FROM treasury
WHERE is_mainchain AND block_height <= $1;`
Copy link
Member

@chappjc chappjc May 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I might have gone with a GROUP BY (tx_type) for this, but yours does make the scan easier I think, and the cost is really not bad, so I like yours.

For ref

# SELECT tx_type, count(*), sum(value) FROM treasury WHERE is_mainchain AND block_height <= $1 GROUP BY tx_type;

 tx_type | count  |      sum      
---------+--------+---------------
       6 | 118285 | 1486404783273
       5 |      1 |    -100002550
       4 |      1 |    5000000000
(3 rows)

^ requires scanning N rows, and adjusting sign of tspends.

# SELECT                                                                                                            
                COUNT(CASE WHEN tx_type=4 THEN 1 END) as tadds,
                SUM(CASE WHEN tx_type=4 THEN value ELSE 0 END) AS spent,
                COUNT(CASE WHEN tx_type=5 THEN 1 END) as tspends,
                SUM(CASE WHEN tx_type=5 THEN -value ELSE 0 END) AS spent,
                COUNT(CASE WHEN tx_type=6 THEN 1 END) as tbases,
                SUM(CASE WHEN tx_type=6 THEN value ELSE 0 END) AS spent
                FROM treasury
                WHERE is_mainchain AND block_height <= 700000;
 tadds |   spent    | tspends |   spent   | tbases |     spent     
-------+------------+---------+-----------+--------+---------------
     1 | 5000000000 |       1 | 100002550 | 118285 | 1486404783273
(1 row)

Time: 32.017 ms

^ one row scan, fixed type positions, and no sign adjustment

Both have <1ms execution time and comparable planner costs (7092.52 for CASEs and 5326.68 for GROUP BYs, both quite small).

Just making a note of this in case we want to revisit it later.

Copy link
Member Author

@buck54321 buck54321 May 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where I'm at right now is

SelectTreasuryBalance = `SELECT
	tx_type,
	COUNT(CASE WHEN block_height <= $1 THEN 1 END),
	COUNT(CASE WHEN block_height > $1 THEN 1 END),
	SUM(CASE WHEN block_height <= $1 THEN value ELSE 0 END),
	SUM(CASE WHEN block_height > $1 THEN value ELSE 0 END)
	FROM treasury
	WHERE is_mainchain
	GROUP BY tx_type;`

Though looking at this now, I'm wondering if it would be faster to do

COUNT(CASE WHEN block_height <= $1 THEN 1 END),
COUNT(1),
SUM(CASE WHEN block_height <= $1 THEN value ELSE 0 END),
SUM(value)

and then take the difference in the caller.

@chappjc
Copy link
Member

chappjc commented May 7, 2021

Getting a bunch of divide by zeros with http://127.0.0.1:7777/treasury?n=0&start=0&txntype=tspend

http: panic serving 127.0.0.1:43498: runtime error: integer divide by zero
goroutine 575232 [running]:
net/http.(*conn).serve.func1(0xc019d84be0)
	/home/jon/go116/src/net/http/server.go:1824 +0x153
panic(0xfc1d40, 0x19dda60)
	/home/jon/go116/src/runtime/panic.go:971 +0x499
github.com/decred/dcrdata/cmd/dcrdata/explorer.calcPages(0x0, 0x0, 0x0, 0xc00cef82a0, 0x20, 0xc00cef82a0, 0x20, 0x0)
	/home/jon/github/decred/dcrdata/cmd/dcrdata/explorer/explorerroutes.go:2613 +0xd65
github.com/decred/dcrdata/cmd/dcrdata/explorer.(*explorerUI).TreasuryPage(0xc000398a00, 0x13fc690, 0xc03d4ef500, 0xc01b051c00)
	/home/jon/github/decred/dcrdata/cmd/dcrdata/explorer/explorerroutes.go:1446 +0x725
net/http.HandlerFunc.ServeHTTP(0xc01f05f570, 0x13fc690, 0xc03d4ef500, 0xc01b051c00)
	/home/jon/go116/src/net/http/server.go:2069 +0x44

I get that on mainnet where there are no treasury txnsn yet by changing the Type on the page, so it seems it just needs to replace 0 by something else

@chappjc
Copy link
Member

chappjc commented May 7, 2021

Getting this when paginating, with a spinning progress and gray table, but the numbers on the table do seem to update:

image

EDIT: Looks like I also get the above error when switching Type, at this.mergedMsgTarget.classList.add('d-hide') in fetchTable. The table updates, but the js bombs here.

@chappjc
Copy link
Member

chappjc commented May 7, 2021

I'm now wondering if when there are no tspends yet, as on mainnet, it should show All.

@buck54321
Copy link
Member Author

buck54321 commented May 7, 2021

I've changed it to all, but we're not showing unconfirmed yet, so I'll do that now.

@buck54321
Copy link
Member Author

Looking to get this in, since I didn't backup my old database. Lemme know when, and I'll squash my commits.

@chappjc
Copy link
Member

chappjc commented May 10, 2021

This is good now IMO. How's the txns counts over the pagination now? I haven't rechecked that with latest commits?

@buck54321
Copy link
Member Author

Seems to be a bug there still, but less noticeable now since there are more pages. Lemme see if I can chase it down. Almost like the tx count is being double somewhere.

@chappjc
Copy link
Member

chappjc commented May 10, 2021

Right, I think it was exactly doubled when there were only immature, and now it changed up, so I think it's got something to do with immature count.

@buck54321
Copy link
Member Author

0cd5038 fixes pagination. I'm gonna squash though.

chappjc and others added 2 commits May 10, 2021 16:50
Begin the treasury page.

Add db queries for treasury page.

Add links to legacy treasury.

Add TreasuryBalance to pageData.HomeInfo, update in Store.

Home page treasury balance is sum of legacy and decentralized.

Decode TSpend vote choices. Presenly only accessible via API
VoteChoices for a transaction.

Label old treasury address as *legacy*
Get pagination and typed txs working for the treasury page. Hide the
chart for now. Will follow up with caches and charts.

Default to 'all' treasury txn types while there are no tadds or tspends
on mainnet yet.

Display immature treasury balance.

Add i/o chart.

Add fiat conversion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants