github.com/MetalBlockchain/metalgo@v1.11.9/pubsub/messages.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package pubsub
     5  
     6  import (
     7  	"github.com/MetalBlockchain/metalgo/api"
     8  	"github.com/MetalBlockchain/metalgo/utils/formatting/address"
     9  	"github.com/MetalBlockchain/metalgo/utils/json"
    10  )
    11  
    12  // NewBloom command for a new bloom filter
    13  //
    14  // Deprecated: The pubsub server is deprecated.
    15  type NewBloom struct {
    16  	// MaxElements size of bloom filter
    17  	MaxElements json.Uint64 `json:"maxElements"`
    18  	// CollisionProb expected error rate of filter
    19  	CollisionProb json.Float64 `json:"collisionProb"`
    20  }
    21  
    22  // NewSet command for a new map set
    23  //
    24  // Deprecated: The pubsub server is deprecated.
    25  type NewSet struct{}
    26  
    27  // AddAddresses command to add addresses
    28  //
    29  // Deprecated: The pubsub server is deprecated.
    30  type AddAddresses struct {
    31  	api.JSONAddresses
    32  
    33  	// addressIds array of addresses, kept as a [][]byte for use in the bloom filter
    34  	addressIds [][]byte
    35  }
    36  
    37  // Command execution command
    38  //
    39  // Deprecated: The pubsub server is deprecated.
    40  type Command struct {
    41  	NewBloom     *NewBloom     `json:"newBloom,omitempty"`
    42  	NewSet       *NewSet       `json:"newSet,omitempty"`
    43  	AddAddresses *AddAddresses `json:"addAddresses,omitempty"`
    44  }
    45  
    46  func (c *Command) String() string {
    47  	switch {
    48  	case c.NewBloom != nil:
    49  		return "newBloom"
    50  	case c.NewSet != nil:
    51  		return "newSet"
    52  	case c.AddAddresses != nil:
    53  		return "addAddresses"
    54  	default:
    55  		return "unknown"
    56  	}
    57  }
    58  
    59  func (c *NewBloom) IsParamsValid() bool {
    60  	p := float64(c.CollisionProb)
    61  	return c.MaxElements > 0 && 0 < p && p <= 1
    62  }
    63  
    64  // parseAddresses converts the bech32 addresses to their byte format.
    65  func (c *AddAddresses) parseAddresses() error {
    66  	if c.addressIds == nil {
    67  		c.addressIds = make([][]byte, len(c.Addresses))
    68  	}
    69  	for i, addrStr := range c.Addresses {
    70  		_, _, addrBytes, err := address.Parse(addrStr)
    71  		if err != nil {
    72  			return err
    73  		}
    74  		c.addressIds[i] = addrBytes
    75  	}
    76  	return nil
    77  }