Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block timestamp to block header #253

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 8 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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