mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Merge branch 'tendermint' into write-transfer-test
This commit is contained in:
commit
f4a6b863fe
@ -28,8 +28,8 @@ class App(BaseApplication):
|
|||||||
def init_chain(self, validators):
|
def init_chain(self, validators):
|
||||||
"""Initialize chain with block of height 0"""
|
"""Initialize chain with block of height 0"""
|
||||||
|
|
||||||
block = Block(hash='', height=0)
|
block = Block(app_hash='', height=0)
|
||||||
self.bigchaindb.store_block(block.to_dict())
|
self.bigchaindb.store_block(block._asdict())
|
||||||
|
|
||||||
def info(self):
|
def info(self):
|
||||||
"""Return height of the latest committed block."""
|
"""Return height of the latest committed block."""
|
||||||
@ -38,7 +38,7 @@ class App(BaseApplication):
|
|||||||
block = self.bigchaindb.get_latest_block()
|
block = self.bigchaindb.get_latest_block()
|
||||||
if block:
|
if block:
|
||||||
r.last_block_height = block['height']
|
r.last_block_height = block['height']
|
||||||
r.last_block_app_hash = block['hash'].encode('utf-8')
|
r.last_block_app_hash = block['app_hash'].encode('utf-8')
|
||||||
else:
|
else:
|
||||||
r.last_block_height = 0
|
r.last_block_height = 0
|
||||||
r.last_block_app_hash = b''
|
r.last_block_app_hash = b''
|
||||||
@ -89,10 +89,9 @@ class App(BaseApplication):
|
|||||||
block = self.bigchaindb.get_latest_block()
|
block = self.bigchaindb.get_latest_block()
|
||||||
|
|
||||||
if self.block_txn_ids:
|
if self.block_txn_ids:
|
||||||
self.block_txn_hash = calculate_hash([block['hash'],
|
self.block_txn_hash = calculate_hash([block['app_hash'], block_txn_hash])
|
||||||
block_txn_hash])
|
|
||||||
else:
|
else:
|
||||||
self.block_txn_hash = block['hash']
|
self.block_txn_hash = block['app_hash']
|
||||||
|
|
||||||
return ResponseEndBlock()
|
return ResponseEndBlock()
|
||||||
|
|
||||||
@ -101,8 +100,8 @@ class App(BaseApplication):
|
|||||||
|
|
||||||
# register a new block only when new transactions are received
|
# register a new block only when new transactions are received
|
||||||
if self.block_txn_ids:
|
if self.block_txn_ids:
|
||||||
block = Block(hash=self.block_txn_hash, height=self.new_height)
|
block = Block(app_hash=self.block_txn_hash, height=self.new_height)
|
||||||
self.bigchaindb.store_block(block.to_dict())
|
self.bigchaindb.store_block(block._asdict())
|
||||||
|
|
||||||
data = self.block_txn_hash.encode('utf-8')
|
data = self.block_txn_hash.encode('utf-8')
|
||||||
return Result.ok(data=data)
|
return Result.ok(data=data)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
import logging
|
import logging
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
@ -114,13 +115,4 @@ class BigchainDB(Bigchain):
|
|||||||
return transaction
|
return transaction
|
||||||
|
|
||||||
|
|
||||||
class Block(object):
|
Block = namedtuple('Block', ('app_hash', 'height'))
|
||||||
|
|
||||||
def __init__(self, hash='', height=0):
|
|
||||||
self.hash = hash
|
|
||||||
self.height = height
|
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
block = {'hash': self.hash,
|
|
||||||
'height': self.height}
|
|
||||||
return block
|
|
||||||
|
@ -1,19 +1,17 @@
|
|||||||
import json
|
import json
|
||||||
import pytest
|
|
||||||
|
from abci.server import ProtocolHandler
|
||||||
|
from io import BytesIO
|
||||||
|
import abci.types_pb2 as types
|
||||||
|
from abci.wire import read_message
|
||||||
|
from abci.messages import to_request_deliver_tx, to_request_check_tx
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.bdb
|
|
||||||
@pytest.mark.tapp
|
|
||||||
def test_app(b):
|
def test_app(b):
|
||||||
from bigchaindb.tendermint import App
|
from bigchaindb.tendermint import App
|
||||||
from bigchaindb.tendermint.utils import calculate_hash
|
from bigchaindb.tendermint.utils import calculate_hash
|
||||||
from abci.server import ProtocolHandler
|
|
||||||
from io import BytesIO
|
|
||||||
import abci.types_pb2 as types
|
|
||||||
from abci.wire import read_message
|
|
||||||
from bigchaindb.common.crypto import generate_key_pair
|
from bigchaindb.common.crypto import generate_key_pair
|
||||||
from bigchaindb.models import Transaction
|
from bigchaindb.models import Transaction
|
||||||
from abci.messages import to_request_deliver_tx, to_request_check_tx
|
|
||||||
|
|
||||||
app = App(b)
|
app = App(b)
|
||||||
p = ProtocolHandler(app)
|
p = ProtocolHandler(app)
|
||||||
@ -29,7 +27,7 @@ def test_app(b):
|
|||||||
block0 = b.get_latest_block()
|
block0 = b.get_latest_block()
|
||||||
assert block0
|
assert block0
|
||||||
assert block0['height'] == 0
|
assert block0['height'] == 0
|
||||||
assert block0['hash'] == ''
|
assert block0['app_hash'] == ''
|
||||||
|
|
||||||
alice = generate_key_pair()
|
alice = generate_key_pair()
|
||||||
bob = generate_key_pair()
|
bob = generate_key_pair()
|
||||||
@ -64,7 +62,7 @@ def test_app(b):
|
|||||||
assert res
|
assert res
|
||||||
assert 'end_block' == res.WhichOneof("value")
|
assert 'end_block' == res.WhichOneof("value")
|
||||||
|
|
||||||
new_block_hash = calculate_hash([block0['hash'], new_block_txn_hash])
|
new_block_hash = calculate_hash([block0['app_hash'], new_block_txn_hash])
|
||||||
|
|
||||||
data = p.process('commit', None)
|
data = p.process('commit', None)
|
||||||
res, err = read_message(BytesIO(data), types.Response)
|
res, err = read_message(BytesIO(data), types.Response)
|
||||||
@ -75,4 +73,23 @@ def test_app(b):
|
|||||||
block0 = b.get_latest_block()
|
block0 = b.get_latest_block()
|
||||||
assert block0
|
assert block0
|
||||||
assert block0['height'] == 1
|
assert block0['height'] == 1
|
||||||
assert block0['hash'] == new_block_hash
|
assert block0['app_hash'] == new_block_hash
|
||||||
|
|
||||||
|
# empty block should not update height
|
||||||
|
r = types.Request()
|
||||||
|
r.begin_block.hash = new_block_hash.encode('utf-8')
|
||||||
|
p.process('begin_block', r)
|
||||||
|
|
||||||
|
r = types.Request()
|
||||||
|
r.end_block.height = 2
|
||||||
|
p.process('end_block', r)
|
||||||
|
|
||||||
|
data = p.process('commit', None)
|
||||||
|
assert res.commit.data == new_block_hash.encode('utf-8')
|
||||||
|
|
||||||
|
block0 = b.get_latest_block()
|
||||||
|
assert block0
|
||||||
|
assert block0['height'] == 1
|
||||||
|
|
||||||
|
# when empty block is generated hash of previous block should be returned
|
||||||
|
assert block0['app_hash'] == new_block_hash
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
from bigchaindb import backend
|
from bigchaindb import backend
|
||||||
|
|
||||||
|
|
||||||
@ -27,3 +29,15 @@ def test_asset_is_separated_from_transaciton(b):
|
|||||||
assert 'asset' not in backend.query.get_transaction(b.connection, tx.id)
|
assert 'asset' not in backend.query.get_transaction(b.connection, tx.id)
|
||||||
assert backend.query.get_asset(b.connection, tx.id)['data'] == asset
|
assert backend.query.get_asset(b.connection, tx.id)['data'] == asset
|
||||||
assert b.get_transaction(tx.id) == tx
|
assert b.get_transaction(tx.id) == tx
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_latest_block(b):
|
||||||
|
from bigchaindb.tendermint.lib import Block
|
||||||
|
|
||||||
|
for i in range(10):
|
||||||
|
app_hash = os.urandom(16).hex()
|
||||||
|
block = Block(app_hash=app_hash, height=i)._asdict()
|
||||||
|
b.store_block(block)
|
||||||
|
|
||||||
|
block = b.get_latest_block()
|
||||||
|
assert block['height'] == 9
|
||||||
|
Loading…
x
Reference in New Issue
Block a user