hopr_db_sql/
resolver.rs

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
use async_trait::async_trait;
use hopr_crypto_types::types::OffchainPublicKey;
use hopr_db_api::errors::DbError;
use hopr_db_api::{errors::Result, resolver::HoprDbResolverOperations};
use hopr_primitive_types::primitives::Address;

use crate::accounts::HoprDbAccountOperations;
use crate::db::HoprDb;

#[async_trait]
impl HoprDbResolverOperations for HoprDb {
    async fn resolve_packet_key(&self, onchain_key: &Address) -> Result<Option<OffchainPublicKey>> {
        Ok(self
            .translate_key(None, *onchain_key)
            .await?
            .map(|k| k.try_into())
            .transpose()
            .map_err(|_e| DbError::LogicalError("failed to transpose the translated key".into()))?)
    }

    async fn resolve_chain_key(&self, offchain_key: &OffchainPublicKey) -> Result<Option<Address>> {
        Ok(self
            .translate_key(None, *offchain_key)
            .await?
            .map(|k| k.try_into())
            .transpose()
            .map_err(|_e| DbError::LogicalError("failed to transpose the translated key".into()))?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use hopr_crypto_types::prelude::{ChainKeypair, Keypair, OffchainKeypair};
    use hopr_internal_types::account::{AccountEntry, AccountType};
    use hopr_primitive_types::prelude::ToHex;
    use sea_orm::{EntityTrait, Set};

    #[async_std::test]
    async fn test_get_offchain_key_should_return_nothing_if_a_mapping_to_chain_key_does_not_exist() -> anyhow::Result<()>
    {
        let db = HoprDb::new_in_memory(ChainKeypair::random()).await?;

        let chain = ChainKeypair::random().public().to_address();

        let actual_pk = db.resolve_packet_key(&chain).await?;
        assert_eq!(actual_pk, None, "offchain key should not be present");
        Ok(())
    }

    #[async_std::test]
    async fn test_get_chain_key_should_return_nothing_if_a_mapping_to_offchain_key_does_not_exist() -> anyhow::Result<()>
    {
        let db = HoprDb::new_in_memory(ChainKeypair::random()).await?;

        let packet = OffchainKeypair::random().public().clone();

        let actual_ck = db.resolve_chain_key(&packet).await?;
        assert_eq!(actual_ck, None, "chain key should not be present");
        Ok(())
    }

    #[async_std::test]
    async fn test_get_chain_key_should_succeed_if_a_mapping_to_offchain_key_exists() -> anyhow::Result<()> {
        let db = HoprDb::new_in_memory(ChainKeypair::random()).await?;

        // Inserting to the table directly to avoid cache

        let chain_1 = ChainKeypair::random().public().to_address();
        let packet_1 = OffchainKeypair::random().public().clone();
        let account_1 = hopr_db_entity::account::ActiveModel {
            chain_key: Set(chain_1.to_hex()),
            packet_key: Set(packet_1.to_hex()),
            ..Default::default()
        };

        let chain_2 = ChainKeypair::random().public().to_address();
        let packet_2 = OffchainKeypair::random().public().clone();
        let account_2 = hopr_db_entity::account::ActiveModel {
            chain_key: Set(chain_2.to_hex()),
            packet_key: Set(packet_2.to_hex()),
            ..Default::default()
        };

        hopr_db_entity::account::Entity::insert_many([account_1, account_2])
            .exec(&db.index_db)
            .await?;

        let actual_ck = db.resolve_chain_key(&packet_1).await?;
        assert_eq!(actual_ck, Some(chain_1), "chain keys must match");
        Ok(())
    }

    #[async_std::test]
    async fn test_get_chain_key_should_succeed_if_a_mapping_to_offchain_key_exists_with_cache() -> anyhow::Result<()> {
        let db = HoprDb::new_in_memory(ChainKeypair::random()).await?;

        // Inserting to the table via API to insert into cache as well

        let chain_1 = ChainKeypair::random().public().to_address();
        let packet_1 = OffchainKeypair::random().public().clone();
        db.insert_account(None, AccountEntry::new(packet_1, chain_1, AccountType::NotAnnounced))
            .await?;

        let chain_2 = ChainKeypair::random().public().to_address();
        let packet_2 = OffchainKeypair::random().public().clone();
        db.insert_account(None, AccountEntry::new(packet_2, chain_2, AccountType::NotAnnounced))
            .await?;

        let actual_ck = db.resolve_chain_key(&packet_1).await?;
        assert_eq!(actual_ck, Some(chain_1), "chain keys must match");
        Ok(())
    }

    #[async_std::test]
    async fn test_get_offchain_key_should_succeed_if_a_mapping_to_chain_key_exists() -> anyhow::Result<()> {
        let db = HoprDb::new_in_memory(ChainKeypair::random()).await?;

        // Inserting to the table directly to avoid cache

        let chain_1 = ChainKeypair::random().public().to_address();
        let packet_1 = OffchainKeypair::random().public().clone();
        let account_1 = hopr_db_entity::account::ActiveModel {
            chain_key: Set(chain_1.to_hex()),
            packet_key: Set(packet_1.to_hex()),
            ..Default::default()
        };

        let chain_2 = ChainKeypair::random().public().to_address();
        let packet_2 = OffchainKeypair::random().public().clone();
        let account_2 = hopr_db_entity::account::ActiveModel {
            chain_key: Set(chain_2.to_hex()),
            packet_key: Set(packet_2.to_hex()),
            ..Default::default()
        };

        hopr_db_entity::account::Entity::insert_many([account_1, account_2])
            .exec(&db.index_db)
            .await?;

        let actual_pk = db.resolve_packet_key(&chain_2).await?;

        assert_eq!(actual_pk, Some(packet_2), "packet keys must match");
        Ok(())
    }

    #[async_std::test]
    async fn test_get_offchain_key_should_succeed_if_a_mapping_to_chain_key_exists_with_cache() -> anyhow::Result<()> {
        let db = HoprDb::new_in_memory(ChainKeypair::random()).await?;

        // Inserting to the table via API to insert into cache as well

        let chain_1 = ChainKeypair::random().public().to_address();
        let packet_1 = OffchainKeypair::random().public().clone();
        db.insert_account(None, AccountEntry::new(packet_1, chain_1, AccountType::NotAnnounced))
            .await?;

        let chain_2 = ChainKeypair::random().public().to_address();
        let packet_2 = OffchainKeypair::random().public().clone();
        db.insert_account(None, AccountEntry::new(packet_2, chain_2, AccountType::NotAnnounced))
            .await?;

        let actual_pk = db.resolve_packet_key(&chain_2).await?;

        assert_eq!(actual_pk, Some(packet_2), "packet keys must match");
        Ok(())
    }
}