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 TypeMethodDescriptionstatic 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 PeerGets the controller Address for this PeerGets the genesis State of this PeerGets the Peer Key of this Peer.Get the Network ID for this PEergetOrder
(AccountKey peerKey) Gets the current chain this Peer sees for a given peer addressgetPeerData
(AStore store) LikegetPeerData(AStore, ACell)
but uses a null root key.getPeerData
(AStore store, ACell rootKey) Gets Peer Data from a Store.Gets the Peer Key of this Peer.Gets the current Order for this Peerlong
Gets the state position of this Peer, which is equal to the number of state transitions executed.getResult
(long blockIndex, long txIndex) Gets the Result of a specific transactionlong
Gets the timestamp of this PeermergeBeliefs
(Belief... beliefs) Merges a set of new Beliefs into this Peer's belief.proposeBlock
(Block block) Propose a new Block.pruneHistory
(long ts) Prunes History before the given timestampstatic Peer
restorePeer
(AStore store, AKeyPair keyPair) LikerestorePeer(AStore, AKeyPair, ACell)
but uses a null root key.static Peer
restorePeer
(AStore store, AKeyPair keyPair, ACell rootKey) 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 PeerupdateBelief
(Belief newBelief) Update this Peer with Consensus State for an updated BeliefUpdates the state of the Peer based on latest consensus BeliefupdateTimestamp
(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 Pairgenesis
- 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
LikerestorePeer(AStore, AKeyPair, ACell)
but uses a null root key.- Throws:
IOException
-
restorePeer
Restores a Peer from the Etch database specified in Config- Parameters:
store
- Store to restore fromkeyPair
- Key Pair to use for restored PeerrootKey
- When not null, assumes the root data is a map and peer data is under that key- Returns:
- Peer instance, or null if root hash was not found
- Throws:
IOException
- If store reading failed
-
getPeerData
LikegetPeerData(AStore, ACell)
but uses a null root key.- Throws:
IOException
-
getPeerData
Gets Peer Data from a Store.- Parameters:
store
- Store to retrieve Peer Data fromrootKey
- When not null, assumes the root data is a map and peer data is under that key- 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
-
updateTimestamp
Updates the timestamp to the specified time, going forwards only- Parameters:
newTimestamp
- New Peer timestamp- Returns:
- This peer updated 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
-
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 Peer- Returns:
- Consensus state for this chain (genesis 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
- if
-
pruneHistory
Prunes History before the given timestamp- Parameters:
ts
- Timestamp from which to to keep History- Returns:
- Updated Peer with pruned History
-
updateBelief
Update this Peer with Consensus State for an updated Belief- Parameters:
newBelief
- Belief to apply- Returns:
- Updated Peer
-
updateState
Updates the state of the Peer based on latest consensus Belief- Returns:
-
getResult
Gets the Result of a specific transaction- Parameters:
blockIndex
- Index of Block in OrdertxIndex
- Index of transaction in block- Returns:
- Result from transaction, or null if the transaction does not exist or is no longer stored
-
getBlockResult
Gets the BlockResult of a specific block index- Parameters:
i
- Index of Block- Returns:
- BlockResult, or null if the BlockResult is not stired
-
proposeBlock
Propose a new Block. Adds the Block to the current proposed Order for this Peer. Also increments Peer timestamp if necessary for new Block- 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
-
getNetworkID
Get the Network ID for this PEer- Returns:
- Network ID
-
getPosition
public long getPosition()Gets the state position of this Peer, which is equal to the number of state transitions executed.- Returns:
- Position
-
getGenesisState
Gets the genesis State of this Peer- Returns:
- Genesis State
-