-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.qml
More file actions
131 lines (115 loc) · 3.73 KB
/
main.qml
File metadata and controls
131 lines (115 loc) · 3.73 KB
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
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import QtGraphicalEffects 1.0
Window {
visible: true
width: 1920
height: 1080
color: palette.backgroundColor
property alias pendingBlock: pendingBlockSetting.block
function mm(count) {
return count * Screen.pixelDensity
}
function validPow(id) {
return id.slice(0,3) === "000"
}
QtObject {
id: palette
property color backgroundColor: "#DAFED3"
property color foregroundColor: "#4BE22B"
property color lightForegroundColor: "#88F471"
property color blockColor: "#98F3E7"
property color blockBorderColor: "#24BEA8"
property color invalidBlockColor: "#FD7582"
property color invalidBlockBorderColor: "#F93043"
property color transactionColor: "#D4FB74"
property color transactionBorderColor: "#BCF52F"
property color blockchainColor: "#D4FB74"
property color blockchainBorderColor: "#BCF52F"
}
Timer {
id: miningTimer
interval: 1
running: true
repeat: true
onTriggered: {
if (validPow(pendingBlock.id)) {
transactionTimer.running = false
miningTimer.running = false
var finishedBlock = pendingBlock
finishedBlock.z = 2
pendingBlockSetting.createBlock()
pendingBlock.previous = finishedBlock.id
pendingBlock.blockHeight = finishedBlock.blockHeight + 1
blockchain.addBlock(finishedBlock)
transactionTimer.running = true
miningTimer.running = true
} else {
pendingBlock.nonce += 1
}
}
}
Timer {
id: transactionTimer
running: true
interval: 1000
onTriggered: {
pendingBlock.addTransaction(users.createTransaction())
interval = Math.random()*3000 + 1000
start()
}
}
Item {
anchors.fill: parent
focus: true
Keys.onEscapePressed: Qt.quit()
Keys.onPressed: {
if (event.key === Qt.Key_Q) Qt.quit()
else if (event.key === Qt.Key_M) miningTimer.running = !miningTimer.running
else if (event.key === Qt.Key_T) transactionTimer.running = !transactionTimer.running
}
}
RowLayout {
id: mainLayout
anchors.fill: parent
anchors.margins: mm(3)
ColumnLayout {
id: miningColumn
Layout.fillHeight: true
Layout.fillWidth: true
spacing: mm(3)
Item {
id: pendingBlockSetting
Layout.fillWidth: true
Layout.preferredHeight: mm(55)
property var block
Component.onCompleted: createBlock()
function createBlock() {
block = blockMaker.createObject(pendingBlockSetting)
}
Component {
id: blockMaker
Block {
setting: pendingBlockSetting
}
}
}
Text {
text: miningTimer.running? "Press M to stop mining" :
"Press M to start mining"
}
Text {
text: transactionTimer.running? "Press T to stop producing transactions"
: "Press T to start producing transactions"
}
Item { width: 1; Layout.fillHeight: true }
Users {
id: users
}
}
Blockchain {
id: blockchain
}
}
}