-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support old and new reputation type (#1664)
* regenerate polkadart types except for reputation * [models/ceremonies] extract current reputations to v1 * append v1 suffix to old reputation type * [enconter-api] fix encointer solo node spec name * [encointer-api] successfully parse v2 reputations * [encointer-api] fix verified linked interpretation of json * [encointer-api] correctly set reputations for v2 * [encointer-account-store] allow registering with reputation that has been linked to non-current cIndex * better doc * fmt * more logs when getting the cIndex for PoA * bump parachain spec version for reputation v2 * lower parachain spec version for reputation v2 * lower parachain spec version for reputation v2 again
- Loading branch information
Showing
162 changed files
with
20,835 additions
and
3,349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:encointer_wallet/models/ceremonies/reputation/v2.dart'; | ||
import 'package:encointer_wallet/utils/enum.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
import 'package:encointer_wallet/models/communities/community_identifier.dart'; | ||
|
||
part 'v1.g.dart'; | ||
|
||
@JsonSerializable() | ||
class CommunityReputationV1 { | ||
CommunityReputationV1(this.communityIdentifier, this.reputation); | ||
|
||
factory CommunityReputationV1.fromJson(Map<String, dynamic> json) => _$CommunityReputationV1FromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$CommunityReputationV1ToJson(this); | ||
|
||
CommunityIdentifier communityIdentifier; | ||
ReputationV1 reputation; | ||
|
||
CommunityReputation toV2(int currentCeremonyIndex) { | ||
return CommunityReputation(communityIdentifier, reputation.toV2(currentCeremonyIndex)); | ||
} | ||
|
||
@override | ||
String toString() { | ||
return jsonEncode(this); | ||
} | ||
} | ||
|
||
// For compatibility with substrate's naming convention. | ||
// ignore: constant_identifier_names | ||
enum ReputationV1 { Unverified, UnverifiedReputable, VerifiedUnlinked, VerifiedLinked } | ||
|
||
extension ReputationV1Extension on ReputationV1 { | ||
String toValue() { | ||
return toEnumValue(this); | ||
} | ||
|
||
bool isVerified() { | ||
return this == ReputationV1.VerifiedUnlinked || this == ReputationV1.VerifiedLinked; | ||
} | ||
|
||
Reputation toV2(int currentCeremonyIndex) { | ||
return switch (this) { | ||
ReputationV1.Unverified => Reputation.values.unverified(), | ||
ReputationV1.UnverifiedReputable => Reputation.values.unverifiedReputable(), | ||
ReputationV1.VerifiedUnlinked => Reputation.values.verifiedUnlinked(), | ||
ReputationV1.VerifiedLinked => Reputation.values.verifiedLinked(currentCeremonyIndex), | ||
}; | ||
} | ||
} | ||
|
||
/// Takes a `List<dynamic>` which contains a `List<List<[int, Map<String, dynamic>]>` and | ||
/// transforms it into a `Map<int, CommunityReputation>`. | ||
Map<int, CommunityReputationV1> reputationsV1FromList(List<dynamic> reputationsList) { | ||
final reputations = reputationsList.cast<List<dynamic>>(); | ||
|
||
return { | ||
for (final cr in reputations) cr[0] as int: CommunityReputationV1.fromJson(cr[1] as Map<String, dynamic>), | ||
}; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:encointer_wallet/utils/enum.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
import 'package:encointer_wallet/models/communities/community_identifier.dart'; | ||
|
||
part 'v2.g.dart'; | ||
|
||
@JsonSerializable() | ||
class CommunityReputation { | ||
CommunityReputation(this.communityIdentifier, this.reputation); | ||
|
||
factory CommunityReputation.fromJson(Map<String, dynamic> json) => _$CommunityReputationFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$CommunityReputationToJson(this); | ||
|
||
CommunityIdentifier communityIdentifier; | ||
Reputation reputation; | ||
|
||
@override | ||
String toString() { | ||
return jsonEncode(this); | ||
} | ||
} | ||
|
||
abstract class Reputation { | ||
const Reputation(); | ||
|
||
factory Reputation.fromJson(dynamic json) { | ||
if (json.runtimeType == String) { | ||
final variant = json as String; | ||
switch (variant) { | ||
case 'Unverified': | ||
return const Unverified(); | ||
case 'UnverifiedReputable': | ||
return const UnverifiedReputable(); | ||
case 'VerifiedUnlinked': | ||
return const VerifiedUnlinked(); | ||
default: | ||
throw Exception('Reputation: Invalid variant: "$variant"'); | ||
} | ||
} else { | ||
final variant = (json as Map<String, dynamic>).keys.first; | ||
switch (variant) { | ||
case 'VerifiedLinked': | ||
return VerifiedLinked(json.values.first as int); | ||
default: | ||
throw Exception('Reputation: Invalid variant: "$variant"'); | ||
} | ||
} | ||
} | ||
|
||
static const $Reputation values = $Reputation(); | ||
|
||
Map<String, dynamic> toJson(); | ||
} | ||
|
||
class $Reputation { | ||
const $Reputation(); | ||
|
||
Unverified unverified() { | ||
return const Unverified(); | ||
} | ||
|
||
UnverifiedReputable unverifiedReputable() { | ||
return const UnverifiedReputable(); | ||
} | ||
|
||
VerifiedUnlinked verifiedUnlinked() { | ||
return const VerifiedUnlinked(); | ||
} | ||
|
||
VerifiedLinked verifiedLinked(int value0) { | ||
return VerifiedLinked(value0); | ||
} | ||
} | ||
|
||
class Unverified extends Reputation { | ||
const Unverified(); | ||
|
||
@override | ||
Map<String, dynamic> toJson() => {'Unverified': null}; | ||
|
||
@override | ||
bool operator ==(Object other) => other is Unverified; | ||
|
||
@override | ||
int get hashCode => runtimeType.hashCode; | ||
} | ||
|
||
class UnverifiedReputable extends Reputation { | ||
const UnverifiedReputable(); | ||
|
||
@override | ||
Map<String, dynamic> toJson() => {'UnverifiedReputable': null}; | ||
|
||
@override | ||
bool operator ==(Object other) => other is UnverifiedReputable; | ||
|
||
@override | ||
int get hashCode => runtimeType.hashCode; | ||
} | ||
|
||
class VerifiedUnlinked extends Reputation { | ||
const VerifiedUnlinked(); | ||
|
||
@override | ||
Map<String, dynamic> toJson() => {'VerifiedUnlinked': null}; | ||
|
||
@override | ||
bool operator ==(Object other) => other is VerifiedUnlinked; | ||
|
||
@override | ||
int get hashCode => runtimeType.hashCode; | ||
} | ||
|
||
class VerifiedLinked extends Reputation { | ||
const VerifiedLinked(this.value0); | ||
|
||
/// CeremonyIndexType | ||
final int value0; | ||
|
||
@override | ||
Map<String, int> toJson() => {'VerifiedLinked': value0}; | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
identical( | ||
this, | ||
other, | ||
) || | ||
other is VerifiedLinked && other.value0 == value0; | ||
|
||
@override | ||
int get hashCode => value0.hashCode; | ||
} | ||
|
||
extension ReputationExtension on Reputation { | ||
String toValue() { | ||
return toEnumValue(this); | ||
} | ||
|
||
bool isVerified() { | ||
return runtimeType == VerifiedUnlinked || runtimeType == VerifiedLinked; | ||
} | ||
} | ||
|
||
/// Takes a `List<dynamic>` which contains a `List<List<[int, Map<String, dynamic>]>` and | ||
/// transforms it into a `Map<int, CommunityReputation>`. | ||
Map<int, CommunityReputation> reputationsFromList(List<dynamic> reputationsList) { | ||
final reputations = reputationsList.cast<List<dynamic>>(); | ||
|
||
return { | ||
for (final cr in reputations) cr[0] as int: CommunityReputation.fromJson(cr[1] as Map<String, dynamic>), | ||
}; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.