-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.go
87 lines (67 loc) · 1.96 KB
/
index.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
package explorer
import (
"fmt"
"net/http"
"github.com/labstack/echo"
"github.com/phoreproject/synapse/chainhash"
)
// TransactionData is the data of a transaction that gets passed to templates.
type TransactionData struct {
Recipient string
Amount int64
Slot uint64
}
// ProposerData is the data of a proposer that gets passed to templates.
type ProposerData struct {
Slot uint64
Validator string
}
// IndexData is the data that gets sent to the index page.
type IndexData struct {
Blocks []BlockData
Transactions []TransactionData
}
func (ex *Explorer) renderIndex(c echo.Context) error {
blocks := make([]BlockData, 0)
var dbBlocks []Block
ex.database.database.Order("slot desc").Limit(30).Find(&dbBlocks)
for _, b := range dbBlocks {
var blockHash chainhash.Hash
copy(blockHash[:], b.Hash)
var proposerHash chainhash.Hash
copy(proposerHash[:], b.Proposer)
var parentBlockHash chainhash.Hash
copy(parentBlockHash[:], b.ParentBlockHash)
var stateRoot chainhash.Hash
copy(stateRoot[:], b.StateRoot)
blocks = append(blocks, BlockData{
Slot: b.Slot,
BlockHash: blockHash.String(),
ProposerHash: proposerHash.String(),
ParentBlock: parentBlockHash.String(),
StateRoot: stateRoot.String(),
RandaoReveal: fmt.Sprintf("%x", b.RandaoReveal),
Signature: fmt.Sprintf("%x", b.Signature),
})
}
transactions := make([]TransactionData, 0)
var dbTransactions []Transaction
ex.database.database.Select("recipient_hash, amount, slot").Order("slot desc").Limit(30).Find(&dbTransactions)
for _, t := range dbTransactions {
var recipientHash chainhash.Hash
copy(recipientHash[:], t.RecipientHash)
transactions = append(transactions, TransactionData{
Slot: t.Slot,
Amount: t.Amount,
Recipient: recipientHash.String(),
})
}
err := c.Render(http.StatusOK, "index.html", IndexData{
Blocks: blocks,
Transactions: transactions,
})
if err != nil {
return err
}
return err
}