def write_leveldbs(X, Y, SIM, name=''): leveldb.DestroyDB('../data/' + name) db = leveldb.LevelDB('../data/' + name, create_if_missing=True, error_if_exists=True, write_buffer_size=268435456) wb = leveldb.WriteBatch() num_items = X.shape[0] print '# samples : ', num_items X = X.reshape(num_items, 28, 28) Y = X.reshape(num_items, 28, 28) count = 0 for i in range(num_items): image1 = np.expand_dims(X[i, :, :], axis=0) image2 = np.expand_dims(Y[i, :, :], axis=0) image = np.vstack([image1, image2]) sim = SIM[i] # Load image into datum object datum = caffe.io.array_to_datum(image, sim) wb.Put('%08d_%s' % (count, file), datum.SerializeToString()) count = count + 1 if count % 1000 == 0: # Write batch of images to database db.Write(wb) del wb wb = leveldb.WriteBatch() print 'Processed %i images.' % count if count % 1000 != 0: # Write last batch of images db.Write(wb) print 'Processed a total of %i images.' % count else: print 'Processed a total of %i images.' % count
def test_block_genesis(self): """ 제네시스 블럭 생성 테스트 """ db_name = 'genesis_db' test_db = test_util.make_level_db(db_name) self.assertIsNotNone(test_db, "DB생성 불가") chain = BlockChain(test_db) self.assertIsNotNone(chain.last_block.block_hash, "제너릭 블럭 생성 불가") # 테스트 DB 제거 leveldb.DestroyDB(db_name)
def test_block_add(self): """ 블럭 추가 테스트 제네시스 블럭을 만든후 10개의 트랜잭션을 가진 10개의 블럭을 생성하여 블럭체인에 추가 """ db_name = 'add_test_db' test_db = test_util.make_level_db(db_name) self.assertIsNotNone(test_db, "DB생성 불가") chain = BlockChain(test_db) block = test_util.add_genesis_block() chain.add_block(block) genesis_hash = chain.last_block.block_hash for x in range(0, 10): # 신규 블럭 생성 추가 x 10 tmp_block = self.generate_test_block() tmp_block.block_status = BlockStatus.confirmed tmp_block.generate_block(chain.last_block) chain.add_block(tmp_block) logging.debug("신규 블럭 생성 및 블럭 HASH : %s", chain.last_block.block_hash) self.assertNotEqual(genesis_hash, chain.last_block.block_hash, "블럭 추가 불가") # 미인증 블럭 추가 tmp_block = self.generate_test_block() tmp_block.block_status = BlockStatus.unconfirmed self.assertRaises(TypeError, "미인증 블럭 추가", chain.add_block, tmp_block) # Store_data Function 추가 tmp_block.block_status = BlockStatus.confirmed tmp_block.generate_block(chain.last_block) # 블럭 저장함수 last_block_hash = chain.last_block.block_hash chain.add_block(tmp_block) self.assertNotEqual(last_block_hash, chain.last_block.block_hash) self.assertIsNotNone(chain.last_block) # 테스트 DB 제거 leveldb.DestroyDB(db_name)
def test_block_confirm(self): db_name = 'block_confirm_db' test_db = test_util.make_level_db(db_name) self.assertIsNotNone(test_db, "DB생성 불가") chain = BlockChain(test_db) # block을 하나 생성해서 unconfirm 상태에서 추가 unconfirm_block = self.generate_test_block() unconfirm_block.generate_block(chain.last_block) self.assertTrue(chain.add_unconfirm_block(unconfirm_block), "미검증블럭 추가에 실패하였습니다.") # 블럭 검증작업후 블럭을 검증완료 상태로 바꾸며, 블럭체인에 등록 해 줍니다. chain.confirm_block(unconfirm_block.block_hash) # 블럭 검증완료 self.assertEqual(chain.last_block.block_hash, unconfirm_block.block_hash, "블럭이 추가되지 않았습니다.") leveldb.DestroyDB(db_name)
def __init__(self, dbfile='./state'): leveldb.DestroyDB(dbfile) self.db = leveldb.LevelDB(dbfile)
def destroy(self, dbfile='./state'): leveldb.DestroyDB('./state')
def destroy(self): leveldb.DestroyDB(self.db_path)
def tearDown(self): # Blockchain을 삭제 leveldb.DestroyDB(self.db_name)
def remove_db(self) -> bool: try: leveldb.DestroyDB(self.total_path) return True except leveldb.LevelDBError: return False # error 발생
def tearDown(self): # Blockchain을 삭제 ObjectManager().peer_service = None leveldb.DestroyDB(self.db_name) os.system("rm -rf ./blockchain_db*")
def tearDownClass(cls): """ 테스트로 생성한 블럭체인 디비 제거 """ leveldb.DestroyDB(cls.test_block_db)
def remove_db(self) -> bool: try: leveldb.DestroyDB('{0}/{1}'.format(DB_PATH, self.name)) return True except leveldb.LevelDBError: return False # error 발생
genesis_header = [ 0, '', bin_sha256(rlp.encode([])), '', '', bin_sha256(rlp.encode([])), 2**36, 0, 0, '' ] genesis = [genesis_header, [], []] mainblk = Block(rlp.encode(genesis)) print 1 try: db = leveldb.LevelDB("objects") except: os.popen('cp -r objects pbjects').read() leveldb.DestroyDB("objects") os.popen('rm -r objects').read() os.popen('mv pbjects objects').read() leveldb.LevelDB("objects") print 2 def genaddr(seed): priv = bin_sha256(seed) addr = bin_sha256(privtopub(priv)[1:])[-20:] return priv, addr # For testing k1, a1 = genaddr("123") k2, a2 = genaddr("456")
def destroy_store(self): self.close() leveldb.DestroyDB(self._path)
def destroy_leveldb(db_path): leveldb.DestroyDB(db_path) Logger.info("Destroy leveldb: {}".format(db_path))
def _delete_db(self): import leveldb leveldb.DestroyDB(self.db_path)