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

feat: add timestampMs for transactions #93

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
11 changes: 10 additions & 1 deletion helpers/slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function getEpochTime (time) {
var d = beginEpochTime();
var t = d.getTime();

return Math.floor((time - t) / 1000);
return time - t;
}
/**
* @namespace
Expand All @@ -50,6 +50,15 @@ module.exports = {
* @return {number} ADAMANT epoch time constant.
*/
getTime: function (time) {
return Math.floor(getEpochTime(time) / 1000);
},

/**
* @method
* @param {number?} time
* @return {number} ADAMANT epoch time in ms.
*/
getTimeMs: function (time) {
return getEpochTime(time);
},

Expand Down
36 changes: 33 additions & 3 deletions logic/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,15 @@ Transaction.prototype.create = function (data) {
throw 'Invalid keypair';
}

const timestampMs = slots.getTimeMs();

var trs = {
type: data.type,
amount: 0,
senderPublicKey: data.sender.publicKey,
requesterPublicKey: data.requester ? data.requester.publicKey.toString('hex') : null,
timestamp: slots.getTime(),
timestamp: Math.floor(timestampMs / 1000),
timestampMs,
asset: {}
};

Expand Down Expand Up @@ -476,6 +479,10 @@ Transaction.prototype.verify = function (trs, sender, requester, cb) {
return setImmediate(cb, 'Missing sender');
}

if (typeof trs.timestampMs !== 'number') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will previous txs/blocks be valid?
It's important to keep backward compatibility.

return setImmediate(cb, 'Missing timestampMs');
}

// Check transaction type
if (!__private.types[trs.type]) {
return setImmediate(cb, 'Unknown transaction type ' + trs.type);
Expand Down Expand Up @@ -647,10 +654,26 @@ Transaction.prototype.verify = function (trs, sender, requester, cb) {
if (trs.timestamp < INT_32_MIN || trs.timestamp > INT_32_MAX) {
return setImmediate(cb, 'Invalid transaction timestamp. Timestamp is not in the int32 range');
}
if (slots.getSlotNumber(trs.timestamp) > slots.getSlotNumber()) {

if (Math.abs(trs.timestampMs - trs.timestamp * 1000) >= 1000) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will previous txs/blocks be valid?

return setImmediate(cb, 'Invalid transaction timestamp. Timestamp and timestampMs delta is greater than 1000ms');
}

const currentTime = slots.getTime();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns time in seconds, not in ms, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes


const currentSlotNumber = slots.getSlotNumber(currentTime);
const transactionSlotNumber = slots.getSlotNumber(trs.timestamp);

if (transactionSlotNumber > currentSlotNumber) {
return setImmediate(cb, 'Invalid transaction timestamp. Timestamp is in the future');
}

// Transaction may be maximum 15 seconds behind the node
const earliestValidTime = currentTime - 15;
if (transactionSlotNumber < slots.getSlotNumber(earliestValidTime)) {
return setImmediate(cb, 'Invalid transaction timestamp. Timestamp is more than 15 seconds in the past');
}

// Call verify on transaction type
__private.types[trs.type].verify.call(this, trs, sender, function (err) {
if (err) {
Expand Down Expand Up @@ -975,6 +998,7 @@ Transaction.prototype.dbFields = [
'blockId',
'type',
'timestamp',
'timestampMs',
'senderPublicKey',
'requesterPublicKey',
'senderId',
Expand Down Expand Up @@ -1017,6 +1041,7 @@ Transaction.prototype.dbSave = function (trs) {
blockId: trs.blockId,
type: trs.type,
timestamp: trs.timestamp,
timestampMs: trs.timestampMs,
senderPublicKey: senderPublicKey,
requesterPublicKey: requesterPublicKey,
senderId: trs.senderId,
Expand Down Expand Up @@ -1066,6 +1091,7 @@ Transaction.prototype.afterSave = function (trs, cb) {
* @property {string} blockId
* @property {number} type
* @property {number} timestamp
* @property {number} timestampMs
* @property {publicKey} senderPublicKey
* @property {publicKey} requesterPublicKey
* @property {string} senderId
Expand Down Expand Up @@ -1108,6 +1134,9 @@ Transaction.prototype.schema = {
timestamp: {
type: 'integer'
},
timestampMs: {
type: 'integer'
},
senderPublicKey: {
type: 'string',
format: 'publicKey'
Expand Down Expand Up @@ -1150,7 +1179,7 @@ Transaction.prototype.schema = {
type: 'object'
}
},
required: ['type', 'timestamp', 'senderPublicKey', 'signature']
required: ['type', 'timestamp', 'timestampMs', 'senderPublicKey', 'signature']
};

/**
Expand Down Expand Up @@ -1207,6 +1236,7 @@ Transaction.prototype.dbRead = function (raw) {
type: parseInt(raw.t_type),
block_timestamp: parseInt(raw.block_timestamp),
timestamp: parseInt(raw.t_timestamp),
timestampMs: parseInt(raw.t_timestampMs),
senderPublicKey: raw.t_senderPublicKey,
requesterPublicKey: raw.t_requesterPublicKey,
senderId: raw.t_senderId,
Expand Down
85 changes: 85 additions & 0 deletions sql/migrations/20241218010631_addTimestampMs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Add `timestampMs` Column to Transactions
*/

BEGIN;

-- Add timestampMs column to Transactions
ALTER TABLE "trs" ADD COLUMN "timestampMs" BIGINT;

-- Add timestampMs index
CREATE INDEX IF NOT EXISTS "timestamp_ms" ON "trs"("timestampMs");

-- Add timestampMs column to trs_list view
DROP VIEW IF EXISTS trs_list;

CREATE VIEW trs_list AS

SELECT t."id" AS "t_id",
b."height" AS "b_height",
t."blockId" AS "t_blockId",
t."type" AS "t_type",
t."timestamp" AS "t_timestamp",
t."timestampMs" AS "t_timestampMs",
b."timestamp" AS "b_timestamp",
t."senderPublicKey" AS "t_senderPublicKey",
m."publicKey" AS "m_recipientPublicKey",
UPPER(t."senderId") AS "t_senderId",
UPPER(t."recipientId") AS "t_recipientId",
t."amount" AS "t_amount",
t."fee" AS "t_fee",
ENCODE(t."signature", 'hex') AS "t_signature",
ENCODE(t."signSignature", 'hex') AS "t_SignSignature",
t."signatures" AS "t_signatures",
(SELECT height + 1 FROM blocks ORDER BY height DESC LIMIT 1) - b."height" AS "confirmations"

FROM trs t
LEFT JOIN blocks b ON t."blockId" = b."id"
LEFT JOIN mem_accounts m ON t."recipientId" = m."address";

-- Add timestampMs column to trs_list_full view
DROP VIEW IF EXISTS trs_list_full;

CREATE VIEW trs_list_full AS

SELECT t."id" AS "t_id",
b."height" AS "b_height",
t."blockId" AS "t_blockId",
t."type" AS "t_type",
t."timestamp" AS "t_timestamp",
b."timestamp" AS "b_timestamp",
t."senderPublicKey" AS "t_senderPublicKey",
m."publicKey" AS "m_recipientPublicKey",
UPPER(t."senderId") AS "t_senderId",
UPPER(t."recipientId") AS "t_recipientId",
t."amount" AS "t_amount",
t."fee" AS "t_fee",
ENCODE(t."signature", 'hex') AS "t_signature",
ENCODE(t."signSignature", 'hex') AS "t_SignSignature",
t."signatures" AS "t_signatures",
(SELECT height + 1 FROM blocks ORDER BY height DESC LIMIT 1) - b."height" AS "confirmations",
d."username" AS "d_username",
v."votes" AS "v_votes",
ms."min" AS "m_min",
ms."lifetime" AS "m_lifetime",
ms."keysgroup" AS "m_keysgroup",
c."message" AS "c_message",
c."own_message" AS "c_own_message",
c."type" AS "c_type",
st."type" as "st_type",
st."stored_value" as "st_stored_value",
st."stored_key" as "st_stored_key"
FROM trs t

LEFT JOIN blocks b ON t."blockId" = b."id"
LEFT JOIN mem_accounts m ON t."recipientId" = m."address"
LEFT OUTER JOIN delegates AS d ON d."transactionId" = t."id"
LEFT OUTER JOIN votes AS v ON v."transactionId" = t."id"
LEFT OUTER JOIN signatures AS s ON s."transactionId" = t."id"
LEFT OUTER JOIN multisignatures AS ms ON ms."transactionId" = t."id"
LEFT OUTER JOIN chats AS c ON c."transactionId" = t."id"
LEFT OUTER JOIN states AS st ON st."transactionId" = t."id";



COMMIT;
4 changes: 4 additions & 0 deletions test/common/stubs/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const firstTransfer = {
type: 0,
block_timestamp: 224138470,
timestamp: 224138454,
timestampMs: 224138454000,
senderPublicKey:
'b0b4d346382aa07b23c0b733d040424532201b9eb22004b66a79d4b44e9d1449',
senderId: 'U9781760580710719871',
Expand All @@ -55,6 +56,7 @@ const secondTransfer = {
type: 0,
block_timestamp: 224138470,
timestamp: 224138455,
timestampMs: 224138455000,
senderPublicKey:
'b0b4d346382aa07b23c0b733d040424532201b9eb22004b66a79d4b44e9d1449',
senderId: 'U9781760580710719871',
Expand All @@ -77,6 +79,7 @@ const firstMessage = {
type: 8,
block_timestamp: 224138470,
timestamp: 224138456,
timestampMs: 224138456000,
senderPublicKey:
'b0b4d346382aa07b23c0b733d040424532201b9eb22004b66a79d4b44e9d1449',
senderId: 'U9781760580710719871',
Expand Down Expand Up @@ -105,6 +108,7 @@ const secondMessage = {
type: 8,
block_timestamp: 224138470,
timestamp: 224138458,
timestampMs: 224138458000,
senderPublicKey:
'b0b4d346382aa07b23c0b733d040424532201b9eb22004b66a79d4b44e9d1449',
senderId: 'U9781760580710719871',
Expand Down
1 change: 1 addition & 0 deletions test/common/stubs/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const existingTransactionWithAsset = {
const unconfirmedTransaction = {
type: 0,
timestamp: 228144202,
timestampMs: 228144202000,
amount: 50000000,
fee: 50000000,
senderPublicKey:
Expand Down
6 changes: 6 additions & 0 deletions test/common/stubs/transactions/common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const accounts = require('../../../../helpers/accounts.js');

const validSender = {
balance: 8067474861277,
u_balance: 8067474861277,
Expand All @@ -9,6 +11,9 @@ const validSender = {
address: 'U810656636599221322',
};

const validSenderHash = accounts.createPassPhraseHash(validSender.password);
const validSenderKeyPair = accounts.makeKeypair(validSenderHash);

const senderDefault = {
username: null,
isDelegate: 0,
Expand All @@ -29,5 +34,6 @@ const senderDefault = {

module.exports = {
validSender,
validSenderKeyPair,
senderDefault
};
2 changes: 2 additions & 0 deletions test/common/stubs/transactions/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const validTransaction = {
type: 0,
block_timestamp: null,
timestamp: 0,
timestampMs: 0,
senderPublicKey:
'b80bb6459608dcdeb9a98d1f2b0111b2bf11e53ef2933e6769bb0198e3a97aae',
senderId: 'U15365455923155964650',
Expand Down Expand Up @@ -56,6 +57,7 @@ const validUnconfirmedTransaction = {
recipientId: 'U7771441689362721578',
fee: 50000000,
timestamp: 1000,
timestampMs: 1000000,
asset: {}
};

Expand Down
Loading