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
// Copyright (c) 2023 MASSA LABS <info@massa.net>

use crate::StateChanges;
use massa_async_pool::AsyncMessageId;
use massa_ledger_exports::{SetOrKeep, SetUpdateOrDelete};
use massa_proto_rs::massa::model::v1 as grpc_model;

impl From<StateChanges> for grpc_model::StateChanges {
    fn from(value: StateChanges) -> Self {
        grpc_model::StateChanges {
            executed_ops_changes: value
                .executed_ops_changes
                .into_iter()
                .map(|(op_id, (op_exec_status, op_valid_until_slot))| {
                    grpc_model::ExecutedOpsChangeEntry {
                        operation_id: op_id.to_string(),
                        value: Some(grpc_model::ExecutedOpsChangeValue {
                            status: if op_exec_status {
                                //TODO to be enhanced
                                grpc_model::OperationExecutionStatus::Success as i32
                            } else {
                                grpc_model::OperationExecutionStatus::Failed as i32
                            },
                            slot: Some(op_valid_until_slot.into()),
                        }),
                    }
                })
                .collect(),
            async_pool_changes: value
                .async_pool_changes
                .0
                .into_iter()
                .map(|(async_msg_id, change)| match change {
                    SetUpdateOrDelete::Set(async_msg) => grpc_model::AsyncPoolChangeEntry {
                        async_message_id: async_msg_id_to_string(async_msg_id),
                        value: Some(grpc_model::AsyncPoolChangeValue {
                            r#type: grpc_model::AsyncPoolChangeType::Set as i32,
                            message: Some(
                                grpc_model::async_pool_change_value::Message::CreatedMessage(
                                    async_msg.into(),
                                ),
                            ),
                        }),
                    },
                    SetUpdateOrDelete::Update(async_msg_update) => {
                        grpc_model::AsyncPoolChangeEntry {
                            async_message_id: async_msg_id_to_string(async_msg_id),
                            value: Some(grpc_model::AsyncPoolChangeValue {
                                r#type: grpc_model::AsyncPoolChangeType::Update as i32,
                                message: Some(
                                    grpc_model::async_pool_change_value::Message::UpdatedMessage(
                                        async_msg_update.into(),
                                    ),
                                ),
                            }),
                        }
                    }
                    SetUpdateOrDelete::Delete => grpc_model::AsyncPoolChangeEntry {
                        async_message_id: async_msg_id_to_string(async_msg_id),
                        value: Some(grpc_model::AsyncPoolChangeValue {
                            r#type: grpc_model::AsyncPoolChangeType::Delete as i32,
                            message: None,
                        }),
                    },
                })
                .collect(),
            ledger_changes: value
                .ledger_changes
                .0
                .into_iter()
                .map(|(key, value)| grpc_model::LedgerChangeEntry {
                    address: key.to_string(),
                    value: Some(match value {
                        SetUpdateOrDelete::Set(value) => grpc_model::LedgerChangeValue {
                            r#type: grpc_model::LedgerChangeType::Set as i32,
                            entry: Some(grpc_model::ledger_change_value::Entry::CreatedEntry(
                                value.into(),
                            )),
                        },
                        SetUpdateOrDelete::Update(value) => grpc_model::LedgerChangeValue {
                            r#type: grpc_model::LedgerChangeType::Update as i32,
                            entry: Some(grpc_model::ledger_change_value::Entry::UpdatedEntry(
                                value.into(),
                            )),
                        },
                        SetUpdateOrDelete::Delete => grpc_model::LedgerChangeValue {
                            r#type: grpc_model::LedgerChangeType::Delete as i32,
                            entry: None,
                        },
                    }),
                })
                .collect(),
            executed_denunciations_changes: value
                .executed_denunciations_changes
                .into_iter()
                .map(|de_idx| de_idx.into())
                .collect(),
            execution_trail_hash_change: match value.execution_trail_hash_change {
                SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepString {
                    change: Some(grpc_model::set_or_keep_string::Change::Set(
                        value.to_string(),
                    )),
                }),
                SetOrKeep::Keep => Some(grpc_model::SetOrKeepString {
                    change: Some(grpc_model::set_or_keep_string::Change::Keep(
                        grpc_model::Empty {},
                    )),
                }),
            },
        }
    }
}

fn async_msg_id_to_string(id: AsyncMessageId) -> String {
    bs58::encode(format!("{}{}{}{}", id.0 .0.numer(), id.0 .0.denom(), id.1, id.2).as_bytes())
        .with_check()
        .into_string()
}