-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblock.go
75 lines (63 loc) · 1.67 KB
/
block.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
package blockchain
import (
"fmt"
)
type Block struct {
// Request params.
Hash string
Index int64 `json:"block_index"`
Version int64 `json:"ver"`
PreviousBlock string `json:"prev_block"`
MerkelRoot string `json:"mrkl_root"`
Time int64
Bits int64
Fee int64
Nonce int64
TransactionCount int64 `json:"n_tx"`
Size int64
MainChain bool `json:"main_chain"`
Height int64
ReceivedTime int64 `json:"received_time"`
RelayedBy string `json:"relayed_by"`
Transactions []Transaction `json:"tx"`
}
func blockHashURL(hash string) string {
return fmt.Sprintf("%s/rawblock/%s", rootURL, hash)
}
func blockIndexURL(index int64) string {
return fmt.Sprintf("%s/rawblock/%d", rootURL, index)
}
func (b *Block) load(bc *BlockChain) error {
url := ""
if b.Hash != "" {
url = blockHashURL(b.Hash)
} else {
url = blockIndexURL(b.Index)
}
return bc.httpGetJSON(url, b)
}
type LatestBlock struct {
Hash string
Time int64
BlockIndex int64 `json:"block_index"`
Height int64
TransactionIndexes []int64 `json:"txIndexes"`
}
func latestBlockURL() string {
return fmt.Sprintf("%s/latestblock", rootURL)
}
func (b *LatestBlock) load(bc *BlockChain) error {
url := latestBlockURL()
return bc.httpGetJSON(url, b)
}
type BlockHeight struct {
Height int64 `json:"-"`
Blocks []*Block
}
func blockHeightURL(height int64) string {
return fmt.Sprintf("%s/block-height/%d?format=json", rootURL, height)
}
func (bh *BlockHeight) load(bc *BlockChain) error {
url := blockHeightURL(bh.Height)
return bc.httpGetJSON(url, bh)
}