forked from daxeel/blockshell
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbscli.py
More file actions
172 lines (149 loc) Β· 4.87 KB
/
bscli.py
File metadata and controls
172 lines (149 loc) Β· 4.87 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
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
# -*- coding: utf-8 -*-
# ===================================================
# ==================== META DATA ===================
# ==================================================
__author__ = "Daxeel Soni"
__url__ = "https://daxeel.github.io"
__email__ = "daxeelsoni44@gmail.com"
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "Daxeel Soni"
# ==================================================
# ================= IMPORT MODULES =================
# ==================================================
import click
import urllib
import json
import sys
from blockchain.chain import Block, Blockchain
# ==================================================
# ===== SUPPORTED COMMANDS LIST IN BLOCKSHELL ======
# ==================================================
SUPPORTED_COMMANDS = [
'sendtx',
'mineblock',
'allblocks',
'mempool',
'getblock',
'help'
]
# Init blockchain
coin = Blockchain()
# Create group of commands
@click.group()
def cli():
"""
Create a group of commands for CLI
"""
pass
# ==================================================
# ============= BLOCKSHELL CLI COMMAND =============
# ==================================================
@cli.command()
@click.option("--difficulty", default=3, help="Define difficulty level of blockchain.")
def init(difficulty):
"""Initialize local blockchain"""
print("""
____ _ _ _____ _ _ _
| _ \ | | | | / ____| | | | | | |
| |_) | | | ___ ___ | | __ | (___ | |__ ___ | | | |
| _ < | | / _ \ / __| | |/ / \___ \ | '_ \ / _ \ | | | |
| |_) | | | | (_) | | (__ | < ____) | | | | | | __/ | | | |
|____/ |_| \___/ \___| |_|\_\ |_____/ |_| |_| \___| |_| |_|
> A command line utility for learning Blockchain concepts.
> Type 'help' to see supported commands.
> Project by Daxeel Soni, edits and additional features by Rustie Lin
""")
# Set difficulty of blockchain
coin.difficulty = difficulty
# Start blockshell shell
while True:
if sys.version_info[0] > 2:
cmd = input("[BlockShell] $ ")
else:
cmd = raw_input("[BlockShell] $ ")
processInput(cmd)
# Process input from Blockshell shell
def processInput(cmd):
"""
Method to process user input from Blockshell CLI.
"""
userCmd = cmd.split()[0]
if len(cmd) > 0:
if userCmd in SUPPORTED_COMMANDS:
globals()[userCmd](cmd)
else:
# error
msg = "Command not found. Try help command for documentation"
throwError(msg)
# ==================================================
# =========== BLOCKSHELL COMMAND METHODS ===========
# ==================================================
def sendtx(cmd):
"""
Send Transaction - Method to perform new transaction on blockchain.
"""
txData = cmd.split("sendtx ")[-1]
if "{" in txData:
txData = json.loads(txData)
print("Sending transaction...")
coin.addTx(txData)
def mineblock(cmd):
"""
Mine a block - Method to mine using PoW and add a block to the blockchain.
"""
indices = cmd.split()[1:]
data = []
try:
indices = [int(x) for x in indices]
indices.sort()
if len(indices) > 0:
while indices:
data.append(coin.mempool.pop(indices.pop()))
else:
while coin.mempool:
data.append(coin.mempool.pop())
coin.addBlock(Block(data))
except:
throwError("Invalid block indices")
def allblocks(cmd):
"""
Method to list all mined blocks.
"""
print("")
for eachBlock in coin.chain:
print(eachBlock.hash)
print("")
def mempool(cmd):
"""
Method to list all pending transactions in the mempool.
"""
print("")
for i in range(0, len(coin.mempool)):
print(i, coin.mempool[i])
print("")
def getblock(cmd):
"""
Method to fetch the details of block for given hash.
"""
blockHash = cmd.split(" ")[-1]
for eachBlock in coin.chain:
if eachBlock.hash == blockHash:
print("")
print(json.dumps(eachBlock.__dict__, indent=4, sort_keys=True))
print("")
def help(cmd):
"""
Method to display supported commands in Blockshell
"""
print("Commands:")
print(" sendtx <transaction data> Send new transaction")
print(" mineblocks [transaction indices] Adds transactions to a block and mines")
print(" allblocks Fetch all mined blocks in blockchain")
print(" mempool Fetch all pending transactions in the mempool")
print(" getblock <block hash> Fetch information about particular block")
def throwError(msg):
"""
Method to throw an error from Blockshell.
"""
print("Error : " + msg)