-
Notifications
You must be signed in to change notification settings - Fork 0
/
concretereadablechain.go
193 lines (181 loc) · 6.57 KB
/
concretereadablechain.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
package chainstorage
import (
"github.com/KitchenMishap/pudding-shed/chainreadinterface"
"github.com/KitchenMishap/pudding-shed/indexedhashes"
"github.com/KitchenMishap/pudding-shed/wordfile"
)
type concreteReadableChain struct {
blkFirstTrans wordfile.ReadAtWordCounter
blkHashes indexedhashes.HashReader
trnHashes indexedhashes.HashReader
trnFirstTxi wordfile.ReadAtWordCounter
trnFirstTxo wordfile.ReadAtWordCounter
txiTx wordfile.ReadAtWordCounter
txiVout wordfile.ReadAtWordCounter
txoSats wordfile.ReadAtWordCounter
}
// Functions to implement IBlockTree as part of IBlockChain
func (crc *concreteReadableChain) GenesisBlock() chainreadinterface.IBlockHandle {
return &BlockHandle{HashHeight{height: 0, hashSpecified: false, heightSpecified: true}, crc}
}
func (crc *concreteReadableChain) ParentBlock(block chainreadinterface.IBlockHandle) chainreadinterface.IBlockHandle {
if block.IsInvalid() {
return crc.InvalidBlock()
}
if !block.HeightSpecified() {
panic("this implementation of ParentBlock assumes block height is specified")
}
parentHeight := block.Height() - 1
return &BlockHandle{HashHeight{height: parentHeight, hashSpecified: false, heightSpecified: true}, crc}
}
func (crc *concreteReadableChain) GenesisTransaction() (chainreadinterface.ITransHandle, error) {
return &TransHandle{HashHeight{height: 0, hashSpecified: false, heightSpecified: true}, crc}, nil
}
func (crc *concreteReadableChain) PreviousTransaction(trans chainreadinterface.ITransHandle) chainreadinterface.ITransHandle {
if trans.IsInvalid() {
return crc.InvalidTrans()
}
if !trans.HeightSpecified() {
panic("this implementation of PreviousTransaction assumes trans height is specified")
}
prevHeight := trans.Height() - 1
return &TransHandle{HashHeight{height: prevHeight, hashSpecified: false, heightSpecified: true}, crc}
}
func (crc *concreteReadableChain) IsBlockTree() bool {
return false
}
func (crc *concreteReadableChain) BlockInterface(hBlock chainreadinterface.IBlockHandle) (chainreadinterface.IBlock, error) {
if hBlock.HeightSpecified() {
blockHeight := hBlock.Height()
return &Block{height: blockHeight, data: crc}, nil
} else if hBlock.HashSpecified() {
hash, err := hBlock.Hash()
if err != nil {
return &Block{}, err
}
blockHeight, err := crc.blkHashes.IndexOfHash(&hash)
if err != nil {
return &Block{}, err
}
return &Block{height: blockHeight, data: crc}, nil
} else {
panic("neither height nor hash was specified in BlockHandle")
}
}
func (crc *concreteReadableChain) TransInterface(hTrans chainreadinterface.ITransHandle) (chainreadinterface.ITransaction, error) {
if hTrans.HeightSpecified() {
transHeight := hTrans.Height()
return &Transaction{height: transHeight, data: crc}, nil
} else if hTrans.HashSpecified() {
hash, err := hTrans.Hash()
if err != nil {
return &Transaction{}, err
}
transHeight, err := crc.trnHashes.IndexOfHash(&hash)
if err != nil {
return &Transaction{}, err
}
return &Transaction{height: transHeight, data: crc}, nil
} else {
panic("neither height nor hash was specified in TransHandle")
}
}
func (crc *concreteReadableChain) TxiInterface(hTxi chainreadinterface.ITxiHandle) (chainreadinterface.ITxi, error) {
if hTxi.TxiHeightSpecified() {
return &Txi{height: hTxi.TxiHeight(), data: crc}, nil
} else if hTxi.ParentSpecified() {
hTrans := hTxi.ParentTrans()
trans, err := crc.TransInterface(hTrans)
if err != nil {
return &Txi{}, err
}
transFirstTxiHeight, error := crc.trnFirstTxi.ReadWordAt(trans.Height())
if error != nil {
return &Txi{}, err
}
index := hTxi.ParentIndex()
txiHeight := transFirstTxiHeight + index
return &Txi{height: txiHeight, data: crc}, nil
} else {
panic("hTxi specifies neither txiHeight nor parent")
}
}
func (crc *concreteReadableChain) TxoInterface(hTxo chainreadinterface.ITxoHandle) (chainreadinterface.ITxo, error) {
if hTxo.TxoHeightSpecified() {
return &Txo{height: hTxo.TxoHeight(), data: crc}, nil
} else if hTxo.ParentSpecified() {
hTrans := hTxo.ParentTrans()
trans, err := crc.TransInterface(hTrans)
if err != nil {
return &Txo{}, err
}
transFirstTxoHeight, err := crc.trnFirstTxo.ReadWordAt(trans.Height())
if err != nil {
return &Txo{}, err
}
index := hTxo.ParentIndex()
txoHeight := transFirstTxoHeight + index
return &Txo{height: txoHeight, data: crc}, nil
} else {
panic("hTxo specifies neither txoHeight nor parent")
}
}
// Functions to implement IBlockChain
func (crc *concreteReadableChain) InvalidBlock() chainreadinterface.IBlockHandle {
return &BlockHandle{HashHeight{height: -1, hashSpecified: false, heightSpecified: true}, crc}
}
func (crc *concreteReadableChain) InvalidTrans() chainreadinterface.ITransHandle {
return &TransHandle{HashHeight{height: -1, hashSpecified: false, heightSpecified: true}, crc}
}
func (crc *concreteReadableChain) LatestBlock() (chainreadinterface.IBlockHandle, error) {
blocksInChain, err := crc.blkHashes.CountHashes()
if err != nil || blocksInChain == 0 {
return crc.InvalidBlock(), err
}
return &BlockHandle{HashHeight{
height: blocksInChain - 1,
hash: indexedhashes.Sha256{},
heightSpecified: true,
hashSpecified: false,
}, crc}, nil
}
func (crc *concreteReadableChain) NextBlock(hBlock chainreadinterface.IBlockHandle) (chainreadinterface.IBlockHandle, error) {
givenBlockNum := hBlock.Height()
nextBlockNum := givenBlockNum + 1
blocksInChain, err := crc.blkHashes.CountHashes()
if err != nil || nextBlockNum >= blocksInChain {
return crc.InvalidBlock(), err
}
return &BlockHandle{HashHeight{
height: nextBlockNum,
hash: indexedhashes.Sha256{},
heightSpecified: true,
hashSpecified: false,
}, crc}, nil
}
func (crc *concreteReadableChain) LatestTransaction() (chainreadinterface.ITransHandle, error) {
transInChain, err := crc.trnHashes.CountHashes()
if err != nil || transInChain == 0 {
return crc.InvalidTrans(), err
}
return &TransHandle{HashHeight{
height: transInChain - 1,
hash: indexedhashes.Sha256{},
heightSpecified: true,
hashSpecified: false,
}, crc}, nil
}
func (crc *concreteReadableChain) NextTransaction(hTrans chainreadinterface.ITransHandle) (chainreadinterface.ITransHandle, error) {
givenTransNum := hTrans.Height()
nextTransNum := givenTransNum + 1
transInChain, err := crc.trnHashes.CountHashes()
if err != nil || nextTransNum >= transInChain {
return crc.InvalidTrans(), err
}
return &TransHandle{HashHeight{
height: nextTransNum,
hash: indexedhashes.Sha256{},
heightSpecified: true,
hashSpecified: false,
}, crc}, nil
}