sui_indexer_alt_schema/
objects.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use anyhow::Context;
use diesel::{
    backend::Backend, deserialize, expression::AsExpression, prelude::*, serialize,
    sql_types::SmallInt, FromSqlRow,
};

use sui_field_count::FieldCount;
use sui_types::object::{Object, Owner};

use crate::schema::{
    coin_balance_buckets, coin_balance_buckets_deletion_reference, kv_objects, obj_info,
    obj_info_deletion_reference, obj_versions,
};

#[derive(Insertable, Debug, Clone, FieldCount, Queryable)]
#[diesel(table_name = kv_objects, primary_key(object_id, object_version))]
#[diesel(treat_none_as_default_value = false)]
pub struct StoredObject {
    pub object_id: Vec<u8>,
    pub object_version: i64,
    pub serialized_object: Option<Vec<u8>>,
}

#[derive(
    Insertable, Selectable, Debug, Clone, PartialEq, Eq, FieldCount, Queryable, QueryableByName,
)]
#[diesel(table_name = obj_versions, primary_key(object_id, object_version))]
pub struct StoredObjVersion {
    pub object_id: Vec<u8>,
    pub object_version: i64,
    pub object_digest: Option<Vec<u8>>,
    pub cp_sequence_number: i64,
}

#[derive(AsExpression, FromSqlRow, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[diesel(sql_type = SmallInt)]
#[repr(i16)]
pub enum StoredOwnerKind {
    Immutable = 0,
    Address = 1,
    Object = 2,
    Shared = 3,
}

#[derive(AsExpression, FromSqlRow, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[diesel(sql_type = SmallInt)]
#[repr(i16)]
pub enum StoredCoinOwnerKind {
    Fastpath = 0,
    Consensus = 1,
}

#[derive(Insertable, Debug, Clone, FieldCount, Queryable)]
#[diesel(table_name = obj_info, primary_key(object_id, cp_sequence_number))]
#[diesel(treat_none_as_default_value = false)]
pub struct StoredObjInfo {
    pub object_id: Vec<u8>,
    pub cp_sequence_number: i64,
    pub owner_kind: Option<StoredOwnerKind>,
    pub owner_id: Option<Vec<u8>>,
    pub package: Option<Vec<u8>>,
    pub module: Option<String>,
    pub name: Option<String>,
    pub instantiation: Option<Vec<u8>>,
}

#[derive(Insertable, Debug, Clone, FieldCount, Queryable)]
#[diesel(table_name = obj_info_deletion_reference, primary_key(cp_sequence_number, object_id))]
pub struct StoredObjInfoDeletionReference {
    pub object_id: Vec<u8>,
    pub cp_sequence_number: i64,
}

#[derive(Insertable, Queryable, Debug, Clone, FieldCount, Eq, PartialEq)]
#[diesel(table_name = coin_balance_buckets, primary_key(object_id, cp_sequence_number))]
#[diesel(treat_none_as_default_value = false)]
pub struct StoredCoinBalanceBucket {
    pub object_id: Vec<u8>,
    pub cp_sequence_number: i64,
    pub owner_kind: Option<StoredCoinOwnerKind>,
    pub owner_id: Option<Vec<u8>>,
    pub coin_type: Option<Vec<u8>>,
    pub coin_balance_bucket: Option<i16>,
}

#[derive(Insertable, Queryable, Debug, Clone, FieldCount, Eq, PartialEq)]
#[diesel(table_name = coin_balance_buckets_deletion_reference, primary_key(cp_sequence_number, object_id))]
pub struct StoredCoinBalanceBucketDeletionReference {
    pub object_id: Vec<u8>,
    pub cp_sequence_number: i64,
}

impl StoredObjInfo {
    pub fn from_object(object: &Object, cp_sequence_number: i64) -> anyhow::Result<Self> {
        let type_ = object.type_();
        Ok(Self {
            object_id: object.id().to_vec(),
            cp_sequence_number,
            owner_kind: Some(match object.owner() {
                Owner::AddressOwner(_) => StoredOwnerKind::Address,
                Owner::ObjectOwner(_) => StoredOwnerKind::Object,
                Owner::Shared { .. } => StoredOwnerKind::Shared,
                Owner::Immutable => StoredOwnerKind::Immutable,
                // We do not distinguish between fastpath owned and consensus owned
                // objects.
                Owner::ConsensusAddressOwner { .. } => StoredOwnerKind::Address,
            }),

            owner_id: match object.owner() {
                Owner::AddressOwner(a) => Some(a.to_vec()),
                Owner::ObjectOwner(o) => Some(o.to_vec()),
                Owner::Shared { .. } | Owner::Immutable { .. } => None,
                Owner::ConsensusAddressOwner { owner, .. } => Some(owner.to_vec()),
            },

            package: type_.map(|t| t.address().to_vec()),
            module: type_.map(|t| t.module().to_string()),
            name: type_.map(|t| t.name().to_string()),
            instantiation: type_
                .map(|t| bcs::to_bytes(&t.type_params()))
                .transpose()
                .with_context(|| {
                    format!(
                        "Failed to serialize type parameters for {}",
                        object.id().to_canonical_display(/* with_prefix */ true),
                    )
                })?,
        })
    }
}

impl<DB: Backend> serialize::ToSql<SmallInt, DB> for StoredOwnerKind
where
    i16: serialize::ToSql<SmallInt, DB>,
{
    fn to_sql<'b>(&'b self, out: &mut serialize::Output<'b, '_, DB>) -> serialize::Result {
        match self {
            StoredOwnerKind::Immutable => 0.to_sql(out),
            StoredOwnerKind::Address => 1.to_sql(out),
            StoredOwnerKind::Object => 2.to_sql(out),
            StoredOwnerKind::Shared => 3.to_sql(out),
        }
    }
}

impl<DB: Backend> deserialize::FromSql<SmallInt, DB> for StoredOwnerKind
where
    i16: deserialize::FromSql<SmallInt, DB>,
{
    fn from_sql(raw: DB::RawValue<'_>) -> deserialize::Result<Self> {
        Ok(match i16::from_sql(raw)? {
            0 => StoredOwnerKind::Immutable,
            1 => StoredOwnerKind::Address,
            2 => StoredOwnerKind::Object,
            3 => StoredOwnerKind::Shared,
            o => return Err(format!("Unexpected StoredOwnerKind: {o}").into()),
        })
    }
}

impl<DB: Backend> serialize::ToSql<SmallInt, DB> for StoredCoinOwnerKind
where
    i16: serialize::ToSql<SmallInt, DB>,
{
    fn to_sql<'b>(&'b self, out: &mut serialize::Output<'b, '_, DB>) -> serialize::Result {
        match self {
            StoredCoinOwnerKind::Fastpath => 0.to_sql(out),
            StoredCoinOwnerKind::Consensus => 1.to_sql(out),
        }
    }
}

impl<DB: Backend> deserialize::FromSql<SmallInt, DB> for StoredCoinOwnerKind
where
    i16: deserialize::FromSql<SmallInt, DB>,
{
    fn from_sql(raw: DB::RawValue<'_>) -> deserialize::Result<Self> {
        Ok(match i16::from_sql(raw)? {
            0 => StoredCoinOwnerKind::Fastpath,
            1 => StoredCoinOwnerKind::Consensus,
            o => return Err(format!("Unexpected StoredCoinOwnerKind: {o}").into()),
        })
    }
}