Package convex.core

Class Peer

java.lang.Object
convex.core.Peer

public class Peer extends Object

Immutable class representing the encapsulated state of a Peer

SECURITY:
  • Needs to contain the Peer's unlocked private key for online signing.
  • Manages Peer state transitions given external events. Must do so correctly.

Must have at least one state, the initial state. New states will be added as consensus updates happen.

"Don't worry about what anybody else is going to do. The best way to predict the future is to invent it." - Alan Kay
  • Method Details

    • fromData

      public static Peer fromData(AKeyPair keyPair, AMap<Keyword,​ACell> peerData)
      Constructs a Peer instance from persisted PEer Data
      Parameters:
      keyPair - Key Pair for Peer
      peerData - Peer data map
      Returns:
      NEw Peer instance
    • toData

      public AMap<Keyword,​ACell> toData()
      Gets the Peer Datat map for this Peer
      Returns:
      Peer data
    • create

      public static Peer create(AKeyPair peerKP, State initialState)
      Creates a Peer
      Parameters:
      peerKP - Key Pair
      initialState - Genesis State
      Returns:
      New Peer instance
    • create

      public static Peer create(AKeyPair peerKP, State initialState, Belief remoteBelief)
      Create a Peer instance from a remotely acquired Belief
      Parameters:
      peerKP - Peer KeyPair
      initialState - Initial genesis State of the Network
      remoteBelief - Remote belief to sync with
      Returns:
      New Peer instance
    • restorePeer

      public static Peer restorePeer(AStore store, AKeyPair keyPair) throws IOException
      Restores a Peer from the Etch database specified in Config
      Parameters:
      store - Store to restore from
      keyPair - Key Pair to use for restored Peer
      Returns:
      Peer instance, or null if root hash was not found
      Throws:
      IOException - If store reading failed
    • getPeerData

      public static AMap<Keyword,​ACell> getPeerData(AStore store) throws IOException
      Gets Peer Data from a Store.
      Parameters:
      store - Store to retrieve Peer Datat from
      Returns:
      Peer data map, or null if not available
      Throws:
      IOException - If a store IO error occurs
    • createGenesisPeer

      public static Peer createGenesisPeer(AKeyPair keyPair, State genesisState)
      Creates a new Peer instance at server startup using the provided configuration. Current store must be set to store for server.
      Parameters:
      keyPair - Key pair for genesis peer
      genesisState - Genesis state, or null to generate fresh state
      Returns:
      A new Peer instance
    • getMergeContext

      public MergeContext getMergeContext()
      Gets a MergeContext for this Peer
      Returns:
      MergeContext
    • updateTimestamp

      public Peer updateTimestamp(long newTimestamp)
      Updates the timestamp to the specified time, going forwards only
      Parameters:
      newTimestamp - New Peer timestamp
      Returns:
      This peer upated with the given timestamp
    • executeQuery

      public <T extends ACell> Context<T> executeQuery(ACell form, Address address)
      Compiles and executes a query on the current consensus state of this Peer.
      Type Parameters:
      T - Type of result
      Parameters:
      form - Form to compile and execute.
      address - Address to use for query execution
      Returns:
      The Context containing the query results. Will be NOBODY error if address / account does not exist
    • estimateCost

      public long estimateCost(ATransaction trans)
      Estimates the coin cost of a executing a given transaction by performing a "dry run". This will be exact if no intermediate transactions affect the state, and if no time-dependent functionality is used.
      Parameters:
      trans - Transaction to test
      Returns:
      Estimated cost
    • executeDryRun

      public <T extends ACell> Context<T> executeDryRun(ATransaction transaction)
      Executes a "dry run" transaction on the current consensus state of this Peer.
      Type Parameters:
      T - Type of Result
      Parameters:
      transaction - Transaction to execute
      Returns:
      The Context containing the transaction results.
    • executeQuery

      public <T extends ACell> Context<T> executeQuery(ACell form)
      Executes a query in this Peer's current Consensus State, using a default address
      Type Parameters:
      T - Type of query result
      Parameters:
      form - Form to execute as a Query
      Returns:
      Context after executing query
    • getTimeStamp

      public long getTimeStamp()
      Gets the timestamp of this Peer
      Returns:
      Timestamp
    • getPeerKey

      public AccountKey getPeerKey()
      Gets the Peer Key of this Peer.
      Returns:
      Peer Key of Peer.
    • getController

      public Address getController()
      Gets the controller Address for this Peer
      Returns:
      Address of Peer controller Account, or null if does not exist
    • getKeyPair

      public AKeyPair getKeyPair()
      Gets the Peer Key of this Peer.
      Returns:
      Address of Peer.
    • getBelief

      public Belief getBelief()
      Get the current Belief of this Peer
      Returns:
      Belief
    • getSignedBelief

      public SignedData<Belief> getSignedBelief()
      Get the signed Belief of this Peer
      Returns:
      Signed Belief
    • sign

      public <T extends ACell> SignedData<T> sign(T value)
      Signs a value with the keypair of this Peer
      Type Parameters:
      T - Type of value to sign
      Parameters:
      value - Value to sign
      Returns:
      Signed data value
    • getConsensusState

      public State getConsensusState()
      Gets the current consensus state for this chain
      Returns:
      Consensus state for this chain (initial state if no block consensus)
    • mergeBeliefs

      public Peer mergeBeliefs(Belief... beliefs) throws BadSignatureException, InvalidDataException
      Merges a set of new Beliefs into this Peer's belief. Beliefs may be null, in which case they are ignored.
      Parameters:
      beliefs - An array of Beliefs. May contain nulls, which will be ignored.
      Returns:
      Updated Peer after Belief Merge
      Throws:
      InvalidDataException - if
      BadSignatureException - IF a Signature validation fails
    • persistState

      public Peer persistState(Consumer<Ref<ACell>> noveltyHandler)
      Persist the state of the Peer to the current store. We ensure states and results are also persisted
      Parameters:
      noveltyHandler - Novelty handler for Belief
      Returns:
      Updates Peer
    • getStates

      public AVector<State> getStates()
      Gets the vector of States maintained by this Peer, starting from the Genesis state (index 0).
      Returns:
      Vector of states
    • getResult

      public Result getResult(long blockIndex, long txIndex)
      Gets the result of a specific transaction
      Parameters:
      blockIndex - Index of Block in Order
      txIndex - Index of transaction in block
      Returns:
      Result from transaction
    • getBlockResult

      public BlockResult getBlockResult(long i)
      Gets the BlockResult of a specific block index
      Parameters:
      i - Index of Block
      Returns:
      BlockResult
    • proposeBlock

      public Peer proposeBlock(Block block)
      Propose a new Block. Adds the block to the current proposed chain for this Peer.
      Parameters:
      block - Block to publish
      Returns:
      Peer after proposing new Block in Peer's own Order
    • getConsensusPoint

      public long getConsensusPoint()
      Gets the Consensus Point for this Peer
      Returns:
      Consensus Point value
    • getPeerOrder

      public Order getPeerOrder()
      Gets the current Order for this Peer
      Returns:
      The Order for this peer in its current Belief. Will return null if the Peer is not a peer in the current consensus state
    • getOrder

      public Order getOrder(AccountKey peerKey)
      Gets the current chain this Peer sees for a given peer address
      Parameters:
      peerKey - Peer Key
      Returns:
      The current Order for the specified peer
    • asOf

      public State asOf(CVMLong timestamp)
      Returns State as-of timestamp. Timestamp doesn't need to be an exact match; a leftmost State will be returned - unless timestamp is too old.
      Parameters:
      timestamp - Timestamp in milliseconds.
      Returns:
      State or null.
    • asOfRange

      public AVector<State> asOfRange(CVMLong timestamp, long interval, int count)
      Construct a vector of States starting at specified timestamp, and with a given interval in milliseconds.
      Parameters:
      timestamp - Timestamp in milliseconds.
      interval - Interval in milliseconds.
      count - Number of times to query.
      Returns:
      Vector of States.
    • getNetworkID

      public Hash getNetworkID()
      Get the Network ID for this PEer
      Returns:
      Network ID