Skip to content

聊天

聊天模塊負責用戶鏈上聊天相關的資料管理,包括用戶的鏈上資訊存儲等

chat/
├── client
│   └── rest
│        ├── error.go          # 介面錯誤集
│        ├── grpc.go           # grpc查询方法
│        ├── handle_account.go # grpc査詢方法
│        ├── handle_chat.go    # chat模塊消息前置處理
│        ├── query.go          # 介面査詢函數
│        ├── rest.go           # 消息與査詢路由的注册
│        └── tx.go             # tx相關方法,包括廣播等
├── keeper
│   ├── genesis.go        # 模塊的狀態化和匯出
│   ├── keeper.go         # 存儲管理器,處理模塊的業務邏輯,並有許可權訪問狀態樹的特定子樹
│   ├── msg_server.go     # 提供上鏈消息的服務處理邏輯
│   ├── params.go         # 模塊參數的設定和獲取
│   ├── query.go          # 狀態査詢函數
├── types
│   ├── codec.go          # 編碼注册類型
│   ├── errors.go         # 模塊特定的錯誤
│   ├── events.go         # 向Tendermint PubSub/Websocket暴露的事件
│   ├── genesis.go        # 模塊的創世狀態
│   ├── interface.go      # 模塊的介面集合
│   ├── keys.go           # 模塊存儲key以及一些其他的常數
│   ├── msg.go            # 聊天模塊交易消息
│   ├── params.go         # 模塊可以通過治理模塊修改的自定義參數
│   └── types.go          # 模塊的類型集合
├── genesis.go            # ABCI的創世狀態初始化和匯出功能
├── handler.go            # 消息路由
└── module.go             # 模塊管理器的模塊設定

目錄

聊天

DaoDst作為一個帶有去中心化聊天的區塊鏈,其中聊天的數據的鏈上管理,主要就是通過'x/chat'模塊進行,

當用戶註冊聊天時,會發起廣播,將用戶的地址,閘道,did,聊天等資訊記錄到鏈上。

當其他功能或模塊需要調用這些數據時,也需要通過chat模塊獲取

狀態

Chat模塊存儲了關於聊天用戶的基礎鏈上數據

// 用户信息
type UserInfo struct {
    //錢包地址
    FromAddress string `json:"from_address" yaml:"from_address"`
    //注册閘道
    RegisterNodeAddress string `json:"register_node_address" yaml:"register_node_address"`
    //當前閘道
    NodeAddress string `json:"node_address" yaml:"node_address"`
    //通訊錄
    AddressBook string `json:"address_book" yaml:"address_book"`
    //通訊錄黑名單(用戶公開金鑰加密)
    ChatBlacklist string `json:"chat_blacklist" yaml:"chat_blacklist"`
    //聊天限制模式(fee | any | list)
    ChatRestrictedMode string `json:"chat_restricted_mode" yaml:"chat_restricted_mode"`
    //通訊錄白名單(用戶公開金鑰加密)
    ChatWhitelist string `json:"chat_whitelist" yaml:"chat_whitelist"`
    //聊天收費費用
    ChatFee types.Coin `json:"chat_fee" yaml:"chat_fee"`
    //持有did
    Mobile []string `json:"mobile" yaml:"mobile"`
    //最後更新時間
    UpdateTime int64 `json:"update_time" yaml:"update_time"`
    //通訊錄黑名單(閘道公開金鑰加密)
    ChatBlackEncList string `json:"chat_black_enc_list" yaml:"chat_black_enc_list"`
    //通訊錄白名單(閘道公開金鑰加密)
    ChatWhiteEncList string `json:"chat_white_enc_list" yaml:"chat_white_enc_list" `
    //備註
    Remarks string `json:"remarks" yaml:"remarks"`
}

用戶在注册的時候會將其初始化,

其中ChatRestrictedMode,ChatFee兩個關於聊天收費的參數會有預設值

Chat模塊的大多數業務邏輯都是基於此結構體的

DID

用戶註冊時需要選擇所屬的閘道,並選擇屬於閘道的一個號碼段 在此號碼段下,用戶將得到一個專屬的號碼(DID),此號碼歸屬與錢包地址(即使閘道不再存在) 處理前置號碼段外,後續的號碼是遞增的

比如某閘道下8888888號碼段,此號碼段下號碼已經到達1234 那麼下一個選擇此號碼段的人,獲得的號碼將會是88888881235

除了注册外,還可以主動銷毀來獲得DID

did 可以通主动转让

銷毀與質押

注册聊天时,需要销毁一定数量的DST,同时,注册合约将会自动把销毁得到的哈希币质押到 Pledge 模块,由此获得质押奖励

聊天收費模式

用戶註冊時,默認的聊天收費模式為”收費“,用戶可以自行發消息上鏈更改

有如下三種模式

  • 收費模式

開啟收費模式後,主動發起聊天或者拉入房間需要支付一定費用

  • 公開模式

開啟公開模式後,任何人都可以主動向你發起聊天

  • 白名單模式

開啟白名單模式後,只有在白名單內的地址可以向你發起聊天(白名單的設定同屬於chat模塊)

功能

聊天模塊' Keeper '授予對聊天模塊狀態的存取權限 並實現' statedb。 介面,以支持' StateDB '實現。 Keeper包含一個允許資料庫的存儲鍵 寫入只能由Chat模塊訪問的多庫的具體子樹。 而不是使用樹和資料庫進行査詢和持久化(' StateDB '實現), DaoDst使用Cosmos的“KVStore”(鍵值存儲)和Cosmos SDK的“Keeper”來促進狀態轉換。

為了支持介面功能,它導入了4個模塊Keepers:

  • auth: 帳號的增删改查
  • bank: 供應量和餘額的增删改查
  • comm: 閘道相關資料的管理
  • pledge: 質押、銷毀與獎勵
type Keeper struct {
    storeKey   sdk.StoreKey
    cdc        codec.BinaryCodec
    paramstore paramtypes.Subspace

    accountKeeper types.AccountKeeper
    bankKeeper    types.BankKeeper
    commKeeper    commkeeper.Keeper
    pledgeKeeper  pledgekeeper.Keeper
}

消息

MsgSendGift

通過此消息將地址注册到chat模塊,並初始化對應資訊
      type MsgRegister struct {
          FromAddress    string     `protobuf:"bytes,1,opt,name=from_address,json=fromAddress,proto3" json:"from_address,omitempty" yaml:"from_address"`
          NodeAddress    string     `protobuf:"bytes,2,opt,name=node_address,json=nodeAddress,proto3" json:"node_address,omitempty" yaml:"node_address"`
          MortgageAmount types.Coin `protobuf:"bytes,3,opt,name=mortgage_amount,json=mortgageAmount,proto3" json:"mortgage_amount" yaml:"mortgage_amount"`
          MobilePrefix   string     `protobuf:"bytes,4,opt,name=mobile_prefix,json=mobilePrefix,proto3" json:"mobile_prefix,omitempty" yaml:"mobile_prefix"`
      }

MsgSetChatInfo

通過此消息修改地址對應的chat模塊存儲的數據,該消息接受全量的用戶數據,並覆蓋原來的數據
    type MsgSetChatInfo struct {
        FromAddress        string     `protobuf:"bytes,1,opt,name=from_address,json=fromAddress,proto3" json:"from_address,omitempty" yaml:"from_address"`
        NodeAddress        string     `protobuf:"bytes,2,opt,name=node_address,json=nodeAddress,proto3" json:"node_address,omitempty" yaml:"node_address"`
        AddressBook        string     `protobuf:"bytes,3,opt,name=address_book,json=addressBook,proto3" json:"address_book,omitempty" yaml:"address_book"`
        ChatBlacklist      string     `protobuf:"bytes,4,opt,name=chat_blacklist,json=chatBlacklist,proto3" json:"chat_blacklist,omitempty" yaml:"chat_blacklist"`
        ChatRestrictedMode string     `protobuf:"bytes,5,opt,name=chat_restricted_mode,json=chatRestrictedMode,proto3" json:"chat_restricted_mode,omitempty" yaml:"chat_limit"`
        ChatFee            types.Coin `protobuf:"bytes,6,opt,name=chat_fee,json=chatFee,proto3" json:"chat_fee" yaml:"chat_fee"`
        ChatWhitelist      string     `protobuf:"bytes,7,opt,name=chat_whitelist,json=chatWhitelist,proto3" json:"chat_whitelist,omitempty" yaml:"chat_whitelist"`
        UpdateTime         int64      `protobuf:"varint,8,opt,name=update_time,json=updateTime,proto3" json:"update_time,omitempty" yaml:"update_time"`
        ChatBlacklistEnc   string     `protobuf:"bytes,9,opt,name=chat_blacklist_enc,json=chatBlacklistEnc,proto3" json:"chat_blacklist_enc,omitempty" yaml:"chat_blacklist_enc"`
        ChatWhitelistEnc   string     `protobuf:"bytes,10,opt,name=chat_whitelist_enc,json=chatWhitelistEnc,proto3" json:"chat_whitelist_enc,omitempty" yaml:"chat_whitelist_enc"`
    }

MsgBurnGetMobile

通過此消息通過銷毀的管道獲得did,獲得的did的首碼為選擇的did號段,尾碼則在此號段下遞增

如果號段與閘道不匹配或號段下號碼已滿,則獲取失敗
    type MsgBurnGetMobile struct {
        FromAddress  string `protobuf:"bytes,1,opt,name=from_address,json=fromAddress,proto3" json:"from_address,omitempty" yaml:"from_address"`
        MobilePrefix string `protobuf:"bytes,2,opt,name=mobile_prefix,json=mobilePrefix,proto3" json:"mobile_prefix,omitempty" yaml:"mobile_prefix"`
    }

MsgMobileTransfer

持有的did可以轉讓

如果對方持有的did已經到達最大持有量,則轉讓失敗
    type MsgMobileTransfer struct {
        FromAddress string `protobuf:"bytes,1,opt,name=from_address,json=fromAddress,proto3" json:"from_address,omitempty" yaml:"from_address"`
        ToAddress   string `protobuf:"bytes,2,opt,name=to_address,json=toAddress,proto3" json:"to_address,omitempty" yaml:"to_address"`
        Mobile      string `protobuf:"bytes,3,opt,name=mobile,proto3" json:"mobile,omitempty" yaml:"mobile"`
    }

MsgChatSendGift

聊天支付,如果對方設定了聊天收費,需要發起此消息給對方支付一定token
    type MsgChatSendGift struct {
        FromAddress string     `protobuf:"bytes,1,opt,name=from_address,json=fromAddress,proto3" json:"from_address,omitempty" yaml:"from_address"`
        ToAddress   string     `protobuf:"bytes,2,opt,name=to_address,json=toAddress,proto3" json:"to_address,omitempty" yaml:"to_address"`
        GiftValue   types.Coin `protobuf:"bytes,3,opt,name=gift_value,json=giftValue,proto3" json:"gift_value" yaml:"gift_value"`
    }

參數

chat模塊包含如下模塊參數

參數

Key 类型 预设值
MaxPhoneNumber uint64 10
DestroyPhoneNumberCoin github.com/cosmos/cosmos-sdk/types.Coin 100000000000000000000dst
ChatFee github.com/cosmos/cosmos-sdk/types.Coin 1000000000000000000dst
MinRegisterBurnAmount github.com/cosmos/cosmos-sdk/types.Coin 100000000000000000000dst