# Tangle-Accelerator Development
###### tags: `tangle-accelerator`
<img src="https://i.imgur.com/9bjZKpJ.jpg" >
## HTTP Server
* build with C++ boost/beast library
* ==TODO==: 測試多個 request 同時進來
## JSON parser
解析 request,並以 JSON 格式 response,使用 cJSON 實做
下列 function 需要 serialization:
* generate_address
* get_tips
* send_transfer
## Function Overview
core function 皆會使用到 `entangled/cclient`,參考到 [IOTA cclient 使用指南](/643NQmXjT9uhq5Do14v8PQ)
### Core
* [x] ta_generate_address - **may need updated**(下方 issue)
* [x] ta_get_tips - **may need updated**
* [x] cclient_get_txn_to_approve - **may need updated**
* [x] cclient_get_tips - **may need updated**
* [ ] ta_send_transfer - **on-going**
* [ ] ta_find_transaction_by_tag - **on-going**
* [ ] ta_get_txn_msg
### Should be deprecated
* insert_to_trytes: cclient 已有其他方式
* ta_attach_debug_message_to_tangle: 交給 test case 處理
### TangleID
* TODO
### send_transfer
#### dcurl
為了使用 dcurl 進行 PoW 加速,我們需要
實做 attachToTangle 流程
### `send_transfer` Flow
* [x] Generate new address
* [x] Make transaction
* [x] Set address
* [x] Set tag
* [x] Set message
* [x] Set value
* [ ] Make bundle
* [x] Add transaction
* [ ] Finalize
* [x] calculate bundle hash
* [ ] Fill bundle hash through txs
* pyota will fill in bundle hash in `finalize`
* [x] getTransactionToApprove
* [x] get transaction trytes
* `transaction_sesrialize_to_flex_trits`
* [x] Fill in trunk, branch in each transaction
* [x] Fill in timestamp in each transaction
* [ ] PoW
* [ ] fill in nonce
## IOTA transaction tryte offset
| offset|start|len|
|:--|:--|:--|
|SIGNATURE_MESSAGE_FRAGMENT_OFFSET|0|6561|
|ADDRESS_TRINARY_OFFSET|6561|243|
|VALUE_TRINARY_OFFSET|6804|81|
|OBSOLETE_TAG_TRINARY_OFFSET|6885|81|
|TIMESTAMP_TRINARY_OFFSET|6966|27|
|CURRENT_INDEX_TRINARY_OFFSET|6993|27|
|LAST_INDEX_TRINARY_OFFSET|7020|27|
|BUNDLE_TRINARY_OFFSET|7047|243|
|TRUNK_TRANSACTION_TRINARY_OFFSET|7290 (2430 tryte)|243|
|BRANCH_TRANSACTION_TRINARY_OFFSET|7533 (2511 tryte)|243|
|TAG_TRINARY_OFFSET|7776|81|
|ATTACHMENT_TIMESTAMP_<br>TRINARY_OFFSET|7857 (2619 tryte)|27|
|ATTACHMENT_TIMESTAMP_<br>LOWER_BOUND_TRINARY_OFFSET|7884|27|
|ATTACHMENT_TIMESTAMP_<br>UPPER_BOUND_TRINARY_OFFSET|7911 (2637 tryte)|27|
|NONCE_TRINARY_OFFSET|7938 (2646 tryte)|81|
## Transaction raw trytes
transaction_sesrialize_to_flex_trits
```python
def as_tryte_string(self):
# type: () -> TransactionTrytes
"""
Returns a TryteString representation of the transaction.
"""
return TransactionTrytes(
self.signature_message_fragment
+ self.address.address
+ self.value_as_trytes
+ self.legacy_tag
+ self.timestamp_as_trytes
+ self.current_index_as_trytes
+ self.last_index_as_trytes
+ self.bundle_hash
+ self.trunk_transaction_hash
+ self.branch_transaction_hash
+ self.tag
+ self.attachment_timestamp_as_trytes
+ self.attachment_timestamp_lower_bound_as_trytes
+ self.attachment_timestamp_upper_bound_as_trytes
+ self.nonce
)
```
## common/model: Transfer
**Transfer**
```c
typedef struct _transfer {
transfer_type_e type;
const flex_trit_t *address;
const flex_trit_t *tag;
int64_t value;
uint64_t timestamp;
void *meta; // extend informations depended on transfer type.
} transfer_t;
```
3 types of transfer, each one has its own meta data structure:
**DATA**
```c
typedef struct _transfer_data {
const flex_trit_t *data;
size_t len;
} transfer_data_t;
```
**VALUE_OUT**
```c
typedef struct _transfer_value_out {
const flex_trit_t *seed;
uint8_t security;
uint64_t seed_index;
} transfer_value_out_t;
```
**VALUE_IN**
```c
typedef struct _transfer_value_in {
// can have up to 2187 trytes as data
size_t len;
const flex_trit_t *data;
} transfer_value_in_t;
```
**Transfer Iterator**
transfer_iterator_new
* In this function, bundle_hash is calculated
transfer_iterator_next
* generate transaction from transfer array
**unkown nouns**
* `remainder` in prepare_transfers
## Send_transfer
1. prepare_transfer
2. get bundle trits
3. send_trytes
## Prepare_transfer
1. transfer_iterator_new
* cacluate bundle hash
2. transfer_interator_next
* generate a transaction_t object
* fill in `bundle`, `address`, `tag`, `obsolete_tag`, `timestamp`, `current_index`, `last_index`
* fill in metadata
* DATA: `message`
* VALUE_OUT: `value`, `signature`
* if index == 0, gen signature
* VALUE_IN: `value`, `signature`
4. bundle_add_transaction
5. bundle_finalize
* Since bundle hash is calculated in `transfer_iterator_new`, why do we still need `bundle_finalize`?
* [source code link](https://github.com/iotaledger/entangled/pull/629/files#diff-1b378ec08c554eb687dd17c1a1671f68R517)
## Send_trytes
1. get_transactions_to_approve
2. attach_to_tangle
3. sotre_and_broadcast
**cache server 不能獨立於 tangle-accelerator 的原因**
* 安全考量,使用者也需要信任 Caching server,但 TA 是安全的 (Root Of Trust)
* 如果 tx invalid, caching server 也不知道,TA 還能直接向 full node 查證