Merge remote-tracking branch 'remotes/origin/feat/128/multiple-input-output' into feat/127/crypto-conditions-ilp-bigchain-integration

This commit is contained in:
diminator 2016-04-18 17:19:02 +02:00
commit d5923d5c67
4 changed files with 141 additions and 30 deletions

View File

@ -185,14 +185,14 @@ class Bigchain(object):
transactions = list(cursor)
return transactions
def get_spent(self, inp):
def get_spent(self, tx_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
given `txid` is only used once.
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:
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': ...}
response = r.table('bigchain').concat_map(lambda doc: doc['block']['transactions'])\
.filter(lambda transaction: transaction['transaction']['fulfillments']
.contains(lambda fulfillment: fulfillment['input'] == inp))\
.contains(lambda fulfillment: fulfillment['input'] == tx_input))\
.run(self.conn)
# a transaction_id should have been spent at most one time
@ -209,7 +209,7 @@ class Bigchain(object):
if transactions:
if len(transactions) != 1:
raise Exception('`{}` was spent more then once. There is a problem with the chain'.format(
inp['txid']))
tx_input['txid']))
else:
return transactions[0]
else:

View File

@ -173,10 +173,10 @@ def create_tx(current_owners, new_owners, inputs, operation, payload=None):
# transfer
if inputs:
for fid, inp in enumerate(inputs):
for fid, tx_input in enumerate(inputs):
fulfillments.append({
'current_owners': current_owners,
'input': inp,
'input': tx_input,
'fulfillment': None,
'fid': fid
})

View File

@ -7,37 +7,148 @@ Transactions, blocks and votes are represented using JSON documents with the fol
```json
{
"id": "<sha3 hash>",
"version": "<transaction version number>",
"transaction": {
"current_owner": "<pub-key>",
"new_owner": "<pub-key>",
"input": "<sha3 hash>",
"fulfillments": ["<list of <fullfillment>"],
"conditions": ["<list of <condition>"],
"operation": "<string>",
"timestamp": "<timestamp from client>",
"data": {
"hash": "<SHA3-256 hash hexdigest of payload>",
"payload": {
"title": "The Winds of Plast",
"creator": "Johnathan Plunkett",
"IPFS_key": "QmfQ5QAjvg4GtA3wg3adpnDJug8ktA1BxurVqBD8rtgVjP"
"payload": "<generic json document>"
}
}
},
"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`
- `id`: sha3 hash of the transaction. The `id` is also the DB primary key.
- `current_owner`: Public key of the current owner of the digital content with hash `hash`
- `new_owner`: Public key of the new owner of the digital content with hash `hash`
- `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.
- `operation`: String representation of the operation being performed (REGISTER, 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.
- `signature`: Signature of the transaction with the `current_owner` private key
- **Transaction header**:
- `id`: sha3 hash of the transaction. The `id` is also the DB primary key.
- `version`: Version of the transaction. For future compability with changes in the transaction model.
- **Transaction body**:
- `fulfillments`: List of fulfillments. Each _fulfillment_ contains a pointer to a unspent digital asset
and a _crypto fulfillment_ that satisfies a spending condition set on the unspent digital asset. A _fulfillment_
is usually a signature proving the ownership of the digital asset.
See [conditions and fulfillments](models.md#conditions-and-fulfillments)
- `conditions`: List of conditions. Each _condition_ a _crypto condition_ that needs to be fulfilled by the
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

View File

@ -27,8 +27,8 @@ class TestBigchainApi(object):
def test_create_transaction_create(self, b, user_sk):
tx = b.create_transaction(b.me, user_sk, None, 'CREATE')
assert sorted(tx) == sorted(['id', 'transaction', 'version'])
assert sorted(tx['transaction']) == sorted(['conditions', 'data', 'fulfillments', 'operation', 'timestamp'])
assert sorted(tx) == ['id', 'transaction', 'version']
assert sorted(tx['transaction']) == ['conditions', 'data', 'fulfillments', 'operation', 'timestamp']
def test_create_transaction_with_unsupported_payload_raises(self, b):
with pytest.raises(TypeError):
@ -41,8 +41,8 @@ class TestBigchainApi(object):
tx = b.create_transaction(user_vk, b.me, input_tx, 'TRANSFER')
assert sorted(tx) == sorted(['id', 'transaction', 'version'])
assert sorted(tx['transaction']) == sorted(['conditions', 'data', 'fulfillments', 'operation', 'timestamp'])
assert sorted(tx) == ['id', 'transaction', 'version']
assert sorted(tx['transaction']) == ['conditions', 'data', 'fulfillments', 'operation', 'timestamp']
tx_signed = b.sign_transaction(tx, user_sk)