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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
package test
import (
"Chain/pkg/block"
"Chain/pkg/blockchain/blockinfodatabase"
"Chain/pkg/blockchain/chainwriter"
"fmt"
"os"
)
// cleanUp removes any directories created by a test.
func cleanUp() {
removeBlockInfoDB()
removeCoinDB()
removeDataDB()
}
// removeCoinDB removes the coin database's level db.
func removeCoinDB() {
if _, err := os.Stat("./coindata"); !os.IsNotExist(err) {
if err2 := os.RemoveAll("./coindata"); err2 != nil {
fmt.Errorf("coudld not remove leveldb coindata")
}
}
}
// removeBlockInfoDB removes the block info database's level db.
func removeBlockInfoDB() {
if _, err := os.Stat("./blockinfodata"); !os.IsNotExist(err) {
if err2 := os.RemoveAll("./blockinfodata"); err2 != nil {
fmt.Errorf("coudld not remove leveldb blockinfodata")
}
}
}
//removeDataDB removes the chain writer's data directory.
func removeDataDB() {
if _, err := os.Stat("./data"); !os.IsNotExist(err) {
if err2 := os.RemoveAll("./data"); err2 != nil {
fmt.Errorf("coudld not remove directory data")
}
}
}
// MockedHeader returns a mocked Header.
func MockedHeader() *block.Header {
return &block.Header{
Version: 0,
PreviousHash: "",
MerkleRoot: "",
DifficultyTarget: "",
Nonce: 0,
Timestamp: 0,
}
}
// MockedBlockRecord returns a mocked BlockRecord.
func MockedBlockRecord() *blockinfodatabase.BlockRecord {
return &blockinfodatabase.BlockRecord{
Header: MockedHeader(),
Height: 0,
NumberOfTransactions: 0,
BlockFile: "./blockinfodata/block_0",
BlockStartOffset: 0,
BlockEndOffset: 10,
UndoFile: "",
UndoStartOffset: 0,
UndoEndOffset: 0,
}
}
// MockedTransactionInput returns a mocked TransactionInput.
func MockedTransactionInput() *block.TransactionInput {
return &block.TransactionInput{
ReferenceTransactionHash: "",
OutputIndex: 0,
UnlockingScript: "",
}
}
// MockedTransactionOutput returns a mocked TransactionOutput.
func MockedTransactionOutput() *block.TransactionOutput {
return &block.TransactionOutput{
Amount: 0,
LockingScript: "",
}
}
// MockedBlock returns a mocked Transaction.
func MockedTransaction() *block.Transaction {
return &block.Transaction{
Version: 0,
Inputs: []*block.TransactionInput{MockedTransactionInput()},
Outputs: []*block.TransactionOutput{MockedTransactionOutput()},
LockTime: 0,
}
}
// MockedBlock returns a mocked Block.
func MockedBlock() *block.Block {
return &block.Block{
Header: MockedHeader(),
Transactions: []*block.Transaction{MockedTransaction()},
}
}
// MockedUndoBlock returns a mocked UndoBlock.
func MockedUndoBlock() *chainwriter.UndoBlock {
return &chainwriter.UndoBlock{
TransactionInputHashes: []string{""},
OutputIndexes: []uint32{1},
Amounts: []uint32{1},
LockingScripts: []string{""},
}
}
//GenesisBlock returns the genesis block for testing purposes.
func GenesisBlock() *block.Block {
txo := &block.TransactionOutput{
Amount: 1_000_000_000,
LockingScript: "pubkey",
}
genTx := &block.Transaction{
Version: 0,
Inputs: nil,
Outputs: []*block.TransactionOutput{txo},
LockTime: 0,
}
return &block.Block{
Header: &block.Header{
Version: 0,
PreviousHash: "",
MerkleRoot: "",
DifficultyTarget: "",
Nonce: 0,
Timestamp: 0,
},
Transactions: []*block.Transaction{genTx},
}
}
// MakeBlockFromPrev creates a new Block from an existing Block,
// using the old Block's TransactionOutputs as TransactionInputs
// for the new Transaction.
func MakeBlockFromPrev(b *block.Block) *block.Block {
newHeader := &block.Header{
Version: 0,
PreviousHash: b.Hash(),
MerkleRoot: "",
DifficultyTarget: "",
Nonce: 0,
Timestamp: 0,
}
var transactions []*block.Transaction
for _, tx := range b.Transactions {
txHash := tx.Hash()
for i, txo := range tx.Outputs {
txi := &block.TransactionInput{
ReferenceTransactionHash: txHash,
OutputIndex: uint32(i),
UnlockingScript: "",
}
txo1 := &block.TransactionOutput{
Amount: txo.Amount / 2,
LockingScript: "",
}
tx1 := &block.Transaction{
Version: uint32(i),
Inputs: []*block.TransactionInput{txi},
Outputs: []*block.TransactionOutput{txo1},
LockTime: 0,
}
transactions = append(transactions, tx1)
}
}
return &block.Block{
Header: newHeader,
Transactions: transactions,
}
}
// UndoBlockFromBlock creates an UndoBlock from a Block.
// This function only works because we're not using inputs from
// other Blocks. It also does not actually take care of amounts
// or public keys.
func UndoBlockFromBlock(b *block.Block) *chainwriter.UndoBlock {
var transactionHashes []string
var outputIndexes []uint32
var amounts []uint32
var lockingScripts []string
for _, tx := range b.Transactions {
for _, txi := range tx.Inputs {
transactionHashes = append(transactionHashes, txi.ReferenceTransactionHash)
outputIndexes = append(outputIndexes, txi.OutputIndex)
amounts = append(amounts, 0)
lockingScripts = append(lockingScripts, "")
}
}
return &chainwriter.UndoBlock{
TransactionInputHashes: transactionHashes,
OutputIndexes: outputIndexes,
Amounts: amounts,
LockingScripts: lockingScripts,
}
}
|