Skip to content

Commit 68fa3fa

Browse files
Merge pull request #12872 from MinaProtocol/fix/no-token-owner-rampup
Allow no token owner in archive db (rampup)
2 parents 168b272 + 12b4afb commit 68fa3fa

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

src/app/archive/lib/processor.ml

+43-11
Original file line numberDiff line numberDiff line change
@@ -94,36 +94,68 @@ module Token = struct
9494
let find_opt (module Conn : CONNECTION) =
9595
make_finder Conn.find_opt Caqti_request.find_opt
9696

97+
let find_no_owner_opt (module Conn : CONNECTION) token_id =
98+
let value = Token_id.to_string token_id in
99+
Conn.find_opt
100+
(Caqti_request.find_opt Caqti_type.string Caqti_type.int
101+
{sql| SELECT id
102+
FROM tokens
103+
WHERE value = $1
104+
AND owner_public_key_id IS NULL
105+
AND owner_token_id IS NULL
106+
|sql} )
107+
value
108+
109+
let set_owner (module Conn : CONNECTION) ~id ~owner_public_key_id
110+
~owner_token_id =
111+
Conn.find
112+
(Caqti_request.find
113+
Caqti_type.(tup3 int int int)
114+
Caqti_type.int
115+
{sql| UPDATE tokens
116+
SET owner_public_key_id = $2, owner_token_id = $3
117+
WHERE id = $1
118+
RETURNING id
119+
|sql} )
120+
(id, owner_public_key_id, owner_token_id)
121+
97122
let add_if_doesn't_exist (module Conn : CONNECTION) token_id =
98123
let open Deferred.Result.Let_syntax in
99-
let value = Token_id.(to_string token_id) in
124+
let value = Token_id.to_string token_id in
100125
match Token_owners.find_owner token_id with
101126
| None ->
102-
assert (Token_id.(equal default) token_id) ;
127+
(* not necessarily the default token *)
103128
Mina_caqti.select_insert_into_cols ~select:("id", Caqti_type.int)
104129
~table_name ~cols:(Fields.names, typ)
105130
(module Conn)
106131
{ value; owner_public_key_id = None; owner_token_id = None }
107-
| Some acct_id ->
132+
| Some acct_id -> (
108133
assert (not @@ Token_id.(equal default) token_id) ;
134+
assert (
135+
Token_id.equal (Account_id.derive_token_id ~owner:acct_id) token_id ) ;
109136
(* we can only add this token if its owner exists
110137
that means if we add several tokens in a block,
111138
we must add them in topologically sorted order
112139
*)
113140
let%bind owner_public_key_id =
114141
let owner_pk = Account_id.public_key acct_id in
115-
let%map id = Public_key.add_if_doesn't_exist (module Conn) owner_pk in
116-
Some id
142+
Public_key.add_if_doesn't_exist (module Conn) owner_pk
117143
in
118144
let%bind owner_token_id =
119145
let owner_tid = Account_id.token_id acct_id in
120-
let%map id = find (module Conn) owner_tid in
121-
Some id
146+
find (module Conn) owner_tid
122147
in
123-
Mina_caqti.select_insert_into_cols ~select:("id", Caqti_type.int)
124-
~table_name ~cols:(Fields.names, typ)
125-
(module Conn)
126-
{ value; owner_public_key_id; owner_token_id }
148+
match%bind find_no_owner_opt (module Conn) token_id with
149+
| Some id ->
150+
(* existing entry, no owner *)
151+
set_owner (module Conn) ~id ~owner_public_key_id ~owner_token_id
152+
| None ->
153+
let owner_public_key_id = Some owner_public_key_id in
154+
let owner_token_id = Some owner_token_id in
155+
Mina_caqti.select_insert_into_cols ~select:("id", Caqti_type.int)
156+
~table_name ~cols:(Fields.names, typ)
157+
(module Conn)
158+
{ value; owner_public_key_id; owner_token_id } )
127159
end
128160

129161
module Voting_for = struct

0 commit comments

Comments
 (0)