Package convex.core

Class Belief

All Implemented Interfaces:
IAssociative<Keyword,ACell>, 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(Keyword 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
    • updateRefs

      public Belief updateRefs(IRefFunction func)
      Description copied from class: ACell
      Updates all Refs in this object using the given function. The function *must not* change the hash value of Refs, in order to ensure structural integrity of modified data structures. The implementation *should* re-attach any original encoding in order to prevent re-encoding or surplus hashing This is a building block for a very sneaky trick that enables use to do a lot of efficient operations on large trees of smart references. Must return the same object if no Refs are altered.
      Overrides:
      updateRefs in class ACell
      Parameters:
      func - Ref update function
      Returns:
      Cell with updated Refs
    • 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.
    • create

      public static Belief create(SignedData<Order>... orders)
    • create

      public static Belief create(HashMap<AccountKey,SignedData<Order>> orderMap)
    • 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
    • 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. Cell must be canonical, or else an error may occur.
      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 highly likely to contain the entire object when encoded, including the tag byte. Should not traverse soft Refs, i.e. must be usable on arbitrary partial data structures
      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(Blob b, int pos) throws BadFormatException
      Throws:
      BadFormatException
    • 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
    • equals

      public boolean equals(ACell a)
      Description copied from class: ACell
      Checks for equality with another Cell. In general, Cells are considered equal if they have the same canonical representation, i.e. an identical encoding with the same hash value. Subclasses SHOULD override this if they have a more efficient equals implementation. MUST NOT require reads from Store.
      Overrides:
      equals in class ACell
      Parameters:
      a - Cell to compare with. May be null.
      Returns:
      True if this cell is equal to the other object
    • 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
    • getRefCount

      public int getRefCount()
      Description copied from class: ACell
      Gets the number of Refs contained within this Cell. This number is final / immutable for any given instance and is defined by the Cell encoding rules. WARNING: may not be valid id Cell is not canonical Contained Refs may be either external or embedded.
      Specified by:
      getRefCount in class ACell
      Returns:
      The number of Refs in this Cell
    • getRef

      public <R extends ACell> Ref<R> getRef(int i)
      Description copied from class: ACell
      Gets a numbered child Ref from within this Cell. WARNING: May be unreliable is cell is not canonical
      Overrides:
      getRef in class ACell
      Type Parameters:
      R - Type of referenced Cell
      Parameters:
      i - Index of ref to get
      Returns:
      The Ref at the specified index
    • getOrdersHashMap

      public HashMap<AccountKey,SignedData<Order>> getOrdersHashMap()
      Gets a new HashMap containing all Orders
      Returns:
      HashMap of current orders
    • getFormat

      public RecordFormat getFormat()
      Description copied from class: ARecord
      Gets the RecordFormat instance that describes this Record's layout
      Specified by:
      getFormat in class ARecord
      Returns:
      RecordFormat instance
    • extractOrders

      public static Collection<SignedData<Order>> extractOrders(ACell payload)
      Extract a collection of Orders from a Cell, suitable for Belief merge
      Parameters:
      payload - Cell to extra orders from
      Returns:
      Collection of signed orders
    • proposeBlock

      public Belief proposeBlock(AKeyPair kp, SignedData<Block> signedBlock)