Skip to content

Commit 015c6d5

Browse files
Convert MerkleProof timestamp from Long to java.time.Instant
Co-authored-by: mitchelllisle <18128531+mitchelllisle@users.noreply.github.com>
1 parent 6fa5962 commit 015c6d5

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/main/scala/org/mitchelllisle/analysers/MerkleTree.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.mitchelllisle.analysers
33
import org.apache.spark.sql.{DataFrame, functions => F}
44
import org.apache.spark.sql.types._
55
import java.security.MessageDigest
6+
import java.time.Instant
67
import scala.annotation.tailrec
78

89
/** MerkleTreeAnalyser provides cryptographic proof capabilities for data retention and deletion verification.
@@ -14,7 +15,7 @@ object MerkleTree {
1415
rootHash: String,
1516
recordCount: Long,
1617
leafHashes: Seq[String],
17-
timestamp: Long = System.currentTimeMillis()
18+
timestamp: Instant = Instant.now()
1819
)
1920

2021
case class DeletionProof(
@@ -98,7 +99,7 @@ object MerkleTree {
9899
rootHash = rootHash,
99100
recordCount = recordCount,
100101
leafHashes = leafHashes,
101-
timestamp = System.currentTimeMillis()
102+
timestamp = Instant.now()
102103
)
103104
}
104105

src/test/scala/MerkleTreeTest.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import org.apache.spark.sql.DataFrame
22
import org.mitchelllisle.analysers.MerkleTree
33
import org.scalatest.flatspec.AnyFlatSpec
4+
import java.time.Instant
45

56
class MerkleTreeTest extends AnyFlatSpec with SparkFunSuite {
67
import spark.implicits._
@@ -21,7 +22,8 @@ class MerkleTreeTest extends AnyFlatSpec with SparkFunSuite {
2122
assert(proof.rootHash.nonEmpty)
2223
assert(proof.recordCount == 4)
2324
assert(proof.leafHashes.length == 4)
24-
assert(proof.timestamp > 0)
25+
assert(proof.timestamp.isInstanceOf[Instant])
26+
assert(proof.timestamp.isBefore(Instant.now().plusSeconds(1)))
2527
}
2628

2729
"apply" should "produce same result as createMerkleProof" in {
@@ -183,4 +185,14 @@ class MerkleTreeTest extends AnyFlatSpec with SparkFunSuite {
183185

184186
assert(hashes1.sameElements(hashes2))
185187
}
188+
189+
"timestamp" should "use proper Instant type and be recent" in {
190+
val beforeTime = Instant.now().minusSeconds(1)
191+
val proof = MerkleTree.createMerkleProof(testData, columns, idColumn)
192+
val afterTime = Instant.now().plusSeconds(1)
193+
194+
assert(proof.timestamp.isInstanceOf[Instant])
195+
assert(proof.timestamp.isAfter(beforeTime))
196+
assert(proof.timestamp.isBefore(afterTime))
197+
}
186198
}

0 commit comments

Comments
 (0)