Skip to content

Commit

Permalink
Add block timestamp to block header
Browse files Browse the repository at this point in the history
Upgrade SDK to 0.0.13
Tests changes and due to the addition of type timestamp in the block header.
  • Loading branch information
TrustHenry committed Jan 14, 2021
1 parent 9e40408 commit 22a0f5e
Show file tree
Hide file tree
Showing 13 changed files with 1,790 additions and 1,814 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# From Agora Runner
FROM node:14.15.1-alpine3.12
FROM node:14.15.4-alpine3.12
RUN apk add --no-cache git py-pip alpine-sdk \
bash autoconf libtool automake

Expand Down
2 changes: 2 additions & 0 deletions docs/Database_Schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
| signature | BLOB | | Y | | Schnorr multisig of all validators which signed this block |
| tx_count | INTEGER | | Y | | The number of transactions in the block|
| enrollment_count | INTEGER | | Y | | The number of enrollments in the block|
| time_stamp | INTEGER | | Y | | Block unix timestamp |

### _Create Script_

Expand All @@ -28,6 +29,7 @@ CREATE TABLE IF NOT EXISTS "blocks" (
"signature" BLOB NOT NULL,
"tx_count" INTEGER NOT NULL,
"enrollment_count" INTEGER NOT NULL,
"time_stamp" INTEGER NOT NULL,
PRIMARY KEY("height")
)
```
Expand Down
515 changes: 239 additions & 276 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"argparse": "^2.0.1",
"assert": "^2.0.0",
"axios": "^0.21.1",
"boa-sdk-ts": "^0.0.10",
"boa-sdk-ts": "^0.0.13",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"crc": "^3.8.0",
Expand Down
2 changes: 0 additions & 2 deletions src/Stoa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ class Stoa extends WebService
// parse application/json
this.app.use(bodyParser.json())
this.app.use(cors(cors_options));
// enable pre-flight
this.app.options('*', cors(cors_options));

// Prepare routes
this.app.get("/block_height", this.getBlockHeight.bind(this));
Expand Down
27 changes: 14 additions & 13 deletions src/modules/storage/LedgerStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class LedgerStorage extends Storages
signature BLOB NOT NULL,
tx_count INTEGER NOT NULL,
enrollment_count INTEGER NOT NULL,
time_stamp INTEGER NOT NULL,
PRIMARY KEY(height)
);
Expand Down Expand Up @@ -194,9 +195,9 @@ export class LedgerStorage extends Storages
let block_hash = hashFull(block.header);
storage.query(
`INSERT INTO blocks
(height, hash, prev_block, validators, merkle_root, signature, tx_count, enrollment_count)
(height, hash, prev_block, validators, merkle_root, signature, tx_count, enrollment_count, time_stamp)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?)`,
(?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
block.header.height.toString(),
block_hash.toBinary(Endian.Little),
Expand All @@ -205,7 +206,8 @@ export class LedgerStorage extends Storages
block.header.merkle_root.toBinary(Endian.Little),
block.header.signature.toBinary(Endian.Little),
block.txs.length,
block.header.enrollments.length
block.header.enrollments.length,
block.header.timestamp,
]
)
.then(() =>
Expand Down Expand Up @@ -253,7 +255,7 @@ export class LedgerStorage extends Storages
{
let sql =
`SELECT
height, hash, prev_block, validators, merkle_root, signature, tx_count, enrollment_count
height, hash, prev_block, validators, merkle_root, signature, tx_count, enrollment_count, time_stamp
FROM
blocks
WHERE height = ?`;
Expand Down Expand Up @@ -1019,13 +1021,12 @@ export class LedgerStorage extends Storages
*/
public getUTXO (address: string): Promise<any[]>
{
// TODO We should apply the block's timestamp to this method when it is added
let sql_utxo =
`SELECT
O.utxo_key as utxo,
O.amount,
T.block_height,
STRFTIME('%s', '2020-01-01 00:00:00') * 1000 + T.block_height * 10 * 60000 as block_time,
B.time_stamp as block_time,
T.type,
T.unlock_height
FROM
Expand Down Expand Up @@ -1081,10 +1082,9 @@ export class LedgerStorage extends Storages
public getWalletTransactionsHistory (address: string, page_size: number, page: number,
type: Array<number>, begin?: number, end?: number, peer?: string) : Promise<any[]>
{
// TODO We should apply the block's timestamp to this method when it is added
let filter_type = 'AND FTX.display_tx_type in (' + type.map(n =>`${n}`).join(',') + ')'
let filter_date = ((begin !== undefined) && (end !== undefined))
? `AND (STRFTIME('%s', '2020-01-01 00:00:00') * 1000 + T.block_height * 10 * 60000) BETWEEN ${begin} AND ${end}`
? `AND B.time_stamp BETWEEN ${begin} AND ${end}`
: ``;
let filter_peer_field;
let filter_peer_condition;
Expand Down Expand Up @@ -1130,11 +1130,11 @@ export class LedgerStorage extends Storages
SELECT
TX.address,
TX.block_height as height,
STRFTIME('%s', '2020-01-01 00:00:00') * 1000 + TX.block_height * 10 * 60000 as block_time,
TX.time_stamp as block_time,
TX.tx_hash,
TX.type,
TX.unlock_height,
STRFTIME('%s', '2020-01-01 00:00:00') * 1000 + TX.unlock_height * 10 * 60000 as unlock_time,
(TX.time_stamp + (TX.unlock_height - TX.block_height) * 10 * 60) as unlock_time,
(SUM(TX.income) - SUM(TX.spend)) as amount,
IFNULL(CASE
WHEN (SUM(TX.income) - SUM(TX.spend)) > 0 THEN
Expand Down Expand Up @@ -1172,6 +1172,7 @@ export class LedgerStorage extends Storages
SELECT
S.address,
T.block_height,
B.time_stamp,
T.tx_hash,
T.tx_index,
T.type,
Expand All @@ -1194,6 +1195,7 @@ export class LedgerStorage extends Storages
SELECT
O.address,
T.block_height,
B.time_stamp,
T.tx_hash,
T.tx_index,
T.type,
Expand Down Expand Up @@ -1228,15 +1230,14 @@ export class LedgerStorage extends Storages
{
let hash = new Hash(tx_hash).toBinary(Endian.Little);

// TODO We should apply the block's timestamp to this method when it is added
let sql_tx =
`SELECT
T.block_height as height,
STRFTIME('%s', '2020-01-01 00:00:00') * 1000 + T.block_height * 10 * 60000 as block_time,
B.time_stamp as block_time,
T.tx_hash,
T.type,
T.unlock_height,
STRFTIME('%s', '2020-01-01 00:00:00') * 1000 + T.unlock_height * 10 * 60000 as unlock_time,
(B.time_stamp + (T.unlock_height - T.block_height) * 10 * 60) as unlock_time,
P.payload
FROM
blocks B
Expand Down
Loading

0 comments on commit 22a0f5e

Please sign in to comment.