Package convex.core
Class Peer
java.lang.Object
convex.core.Peer
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 Summary
Modifier and TypeMethodDescriptionReturns State as-of timestamp.Construct a vector of States starting at specified timestamp, and with a given interval in milliseconds.static Peer
Creates a Peerstatic Peer
Create a Peer instance from a remotely acquired Beliefstatic Peer
createGenesisPeer
(AKeyPair keyPair, State genesisState) Creates a new Peer instance at server startup using the provided configuration.long
estimateCost
(ATransaction trans) Estimates the coin cost of a executing a given transaction by performing a "dry run".executeDryRun
(ATransaction transaction) Executes a "dry run" transaction on the current consensus state of this Peer.executeQuery
(ACell form) Executes a query in this Peer's current Consensus State, using a default addressexecuteQuery
(ACell form, Address address) Compiles and executes a query on the current consensus state of this Peer.static Peer
Constructs a Peer instance from persisted PEer DataGet the current Belief of this PeergetBlockResult
(long i) Gets the BlockResult of a specific block indexlong
Gets the Consensus Point for this PeerGets the current consensus state for this chainGets the controller Address for this PeerGets the Peer Key of this Peer.Gets a MergeContext for this PeerGet the Network ID for this PEergetOrder
(AccountKey peerKey) Gets the current chain this Peer sees for a given peer addressgetPeerData
(AStore store) Gets Peer Data from a Store.Gets the Peer Key of this Peer.Gets the current Order for this PeergetResult
(long blockIndex, long txIndex) Gets the result of a specific transactionGet the signed Belief of this PeerGets the vector of States maintained by this Peer, starting from the Genesis state (index 0).long
Gets the timestamp of this PeermergeBeliefs
(Belief... beliefs) Merges a set of new Beliefs into this Peer's belief.persistState
(Consumer<Ref<ACell>> noveltyHandler) Persist the state of the Peer to the current store.proposeBlock
(Block block) Propose a new Block.static Peer
restorePeer
(AStore store, AKeyPair keyPair) Restores a Peer from the Etch database specified in Config<T extends ACell>
SignedData<T>sign
(T value) Signs a value with the keypair of this PeertoData()
Gets the Peer Data map for this PeerupdateTimestamp
(long newTimestamp) Updates the timestamp to the specified time, going forwards only
-
Method Details
-
fromData
Constructs a Peer instance from persisted PEer Data- Parameters:
keyPair
- Key Pair for PeerpeerData
- Peer data map- Returns:
- New Peer instance
-
toData
Gets the Peer Data map for this Peer- Returns:
- Peer data
-
create
Creates a Peer- Parameters:
peerKP
- Key PairinitialState
- Genesis State- Returns:
- New Peer instance
-
create
Create a Peer instance from a remotely acquired Belief- Parameters:
peerKP
- Peer KeyPairinitialState
- Initial genesis State of the NetworkremoteBelief
- Remote belief to sync with- Returns:
- New Peer instance
-
restorePeer
Restores a Peer from the Etch database specified in Config- Parameters:
store
- Store to restore fromkeyPair
- 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
Gets Peer Data from a Store.- Parameters:
store
- Store to retrieve Peer Data from- Returns:
- Peer data map, or null if not available
- Throws:
IOException
- If a store IO error occurs
-
createGenesisPeer
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 peergenesisState
- Genesis state, or null to generate fresh state- Returns:
- A new Peer instance
-
getMergeContext
Gets a MergeContext for this Peer- Returns:
- MergeContext
-
updateTimestamp
Updates the timestamp to the specified time, going forwards only- Parameters:
newTimestamp
- New Peer timestamp- Returns:
- This peer upated with the given timestamp
-
executeQuery
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. If null, core address will be used- Returns:
- The Context containing the query results. Will be NOBODY error if address / account does not exist
-
estimateCost
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
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
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
Gets the Peer Key of this Peer.- Returns:
- Peer Key of Peer.
-
getController
Gets the controller Address for this Peer- Returns:
- Address of Peer controller Account, or null if does not exist
-
getKeyPair
Gets the Peer Key of this Peer.- Returns:
- Address of Peer.
-
getBelief
Get the current Belief of this Peer- Returns:
- Belief
-
getSignedBelief
Get the signed Belief of this Peer- Returns:
- Signed Belief
-
sign
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
Gets the current consensus state for this chain- Returns:
- Consensus state for this chain (initial state if no block consensus)
-
mergeBeliefs
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
- ifBadSignatureException
- IF a Signature validation fails
-
persistState
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
Gets the vector of States maintained by this Peer, starting from the Genesis state (index 0).- Returns:
- Vector of states
-
getResult
Gets the result of a specific transaction- Parameters:
blockIndex
- Index of Block in OrdertxIndex
- Index of transaction in block- Returns:
- Result from transaction
-
getBlockResult
Gets the BlockResult of a specific block index- Parameters:
i
- Index of Block- Returns:
- BlockResult
-
proposeBlock
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
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
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
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
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
Get the Network ID for this PEer- Returns:
- Network ID
-