Browse Source

backend: fix timestamp issue

Joystream Stats 4 years ago
parent
commit
aa8b3cb692
1 changed files with 11 additions and 5 deletions
  1. 11 5
      server/joystream/index.ts

+ 11 - 5
server/joystream/index.ts

@@ -84,10 +84,12 @@ const getEraAtHash = (api: Api, hash: string) =>
 const getEraAtBlock = async (api: Api, block: number) =>
 const getEraAtBlock = async (api: Api, block: number) =>
   getEraAtHash(api, await getBlockHash(api, block))
   getEraAtHash(api, await getBlockHash(api, block))
 
 
-const getTimestamp = async (api: Api, hash?: string) =>
-  hash
-    ? moment.utc(await api.query.timestamp.now.at(hash)).valueOf()
-    : moment.utc(await api.query.timestamp.now()).valueOf()
+const getTimestamp = async (api: Api, hash?: string) => {
+  const timestamp = hash
+    ? await api.query.timestamp.now.at(hash)
+    : await api.query.timestamp.now()
+  return moment.utc(timestamp.toNumber()).valueOf()
+}
 
 
 const findCouncilAtBlock = (api: Api, block: number) =>
 const findCouncilAtBlock = (api: Api, block: number) =>
   Council.findOne({
   Council.findOne({
@@ -137,12 +139,16 @@ const addBlock = async (
 }
 }
 
 
 const processBlock = async (api: Api, id: number) => {
 const processBlock = async (api: Api, id: number) => {
+  const exists = await Block.findByPk(id)
+  if (exists) return exists
+
   processing = `block ${id}`
   processing = `block ${id}`
   const last = await Block.findByPk(id - 1)
   const last = await Block.findByPk(id - 1)
   const [block] = await Block.findOrCreate({ where: { id } })
   const [block] = await Block.findOrCreate({ where: { id } })
   block.hash = await getBlockHash(api, id)
   block.hash = await getBlockHash(api, id)
   block.timestamp = await getTimestamp(api, block.hash)
   block.timestamp = await getTimestamp(api, block.hash)
-  block.blocktime = last ? block.timestamp - last.timestamp : 6000
+  block.blocktime =
+    last && last.timestamp > 0 ? block.timestamp - last.timestamp : 6000
   block.save()
   block.save()
 
 
   processEvents(api, id, block.hash)
   processEvents(api, id, block.hash)