mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Merge remote-tracking branch 'remotes/origin/feat/128/multiple-input-output' into feat/127/crypto-conditions-ilp-bigchain-integration
This commit is contained in:
commit
d5923d5c67
@ -185,14 +185,14 @@ class Bigchain(object):
|
|||||||
transactions = list(cursor)
|
transactions = list(cursor)
|
||||||
return transactions
|
return transactions
|
||||||
|
|
||||||
def get_spent(self, inp):
|
def get_spent(self, tx_input):
|
||||||
"""Check if a `txid` was already used as an input.
|
"""Check if a `txid` was already used as an input.
|
||||||
|
|
||||||
A transaction can be used as an input for another transaction. Bigchain needs to make sure that a
|
A transaction can be used as an input for another transaction. Bigchain needs to make sure that a
|
||||||
given `txid` is only used once.
|
given `txid` is only used once.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
inp (dict): Input of a transaction in the form `{'txid': 'transaction id', 'cid': 'condition id'}`
|
tx_input (dict): Input of a transaction in the form `{'txid': 'transaction id', 'cid': 'condition id'}`
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The transaction that used the `txid` as an input if it exists else it returns `None`
|
The transaction that used the `txid` as an input if it exists else it returns `None`
|
||||||
@ -201,7 +201,7 @@ class Bigchain(object):
|
|||||||
# checks if the bigchain has any transaction with input {'txid': ..., 'cid': ...}
|
# checks if the bigchain has any transaction with input {'txid': ..., 'cid': ...}
|
||||||
response = r.table('bigchain').concat_map(lambda doc: doc['block']['transactions'])\
|
response = r.table('bigchain').concat_map(lambda doc: doc['block']['transactions'])\
|
||||||
.filter(lambda transaction: transaction['transaction']['fulfillments']
|
.filter(lambda transaction: transaction['transaction']['fulfillments']
|
||||||
.contains(lambda fulfillment: fulfillment['input'] == inp))\
|
.contains(lambda fulfillment: fulfillment['input'] == tx_input))\
|
||||||
.run(self.conn)
|
.run(self.conn)
|
||||||
|
|
||||||
# a transaction_id should have been spent at most one time
|
# a transaction_id should have been spent at most one time
|
||||||
@ -209,7 +209,7 @@ class Bigchain(object):
|
|||||||
if transactions:
|
if transactions:
|
||||||
if len(transactions) != 1:
|
if len(transactions) != 1:
|
||||||
raise Exception('`{}` was spent more then once. There is a problem with the chain'.format(
|
raise Exception('`{}` was spent more then once. There is a problem with the chain'.format(
|
||||||
inp['txid']))
|
tx_input['txid']))
|
||||||
else:
|
else:
|
||||||
return transactions[0]
|
return transactions[0]
|
||||||
else:
|
else:
|
||||||
|
@ -173,10 +173,10 @@ def create_tx(current_owners, new_owners, inputs, operation, payload=None):
|
|||||||
|
|
||||||
# transfer
|
# transfer
|
||||||
if inputs:
|
if inputs:
|
||||||
for fid, inp in enumerate(inputs):
|
for fid, tx_input in enumerate(inputs):
|
||||||
fulfillments.append({
|
fulfillments.append({
|
||||||
'current_owners': current_owners,
|
'current_owners': current_owners,
|
||||||
'input': inp,
|
'input': tx_input,
|
||||||
'fulfillment': None,
|
'fulfillment': None,
|
||||||
'fid': fid
|
'fid': fid
|
||||||
})
|
})
|
||||||
|
@ -7,37 +7,148 @@ Transactions, blocks and votes are represented using JSON documents with the fol
|
|||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"id": "<sha3 hash>",
|
"id": "<sha3 hash>",
|
||||||
|
"version": "<transaction version number>",
|
||||||
"transaction": {
|
"transaction": {
|
||||||
"current_owner": "<pub-key>",
|
"fulfillments": ["<list of <fullfillment>"],
|
||||||
"new_owner": "<pub-key>",
|
"conditions": ["<list of <condition>"],
|
||||||
"input": "<sha3 hash>",
|
|
||||||
"operation": "<string>",
|
"operation": "<string>",
|
||||||
"timestamp": "<timestamp from client>",
|
"timestamp": "<timestamp from client>",
|
||||||
"data": {
|
"data": {
|
||||||
"hash": "<SHA3-256 hash hexdigest of payload>",
|
"hash": "<SHA3-256 hash hexdigest of payload>",
|
||||||
"payload": {
|
"payload": "<generic json document>"
|
||||||
"title": "The Winds of Plast",
|
|
||||||
"creator": "Johnathan Plunkett",
|
|
||||||
"IPFS_key": "QmfQ5QAjvg4GtA3wg3adpnDJug8ktA1BxurVqBD8rtgVjP"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
"signature": "<signature of the transaction>"
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
A transaction is an operation between the `current_owner` and the `new_owner` over the digital content described by `hash`. For example if could be a transfer of ownership of the digital content `hash`
|
A transaction is an operation between the `current_owner` and the `new_owner` over the digital content described by `hash`. For example if could be a transfer of ownership of the digital content `hash`
|
||||||
|
|
||||||
- `id`: sha3 hash of the transaction. The `id` is also the DB primary key.
|
- **Transaction header**:
|
||||||
- `current_owner`: Public key of the current owner of the digital content with hash `hash`
|
- `id`: sha3 hash of the transaction. The `id` is also the DB primary key.
|
||||||
- `new_owner`: Public key of the new owner of the digital content with hash `hash`
|
- `version`: Version of the transaction. For future compability with changes in the transaction model.
|
||||||
- `input`: id (sha3 hash) of the transaction in which the content was transfered to the user (similar to input in the blockchain). Right now we will assume that there is only one input per transaction to simplify the prototype. This can be changed in the future to allow multiple inputs per transaction.
|
- **Transaction body**:
|
||||||
- `operation`: String representation of the operation being performed (REGISTER, TRANSFER, ...) this will define how
|
- `fulfillments`: List of fulfillments. Each _fulfillment_ contains a pointer to a unspent digital asset
|
||||||
the transactions should be validated
|
and a _crypto fulfillment_ that satisfies a spending condition set on the unspent digital asset. A _fulfillment_
|
||||||
- `timestamp`: Time of creation of the transaction in UTC. It's provided by the client.
|
is usually a signature proving the ownership of the digital asset.
|
||||||
- `data`: JSON object describing the asset (digital content). It contains at least the field `hash` which is a
|
See [conditions and fulfillments](models.md#conditions-and-fulfillments)
|
||||||
sha3 hash of the digital content.
|
- `conditions`: List of conditions. Each _condition_ a _crypto condition_ that needs to be fulfilled by the
|
||||||
- `signature`: Signature of the transaction with the `current_owner` private key
|
new owner in order to spend the digital asset.
|
||||||
|
See [conditions and fulfillments](models.md#conditions-and-fulfillments)
|
||||||
|
- `operation`: String representation of the operation being performed (CREATE, TRANSFER, ...) this will define how
|
||||||
|
the transactions should be validated
|
||||||
|
- `timestamp`: Time of creation of the transaction in UTC. It's provided by the client.
|
||||||
|
- `data`: JSON object describing the asset (digital content). It contains at least the field `hash` which is a
|
||||||
|
sha3 hash of the digital content.
|
||||||
|
|
||||||
|
## Conditions and Fulfillments
|
||||||
|
|
||||||
|
### Conditions
|
||||||
|
|
||||||
|
##### Simple Signature
|
||||||
|
|
||||||
|
If there is only one _new owner_ the condition will be a single signature condition.
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"cid": "<condition index>",
|
||||||
|
"condition": {
|
||||||
|
"details": {
|
||||||
|
"bitmask": "<explain>",
|
||||||
|
"public_key": "<explain>",
|
||||||
|
"signature": null,
|
||||||
|
"type": "fulfillment",
|
||||||
|
"type_id": "<explain>"
|
||||||
|
},
|
||||||
|
"uri": "<explain>"
|
||||||
|
},
|
||||||
|
"new_owners": ["<explain>"]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- **Condition header**:
|
||||||
|
- `cid`: Condition index so that we can reference this output as an input to another transaction. It also matches
|
||||||
|
the input `fid`, making this the condition to fulfill in order to spend the digital asset used as input with `fid`
|
||||||
|
- `new_owners`: List of public keys of the new owners.
|
||||||
|
- **Condition body**:
|
||||||
|
- `bitmask`:
|
||||||
|
- `public_key`:
|
||||||
|
- `signature`:
|
||||||
|
- `type`:
|
||||||
|
- `type_id`:
|
||||||
|
- `uri`:
|
||||||
|
|
||||||
|
##### Multi Signature
|
||||||
|
|
||||||
|
If there are multiple _new owners_ by default we create a condition requiring a signature from each new owner in order
|
||||||
|
to spend the digital asset.
|
||||||
|
|
||||||
|
Example of a condition with two _new owners_:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"cid": "<condition index>",
|
||||||
|
"condition": {
|
||||||
|
"details": {
|
||||||
|
"bitmask": 41,
|
||||||
|
"subfulfillments": [
|
||||||
|
{
|
||||||
|
"bitmask": 32,
|
||||||
|
"public_key": "<new owner 1 public key>",
|
||||||
|
"signature": null,
|
||||||
|
"type": "fulfillment",
|
||||||
|
"type_id": 4,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bitmask": 32,
|
||||||
|
"public_key": "<new owner 2 public key>",
|
||||||
|
"signature": null,
|
||||||
|
"type": "fulfillment",
|
||||||
|
"type_id": 4,
|
||||||
|
"weight": 1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"threshold": 2,
|
||||||
|
"type": "fulfillment",
|
||||||
|
"type_id": 2
|
||||||
|
},
|
||||||
|
"uri": "cc:2:29:ytNK3X6-bZsbF-nCGDTuopUIMi1HCyCkyPewm6oLI3o:206"},
|
||||||
|
"new_owners": [
|
||||||
|
"<new owner 1 public key>",
|
||||||
|
"<new owner 2 public key>"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `subfulfillments`:
|
||||||
|
- `weight`:
|
||||||
|
- `threshold`:
|
||||||
|
|
||||||
|
|
||||||
|
### Fulfillments
|
||||||
|
|
||||||
|
##### Simple Signature
|
||||||
|
|
||||||
|
If there is only one _current owner_ the fulfillment will be a single signature fulfillment.
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"current_owners": ["<Public Key>"],
|
||||||
|
"fid": 0,
|
||||||
|
"fulfillment": "cf:4:RxFzIE679tFBk8zwEgizhmTuciAylvTUwy6EL6ehddHFJOhK5F4IjwQ1xLu2oQK9iyRCZJdfWAefZVjTt3DeG5j2exqxpGliOPYseNkRAWEakqJ_UrCwgnj92dnFRAEE",
|
||||||
|
"input": {
|
||||||
|
"cid": 0,
|
||||||
|
"txid": "11b3e7d893cc5fdfcf1a1706809c7def290a3b10b0bef6525d10b024649c42d3"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `fid`: Fulfillment index. It matches a `cid` in the conditions with a new _crypto condition_ that the new owner(s)
|
||||||
|
need to fulfill to spend this digital asset
|
||||||
|
- `current_owners`: Public key of the current owner(s)
|
||||||
|
- `fulfillment`:
|
||||||
|
- `input`: Pointer to the digital asset and condition of a previous transaction
|
||||||
|
- `cid`: Condition index
|
||||||
|
- `txid`: Transaction id
|
||||||
|
|
||||||
## The Block Model
|
## The Block Model
|
||||||
|
|
||||||
|
@ -27,8 +27,8 @@ class TestBigchainApi(object):
|
|||||||
def test_create_transaction_create(self, b, user_sk):
|
def test_create_transaction_create(self, b, user_sk):
|
||||||
tx = b.create_transaction(b.me, user_sk, None, 'CREATE')
|
tx = b.create_transaction(b.me, user_sk, None, 'CREATE')
|
||||||
|
|
||||||
assert sorted(tx) == sorted(['id', 'transaction', 'version'])
|
assert sorted(tx) == ['id', 'transaction', 'version']
|
||||||
assert sorted(tx['transaction']) == sorted(['conditions', 'data', 'fulfillments', 'operation', 'timestamp'])
|
assert sorted(tx['transaction']) == ['conditions', 'data', 'fulfillments', 'operation', 'timestamp']
|
||||||
|
|
||||||
def test_create_transaction_with_unsupported_payload_raises(self, b):
|
def test_create_transaction_with_unsupported_payload_raises(self, b):
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
@ -41,8 +41,8 @@ class TestBigchainApi(object):
|
|||||||
|
|
||||||
tx = b.create_transaction(user_vk, b.me, input_tx, 'TRANSFER')
|
tx = b.create_transaction(user_vk, b.me, input_tx, 'TRANSFER')
|
||||||
|
|
||||||
assert sorted(tx) == sorted(['id', 'transaction', 'version'])
|
assert sorted(tx) == ['id', 'transaction', 'version']
|
||||||
assert sorted(tx['transaction']) == sorted(['conditions', 'data', 'fulfillments', 'operation', 'timestamp'])
|
assert sorted(tx['transaction']) == ['conditions', 'data', 'fulfillments', 'operation', 'timestamp']
|
||||||
|
|
||||||
tx_signed = b.sign_transaction(tx, user_sk)
|
tx_signed = b.sign_transaction(tx, user_sk)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user