Package convex.core

Class Belief

All Implemented Interfaces:
IValidated, IWriteable, Map<Keyword,​ACell>

public class Belief extends ARecord
Class representing a Peer's view of the overall network consensus state. Belief is immutable, and is designed to be independent of any particular Peer so that it can be efficiently merged towards consensus. Belief can be merged with other Beliefs from the perspective of a Peer. This property is fundamental to the Convex consensus algorithm. "Sorry to be a wet blanket. Writing a description for this thing for general audiences is bloody hard. There's nothing to relate it to." – Satoshi Nakamoto
  • Method Details

    • get

      public ACell get(ACell k)
      Description copied from class: ARecord
      Gets the record field content for a given key, or null if not found.
      Specified by:
      get in class ARecord
      Parameters:
      k - Key to look up in this record
      Returns:
      Field value for the given key
    • updateAll

      protected Belief updateAll(ACell[] newVals)
      Description copied from class: ARecord
      Updates all values in this record, in declared field order. Returns this if all values are identical.
      Specified by:
      updateAll in class ARecord
      Parameters:
      newVals - New values to replace current
      Returns:
      Updated Record
    • initial

      public static Belief initial()
      Gets an empty Belief
      Returns:
      Empty Belief
    • create

      public static Belief create(AKeyPair kp, Order order)
      Create a Belief with a single order signed by the given key pair, using initial timestamp.
      Parameters:
      kp - Peer Key pair with which to sign the order.
      order - Order of blocks that the Peer is proposing
      Returns:
      new Belief representing the isolated belief of a single Peer.
    • createSingleOrder

      public static Belief createSingleOrder(AKeyPair kp)
      Create a Belief with a single empty order. USeful for Peer startup.
      Parameters:
      kp - Keypair for Peer
      Returns:
      New Belief
    • merge

      public Belief merge(MergeContext mc, Belief... beliefs) throws BadSignatureException, InvalidDataException
      The Belief merge function
      Parameters:
      mc - MergeContext for Belief Merge
      beliefs - An array of Beliefs. May contain nulls, which will be ignored.
      Returns:
      The updated merged belief, or the same Belief if there is no change.
      Throws:
      BadSignatureException - In case of a bad signature
      InvalidDataException - In case of invalid data
    • computeWinningOrder

      public static AVector<Block> computeWinningOrder(HashMap<Order,​Double> stakedOrders, long consensusPoint, double initialTotalStake)
      Compute the new winning Order for this Peer, including any new blocks encountered
      Parameters:
      stakedOrders - Amount of stake on each distinct Order
      consensusPoint - Current consensus point
      initialTotalStake - Total stake under consideration
      Returns:
      Vector of Blocks in wiing Order
    • computeVote

      public static <V> double computeVote(HashMap<V,​Double> m)
      Computes the total vote for all entries in a HashMap
      Type Parameters:
      V - The type of values used as keys in the HashMap
      Parameters:
      m - A map of values to votes
      Returns:
      The total voting stake
    • prepareStakedOrders

      public static double prepareStakedOrders(AMap<AccountKey,​SignedData<Order>> peerOrders, HashMap<AccountKey,​Double> peerStakes, HashMap<Order,​Double> dest)
      Compute the total stake for every distinct Order seen. Stores results in a map of Orders to staked value.
      Parameters:
      peerOrders - A map of peer addresses to signed proposed Orders
      peerStakes - A map of peers addresses to weighted stakes for each peer
      dest - Destination hashmap to store the stakes for each Order
      Returns:
      The total stake of all chains among peers under consideration
    • withOrders

      public Belief withOrders(BlobMap<AccountKey,​SignedData<Order>> newOrders)
      Updates this Belief with a new set of Chains for each peer address
      Parameters:
      newOrders - New map of peer keys to Orders
      Returns:
      The updated belief, or the same Belief if no change.
    • encode

      public int encode(byte[] bs, int pos)
      Description copied from class: ACell
      Writes this Cell's encoding to a byte array, including a tag byte which will be written first
      Specified by:
      encode in interface IWriteable
      Specified by:
      encode in class ACell
      Parameters:
      bs - A byte array to which to write the encoding
      pos - The offset into the byte array
      Returns:
      New position after writing
    • estimatedEncodingSize

      public int estimatedEncodingSize()
      Description copied from interface: IWriteable
      Estimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is likely to contain the entire object when represented in binary format, including the tag byte.
      Specified by:
      estimatedEncodingSize in interface IWriteable
      Overrides:
      estimatedEncodingSize in class ARecord
      Returns:
      The estimated size for the binary representation of this object.
    • read

      public static Belief read(ByteBuffer bb) throws BadFormatException
      Read a Belief from a ByteBuffer. Assumes tag already read.
      Parameters:
      bb - ByteBuffer to read from
      Returns:
      Belief instance
      Throws:
      BadFormatException - If encoding is invalid
    • getTag

      public byte getTag()
      Description copied from class: ARecord
      Gets the tag byte for this record type. The Tag is the byte used to identify the record in the binary encoding.
      Specified by:
      getTag in class ARecord
      Returns:
      Record tag byte
    • getOrder

      public Order getOrder(AccountKey address)
      Gets the current Order for a given Address within this Belief.
      Parameters:
      address - Address of peer
      Returns:
      The chain for the peer within this Belief, or null if noy found.
    • getOrders

      public BlobMap<AccountKey,​SignedData<Order>> getOrders()
      Get the map of orders for this Belief
      Returns:
      Orders map
    • validateCell

      public void validateCell() throws InvalidDataException
      Description copied from class: ACell
      Validates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.
      Specified by:
      validateCell in class ACell
      Throws:
      InvalidDataException - If the Cell is invalid
    • getTimestamp

      public long getTimestamp()
      Returns the timestamp of this Belief. A Belief should have a new timestamp if and only if the Peer incorporates new information.
      Returns:
      Timestamp of belief
    • equals

      public boolean equals(AMap<Keyword,​ACell> a)
      Description copied from class: AMap
      Checks this map for equality with another map. In general, maps should be considered equal if they have the same canonical representation, i.e. the same hash value. Subclasses may override this this they have a more efficient equals implementation or a more specific definition of equality.
      Specified by:
      equals in class AMap<Keyword,​ACell>
      Parameters:
      a - Map to compare with
      Returns:
      true if maps are equal, false otherwise.
    • equals

      public boolean equals(Belief a)
      Tests if this Belief is equal to another
      Parameters:
      a - Belief to compare with
      Returns:
      true if equal, false otherwise