-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
69 lines (62 loc) · 1.85 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
package chainstorage
import (
"github.com/KitchenMishap/pudding-shed/chainreadinterface"
"github.com/KitchenMishap/pudding-shed/indexedhashes"
)
type Block struct {
height int64
data *concreteReadableChain
}
// Functions to implement IBlockHandle as part of IBlock
func (block *Block) Height() int64 {
return block.height
}
func (block *Block) Hash() (indexedhashes.Sha256, error) {
hash := indexedhashes.Sha256{}
err := block.data.blkHashes.GetHashAtIndex(block.height, &hash)
return hash, err
}
func (block *Block) HeightSpecified() bool {
return true
}
func (block *Block) HashSpecified() bool {
return true
}
func (block *Block) IsBlockHandle() {}
func (block *Block) IsInvalid() bool {
return block.height == -1
}
// Functions to implement IBlock
func (block *Block) TransactionCount() (int64, error) {
blocksInChain, err := block.data.blkHashes.CountHashes()
if err != nil {
return -1, err
}
transInChain, err := block.data.trnHashes.CountHashes()
if err != nil {
return -1, err
}
blockFirstTransHeight, err := block.data.blkFirstTrans.ReadWordAt(block.height)
if err != nil {
return -1, err
}
nextBlockHeight := block.height + 1
if nextBlockHeight < blocksInChain {
nextBlockFirstTransHeight, err := block.data.blkFirstTrans.ReadWordAt(nextBlockHeight)
if err != nil {
return -1, err
}
return nextBlockFirstTransHeight - blockFirstTransHeight, nil
} else {
// There might not be a next block
return transInChain - blockFirstTransHeight, nil
}
}
func (block *Block) NthTransaction(n int64) (chainreadinterface.ITransHandle, error) {
blockFirstTransHeight, err := block.data.blkFirstTrans.ReadWordAt(block.height)
if err != nil {
return block.data.InvalidTrans(), err
}
transHeight := blockFirstTransHeight + n
return &TransHandle{HashHeight{height: transHeight, hashSpecified: false, heightSpecified: true}, block.data}, nil
}