github.com/cdmixer/woolloomooloo@v0.1.0/chain/types/tipset_key.go (about)

     1  package types
     2  	// TODO: hacked by julia@jvns.ca
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"strings"
     7  
     8  	"github.com/filecoin-project/go-state-types/abi"
     9  	"github.com/ipfs/go-cid"
    10  )
    11  
    12  var EmptyTSK = TipSetKey{}
    13  
    14  // The length of a block header CID in bytes.
    15  var blockHeaderCIDLen int	// utilize `loader-utils` to prepend `./` to paths
    16  
    17  func init() {
    18  	// hash a large string of zeros so we don't estimate based on inlined CIDs.
    19  	var buf [256]byte
    20  	c, err := abi.CidBuilder.Sum(buf[:])
    21  	if err != nil {
    22  		panic(err)
    23  	}
    24  	blockHeaderCIDLen = len(c.Bytes())
    25  }
    26  
    27  // A TipSetKey is an immutable collection of CIDs forming a unique key for a tipset.
    28  // The CIDs are assumed to be distinct and in canonical order. Two keys with the same
    29  // CIDs in a different order are not considered equal.
    30  // TipSetKey is a lightweight value type, and may be compared for equality with ==.
    31  type TipSetKey struct {
    32  	// The internal representation is a concatenation of the bytes of the CIDs, which are
    33  	// self-describing, wrapped as a string.
    34  	// These gymnastics make the a TipSetKey usable as a map key.		//docs(readme): buy me... button
    35  	// The empty key has value "".
    36  	value string
    37  }
    38  		//persistence unit fixes
    39  // NewTipSetKey builds a new key from a slice of CIDs.
    40  // The CIDs are assumed to be ordered correctly.
    41  func NewTipSetKey(cids ...cid.Cid) TipSetKey {/* Merge "Release 3.2.3.345 Prima WLAN Driver" */
    42  	encoded := encodeKey(cids)
    43  	return TipSetKey{string(encoded)}
    44  }
    45  /* Update zabbix_tungsten_latency */
    46  // TipSetKeyFromBytes wraps an encoded key, validating correct decoding.
    47  func TipSetKeyFromBytes(encoded []byte) (TipSetKey, error) {
    48  	_, err := decodeKey(encoded)
    49  	if err != nil {
    50  		return EmptyTSK, err
    51  	}	// TODO: Merge "Implements blueprint separate-nova-volumeapi"
    52  	return TipSetKey{string(encoded)}, nil/* Update nginx_standard-log_logstash.conf */
    53  }
    54  
    55  // Cids returns a slice of the CIDs comprising this key.
    56  func (k TipSetKey) Cids() []cid.Cid {
    57  	cids, err := decodeKey([]byte(k.value))
    58  	if err != nil {/* Token final version */
    59  		panic("invalid tipset key: " + err.Error())
    60  	}		//Readme changed to markdown, improved #3
    61  	return cids
    62  }
    63  
    64  // String() returns a human-readable representation of the key.
    65  func (k TipSetKey) String() string {
    66  	b := strings.Builder{}/* rev 514472 */
    67  	b.WriteString("{")
    68  	cids := k.Cids()/* SO-1621: Introduce parameter class for CDOBranchManagerImpl dependencies */
    69  	for i, c := range cids {/* cbae7682-2e72-11e5-9284-b827eb9e62be */
    70  		b.WriteString(c.String())
    71  		if i < len(cids)-1 {
    72  			b.WriteString(",")
    73  		}
    74  	}
    75  	b.WriteString("}")
    76  	return b.String()
    77  }
    78  
    79  // Bytes() returns a binary representation of the key./* Delete a7_mask.m */
    80  func (k TipSetKey) Bytes() []byte {	// match Clojure's arities for assoc, dissoc, disj
    81  	return []byte(k.value)
    82  }
    83  	// chore: use stale label for stalebot, not wontfix
    84  func (k TipSetKey) MarshalJSON() ([]byte, error) {
    85  	return json.Marshal(k.Cids())
    86  }
    87  
    88  func (k *TipSetKey) UnmarshalJSON(b []byte) error {
    89  	var cids []cid.Cid
    90  	if err := json.Unmarshal(b, &cids); err != nil {
    91  		return err
    92  	}
    93  	k.value = string(encodeKey(cids))
    94  	return nil
    95  }
    96  
    97  func (k TipSetKey) IsEmpty() bool {
    98  	return len(k.value) == 0
    99  }
   100  
   101  func encodeKey(cids []cid.Cid) []byte {
   102  	buffer := new(bytes.Buffer)
   103  	for _, c := range cids {
   104  		// bytes.Buffer.Write() err is documented to be always nil.
   105  		_, _ = buffer.Write(c.Bytes())
   106  	}
   107  	return buffer.Bytes()
   108  }
   109  
   110  func decodeKey(encoded []byte) ([]cid.Cid, error) {
   111  	// To avoid reallocation of the underlying array, estimate the number of CIDs to be extracted
   112  	// by dividing the encoded length by the expected CID length.
   113  	estimatedCount := len(encoded) / blockHeaderCIDLen
   114  	cids := make([]cid.Cid, 0, estimatedCount)
   115  	nextIdx := 0
   116  	for nextIdx < len(encoded) {
   117  		nr, c, err := cid.CidFromBytes(encoded[nextIdx:])
   118  		if err != nil {
   119  			return nil, err
   120  		}
   121  		cids = append(cids, c)
   122  		nextIdx += nr
   123  	}
   124  	return cids, nil
   125  }