Skip to content

feat: extract compute_hash function #929

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

Merged
merged 1 commit into from
May 20, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: Create compute_hash function
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
create or replace function compute_hash(previous_hash bytea, r logs)
returns bytea
language plpgsql
as
$$
declare
marshalledAsJSON varchar;
begin
-- select only fields participating in the hash on the backend and format json representation the same way
select '{' ||
'"type":"' || r.type || '",' ||
'"data":' || encode(r.memento, 'escape') || ',' ||
'"date":"' || (to_json(r.date::timestamp)#>>'{}') || 'Z",' ||
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Format timestamps in UTC explicitly.
Casting a timestamptz to timestamp applies the server’s timezone. For a stable hash, format the date in UTC, e.g.:

-  '"date":"' || (to_json(r.date::timestamp)#>>'{}') || 'Z",' ||
+  '"date":"' || to_char(r.date AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') || '",' ||
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
'"date":"' || (to_json(r.date::timestamp)#>>'{}') || 'Z",' ||
'"date":"' || to_char(r.date AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') || '",' ||
🤖 Prompt for AI Agents
In internal/storage/bucket/migrations/35-create-compute-hash-function/up.sql at
line 13, the timestamp is cast to timestamp without timezone, which applies the
server's timezone and can cause inconsistent hashes. Modify the code to
explicitly format the timestamp in UTC by converting the timestamptz to UTC
before formatting, ensuring stable and consistent hash values regardless of
server timezone.

'"idempotencyKey":"' || coalesce(r.idempotency_key, '') || '",' ||
'"id":0,' ||
'"hash":null' ||
'}' into marshalledAsJSON;

return (select public.digest(
case
when previous_hash is null
then marshalledAsJSON::bytea
else '"' || encode(previous_hash::bytea, 'base64')::bytea || E'"\n' || marshalledAsJSON::bytea
end || E'\n', 'sha256'::text
));
end;
$$ set search_path = '{{ .Schema }}';

create or replace function set_log_hash()
returns trigger
security definer
language plpgsql
as
$$
declare
previousHash bytea;
begin
select hash into previousHash
from logs
where ledger = new.ledger
order by seq desc
limit 1;

new.hash = compute_hash(previousHash, new);

return new;
end;
$$ set search_path = '{{ .Schema }}';
Loading