-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: {} | ||
}; | ||
|
||
|
@@ -476,6 +479,10 @@ Transaction.prototype.verify = function (trs, sender, requester, cb) { | |
return setImmediate(cb, 'Missing sender'); | ||
} | ||
|
||
if (typeof trs.timestampMs !== 'number') { | ||
return setImmediate(cb, 'Missing timestampMs'); | ||
} | ||
|
||
// Check transaction type | ||
if (!__private.types[trs.type]) { | ||
return setImmediate(cb, 'Unknown transaction type ' + trs.type); | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returns time in seconds, not in ms, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -975,6 +998,7 @@ Transaction.prototype.dbFields = [ | |
'blockId', | ||
'type', | ||
'timestamp', | ||
'timestampMs', | ||
'senderPublicKey', | ||
'requesterPublicKey', | ||
'senderId', | ||
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -1108,6 +1134,9 @@ Transaction.prototype.schema = { | |
timestamp: { | ||
type: 'integer' | ||
}, | ||
timestampMs: { | ||
type: 'integer' | ||
}, | ||
senderPublicKey: { | ||
type: 'string', | ||
format: 'publicKey' | ||
|
@@ -1150,7 +1179,7 @@ Transaction.prototype.schema = { | |
type: 'object' | ||
} | ||
}, | ||
required: ['type', 'timestamp', 'senderPublicKey', 'signature'] | ||
required: ['type', 'timestamp', 'timestampMs', 'senderPublicKey', 'signature'] | ||
}; | ||
|
||
/** | ||
|
@@ -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, | ||
|
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; |
There was a problem hiding this comment.
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.