Skip to content

Assurances (ρ)

Bases: StateComponent

Source code in pyjamaz/state/components.py
 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
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
class Assurances(StateComponent):
    component_id = 10

    @log_execution_time
    def state_transition_after_disputes(
            self,
            extrinsic_disputes: ExtrinsicDisputes,
            pre_state_assurances: AssurancesState
    ) -> AssurancesAfterDisputesOutput:
        """
        GP-0.7.2-eq:10.15 (ρ†) | Intermediate state transition function for the state's assurances that processes
        disputes extrinsic.

        Parameters
        ----------
        extrinsic_disputes: ExtrinsicDisputes
            GP-0.7.2-eq:4.12 (bold_E_D)
        pre_state_assurances: AssurancesState
            GP-0.7.2-eq:4.12 (ρ)

        Returns
        -------
        AssurancesAfterDisputesOutput
            Output Containing: Intermediate state after processing disputes of AssurancesState (ρ†)
        """
        # Todo: properly set intermediate_state_after_disputes by implementing STF
        intermediate_state_assurances_after_disputes = pre_state_assurances
        return AssurancesAfterDisputesOutput(
            intermediate_state_after_disputes=intermediate_state_assurances_after_disputes
        )

    def validate_after_disputes(
            self,
            extrinsic_assurances: List[Assurance],
            pre_state_validator_pool: ValidatorPoolState,
            header: Header
    ):
        """
        Validation of Assurances input data after disputes.

        Parameters
        ----------
        extrinsic_assurances
        pre_state_validator_pool
        header

        Returns
        -------

        """

        if not self.have_valid_validators(extrinsic_assurances, pre_state_validator_pool):
            raise StateTransitionError(AssurancesErrorCode.bad_validator_index)

        if not self.are_assurances_sorted(extrinsic_assurances):
            raise StateTransitionError(AssurancesErrorCode.not_sorted_or_unique_assurers)

        if self.has_duplicated_validators(extrinsic_assurances):
            raise StateTransitionError(AssurancesErrorCode.not_sorted_or_unique_assurers)

        for assurance in extrinsic_assurances:

            if assurance.anchor != header.parent:
                raise StateTransitionError(AssurancesErrorCode.bad_attestation_parent)

            validator = pre_state_validator_pool.validators[assurance.validator_index]

            if not self.has_valid_signature(assurance, validator):
                raise StateTransitionError(AssurancesErrorCode.bad_signature)

    @log_execution_time
    def state_transition_after_assurances(
            self,
            extrinsic_assurances: List[Assurance],
            intermediate_state_assurances_after_disputes: AssurancesState,
            header: Header
    ) -> AssurancesAfterAssurancesOutput:
        """
        GP-0.7.2-eq:11.29 (ρ‡) | Intermediate state transition function for the state's assurances that processes
        assurances extrinsic.

        Parameters
        ----------
        extrinsic_assurances: List[Assurance]
            GP-0.7.2-eq:4.13 (bold_E_A)
        intermediate_state_assurances_after_disputes: AssurancesState
            GP-0.7.2-eq:4.13 (ρ†)
        header: Header

        Returns
        -------
        AssurancesAfterAssurancesOutput
            Output Containing: Intermediate state after processing assurances of AssurancesState (ρ‡)
        """

        intermediate_state_assurances_after_assurances = deepcopy(intermediate_state_assurances_after_disputes)

        total_assurances_per_core = {c: 0 for c in range(0, gp_const.CORE_COUNT)}
        reported = []

        for assurance in extrinsic_assurances:

            for core in assurance.cores_engaged:
                if intermediate_state_assurances_after_disputes.assurances[core] is None:
                    raise StateTransitionError(AssurancesErrorCode.core_not_engaged)
                else:
                    total_assurances_per_core[core] += 1

        # Check for available reports
        for idx, assurance in enumerate(intermediate_state_assurances_after_disputes.assurances):
            if assurance:
                if total_assurances_per_core[assurance.report.core_index] > 2 / 3 * gp_const.VALIDATOR_COUNT:
                    # GP-0.7.2-eq:11.16 | Work report becomes available
                    reported.append(intermediate_state_assurances_after_disputes.assurances[idx].report)

                    # GP-0.7.2-eq:11.17 | Remove from assurances
                    intermediate_state_assurances_after_assurances.assurances[idx] = None

                # GP-0.7.2-eq:11.17 Check for timed out work reports
                if assurance and header.timeslot >= assurance.timeout + gp_const.UNAVAILABLE_WORK_REPLACEMENT_PERIOD:
                    intermediate_state_assurances_after_assurances.assurances[idx] = None

        return AssurancesAfterAssurancesOutput(
            intermediate_state_after_assurances=intermediate_state_assurances_after_assurances,
            reported=reported
        )

    @staticmethod
    def have_valid_validators(assurances: List[Assurance], post_state_validator_pool: ValidatorPoolState) -> bool:
        """
        GP-0.7.2-eq:11.10 | Validator index is element of current ValidatorPool

        Parameters
        ----------
        assurances: List[Assurance]
        post_state_validator_pool: ValidatorPoolState

        Returns
        -------
        bool
        """
        return all([a.validator_index < len(post_state_validator_pool.validators) for a in assurances])

    @staticmethod
    def are_assurances_sorted(assurances: List[Assurance]) -> bool:
        """
        GP-0.7.2-eq:11.12 | Are assurances correctly sorted by validator index

        Parameters
        ----------
        assurances: List[Assurance]

        Returns
        -------
        bool
        """
        return all(
            assurances[i].validator_index <= assurances[i + 1].validator_index for i in range(len(assurances) - 1)
        )

    @staticmethod
    def has_duplicated_validators(assurances: List[Assurance]) -> bool:
        validator_indexes = [a.validator_index for a in assurances]
        return len(validator_indexes) != len(set(validator_indexes))

    @staticmethod
    def has_valid_signature(assurance: Assurance, validator: ValidatorData) -> bool:
        data = b"jam_available" + blake2b_256_hash(assurance.anchor + assurance.bitfield_bytes)
        return ed_verify(bytes(assurance.signature), data, validator.ed25519)

    def validate_guarantees(
            self,
            extrinsic_guarantees: List[Guarantee],
            pre_services_state: ServicesState,
            intermediate_state_recent_history: RecentHistoryState,
            pre_authorizer_pools: AuthorizerPoolsState,
            intermediate_state_assurances_after_assurances: AssurancesState,
            post_state_validator_pool: ValidatorPoolState,
            header: Header,
            pre_accumulation_history: AccumulationHistoryState,
            post_entropy: EntropyState,
            post_state_timeslot: TimeslotState,
            post_state_validator_archive: ValidatorArchiveState,
            post_state_disputes: DisputesState
    ):

        # GP-0.7.2-eq:11.29 (r or I)
        work_reports = [g.report for g in extrinsic_guarantees]

        # GP-0.7.2-eq:11.41 | Segment-root lookup
        segment_root_lookup = {
            g.report.package_spec.hash: g.report.package_spec.exports_root for g in extrinsic_guarantees
        }

        # Extend segment-root lookup with recent history (GP-0.7.2-eq:11.39)
        for b in intermediate_state_recent_history.recent_blocks:
            segment_root_lookup.update({r.hash: r.exports_root for r in b.reported})

        # TODO: rename variable w to r or I (maybe)
        for w in work_reports:

            # TODO add GP ref
            if len(w.results) == 0:
                raise StateTransitionError(GuaranteeErrorCode.missing_work_results)

            # GP-0.7.2-eq:11.8 | Work report respects gas requirements
            self.check_size_limit(w)
            # GP-0.7.2-eq:11.30 | Work report respects gas requirements
            self.check_gas_requirements(w, pre_services_state)
            # GP-0.7.2-eq:11.3 | Work report respects dependency limit
            if w.dependency_count() > gp_const.MAXIMUM_DEPENDENCIES_WORK_REPORT:
                raise StateTransitionError(GuaranteeErrorCode.too_many_dependencies)
            # GP-0.7.2-eq:11.41,11.42 | Verify if segment roots mentioned in work-package are correct
            if not all([
                segment_root_lookup.get(work_package_hash, None) == segment_tree_root
                for work_package_hash, segment_tree_root  in w.segment_root_lookup.items()
            ]):
                raise StateTransitionError(GuaranteeErrorCode.segment_root_lookup_invalid)

        if not self.are_guarentees_sorted(extrinsic_guarantees):
            raise StateTransitionError(GuaranteeErrorCode.out_of_order_guarantee)

        if self.has_duplicated_guarentees(extrinsic_guarantees):
            raise StateTransitionError(GuaranteeErrorCode.out_of_order_guarantee)

        # GP-0.7.2-eq:11.31 (x)
        context_items = [w.context for w in work_reports]
        # GP-0.7.2-eq:11.31 (p)
        extrinsic_work_package_hashes = {w.package_spec.hash for w in work_reports}

        recent_history_work_package_hashes = [
            h.hash for b in intermediate_state_recent_history.recent_blocks for h in b.reported
        ]

        # GP-0.7.2-eq:11.32 | Check for duplicate
        if len(extrinsic_work_package_hashes) != len(work_reports):
            raise StateTransitionError(GuaranteeErrorCode.duplicate_package)

        # GP-0.7.2-eq:11.38 | Check if work-package appear in pipeline
        if self.work_packages_exists_in_pipeline(
                extrinsic_work_package_hashes,
                intermediate_state_recent_history,
                pre_accumulation_history
        ):
            raise StateTransitionError(GuaranteeErrorCode.duplicate_package)


        for context in context_items:
            # GP-0.7.2-eq:11.34 | Check for expired lookup anchors
            if context.lookup_anchor_slot < header.timeslot - gp_const.MAXIMUM_AGE_LOOKUP_ANCHOR:
                raise StateTransitionError(GuaranteeErrorCode.anchor_not_recent)

            # GP-0.7.2-eq:11.35 | Anchor must be in recent history
            recent_block = intermediate_state_recent_history.get_recent_block(context.anchor)

            if not recent_block:
                raise StateTransitionError(GuaranteeErrorCode.anchor_not_recent)

            if recent_block.state_root != context.state_root:
                raise StateTransitionError(GuaranteeErrorCode.bad_state_root)

            if recent_block.beefy_root != context.beefy_root:
                raise StateTransitionError(GuaranteeErrorCode.bad_beefy_mmr_root)


        for guarantee in extrinsic_guarantees:

            # GP-0.7.2-eq:11.26 | Check validity time slot
            if guarantee.slot > post_state_timeslot.number:
                raise StateTransitionError(GuaranteeErrorCode.future_report_slot)

            if guarantee.slot < gp_const.ROTATION_PERIOD_CORE * (post_state_timeslot.number // gp_const.ROTATION_PERIOD_CORE - 1) :
                raise StateTransitionError(GuaranteeErrorCode.report_epoch_before_last)

            guarantor_assignments = self.get_guarantor_assignments(guarantee, post_state_timeslot)

            if guarantee.report.core_index > len(intermediate_state_assurances_after_assurances.assurances):
                raise StateTransitionError(GuaranteeErrorCode.bad_core_index)

            if not self.are_guarantors_unqiue_and_sorted(guarantee.signatures):
                raise StateTransitionError(GuaranteeErrorCode.not_sorted_or_unique_guarantors)

            # GP-0.7.2-eq:11.23
            if len(guarantee.signatures) < 2 or len(guarantee.signatures) > 3:
                raise StateTransitionError(GuaranteeErrorCode.insufficient_guarantees)

            for credential in guarantee.signatures:

                if credential.validator_index >= gp_const.VALIDATOR_COUNT:
                    raise StateTransitionError(GuaranteeErrorCode.bad_validator_index)

                # GP-0.7.2-eq:11.26 | Check for valid assignment
                guarantor_assignment = guarantor_assignments[credential.validator_index]

                if guarantor_assignment.core_index != guarantee.report.core_index:
                    raise StateTransitionError(GuaranteeErrorCode.wrong_assignment)

                # Check if validator not on offender list TODO create global checked Validator set
                if guarantor_assignment.validator_ed25519 in post_state_disputes.offenders:
                    raise StateTransitionError(GuaranteeErrorCode.banned_validator)

                if not self.valid_guarantee_signature(credential, guarantee, guarantor_assignment.validator_ed25519):
                    raise StateTransitionError(GuaranteeErrorCode.bad_signature)

            # GP-0.7.2-eq:11.29 | Check if core is available
            if intermediate_state_assurances_after_assurances.assurances[guarantee.report.core_index] is not None:
                raise StateTransitionError(GuaranteeErrorCode.core_engaged)

            # GP-0.7.2-eq:11.29 | Check if authorizer hash is present in authorizer pool of core
            if guarantee.report.authorizer_hash not in pre_authorizer_pools.authorizer_pools[guarantee.report.core_index]:
                raise StateTransitionError(GuaranteeErrorCode.core_unauthorized)

            # GP-0.7.2-eq:11.39 | Check work-package prerequisites
            for prerequisite in guarantee.report.context.prerequisites:
                if (
                    prerequisite not in recent_history_work_package_hashes and
                    prerequisite not in extrinsic_work_package_hashes
                ):
                    raise StateTransitionError(GuaranteeErrorCode.dependency_missing)


    def get_guarantor_assignments(
            self, guarantee: Guarantee, post_state_timeslot: TimeslotState
    ) -> List[GuarantorAssignment]:
        """
        GP-0.7.2-eq:11.26 | Get applicable mapping (M or M*) of Validator ED25519 and assigned core index

        Parameters
        ----------
        guarantee
        post_state_timeslot

        Returns
        -------
        Dict[bytes, int] Mapping of Validator ED25519 and assigned core index
        """
        if post_state_timeslot.number // gp_const.ROTATION_PERIOD_CORE == \
                guarantee.slot // gp_const.ROTATION_PERIOD_CORE:
            return self.block_context.guarantor_assignments
        else:
            return self.block_context.prev_guarantor_assignments

    @staticmethod
    def check_size_limit(work_report: WorkReport):
        """
        GP-0.7.2-eq:11.8 | Work report respects size limit

        Parameters
        ----------
        work_report

        Returns
        -------

        """
        if (len(work_report.auth_output) + sum([len(r.result.ok or bytes(0)) for r in work_report.results])
                > gp_const.MAXIMUM_SIZE_ENCODED_WORK_REPORT):
            raise StateTransitionError(GuaranteeErrorCode.work_report_too_big)

    def check_gas_requirements(self, work_report: WorkReport, services_state: ServicesState):
        """
        GP-0.7.2-eq:11.30 | Work report respects gas requirements

        Parameters
        ----------
        work_report
        services_state

        Returns
        -------

        """
        total_gas = 0

        for result in work_report.results:
            try:
                services_state.retrieve_service_account(result.service_id)
            except StateKeyNoResult:
                raise StateTransitionError(GuaranteeErrorCode.bad_service_id)

            service = services_state.retrieve_service_account(result.service_id)

            if result.code_hash != service.code_hash:
                raise StateTransitionError(GuaranteeErrorCode.bad_code_hash)

            # GP-0.7.2-eq:11.30 | Work report respects gas requirements

            if result.accumulate_gas < service.gas_limit_accumulate:
                raise StateTransitionError(GuaranteeErrorCode.service_item_gas_too_low)

            total_gas += result.accumulate_gas
            if total_gas > gp_const.GAS_ACCUMULATION:
                raise StateTransitionError(GuaranteeErrorCode.work_report_gas_too_high)



    @staticmethod
    def work_packages_exists_in_pipeline(
            work_package_hashes: Set[bytes],
            recent_history_state: RecentHistoryState,
            accumulation_history: AccumulationHistoryState
    ) -> bool:
        """
        GP-0.7.2-eq:11.36,11.37,11.38 | Check if work-packages appear in pipeline

        Parameters
        ----------
        work_package_hashes
        recent_history_state
        accumulation_history

        Returns
        -------
        bool
        """
        # TODO finish additional checks 11.36 and 11.37
        if any(w in accumulation_history.accumulation_history for w in work_package_hashes):
            return True

        for recent_block in recent_history_state.recent_blocks:
            for item in recent_block.reported:
                if item.hash in work_package_hashes:
                    return True

        # TODO q = prerequisites acc. queue -> add state
        # TODO a zoek in pre_state_assurances

        return False

    @log_execution_time
    def state_transition_after_guarantees(
            self,
            extrinsic_guarantees: List[Guarantee],
            intermediate_state_assurances_after_assurances: AssurancesState,
            pre_state_validator_pool: ValidatorPoolState,
            post_state_timeslot: TimeslotState
    ) -> AssurancesAfterGuaranteesOutput:
        """
        GP-0.7.2-eq:11.43 (ρ') | State transition function for the state's assurances that processes guarantees
        extrinsic.

        Parameters
        ----------
        extrinsic_guarantees: List[Guarantee]
            GP-0.7.2-eq:4.14 (bold_E_G)
        intermediate_state_assurances_after_assurances: AssurancesState
            GP-0.7.2-eq:4.14 (ρ‡)
        pre_state_validator_pool: ValidatorPoolState
            GP-0.7.2-eq:4.14 (κ)
        post_state_timeslot: TimeslotState
            GP-0.7.2-eq:4.14 (τ')

        Returns
        -------
        AssurancesAfterGuaranteesOutput
            Output containing: Posterior state after processing guarantees of AssurancesState (ρ')
        """
        post_state_assurances = deepcopy(intermediate_state_assurances_after_assurances)

        reported = []
        reporters = []

        for guarantee in extrinsic_guarantees:

            # GP-0.7.2-eq:11.43 | Assign work report to core
            post_state_assurances.assurances[guarantee.report.core_index] = AssuranceStateItem(
                report=guarantee.report,
                timeout=post_state_timeslot.number
            )

            reported.append(
                ReportedPackage(
                    work_package_hash=guarantee.report.package_spec.hash,
                    segment_tree_root=guarantee.report.package_spec.exports_root
                )
            )

            guarantor_assignments = self.get_guarantor_assignments(guarantee, post_state_timeslot)

            for signature in guarantee.signatures:
                reporters.append(guarantor_assignments[signature.validator_index].validator_ed25519)

        # Make reporters unique
        reporters = list(set(reporters))

        # Sort output lists
        reported.sort(key=lambda rp: rp.work_package_hash)
        reporters.sort()

        return AssurancesAfterGuaranteesOutput(
            post_state=post_state_assurances,
            reported=reported,
            reporters=reporters
        )


    @staticmethod
    def valid_guarantee_signature(credential: Credential, guarantee: Guarantee, validator_ed25519: bytes) -> bool:
        """
        GP-0.7.2-eq:11.23 | Valid signatures for guarantee

        Parameters
        ----------
        credential
        guarantee
        validator_ed25519

        Returns
        -------
        bool
        """

        data = b"jam_guarantee" + blake2b_256_hash(guarantee.report.to_jam_bytes().to_bytes())
        return ed_verify(bytes(credential.signature), data, validator_ed25519)

    @staticmethod
    def are_guarentees_sorted(guarantees: List[Guarantee]) -> bool:
        """
        GP-0.7.2-eq:11.25 | The core index of guarantees must be in ascending order

        Parameters
        ----------
        guarantees: List[Guarantee]

        Returns
        -------
        bool
        """
        return all(
            guarantees[i].report.core_index <= guarantees[i + 1].report.core_index for i in range(len(guarantees) - 1)
        )

    @staticmethod
    def has_duplicated_guarentees(guarantees: List[Guarantee]) -> bool:
        """
        GP-0.7.2-eq:11.25 | The core index of each guarantee must be unique

        Parameters
        ----------
        guarantees

        Returns
        -------
        bool
        """
        core_indices = [g.report.core_index for g in guarantees]
        return len(core_indices) != len(set(core_indices))

    @staticmethod
    def are_guarantors_unqiue_and_sorted(signatures: List[Credential]) -> bool:
        """
        GP-0.7.2-eq:11.25 | Are signatures unique and correctly sorted by validator index

        Parameters
        ----------
        signatures: List[Credential]

        Returns
        -------
        bool
        """
        for i, s in enumerate(signatures):
            if i < len(signatures) - 1:
                if s.validator_index >= signatures[i+1].validator_index:
                    return False
        return True

    def retrieve_state(self) -> AssurancesState:
        value = self.retrieve()
        return AssurancesState.from_jam_bytes(JamBytes(value))

state_transition_after_disputes(extrinsic_disputes: ExtrinsicDisputes, pre_state_assurances: AssurancesState) -> AssurancesAfterDisputesOutput

GP-0.7.2-eq:10.15 (ρ†) | Intermediate state transition function for the state's assurances that processes disputes extrinsic.

Parameters:

Name Type Description Default
extrinsic_disputes ExtrinsicDisputes

GP-0.7.2-eq:4.12 (bold_E_D)

required
pre_state_assurances AssurancesState

GP-0.7.2-eq:4.12 (ρ)

required

Returns:

Type Description
AssurancesAfterDisputesOutput

Output Containing: Intermediate state after processing disputes of AssurancesState (ρ†)

Source code in pyjamaz/state/components.py
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
@log_execution_time
def state_transition_after_disputes(
        self,
        extrinsic_disputes: ExtrinsicDisputes,
        pre_state_assurances: AssurancesState
) -> AssurancesAfterDisputesOutput:
    """
    GP-0.7.2-eq:10.15 (ρ†) | Intermediate state transition function for the state's assurances that processes
    disputes extrinsic.

    Parameters
    ----------
    extrinsic_disputes: ExtrinsicDisputes
        GP-0.7.2-eq:4.12 (bold_E_D)
    pre_state_assurances: AssurancesState
        GP-0.7.2-eq:4.12 (ρ)

    Returns
    -------
    AssurancesAfterDisputesOutput
        Output Containing: Intermediate state after processing disputes of AssurancesState (ρ†)
    """
    # Todo: properly set intermediate_state_after_disputes by implementing STF
    intermediate_state_assurances_after_disputes = pre_state_assurances
    return AssurancesAfterDisputesOutput(
        intermediate_state_after_disputes=intermediate_state_assurances_after_disputes
    )

validate_after_disputes(extrinsic_assurances: List[Assurance], pre_state_validator_pool: ValidatorPoolState, header: Header)

Validation of Assurances input data after disputes.

Parameters:

Name Type Description Default
extrinsic_assurances List[Assurance]
required
pre_state_validator_pool ValidatorPoolState
required
header Header
required
Source code in pyjamaz/state/components.py
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
def validate_after_disputes(
        self,
        extrinsic_assurances: List[Assurance],
        pre_state_validator_pool: ValidatorPoolState,
        header: Header
):
    """
    Validation of Assurances input data after disputes.

    Parameters
    ----------
    extrinsic_assurances
    pre_state_validator_pool
    header

    Returns
    -------

    """

    if not self.have_valid_validators(extrinsic_assurances, pre_state_validator_pool):
        raise StateTransitionError(AssurancesErrorCode.bad_validator_index)

    if not self.are_assurances_sorted(extrinsic_assurances):
        raise StateTransitionError(AssurancesErrorCode.not_sorted_or_unique_assurers)

    if self.has_duplicated_validators(extrinsic_assurances):
        raise StateTransitionError(AssurancesErrorCode.not_sorted_or_unique_assurers)

    for assurance in extrinsic_assurances:

        if assurance.anchor != header.parent:
            raise StateTransitionError(AssurancesErrorCode.bad_attestation_parent)

        validator = pre_state_validator_pool.validators[assurance.validator_index]

        if not self.has_valid_signature(assurance, validator):
            raise StateTransitionError(AssurancesErrorCode.bad_signature)

state_transition_after_assurances(extrinsic_assurances: List[Assurance], intermediate_state_assurances_after_disputes: AssurancesState, header: Header) -> AssurancesAfterAssurancesOutput

GP-0.7.2-eq:11.29 (ρ‡) | Intermediate state transition function for the state's assurances that processes assurances extrinsic.

Parameters:

Name Type Description Default
extrinsic_assurances List[Assurance]

GP-0.7.2-eq:4.13 (bold_E_A)

required
intermediate_state_assurances_after_disputes AssurancesState

GP-0.7.2-eq:4.13 (ρ†)

required
header Header
required

Returns:

Type Description
AssurancesAfterAssurancesOutput

Output Containing: Intermediate state after processing assurances of AssurancesState (ρ‡)

Source code in pyjamaz/state/components.py
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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
@log_execution_time
def state_transition_after_assurances(
        self,
        extrinsic_assurances: List[Assurance],
        intermediate_state_assurances_after_disputes: AssurancesState,
        header: Header
) -> AssurancesAfterAssurancesOutput:
    """
    GP-0.7.2-eq:11.29 (ρ‡) | Intermediate state transition function for the state's assurances that processes
    assurances extrinsic.

    Parameters
    ----------
    extrinsic_assurances: List[Assurance]
        GP-0.7.2-eq:4.13 (bold_E_A)
    intermediate_state_assurances_after_disputes: AssurancesState
        GP-0.7.2-eq:4.13 (ρ†)
    header: Header

    Returns
    -------
    AssurancesAfterAssurancesOutput
        Output Containing: Intermediate state after processing assurances of AssurancesState (ρ‡)
    """

    intermediate_state_assurances_after_assurances = deepcopy(intermediate_state_assurances_after_disputes)

    total_assurances_per_core = {c: 0 for c in range(0, gp_const.CORE_COUNT)}
    reported = []

    for assurance in extrinsic_assurances:

        for core in assurance.cores_engaged:
            if intermediate_state_assurances_after_disputes.assurances[core] is None:
                raise StateTransitionError(AssurancesErrorCode.core_not_engaged)
            else:
                total_assurances_per_core[core] += 1

    # Check for available reports
    for idx, assurance in enumerate(intermediate_state_assurances_after_disputes.assurances):
        if assurance:
            if total_assurances_per_core[assurance.report.core_index] > 2 / 3 * gp_const.VALIDATOR_COUNT:
                # GP-0.7.2-eq:11.16 | Work report becomes available
                reported.append(intermediate_state_assurances_after_disputes.assurances[idx].report)

                # GP-0.7.2-eq:11.17 | Remove from assurances
                intermediate_state_assurances_after_assurances.assurances[idx] = None

            # GP-0.7.2-eq:11.17 Check for timed out work reports
            if assurance and header.timeslot >= assurance.timeout + gp_const.UNAVAILABLE_WORK_REPLACEMENT_PERIOD:
                intermediate_state_assurances_after_assurances.assurances[idx] = None

    return AssurancesAfterAssurancesOutput(
        intermediate_state_after_assurances=intermediate_state_assurances_after_assurances,
        reported=reported
    )

have_valid_validators(assurances: List[Assurance], post_state_validator_pool: ValidatorPoolState) -> bool staticmethod

GP-0.7.2-eq:11.10 | Validator index is element of current ValidatorPool

Parameters:

Name Type Description Default
assurances List[Assurance]
required
post_state_validator_pool ValidatorPoolState
required

Returns:

Type Description
bool
Source code in pyjamaz/state/components.py
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
@staticmethod
def have_valid_validators(assurances: List[Assurance], post_state_validator_pool: ValidatorPoolState) -> bool:
    """
    GP-0.7.2-eq:11.10 | Validator index is element of current ValidatorPool

    Parameters
    ----------
    assurances: List[Assurance]
    post_state_validator_pool: ValidatorPoolState

    Returns
    -------
    bool
    """
    return all([a.validator_index < len(post_state_validator_pool.validators) for a in assurances])

are_assurances_sorted(assurances: List[Assurance]) -> bool staticmethod

GP-0.7.2-eq:11.12 | Are assurances correctly sorted by validator index

Parameters:

Name Type Description Default
assurances List[Assurance]
required

Returns:

Type Description
bool
Source code in pyjamaz/state/components.py
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
@staticmethod
def are_assurances_sorted(assurances: List[Assurance]) -> bool:
    """
    GP-0.7.2-eq:11.12 | Are assurances correctly sorted by validator index

    Parameters
    ----------
    assurances: List[Assurance]

    Returns
    -------
    bool
    """
    return all(
        assurances[i].validator_index <= assurances[i + 1].validator_index for i in range(len(assurances) - 1)
    )

get_guarantor_assignments(guarantee: Guarantee, post_state_timeslot: TimeslotState) -> List[GuarantorAssignment]

GP-0.7.2-eq:11.26 | Get applicable mapping (M or M*) of Validator ED25519 and assigned core index

Parameters:

Name Type Description Default
guarantee Guarantee
required
post_state_timeslot TimeslotState
required

Returns:

Type Description
Dict[bytes, int] Mapping of Validator ED25519 and assigned core index
Source code in pyjamaz/state/components.py
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
def get_guarantor_assignments(
        self, guarantee: Guarantee, post_state_timeslot: TimeslotState
) -> List[GuarantorAssignment]:
    """
    GP-0.7.2-eq:11.26 | Get applicable mapping (M or M*) of Validator ED25519 and assigned core index

    Parameters
    ----------
    guarantee
    post_state_timeslot

    Returns
    -------
    Dict[bytes, int] Mapping of Validator ED25519 and assigned core index
    """
    if post_state_timeslot.number // gp_const.ROTATION_PERIOD_CORE == \
            guarantee.slot // gp_const.ROTATION_PERIOD_CORE:
        return self.block_context.guarantor_assignments
    else:
        return self.block_context.prev_guarantor_assignments

check_size_limit(work_report: WorkReport) staticmethod

GP-0.7.2-eq:11.8 | Work report respects size limit

Parameters:

Name Type Description Default
work_report WorkReport
required
Source code in pyjamaz/state/components.py
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
@staticmethod
def check_size_limit(work_report: WorkReport):
    """
    GP-0.7.2-eq:11.8 | Work report respects size limit

    Parameters
    ----------
    work_report

    Returns
    -------

    """
    if (len(work_report.auth_output) + sum([len(r.result.ok or bytes(0)) for r in work_report.results])
            > gp_const.MAXIMUM_SIZE_ENCODED_WORK_REPORT):
        raise StateTransitionError(GuaranteeErrorCode.work_report_too_big)

check_gas_requirements(work_report: WorkReport, services_state: ServicesState)

GP-0.7.2-eq:11.30 | Work report respects gas requirements

Parameters:

Name Type Description Default
work_report WorkReport
required
services_state ServicesState
required
Source code in pyjamaz/state/components.py
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
def check_gas_requirements(self, work_report: WorkReport, services_state: ServicesState):
    """
    GP-0.7.2-eq:11.30 | Work report respects gas requirements

    Parameters
    ----------
    work_report
    services_state

    Returns
    -------

    """
    total_gas = 0

    for result in work_report.results:
        try:
            services_state.retrieve_service_account(result.service_id)
        except StateKeyNoResult:
            raise StateTransitionError(GuaranteeErrorCode.bad_service_id)

        service = services_state.retrieve_service_account(result.service_id)

        if result.code_hash != service.code_hash:
            raise StateTransitionError(GuaranteeErrorCode.bad_code_hash)

        # GP-0.7.2-eq:11.30 | Work report respects gas requirements

        if result.accumulate_gas < service.gas_limit_accumulate:
            raise StateTransitionError(GuaranteeErrorCode.service_item_gas_too_low)

        total_gas += result.accumulate_gas
        if total_gas > gp_const.GAS_ACCUMULATION:
            raise StateTransitionError(GuaranteeErrorCode.work_report_gas_too_high)

work_packages_exists_in_pipeline(work_package_hashes: Set[bytes], recent_history_state: RecentHistoryState, accumulation_history: AccumulationHistoryState) -> bool staticmethod

GP-0.7.2-eq:11.36,11.37,11.38 | Check if work-packages appear in pipeline

Parameters:

Name Type Description Default
work_package_hashes Set[bytes]
required
recent_history_state RecentHistoryState
required
accumulation_history AccumulationHistoryState
required

Returns:

Type Description
bool
Source code in pyjamaz/state/components.py
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
@staticmethod
def work_packages_exists_in_pipeline(
        work_package_hashes: Set[bytes],
        recent_history_state: RecentHistoryState,
        accumulation_history: AccumulationHistoryState
) -> bool:
    """
    GP-0.7.2-eq:11.36,11.37,11.38 | Check if work-packages appear in pipeline

    Parameters
    ----------
    work_package_hashes
    recent_history_state
    accumulation_history

    Returns
    -------
    bool
    """
    # TODO finish additional checks 11.36 and 11.37
    if any(w in accumulation_history.accumulation_history for w in work_package_hashes):
        return True

    for recent_block in recent_history_state.recent_blocks:
        for item in recent_block.reported:
            if item.hash in work_package_hashes:
                return True

    # TODO q = prerequisites acc. queue -> add state
    # TODO a zoek in pre_state_assurances

    return False

state_transition_after_guarantees(extrinsic_guarantees: List[Guarantee], intermediate_state_assurances_after_assurances: AssurancesState, pre_state_validator_pool: ValidatorPoolState, post_state_timeslot: TimeslotState) -> AssurancesAfterGuaranteesOutput

GP-0.7.2-eq:11.43 (ρ') | State transition function for the state's assurances that processes guarantees extrinsic.

Parameters:

Name Type Description Default
extrinsic_guarantees List[Guarantee]

GP-0.7.2-eq:4.14 (bold_E_G)

required
intermediate_state_assurances_after_assurances AssurancesState

GP-0.7.2-eq:4.14 (ρ‡)

required
pre_state_validator_pool ValidatorPoolState

GP-0.7.2-eq:4.14 (κ)

required
post_state_timeslot TimeslotState

GP-0.7.2-eq:4.14 (τ')

required

Returns:

Type Description
AssurancesAfterGuaranteesOutput

Output containing: Posterior state after processing guarantees of AssurancesState (ρ')

Source code in pyjamaz/state/components.py
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
@log_execution_time
def state_transition_after_guarantees(
        self,
        extrinsic_guarantees: List[Guarantee],
        intermediate_state_assurances_after_assurances: AssurancesState,
        pre_state_validator_pool: ValidatorPoolState,
        post_state_timeslot: TimeslotState
) -> AssurancesAfterGuaranteesOutput:
    """
    GP-0.7.2-eq:11.43 (ρ') | State transition function for the state's assurances that processes guarantees
    extrinsic.

    Parameters
    ----------
    extrinsic_guarantees: List[Guarantee]
        GP-0.7.2-eq:4.14 (bold_E_G)
    intermediate_state_assurances_after_assurances: AssurancesState
        GP-0.7.2-eq:4.14 (ρ‡)
    pre_state_validator_pool: ValidatorPoolState
        GP-0.7.2-eq:4.14 (κ)
    post_state_timeslot: TimeslotState
        GP-0.7.2-eq:4.14 (τ')

    Returns
    -------
    AssurancesAfterGuaranteesOutput
        Output containing: Posterior state after processing guarantees of AssurancesState (ρ')
    """
    post_state_assurances = deepcopy(intermediate_state_assurances_after_assurances)

    reported = []
    reporters = []

    for guarantee in extrinsic_guarantees:

        # GP-0.7.2-eq:11.43 | Assign work report to core
        post_state_assurances.assurances[guarantee.report.core_index] = AssuranceStateItem(
            report=guarantee.report,
            timeout=post_state_timeslot.number
        )

        reported.append(
            ReportedPackage(
                work_package_hash=guarantee.report.package_spec.hash,
                segment_tree_root=guarantee.report.package_spec.exports_root
            )
        )

        guarantor_assignments = self.get_guarantor_assignments(guarantee, post_state_timeslot)

        for signature in guarantee.signatures:
            reporters.append(guarantor_assignments[signature.validator_index].validator_ed25519)

    # Make reporters unique
    reporters = list(set(reporters))

    # Sort output lists
    reported.sort(key=lambda rp: rp.work_package_hash)
    reporters.sort()

    return AssurancesAfterGuaranteesOutput(
        post_state=post_state_assurances,
        reported=reported,
        reporters=reporters
    )

valid_guarantee_signature(credential: Credential, guarantee: Guarantee, validator_ed25519: bytes) -> bool staticmethod

GP-0.7.2-eq:11.23 | Valid signatures for guarantee

Parameters:

Name Type Description Default
credential Credential
required
guarantee Guarantee
required
validator_ed25519 bytes
required

Returns:

Type Description
bool
Source code in pyjamaz/state/components.py
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
@staticmethod
def valid_guarantee_signature(credential: Credential, guarantee: Guarantee, validator_ed25519: bytes) -> bool:
    """
    GP-0.7.2-eq:11.23 | Valid signatures for guarantee

    Parameters
    ----------
    credential
    guarantee
    validator_ed25519

    Returns
    -------
    bool
    """

    data = b"jam_guarantee" + blake2b_256_hash(guarantee.report.to_jam_bytes().to_bytes())
    return ed_verify(bytes(credential.signature), data, validator_ed25519)

are_guarentees_sorted(guarantees: List[Guarantee]) -> bool staticmethod

GP-0.7.2-eq:11.25 | The core index of guarantees must be in ascending order

Parameters:

Name Type Description Default
guarantees List[Guarantee]
required

Returns:

Type Description
bool
Source code in pyjamaz/state/components.py
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
@staticmethod
def are_guarentees_sorted(guarantees: List[Guarantee]) -> bool:
    """
    GP-0.7.2-eq:11.25 | The core index of guarantees must be in ascending order

    Parameters
    ----------
    guarantees: List[Guarantee]

    Returns
    -------
    bool
    """
    return all(
        guarantees[i].report.core_index <= guarantees[i + 1].report.core_index for i in range(len(guarantees) - 1)
    )

has_duplicated_guarentees(guarantees: List[Guarantee]) -> bool staticmethod

GP-0.7.2-eq:11.25 | The core index of each guarantee must be unique

Parameters:

Name Type Description Default
guarantees List[Guarantee]
required

Returns:

Type Description
bool
Source code in pyjamaz/state/components.py
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
@staticmethod
def has_duplicated_guarentees(guarantees: List[Guarantee]) -> bool:
    """
    GP-0.7.2-eq:11.25 | The core index of each guarantee must be unique

    Parameters
    ----------
    guarantees

    Returns
    -------
    bool
    """
    core_indices = [g.report.core_index for g in guarantees]
    return len(core_indices) != len(set(core_indices))

are_guarantors_unqiue_and_sorted(signatures: List[Credential]) -> bool staticmethod

GP-0.7.2-eq:11.25 | Are signatures unique and correctly sorted by validator index

Parameters:

Name Type Description Default
signatures List[Credential]
required

Returns:

Type Description
bool
Source code in pyjamaz/state/components.py
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
@staticmethod
def are_guarantors_unqiue_and_sorted(signatures: List[Credential]) -> bool:
    """
    GP-0.7.2-eq:11.25 | Are signatures unique and correctly sorted by validator index

    Parameters
    ----------
    signatures: List[Credential]

    Returns
    -------
    bool
    """
    for i, s in enumerate(signatures):
        if i < len(signatures) - 1:
            if s.validator_index >= signatures[i+1].validator_index:
                return False
    return True