github.com/annchain/OG@v0.0.9/og/protocol/ogmessage/archive/accesories.go (about)

     1  package archive
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/annchain/OG/arefactor/og/types"
     6  	"github.com/annchain/OG/common/hexutil"
     7  	"github.com/annchain/bloom"
     8  	"strings"
     9  )
    10  
    11  //go:generate msgp
    12  
    13  const (
    14  	BloomItemNumber = 3000
    15  	HashFuncNum     = 8
    16  )
    17  
    18  //msgp:tuple BloomFilter
    19  type BloomFilter struct {
    20  	Data     []byte
    21  	Count    uint32
    22  	Capacity uint32
    23  	filter   *bloom.BloomFilter
    24  }
    25  
    26  func (c *BloomFilter) GetCount() uint32 {
    27  	if c == nil {
    28  		return 0
    29  	}
    30  	return c.Count
    31  }
    32  
    33  func (c *BloomFilter) Encode() error {
    34  	var err error
    35  	c.Data, err = c.filter.Encode()
    36  	return err
    37  }
    38  
    39  func (c *BloomFilter) Decode() error {
    40  	if c.Capacity == 0 {
    41  		c.filter = bloom.New(BloomItemNumber, HashFuncNum)
    42  	} else {
    43  		c.filter = bloom.New(uint(c.Capacity), HashFuncNum)
    44  	}
    45  	return c.filter.Decode(c.Data)
    46  }
    47  
    48  func NewDefaultBloomFilter() *BloomFilter {
    49  	c := &BloomFilter{}
    50  	c.filter = bloom.New(BloomItemNumber, HashFuncNum)
    51  	c.Count = 0
    52  	c.Capacity = 0 //0 if for default
    53  	return c
    54  }
    55  
    56  func NewBloomFilter(m uint32) *BloomFilter {
    57  	if m < BloomItemNumber {
    58  		return NewDefaultBloomFilter()
    59  	}
    60  	c := &BloomFilter{}
    61  	c.filter = bloom.New(uint(m), HashFuncNum)
    62  	c.Count = 0
    63  	c.Capacity = m
    64  	return c
    65  }
    66  
    67  func (c *BloomFilter) AddItem(item []byte) {
    68  	c.filter.Add(item)
    69  	c.Count++
    70  }
    71  
    72  func (c *BloomFilter) LookUpItem(item []byte) (bool, error) {
    73  	if c == nil || c.filter == nil {
    74  		return false, nil
    75  	}
    76  	return c.filter.Test(item), nil
    77  }
    78  
    79  type HashTerminat [4]byte
    80  
    81  type HashTerminats []HashTerminat
    82  
    83  func (h HashTerminat) String() string {
    84  	return hexutil.Encode(h[:])
    85  }
    86  
    87  func (h HashTerminats) String() string {
    88  	var strs []string
    89  	for _, v := range h {
    90  		strs = append(strs, v.String())
    91  	}
    92  	return strings.Join(strs, ", ")
    93  }
    94  
    95  //msgp:tuple MessageBodyData
    96  //type MessageBodyData struct {
    97  //	//RawTxs         *RawTxs
    98  //	//RawTermChanges *RawTermChanges
    99  //	//RawCampaigns   *RawCampaigns
   100  //	RawSequencer *RawSequencer
   101  //	RawTxs       *TxisMarshaler
   102  //}
   103  //
   104  //func (m *MessageBodyData) ToTxis() Txis {
   105  //	var txis Txis
   106  //	if m.RawTxs != nil {
   107  //		txs := m.RawTxs.Txis()
   108  //		txis = append(txis, txs...)
   109  //	}
   110  //	if len(txis) == 0 {
   111  //		return nil
   112  //	}
   113  //	return txis
   114  //}
   115  //
   116  //func (m *MessageBodyData) String() string {
   117  //	return fmt.Sprintf("txs: [%s], Sequencer: %s", m.RawTxs.String(), m.RawSequencer.String())
   118  //}
   119  
   120  // hashOrNumber is a combined field for specifying an origin block.
   121  //msgp:tuple HashOrNumber
   122  type HashOrNumber struct {
   123  	Hash   *types.Hash // Block hash from which to retrieve headers (excludes Number)
   124  	Number *uint64     // Block hash from which to retrieve headers (excludes Hash)
   125  }
   126  
   127  func (m *HashOrNumber) String() string {
   128  	if m.Hash == nil {
   129  		return fmt.Sprintf("hash: nil, number : %d ", *m.Number)
   130  	}
   131  	return fmt.Sprintf("hash: %s, number : %d", m.Hash.String(), m.Number)
   132  }
   133  
   134  //msgp:tuple RawData
   135  type RawData []byte