Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fromDict() 함수 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
G0Yang committed Aug 9, 2018
1 parent 948088b commit bda447c
Show file tree
Hide file tree
Showing 20 changed files with 151 additions and 67 deletions.
15 changes: 15 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"ExpandedNodes": [
"",
"\\chaincode",
"\\client",
"\\data_server",
"\\hash256",
"\\ledger",
"\\Stakeholder",
"\\synchronize",
"\\worldstate"
],
"SelectedNode": "\\Stakeholder\\Stakeholder_1.py",
"PreviewInSolutionExplorer": false
}
Binary file modified .vs/foodchain/v15/.suo
Binary file not shown.
Binary file modified .vs/slnx.sqlite
Binary file not shown.
2 changes: 1 addition & 1 deletion chaincode/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__all__ = ["blockToJson", "chianToJson", "transactionToJson", "server_Json", "randFileName", "client_Json"]
__all__ = ["blockToJson", "chianToJson", "transactionToJson", "randFileName"]
Binary file modified chaincode/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified chaincode/__pycache__/chainToJson.cpython-36.pyc
Binary file not shown.
26 changes: 19 additions & 7 deletions chaincode/blockToJson.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,28 @@ def saveJson(self):
else:
return True
return


def loadJson(self):
Data = None
try:
file = pathlib.Path(self.filename)
file_text = file.read_text(encoding='utf-8')
Data = json.loads(file_text)
except:
return False
else:
return Data
return False



if __name__ == "__main__":
t1 = transaction(이름 = "name", 타입 = "gogo")
t2 = transaction(이름 = "kimchi", 타입 = "take")
b1 = block(t1)
#b1.append(t2)
tj1 = blockToJson(filename = randFileName(), data = b1)
print(tj1.data.toDict())
tj1 = blockToJson(filename = "gn8gar3w665670.json")
tj1.data = tj1.loadJson()

b1 = block()
print("---------------------------------")
b1.fromDict(tj1.data)
print("---------------------------------")
print(b1.toDict())

26 changes: 8 additions & 18 deletions chaincode/chainToJson.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def __init__(self, *args, **kwargs):
print("filename is not None and data is None")
Data = self.loadJson()
print(type(Data))
ch = chain(filename = kwargs['filename'].split('.')[0])
ch.fromDict(Data)
self.data = ch

elif self.filename is not None and self.data is not None:
print("filename is not None and data is not None")
Expand Down Expand Up @@ -74,24 +77,11 @@ def append(self, *args, **kwargs):


if __name__ == "__main__":
t1 = transaction(이름 = "딸기", 타입 = "보냄")
b1 = block(t1)
c1 = chain(b1)


t2 = transaction(이름 = "딸기", 타입 = "받음")
b2 = block(t2)
c1.append(b2)


t3 = transaction(이름 = "딸기", 타입 = "다시 보냄")
t4 = transaction(이름 = "딸기", 타입 = "다시 받음")
b3 = block(t3)
b3.append(t4)
c1.append(b3)

ctj = chainToJson(filename = "8srkziel366214.json")
ctj.data = ctj.loadJson()

ctj = chainToJson(filename = c1.getCHID() + ".json", data = c1)
ch = chain()
ch.fromDict(Dict = ctj.data)

print("----------------------------------------")
ctj.append()
print(ch.toDict())
23 changes: 19 additions & 4 deletions chaincode/transactionToJson.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,27 @@ def saveJson(self):
return True
return


def loadJson(self):
Data = None
try:
file = pathlib.Path(self.filename)
file_text = file.read_text(encoding='utf-8')
Data = json.loads(file_text)
except:
return False
else:
return Data
return False



if __name__ == "__main__":
t1 = transaction(이름 = "kim")
tj1 = transactionToJson(filename = randFileName(), data = t1)
print(tj1.data.toDict())
tj1 = transactionToJson(filename = "52qp2i55919387.json")
tj1.data = tj1.loadJson()

t2 = transaction()
print(type(tj1.data))
t2.fromDict(Dict = tj1.data)

print(t2.toDict())

6 changes: 3 additions & 3 deletions client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ def run_server(self, port = 4000):

time.sleep(2)

flag = c2.generateTX(transaction.transaction("A", "B", "Kim", "010-1234-5678", "딸기", "딸기", "A등급", "군산", "15Kg")) # make TX
flag = c2.generateTX(transaction.transaction("A", "B", "Kim", "010-1234-5678", "딸기", "딸기", "A등급", "군산", "15Kg")) # make TX
flag = c2.generateTX(transaction.transaction("A", "B", "Kim", "010-1234-5678", "딸기", "딸기", "A등급", "군산", "15Kg")) # make TX
flag = c2.generateTX(transaction("A", "B", "Kim", "010-1234-5678", "딸기", "딸기", "A등급", "군산", "15Kg")) # make TX
flag = c2.generateTX(transaction("A", "B", "Kim", "010-1234-5678", "딸기", "딸기", "A등급", "군산", "15Kg")) # make TX
flag = c2.generateTX(transaction("A", "B", "Kim", "010-1234-5678", "딸기", "딸기", "A등급", "군산", "15Kg")) # make TX
print(flag)

time.sleep(2)
Expand Down
Binary file modified ledger/__pycache__/block.cpython-36.pyc
Binary file not shown.
Binary file modified ledger/__pycache__/blockheader.cpython-36.pyc
Binary file not shown.
Binary file modified ledger/__pycache__/chain.cpython-36.pyc
Binary file not shown.
Binary file modified ledger/__pycache__/transaction.cpython-36.pyc
Binary file not shown.
20 changes: 14 additions & 6 deletions ledger/block.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import time, sys, os
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))

#from ledger.transaction import transaction
from ledger.transaction import transaction
from ledger.blockheader import *
from hash256.hash256 import *

class block:
def __init__(self, *args, **kwargs):
self.transactionCount = 0
self.blockSize = 0
self.blocksize = 0
self.BH = blockheader()
self.BB = []
try:
Expand All @@ -19,7 +19,7 @@ def __init__(self, *args, **kwargs):
self.transactionCount = self.transactionCount + 1
self.BB[0].txCount = self.transactionCount
sizeof = str(self.toDict())
self.blockSize = len(sizeof)
self.blocksize = len(sizeof)
except:
print("init Error")
return
Expand All @@ -32,7 +32,7 @@ def append(self, Object):
self.transactionCount = self.transactionCount + 1
self.BB[-1].txCount = self.transactionCount

self.blockSize = len(str(self.toDict()))
self.blocksize = len(str(self.toDict()))
self.BH.setCurrentHash(hash256(str(self.toDict())).getHash())
else:
return False
Expand Down Expand Up @@ -62,7 +62,7 @@ def setPreviousHash(self, string):
def toDict(self):
Dict = {
'transactionCount' : self.transactionCount,
'blocksize' : self.blockSize,
'blocksize' : self.blocksize,
'blockheader' : self.BH.toDict()
}
txList = []
Expand All @@ -74,16 +74,24 @@ def toDict(self):
def fromDict(self, Dict):
try:
try : self.transactionCount = Dict['transactionCount']
try : self.blockSize = Dict['blockSize']
except: print("transactionCount error")

try : self.blocksize = Dict['blocksize']
except: print("blocksize error")

try :
bh = blockheader()
bh.fromDict(Dict = Dict['blockheader'])
self.BH = bh
except: print("blockheader error")

try:
for i in Dict['blockbody']:
tx = transaction()
tx.fromDict(Dict = i)
self.BB.append(tx)
except: print("blockbody error")

except:
return False
else:
Expand Down
15 changes: 10 additions & 5 deletions ledger/blockheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ def toDict(self):

def fromDict(self, Dict):
try:
self.version = Dict['version']
self.blockNumber = Dict['blockNumber']
self.timestamp = Dict['timestamp']
self.currentBlockHash = Dict['currentBlockHash']
self.previousBlockHash = Dict['previousBlockHash']
try: self.version = Dict['version']
except: print()
try: self.blockNumber = Dict['blockNumber']
except: print()
try: self.timestamp = Dict['timestamp']
except: print()
try: self.currentBlockHash = Dict['currentBlockHash']
except: print()
try: self.previousBlockHash = Dict['previousBlockHash']
except: print()
except:
return False
else:
Expand Down
7 changes: 6 additions & 1 deletion ledger/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,20 @@ def toDict(self):
Dict["chains"] = chList
return Dict

def fromFict(self, Dict):
def fromDict(self, Dict):
try:
try : self.CHID = Dict['CHID']
except: print()

try : self.C_Hash = Dict['C_Hash']
except: print()

try:
for i in Dict['chains']:
Block = block()
Block.fromDict(Dict = i)
self.chains.append(Block)
except: print()

except:
return False
Expand Down
50 changes: 40 additions & 10 deletions ledger/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
class transaction:
def __init__(self, *args, **kwargs):

self.flag = {}

# 제안서 부분
self.TXID = ""
self.timestamp = time.time()
Expand Down Expand Up @@ -84,42 +86,47 @@ def setAttribute(self, *args, **kwargs):
# self.endorsers[str(len(self.endorsers))] = kwargs['검증']
# self.N_state = kwargs['타입']
try : self.N_state = kwargs['타입']
except: print("타입 error")
except: self.flag['타입'] = False

try : self.M_name = kwargs['생산자']
except: print("생산자 error")
except: self.flag['생산자'] = False

try : self.M_phone = kwargs['전화번호']
except: print("전화번호 error")
except: self.flag['전화번호'] = False

try : self.endorsers[str(len(self.endorsers))] = kwargs['검증']
except: print("검증 error")
except: self.flag['검증'] = False

try : self.P_name = kwargs['이름']
except: print("이름 error")
except: self.flag['이름'] = False

try : self.P_From = kwargs['산지']
except: print("산지 error")
except: self.flag['산지'] = False

try : self.P_grade = kwargs['등급']
except: print("등급 error")
except: self.flag['등급'] = False

try : self.P_wight = kwargs['무게']
except: print("무게 error")
except: self.flag['무게'] = False

try : self.sign[str(len(self.sign))] = kwargs['사인']
except: print("사인 error")
except: self.flag['사인'] = False

try : self.creatorID = kwargs['서명']
except: print("서명 error")
except: self.flag['서명'] = False

self.T_hash = hash256(str(self.toDict())).getHash()

except:
print("error")
else:
if False in self.flag:
for i in self.flag:
print(i, "is error")
return True



return False

def toDict(self):
Expand Down Expand Up @@ -152,31 +159,54 @@ def toDict(self):
return Dict

def fromDict(self, Dict):
if type(Dict) is not type(dict()):
print("false")
return False
try:
try : self.TXID = Dict['TXID']
except: print()
try : self.timestamp = Dict['timestamp']
except: print()
try : self.verstion = Dict['verstion']
except: print()
try : self.creatorID = Dict['creatorID']
except: print()
try : self.TXType = Dict['TXType']
except: print()
try : self.timeout = Dict['timeout']
except: print()
try : self.T_hash = Dict['T_hash']
except: print()
try : self.txCount = Dict['txCount']
except: print()

try : self.endorsers = Dict['endorsers']
except: print()
try : self.sign = Dict['sign']
except: print()
try : self.produce = Dict['produce']
except: print()

try : self.P_name = Dict['P_name']
except: print()
try : self.P_From = Dict['P_From']
except: print()
try : self.P_grade = Dict['P_grade']
except: print()
try : self.P_wight = Dict['P_wight']
except: print()

try : self.M_name = Dict['M_name']
except: print()
try : self.M_phone = Dict['M_phone']
except: print()
try : self.M_com = Dict['M_com']
except: print()

try : self.N_state = Dict['N_state']
except: print()
try : self.N_else = Dict['N_else']
except: print()
except:
return False
else:
Expand Down
10 changes: 9 additions & 1 deletion redeme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@
-> 4�� �׸� : �� Ŭ�������� �ڱ� �ڽ��� ��ȣ�� �߰��Ͽ� �ش� ��ȣ�� �����Ͽ� �̸��� ������. <- ���� �������� �� ��� �׳� ����Ʈ �������� �Ѿ.
-> 6,7�� �׸� : 1�� ����
3. ���� �̸��� �������� �����Ͽ� ���ϰ� �ߺ��� ������ �۾�
-> �� �����̸��� randFileName() �Լ��� ����Ͽ� Ŭ�������� ch_, tx_, block_ ���ξ ����Ͽ� ������ ������.
-> �� �����̸��� randFileName() �Լ��� ����Ͽ� Ŭ�������� ch_, tx_, block_ ���ξ ����Ͽ� ������ ������.

2018.08.09 ��ġ��Ʈ
1. github private ���� ���� �Ϸ�
-> ���� ���� ���� ����
2. transaction, block, blockheader, chain�� fromDict() �Լ� �߰�
-> ���� ���� Dict�� �� Ŭ���� ������ ��ȯ�ϴ� �۾� �Ϸ�.
3. �� ���� ���� �� ��� ������
4. �� ���α׷������� ������ ������ �ʿ䰡 ����.
Loading

0 comments on commit bda447c

Please sign in to comment.