aboutsummaryrefslogtreecommitdiffhomepage
path: root/tde2e/td/e2e/Blockchain.cpp
blob: 139e4a3e0288deff8b4590572d87821c1fda9a0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/e2e/Blockchain.h"

#include "td/e2e/Keys.h"

#include "td/telegram/e2e_api.hpp"

#include "td/utils/algorithm.h"
#include "td/utils/as.h"
#include "td/utils/common.h"
#include "td/utils/crypto.h"
#include "td/utils/format.h"
#include "td/utils/misc.h"
#include "td/utils/overloaded.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/tl_parsers.h"

#include <algorithm>
#include <limits>
#include <map>
#include <set>
#include <tuple>
#include <utility>

namespace tde2e_core {

GroupParticipant GroupParticipant::from_tl(const td::e2e_api::e2e_chain_groupParticipant &participant) {
  return GroupParticipant{participant.user_id_, participant.flags_, PublicKey::from_u256(participant.public_key_),
                          participant.version_};
}

e2e::object_ptr<e2e::e2e_chain_groupParticipant> GroupParticipant::to_tl() const {
  return e2e::make_object<e2e::e2e_chain_groupParticipant>(user_id, public_key.to_u256(), flags, add_users(),
                                                           remove_users(), version);
}

td::int32 GroupState::version() const {
  if (participants.empty()) {
    return 0;
  }
  td::int32 version = participants.front().version;
  for (auto &participant : participants) {
    version = std::min(version, participant.version);
  }
  return std::clamp(version, 0, 255);
}

td::Result<GroupParticipant> GroupState::get_participant(td::int64 user_id) const {
  for (const auto &participant : participants) {
    if (participant.user_id == user_id) {
      return participant;
    }
  }
  return td::Status::Error("Participant not found");
}

td::Result<GroupParticipant> GroupState::get_participant(const PublicKey &public_key) const {
  for (const auto &participant : participants) {
    if (participant.public_key == public_key) {
      return participant;
    }
  }
  return td::Status::Error("Participant not found");
}

Permissions GroupState::get_permissions(const PublicKey &public_key, td::int32 limit_permissions) const {
  limit_permissions &= GroupParticipantFlags::AllPermissions;
  auto r_participant = get_participant(public_key);
  if (r_participant.is_ok()) {
    return Permissions{(r_participant.ok().flags & limit_permissions) | GroupParticipantFlags::IsParticipant};
  }
  return Permissions{(external_permissions & limit_permissions)};
}

GroupStateRef GroupState::from_tl(const td::e2e_api::e2e_chain_groupState &state) {
  auto participant_from_tl = [](const td::e2e_api::object_ptr<td::e2e_api::e2e_chain_groupParticipant> &participant) {
    return GroupParticipant::from_tl(*participant);
  };
  td::optional<td::int32> external_permissions{};
  return std::make_shared<GroupState>(
      GroupState{td::transform(state.participants_, participant_from_tl), state.external_permissions_});
}

e2e::object_ptr<e2e::e2e_chain_groupState> GroupState::to_tl() const {
  return e2e::make_object<e2e::e2e_chain_groupState>(
      td::transform(participants, [](const GroupParticipant &participant) { return participant.to_tl(); }),
      external_permissions);
}

GroupStateRef GroupState::empty_state() {
  static GroupStateRef state = std::make_shared<GroupState>();
  return state;
}

GroupSharedKeyRef GroupSharedKey::from_tl(const td::e2e_api::e2e_chain_sharedKey &shared_key) {
  return std::make_shared<GroupSharedKey>(GroupSharedKey{PublicKey::from_u256(shared_key.ek_),
                                                         shared_key.encrypted_shared_key_, shared_key.dest_user_id_,
                                                         shared_key.dest_header_});
}

e2e::object_ptr<e2e::e2e_chain_sharedKey> GroupSharedKey::to_tl() const {
  return e2e::make_object<e2e::e2e_chain_sharedKey>(ek.to_u256(), encrypted_shared_key,
                                                    std::vector<td::int64>(dest_user_id), std::vector(dest_header));
}

GroupSharedKeyRef GroupSharedKey::empty_shared_key() {
  static GroupSharedKeyRef shared_key = std::make_shared<GroupSharedKey>();
  return shared_key;
}

ChangeSetValue ChangeSetValue::from_tl(const td::e2e_api::e2e_chain_changeSetValue &change) {
  return ChangeSetValue{change.key_, change.value_};
}

e2e::object_ptr<e2e::e2e_chain_changeSetValue> ChangeSetValue::to_tl() const {
  return e2e::make_object<e2e::e2e_chain_changeSetValue>(key, value);
}

ChangeSetGroupState ChangeSetGroupState::from_tl(const td::e2e_api::e2e_chain_changeSetGroupState &change) {
  return ChangeSetGroupState{GroupState::from_tl(*change.group_state_)};
}

e2e::object_ptr<e2e::e2e_chain_changeSetGroupState> ChangeSetGroupState::to_tl() const {
  return e2e::make_object<e2e::e2e_chain_changeSetGroupState>(group_state->to_tl());
}

ChangeSetSharedKey ChangeSetSharedKey::from_tl(const td::e2e_api::e2e_chain_changeSetSharedKey &change) {
  return ChangeSetSharedKey{GroupSharedKey::from_tl(*change.shared_key_)};
}

e2e::object_ptr<e2e::e2e_chain_changeSetSharedKey> ChangeSetSharedKey::to_tl() const {
  return e2e::make_object<e2e::e2e_chain_changeSetSharedKey>(shared_key->to_tl());
}

Change Change::from_tl(const td::e2e_api::e2e_chain_Change &change) {
  Change res;
  downcast_call(
      const_cast<td::e2e_api::e2e_chain_Change &>(change),
      td::overloaded(
          [&](td::e2e_api::e2e_chain_changeNoop &change_t) { res.value = ChangeNoop::from_tl(change_t); },
          [&](td::e2e_api::e2e_chain_changeSetValue &change_t) { res.value = ChangeSetValue::from_tl(change_t); },
          [&](td::e2e_api::e2e_chain_changeSetGroupState &change_t) {
            res.value = ChangeSetGroupState::from_tl(change_t);
          },
          [&](td::e2e_api::e2e_chain_changeSetSharedKey &change_t) {
            res.value = ChangeSetSharedKey::from_tl(change_t);
          }));
  return res;
}

e2e::object_ptr<e2e::e2e_chain_Change> Change::to_tl() const {
  return std::visit(
      td::overloaded(
          [](const ChangeNoop &change) -> td::e2e_api::object_ptr<e2e::e2e_chain_Change> { return change.to_tl(); },
          [](const ChangeSetValue &change) -> td::e2e_api::object_ptr<e2e::e2e_chain_Change> { return change.to_tl(); },
          [](const ChangeSetGroupState &change) -> td::e2e_api::object_ptr<e2e::e2e_chain_Change> {
            return change.to_tl();
          },
          [](const ChangeSetSharedKey &change) -> td::e2e_api::object_ptr<e2e::e2e_chain_Change> {
            return change.to_tl();
          }),
      value);
}

td::UInt256 Block::calc_hash() const {
  if (height_ == -1) {
    return {};
  }
  auto serialized_block = serialize_boxed(*to_tl());
  td::UInt256 hash;
  td::sha256(serialized_block, hash.as_mutable_slice());
  return hash;
}

Block Block::from_tl(const e2e::e2e_chain_block &block) {
  Block result;
  result.state_proof_ = StateProof::from_tl(*block.state_proof_);
  if (block.flags_ & 1) {
    result.o_signature_public_key_ = PublicKey::from_u256(block.signature_public_key_);
  }
  result.signature_ = Signature::from_u512(block.signature_);
  result.prev_block_hash_ = block.prev_block_hash_;
  auto change_from_tl = [&](auto &obj) {
    return Change::from_tl(*obj);
  };
  result.changes_ = td::transform(block.changes_, change_from_tl);
  result.height_ = block.height_;
  return result;
}

td::Result<Block> Block::from_tl_serialized(td::Slice new_block) {
  td::TlParser parser(new_block);
  auto magic = parser.fetch_int();
  if (magic != td::e2e_api::e2e_chain_block::ID) {
    return td::Status::Error(PSLICE() << "Expected magic " << td::format::as_hex(td::e2e_api::e2e_chain_block::ID)
                                      << ", but received " << td::format::as_hex(magic));
  }
  auto block_tl = td::e2e_api::e2e_chain_block::fetch(parser);
  parser.fetch_end();
  TRY_STATUS(parser.get_status());
  return from_tl(*block_tl);
}

e2e::object_ptr<e2e::e2e_chain_block> Block::to_tl() const {
  td::int32 flags{};
  auto state_proof = state_proof_.to_tl();
  td::UInt256 public_key{};
  if (o_signature_public_key_) {
    public_key = o_signature_public_key_.value().to_u256();
    flags |= e2e::e2e_chain_block::SIGNATURE_PUBLIC_KEY_MASK;
  }
  auto changes = td::transform(changes_, [](const auto &change) { return change.to_tl(); });

  return e2e::make_object<e2e::e2e_chain_block>(signature_.to_u512(), flags, prev_block_hash_, std::move(changes),
                                                height_, std::move(state_proof), public_key);
}

std::string Block::to_tl_serialized() const {
  return serialize_boxed(*to_tl());
}

td::StringBuilder &operator<<(td::StringBuilder &sb, const Block &block) {
  return sb << "Block(sign=" << block.signature_
            << "..., prev_hash=" << hex_encode(block.prev_block_hash_.as_slice().substr(0, 8))
            << "\theight=" << block.height_ << " \n"
            << "\tproof=" << block.state_proof_ << "\n"
            << "\tchanges=" << block.changes_ << "\n"
            << "\tsignature_key=" << block.o_signature_public_key_ << ")";
}

td::Result<BitString> key_to_bitstring(td::Slice key) {
  if (key.size() != 32) {
    return td::Status::Error("Invalid key size");
  }
  return BitString(key);
}

td::Result<std::string> KeyValueState::get_value(td::Slice key) const {
  TRY_RESULT(bitstring, key_to_bitstring(key));
  return get(node_, bitstring, snapshot_.value());
}

td::Result<std::string> KeyValueState::gen_proof(td::Span<td::Slice> keys) const {
  TRY_RESULT(pruned_tree, generate_pruned_tree(node_, keys, snapshot_.value()));
  return TrieNode::serialize_for_network(pruned_tree);
}

td::Result<KeyValueState> KeyValueState::create_from_hash(KeyValueHash hash) {
  auto node = std::make_shared<TrieNode>(hash.hash);
  return KeyValueState{std::move(node), td::Slice()};
}

td::Result<KeyValueState> KeyValueState::create_from_snapshot(td::Slice snapshot) {
  TRY_RESULT(node, TrieNode::fetch_from_snapshot(snapshot));
  return KeyValueState{std::move(node), snapshot};
}

td::Result<std::string> KeyValueState::build_snapshot() const {
  return TrieNode::serialize_for_snapshot(node_, snapshot_.value());
}

td::Status KeyValueState::set_value(td::Slice key, td::Slice value) {
  TRY_RESULT(bitstring, key_to_bitstring(key));
  TRY_RESULT_ASSIGN(node_, set(node_, bitstring, value, snapshot_.value()));
  return td::Status::OK();
}

td::UInt256 KeyValueState::get_hash() const {
  return node_->hash;
}

StateProof StateProof::from_tl(const td::e2e_api::e2e_chain_stateProof &proof) {
  StateProof res;
  res.kv_hash = KeyValueHash{proof.kv_hash_};
  if (proof.group_state_) {
    res.o_group_state = GroupState::from_tl(*proof.group_state_);
  }
  if (proof.shared_key_) {
    res.o_shared_key = GroupSharedKey::from_tl(*proof.shared_key_);
  }
  return res;
}

e2e::object_ptr<e2e::e2e_chain_stateProof> StateProof::to_tl() const {
  td::int32 flags{};
  e2e::object_ptr<e2e::e2e_chain_groupState> o_group_state_tl;
  if (o_group_state) {
    o_group_state_tl = o_group_state.value()->to_tl();
    flags |= td::e2e_api::e2e_chain_stateProof::GROUP_STATE_MASK;
  }
  e2e::object_ptr<e2e::e2e_chain_sharedKey> o_shared_key_tl;
  if (o_shared_key) {
    o_shared_key_tl = o_shared_key.value()->to_tl();
    flags |= td::e2e_api::e2e_chain_stateProof::SHARED_KEY_MASK;
  }

  return e2e::make_object<e2e::e2e_chain_stateProof>(flags, kv_hash.hash, std::move(o_group_state_tl),
                                                     std::move(o_shared_key_tl));
}

td::StringBuilder &operator<<(td::StringBuilder &sb, const StateProof &state) {
  sb << "StateProof{";
  sb << "\n\tkv=" << td::format::as_hex_dump<0>(state.kv_hash.hash.as_slice().substr(0, 8));
  if (state.o_group_state) {
    sb << "\n\tgroup=" << **state.o_group_state;
  }
  if (state.o_shared_key) {
    sb << "\n\tgroup=" << **state.o_shared_key;
  }
  return sb << "}";
}

State State::create_empty() {
  return State{KeyValueState{}, GroupState::empty_state(), GroupSharedKey::empty_shared_key()};
}

td::Status State::set_value(td::Slice key, td::Slice value, const Permissions &permissions) {
  if (!permissions.may_set_value()) {
    return Error(E::InvalidBlock_NoPermissions, "Can't set value");
  }
  return key_value_state_.set_value(key, value);
}

td::Status State::set_value_fast(const KeyValueHash &key_value_hash) {
  TRY_RESULT_ASSIGN(key_value_state_, KeyValueState::create_from_hash(key_value_hash));
  return td::Status::OK();
}

td::Status State::validate_group_state(const GroupStateRef &group_state) {
  std::set<td::int64> new_user_ids;
  std::set<PublicKey> new_keys;
  for (const auto &p : group_state->participants) {
    new_user_ids.insert(p.user_id);
    new_keys.insert(p.public_key);
    if ((p.flags & ~GroupParticipantFlags::AllPermissions) != 0) {
      return Error(E::InvalidBlock_InvalidGroupState, "invalid permissions");
    }
  }
  if ((group_state->external_permissions & ~GroupParticipantFlags::AllPermissions) != 0) {
    return Error(E::InvalidBlock_InvalidGroupState, "invalid external permissions");
  }
  if (new_user_ids.size() != group_state->participants.size()) {
    return Error(E::InvalidBlock_InvalidGroupState, "duplicate user_id");
  }
  if (new_keys.size() != group_state->participants.size()) {
    return Error(E::InvalidBlock_InvalidGroupState, "duplicate public_key");
  }
  return td::Status::OK();
}

td::Status State::set_group_state(GroupStateRef group_state, const Permissions &permissions) {
  TRY_STATUS(validate_group_state(group_state));

  std::map<std::pair<td::int64, PublicKey>, td::int32> old_participants;
  std::map<std::pair<td::int64, PublicKey>, td::int32> new_participants;

  for (const auto &p : group_state_->participants) {
    old_participants[std::make_pair(p.user_id, p.public_key)] = p.flags;
  }
  for (const auto &p : group_state->participants) {
    new_participants[std::make_pair(p.user_id, p.public_key)] = p.flags;
  }
  if ((~group_state_->external_permissions & group_state->external_permissions) != 0) {
    return Error(E::InvalidBlock_NoPermissions, "Can't increase external permissions");
  }

  td::int32 needed_flags = 0;
  for (const auto &[p, flags] : old_participants) {
    if (!new_participants.count(p)) {
      if (!permissions.may_remove_users()) {
        return Error(E::InvalidBlock_NoPermissions, "Can't remove users");
      }
    }
  }
  for (const auto &[p, flags] : new_participants) {
    auto old_p = old_participants.find(p);
    if (old_p == old_participants.end()) {
      if (!permissions.may_add_users()) {
        return Error(E::InvalidBlock_NoPermissions, "Can't add users");
      }
      needed_flags |= flags;
    } else if (flags != old_p->second) {
      if (!permissions.may_add_users() || !permissions.may_remove_users()) {
        return Error(E::InvalidBlock_NoPermissions, "Can't add users");
      }
      needed_flags |= flags & ~old_p->second;
    }
  }

  td::int32 missing_flags = needed_flags & ~(permissions.flags & GroupParticipantFlags::AllPermissions);
  if (missing_flags != 0) {
    return Error(E::InvalidBlock_NoPermissions, "Can't give more permissions than we have");
  }
  group_state_ = std::move(group_state);
  return td::Status::OK();
}

td::Status State::clear_shared_key(const Permissions &permissions) {
  if (!permissions.may_change_shared_key()) {
    return Error(E::InvalidBlock_NoPermissions, "Can't clear shared key");
  }
  shared_key_ = GroupSharedKey::empty_shared_key();
  return td::Status::OK();
}

td::Status State::validate_shared_key(const GroupSharedKeyRef &shared_key, const GroupStateRef &group_state) {
  if (shared_key->empty()) {
    return td::Status::OK();
  }
  if (shared_key->dest_user_id.size() != shared_key->dest_header.size()) {
    return Error(E::InvalidBlock_InvalidSharedSecret, "different number of users and headers");
  }
  if (shared_key->dest_user_id.size() != group_state->participants.size()) {
    return Error(E::InvalidBlock_InvalidSharedSecret, "wrong number of users");
  }
  std::set<td::int64> participants;
  for (const auto user_id : shared_key->dest_user_id) {
    participants.insert(user_id);
  }
  if (participants.size() != shared_key->dest_user_id.size()) {
    return Error(E::InvalidBlock_InvalidSharedSecret, "duplicate users");
  }
  for (auto &p : group_state->participants) {
    if (!participants.count(p.user_id)) {
      return Error(E::InvalidBlock_InvalidSharedSecret, "unknown user_id");
    }
  }
  return td::Status::OK();
}

td::Status State::set_shared_key(GroupSharedKeyRef shared_key, const Permissions &permissions) {
  if (*shared_key_ != *GroupSharedKey::empty_shared_key()) {
    return td::Status::Error("Shared key is already set");
  }
  if (!permissions.may_change_shared_key()) {
    return Error(E::InvalidBlock_NoPermissions, "Can't set shared key");
  }
  TRY_STATUS(validate_shared_key(shared_key, group_state_));
  shared_key_ = std::move(shared_key);
  return td::Status::OK();
}

td::Status State::validate_state(const StateProof &state_proof) const {
  if (state_proof.kv_hash.hash != key_value_state_.get_hash()) {
    return td::Status::Error("State hash mismatch");
  }

  if (!has_group_state_change_ && !has_set_value_) {
    return Error(E::InvalidBlock_NoChanges, "There must be at least SetValue or SetGroupState changes");
  }
  if (has_group_state_change_ && state_proof.o_group_state) {
    return Error(E::InvalidBlock_InvalidStateProof_Group,
                 "Group state must be omitted when there is a group state change");
  }
  if (!has_group_state_change_ && !state_proof.o_group_state) {
    return Error(E::InvalidBlock_InvalidStateProof_Group,
                 "Group state must be provided when there is no group state change");
  }
  if (!has_group_state_change_ && **state_proof.o_group_state != *group_state_) {
    return Error(E::InvalidBlock_InvalidStateProof_Group, "group state differs");
  }

  bool shared_key_must_be_omitted = has_group_state_change_ || has_shared_key_change_;
  if (shared_key_must_be_omitted && state_proof.o_shared_key) {
    return Error(E::InvalidBlock_InvalidStateProof_Secret, "Shared key state must be omitted");
  }
  if (!shared_key_must_be_omitted && !state_proof.o_shared_key) {
    return Error(E::InvalidBlock_InvalidStateProof_Secret, "Shared key state must be provided");
  }
  if (!shared_key_must_be_omitted && **state_proof.o_shared_key != *shared_key_) {
    return Error(E::InvalidBlock_InvalidStateProof_Secret, "shared key state differs");
  }

  TRY_STATUS(validate_group_state(group_state_));
  TRY_STATUS(validate_shared_key(shared_key_, group_state_));

  return td::Status::OK();
}

td::Status State::apply_change(const Change &change_outer, const PublicKey &public_key,
                               const ValidateOptions &validate_options) {
  bool full_apply = validate_options.validate_state_hash;
  auto limit_permissions = validate_options.permissions;
  return std::visit(
      td::overloaded(
          [](const ChangeNoop &change) { return td::Status::OK(); },
          [this, full_apply, limit_permissions, &public_key](const ChangeSetValue &change) {
            has_set_value_ = true;
            if (full_apply) {
              return set_value(change.key, change.value, group_state_->get_permissions(public_key, limit_permissions));
            }
            return td::Status::OK();
          },
          [this, limit_permissions, &public_key](const ChangeSetGroupState &change) {
            has_group_state_change_ = true;
            TRY_STATUS(
                set_group_state(change.group_state, group_state_->get_permissions(public_key, limit_permissions)));
            return clear_shared_key(group_state_->get_permissions(public_key, limit_permissions));
          },
          [this, limit_permissions, &public_key](const ChangeSetSharedKey &change) {
            has_shared_key_change_ = true;
            return set_shared_key(change.shared_key, group_state_->get_permissions(public_key, limit_permissions));
          }),
      change_outer.value);
}

td::Status State::apply(Block &block, ValidateOptions validate_options) {
  // To apply the first block an ephemeral -1 block is used
  //   - It has only one participant - Participant(user_id = 0, public_key = signer_public_key, permissions = all)
  if (block.height_ == 0) {
    CHECK(group_state_->empty());
    group_state_ = std::make_unique<GroupState>(GroupState{{}, GroupParticipantFlags::AllPermissions});
  }

  td::optional<PublicKey> o_signature_public_key = block.o_signature_public_key_;
  if (!o_signature_public_key && !group_state_->empty()) {
    o_signature_public_key = group_state_->participants[0].public_key;
  }
  if (!o_signature_public_key) {
    return td::Status::Error("Unknown public key");
  }

  // 5. Verifies the signature of the block.
  if (validate_options.validate_signature) {
    TRY_STATUS(block.verify_signature(o_signature_public_key.value()));
  }

  // 6. Applies the changes to the state.
  //   - If `validate_state_hash` is true, the state hash is validated.
  //   - Otherwise, the state hash is set to the hash of the block.
  has_set_value_ = false;
  has_shared_key_change_ = false;
  has_group_state_change_ = false;
  for (auto &change : block.changes_) {
    TRY_STATUS(apply_change(change, o_signature_public_key.value(), validate_options));
  }
  if (!validate_options.validate_state_hash) {
    TRY_STATUS(set_value_fast(block.state_proof_.kv_hash));
  }

  TRY_STATUS(validate_state(block.state_proof_));

  return td::Status::OK();
}

td::Result<State> State::create_from_block(const Block &block, td::optional<td::Slice> o_snapshot) {
  KeyValueState key_value_state;
  GroupStateRef group_state;
  GroupSharedKeyRef shared_key;

  if (o_snapshot) {
    TRY_RESULT_ASSIGN(key_value_state, KeyValueState::create_from_snapshot(o_snapshot.value()));
  } else {
    TRY_RESULT_ASSIGN(key_value_state, KeyValueState::create_from_hash(block.state_proof_.kv_hash));
  }

  // For the first block we fixup group state.
  if (block.height_ == 0) {
    group_state = std::make_shared<GroupState>(GroupState{{}, GroupParticipantFlags::AllPermissions});
  }

  bool has_set_value = false;
  bool has_group_state_change = false;
  bool has_shared_key_change = false;
  for (const auto &change_v : block.changes_) {
    std::visit(
        td::overloaded([](const ChangeNoop &change) {}, [&](const ChangeSetValue &change) { has_set_value = true; },
                       [&](const ChangeSetGroupState &change) {
                         group_state = change.group_state;
                         shared_key = GroupSharedKey::empty_shared_key();
                         has_group_state_change = true;
                       },
                       [&](const ChangeSetSharedKey &change) {
                         shared_key = change.shared_key;
                         has_shared_key_change = true;
                       }),
        change_v.value);
  }

  if (block.state_proof_.o_group_state) {
    group_state = block.state_proof_.o_group_state.value();
  }
  if (block.state_proof_.o_shared_key) {
    shared_key = block.state_proof_.o_shared_key.value();
  }
  if (!group_state) {
    return Error(E::InvalidBlock_InvalidStateProof_Group, "no group state proof");
  }
  if (!shared_key) {
    return Error(E::InvalidBlock_InvalidStateProof_Secret, "no shared key");
  }

  auto state = State(key_value_state, group_state, shared_key);
  state.has_set_value_ = has_set_value;
  state.has_group_state_change_ = has_group_state_change;
  state.has_shared_key_change_ = has_shared_key_change;
  TRY_STATUS(state.validate_state(block.state_proof_));
  return state;
}

td::Result<Block> Blockchain::build_block(std::vector<Change> changes, const PrivateKey &private_key) const {
  //TODO: check if we are allowed to sign this block
  auto public_key = private_key.to_public_key();
  auto state = state_;
  if (last_block_.height_ == std::numeric_limits<td::int32>::max()) {
    return td::Status::Error("Blockchain::build_block: last block height is too high");
  }
  td::int32 height = last_block_.height_ + 1;
  if (height == 0) {
    state.group_state_ = std::make_shared<GroupState>(GroupState{{}, GroupParticipantFlags::AllPermissions});
  }

  ValidateOptions validate_options;
  validate_options.validate_state_hash = true;
  validate_options.validate_signature = false;
  validate_options.permissions = GroupParticipantFlags::AllPermissions;
  for (const auto &change : changes) {
    TRY_STATUS(state.apply_change(change, public_key, validate_options));
  }

  StateProof state_proof;
  state_proof.kv_hash = KeyValueHash{state.key_value_state_.get_hash()};
  state_proof.o_group_state = state.group_state_;
  state_proof.o_shared_key = state.shared_key_;
  state.has_set_value_ = false;
  state.has_group_state_change_ = false;
  state.has_shared_key_change_ = false;
  for (const auto &change_v : changes) {
    std::visit(td::overloaded([](const ChangeNoop &change) {},
                              [&](const ChangeSetValue &change) { state.has_set_value_ = true; },
                              [&](const ChangeSetGroupState &change) {
                                state_proof.o_group_state = {};
                                state_proof.o_shared_key = {};
                                state.has_group_state_change_ = true;
                              },
                              [&](const ChangeSetSharedKey &change) {
                                state_proof.o_shared_key = {};
                                state.has_shared_key_change_ = true;
                              }),
               change_v.value);
  }
  TRY_STATUS(state.validate_state(state_proof));

  Block block;
  block.height_ = height;
  block.prev_block_hash_ = last_block_hash_;
  block.changes_ = std::move(changes);
  block.o_signature_public_key_ = public_key;
  block.state_proof_ = std::move(state_proof);
  TRY_STATUS(block.sign_inplace(private_key));
  return block;
}

td::Status Blockchain::try_apply_block(Block block, ValidateOptions validate_options) {
  // To apply the first block an ephemeral -1 block is used
  //   - It has hash UInt256(0)
  //   - It has height -1
  //   - It has only one participant - Participant(user_id = 0, public_key = signer_public_key, permissions = all)

  if (block.height_ != get_height() + 1 || get_height() == std::numeric_limits<td::int32>::max()) {
    return Error(E::InvalidBlock_HeightMismatch,
                 PSLICE() << "new_block.height=" << block.height_ << " != 1 + last_block.height=" << get_height());
  }

  if (block.prev_block_hash_ != last_block_hash_) {
    return Error(E::InvalidBlock_HashMismatch);
  }

  // TODO: validate total size of block
  auto state = state_;
  TRY_STATUS(state.apply(block, validate_options));

  // NO errors after this point
  state_ = std::move(state);

  last_block_hash_ = block.calc_hash();

  last_block_ = std::move(block);
  return td::Status::OK();
}

Block Blockchain::set_value(td::Slice key, td::Slice value, const PrivateKey &private_key) const {
  return build_block({Change{ChangeSetValue{key.str(), value.str()}}}, private_key).move_as_ok();
}

td::int64 Blockchain::get_height() const {
  return last_block_.height_;
}

td::Result<td::UInt256> as_key(td::Slice key) {
  if (key.size() != 32) {
    return td::Status::Error("Invalid key size");
  }
  td::UInt256 key_int256;
  key_int256.as_mutable_slice().copy_from(key);
  if (key_int256.is_zero()) {
    return td::Status::Error("Invalid zero key");
  }
  return key_int256;
}

td::Result<Blockchain> Blockchain::create_from_block(Block block, td::optional<td::Slice> o_snapshot) {
  if (block.height_ < 0) {
    return Error(E::InvalidBlock, "negative height");
  }
  Blockchain res;
  res.last_block_hash_ = block.calc_hash();
  TRY_RESULT_ASSIGN(res.state_, State::create_from_block(block, std::move(o_snapshot)));
  res.last_block_ = std::move(block);

  return res;
}

namespace {
bool is_good_magic(td::int32 magic) {
  return magic == td::e2e_api::e2e_chain_block::ID || magic == td::e2e_api::e2e_chain_groupBroadcastNonceCommit::ID ||
         magic == td::e2e_api::e2e_chain_groupBroadcastNonceReveal::ID;
}
}  // namespace

bool Blockchain::is_from_server(td::Slice block) {
  if (block.size() < 4) {
    return false;
  }
  td::int32 server_magic = td::as<td::int32>(block.data());
  return is_good_magic(server_magic - 1) && !is_good_magic(server_magic);
}

td::Result<std::string> Blockchain::from_any_to_local(std::string block) {
  if (is_from_server(block)) {
    return from_server_to_local(std::move(block));
  }
  return block;
}

td::Result<std::string> Blockchain::from_server_to_local(std::string block) {
  if (block.size() < 4) {
    return td::Status::Error("Block is too short");
  }
  td::int32 server_magic = td::as<td::int32>(block.data());
  if (is_good_magic(server_magic)) {
    return td::Status::Error("Trying to apply local block, not from server");
  }
  td::int32 real_magic = server_magic - 1;
  td::as<td::int32>(block.data()) = real_magic;
  return block;
}

td::Result<std::string> Blockchain::from_local_to_server(std::string block) {
  if (block.size() < 4) {
    return td::Status::Error("Block is too short");
  }
  td::int32 magic = td::as<td::int32>(block.data());
  td::as<td::int32>(block.data()) = magic + 1;
  return block;
}

td::Result<ClientBlockchain> ClientBlockchain::create_from_block(td::Slice block_slice, const PublicKey &public_key) {
  TRY_RESULT(block, Block::from_tl_serialized(block_slice));
  TRY_RESULT(blockchain, Blockchain::create_from_block(std::move(block)));
  ClientBlockchain res;
  res.blockchain_ = std::move(blockchain);
  return res;
}

td::Result<ClientBlockchain> ClientBlockchain::create_empty() {
  ClientBlockchain res;
  res.blockchain_ = Blockchain::create_empty();
  return res;
}

td::Result<std::vector<Change>> ClientBlockchain::try_apply_block(td::Slice block_slice) {
  TRY_RESULT(block, Block::from_tl_serialized(block_slice));

  ValidateOptions validate_options;
  validate_options.validate_signature = true;
  validate_options.validate_state_hash = false;
  TRY_STATUS(blockchain_.try_apply_block(block, validate_options));
  for (auto &change : block.changes_) {
    if (std::holds_alternative<ChangeSetValue>(change.value)) {
      auto &change_value = std::get<ChangeSetValue>(change.value);
      auto key = as_key(change_value.key).move_as_ok();  // already verified in try_apply_block
      map_[key] = Entry{block.height_, change_value.value};
    }
  }

  return std::move(block.changes_);
}

td::Status ClientBlockchain::add_proof(td::Slice proof) {
  TRY_RESULT(state, TrieNode::fetch_from_network(proof));

  if (state->hash != blockchain_.state_.key_value_state_.get_hash()) {
    return td::Status::Error("Invalid proof");
  }
  // TODO: merge proof
  blockchain_.state_.key_value_state_.node_ = state;
  return td::Status::OK();
}

td::Result<std::string> ClientBlockchain::build_block(const std::vector<Change> &changes,
                                                      const PrivateKey &private_key) const {
  TRY_RESULT(block, blockchain_.build_block(changes, private_key));
  return block.to_tl_serialized();
}

td::Result<std::string> ClientBlockchain::get_value(td::Slice raw_key) const {
  TRY_RESULT(key, as_key(raw_key));
  auto it = map_.find(key);
  if (it != map_.end()) {
    return it->second.value;
  }
  return blockchain_.state_.key_value_state_.get_value(raw_key);
}

}  // namespace tde2e_core