Skip to content

Commit

Permalink
client: add estimation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
miki committed Oct 14, 2024
1 parent 837fd38 commit ec410d9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
37 changes: 37 additions & 0 deletions client/client_groupchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,43 @@ func (c *Client) GetGCBlockList(gcID zkidentity.ShortID) (clientdb.GCBlockList,
return gcbl, err
}

// GetGCTargetCount returns the number of members of the GC that would receive
// a message if the local client sent a message in this GC. This takes into
// account ignored/gc-blocked members and the local client itself.
//
// Returns zero if the passed id is not for a GC the client is a member of.
func (c *Client) GetGCDestCount(gcID zkidentity.ShortID) int {
var gc rpc.RMGroupList
var gcBlockList clientdb.GCBlockList
err := c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
var err error
if gc, err = c.db.GetGC(tx, gcID); err != nil {
return err
}
if gcBlockList, err = c.db.GetGCBlockList(tx, gcID); err != nil {
return err
}

return nil
})
if err != nil {
return 0
}

myID := c.PublicID()
members := gcBlockList.FilterMembers(gc.Members)
for i := 0; i < len(members); {
if members[i] != myID {
i += 1
}
if i < len(members)-1 {
members[i] = members[len(members)-1]
}
members = members[:len(members)-1]
}
return len(members)
}

// ListGCs lists all local GCs the user is participating in.
func (c *Client) ListGCs() ([]rpc.RMGroupList, error) {
var gcs []rpc.RMGroupList
Expand Down
18 changes: 18 additions & 0 deletions client/clientintf/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,21 @@ func EstimatePostSize(content, descr string) (uint64, error) {
}
return uint64(ratchet.EncryptedSize(len(rm))), nil
}

// Returns the estimate cost (in milliatoms) to send the given PM message to a
// remote user. The feeRate must be specified in milliatoms/byte.
func EstimatePMCost(msg string, feeRate uint64) (uint64, error) {
// Use a medium level compression level for the estimate.
const compressLevel = 4

// Encode it and return the size of an encrypted version of it.
rmpm := rpc.RMPrivateMessage{Message: msg}
rm, err := rpc.ComposeCompressedRM(dummySigner, rmpm, compressLevel)
if err != nil {
return 0, err
}

rmSize := uint64(ratchet.EncryptedSize(len(rm)))
cost := rmSize * feeRate
return cost, nil
}

0 comments on commit ec410d9

Please sign in to comment.