Package convex.api
Class Convex
java.lang.Object
convex.api.Convex
- Direct Known Subclasses:
ConvexLocal
,ConvexRemote
Class representing a client API to the Convex network.
An instance of the type Convex represents a stateful client connection to the
Convex network that can issue transactions both synchronously and
asynchronously. This can be used by both peers and JVM-based clients.
"I'm doing a (free) operating system (just a hobby, won't be big and
professional like gnu)" - Linus Torvalds
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Address
Current Address for this Clientprotected HashMap
<Long, CompletableFuture<Message>> Map of results awaiting completion.protected AKeyPair
Key pair for this ClientResult Consumer for messages received back from a client connectionprotected Long
Sequence number for this client, or null if not yet known.protected long
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T extends ACell>
CompletableFuture<T> Attempts to asynchronously acquire a complete persistent data structure for the given hash from the remote peer.abstract <T extends ACell>
CompletableFuture<T> Attempts to acquire a complete persistent data structure for the given hash from the connected peer.abstract CompletableFuture
<State> Gets the consensus state from the remote Peerprotected CompletableFuture
<Result> awaitResult
(long id, long timeout) Method to start waiting for a complete result.abstract void
close()
Disconnects the client from the network, releasing any connection resources.static ConvexLocal
Connect to a local Server, with no address and keypair setstatic ConvexLocal
Connect to a local Server, using given address and keypairstatic ConvexRemote
connect
(InetSocketAddress hostAddress) Creates an anonymous connection to a Peer, suitable for queriesstatic ConvexRemote
connect
(InetSocketAddress peerAddress, Address address, AKeyPair keyPair) Create a Convex client by connecting to the specified Peer using the given key pairstatic ConvexRemote
connect
(InetSocketAddress peerAddress, Address address, AKeyPair keyPair, AStore store) Create a Convex client by connecting to the specified Peer using the given key pair and using a given storecreateAccount
(AccountKey publicKey) Creates a new account with the given public keycreateAccountSync
(AccountKey publicKey) Creates a new account with the given public keyvoid
finalize()
Returns the current AccountKey for the client using the API.Returns the current AccountKey for the specified address.Returns the current Address for the client using the API.Query the balance for the current accountgetBalance
(Address address) abstract InetSocketAddress
Gets the remote address for this Convex client instanceGet the keypair for this Convex connection.abstract Server
Gets the local Server instance, or null if not a local connectionlong
Gets the current sequence number for this Client, which is the sequence number of the last transaction observed for the current client's Account.long
getSequence
(Address addr) Gets the current sequence number for an account, which is the sequence number of the last transaction observed for the Account.protected boolean
Determines if this Client is configured to automatically generate sequence numbersabstract boolean
Checks if this Convex client instance has an open connection.long
lookupSequence
(Address origin) Look up the sequence number for an accountprotected void
maybeUpdateSequence
(SignedData<ATransaction> signed) Called after a transaction is submitted to update sequence (if possible)prepareTransaction
(ATransaction transaction) Prepares a transaction for network submission - Sets origin account if needed - Sets sequence number (if autosequencing is enabled) - Signs transaction with current key pairSubmits a query to the Convex network, returning a Future once the query has been successfully queued.abstract CompletableFuture
<Result> Submits a query to the Convex network, returning a Future once the query has been successfully queued.Submits a query to the Convex network, returning a Future once the query has been successfully queued.Executes a query synchronously and waits for the ResultExecutes a query synchronously and waits for the ResultExecutes a query synchronously and waits for the ResultExecutes a query synchronously and waits for the ResultExecutes a query synchronously and waits for the Resultabstract CompletableFuture
<Result> requestChallenge
(SignedData<ACell> data) Request a challenge.abstract CompletableFuture
<Result> Submits a status request to the Convex network peer, returning a Future once the request has been successfully queued.requestStatusSync
(long timeoutMillis) Request status using a sync operation.Attempts to resolve a CNS namevoid
setAddress
(Address address) Sets the Address for this connection.void
setAddress
(Address address, AKeyPair kp) Sets the Address and Keypair for this connection.protected void
setAutoSequence
(boolean autoSequence) Configures auto-generation of sequence numbersvoid
setHandler
(Consumer<Message> handler) Sets a handler for messages that are received but not otherwise processed (transaction/query results will be relayed instead to the appropriate handler )void
setKeyPair
(AKeyPair kp) void
setNextSequence
(long nextSequence) void
setTimeout
(long timeout) Sets the default timeout for this Convex client instance.<T extends ACell>
SignedData<T> signData
(T value) Signs a value on behalf of this client, using the currently assigned keypair.abstract String
toString()
Executes a transaction, compiling the given source code as an Invoke.abstract CompletableFuture
<Result> transact
(SignedData<ATransaction> signed) Submits a signed transaction to the Convex network, returning a Future once the transaction has been successfully queued.final CompletableFuture
<Result> transact
(ATransaction transaction) Submits a transaction to the Convex network, returning a future once the transaction has been successfully queued.Executes a transaction, compiling the given source code as an Invoke.transactSync
(SignedData<ATransaction> transaction) Submits a transaction synchronously to the Convex network, returning a ResulttransactSync
(SignedData<ATransaction> transaction, long timeout) Submits a signed transaction synchronously to the Convex network, returning a ResulttransactSync
(ATransaction transaction) Submits a transaction synchronously to the Convex network, returning a ResulttransactSync
(ATransaction transaction, long timeout) Submits a signed transaction synchronously to the Convex network, returning a ResulttransactSync
(String code) Executes a transaction, compiling the given source code as an Invoke.Submits a transfer transaction to the Convex network, returning a future once the transaction has been successfully queued.transferSync
(Address target, long amount) Submits a transfer transaction to the Convex network peer, and waits for confirmation of the result
-
Field Details
-
timeout
protected long timeout -
keyPair
Key pair for this Client -
address
Current Address for this Client -
sequence
Sequence number for this client, or null if not yet known. Used to number new transactions if not otherwise specified. -
awaiting
Map of results awaiting completion. -
messageHandler
-
-
Constructor Details
-
Convex
-
-
Method Details
-
connect
public static ConvexRemote connect(InetSocketAddress hostAddress) throws IOException, TimeoutException Creates an anonymous connection to a Peer, suitable for queries- Parameters:
hostAddress
- Address of Peer- Returns:
- New Convex client instance
- Throws:
IOException
- If IO Error occursTimeoutException
- If connection attempt times out
-
connect
public static ConvexRemote connect(InetSocketAddress peerAddress, Address address, AKeyPair keyPair) throws IOException, TimeoutException Create a Convex client by connecting to the specified Peer using the given key pair- Parameters:
peerAddress
- Address of Peeraddress
- Address of Account to use for ClientkeyPair
- Key pair to use for client transactions- Returns:
- New Convex client instance
- Throws:
IOException
- If connection fails due to IO errorTimeoutException
- If connection attempt times out
-
connect
public static ConvexRemote connect(InetSocketAddress peerAddress, Address address, AKeyPair keyPair, AStore store) throws IOException, TimeoutException Create a Convex client by connecting to the specified Peer using the given key pair and using a given store- Parameters:
peerAddress
- Address of Peeraddress
- Address of Account to use for ClientkeyPair
- Key pair to use for client transactionsstore
- Store to use for this connection- Returns:
- New Convex client instance
- Throws:
IOException
- If connection fails due to IO errorTimeoutException
- If connection attempt times out
-
setAddress
Sets the Address for this connection. This will be used for subsequent transactions and queries. User should also set a new keypair if a different keypair is required for the new Address.- Parameters:
address
- Address to use
-
setAddress
-
setKeyPair
-
setNextSequence
public void setNextSequence(long nextSequence) -
setHandler
-
getSequence
Gets the current sequence number for this Client, which is the sequence number of the last transaction observed for the current client's Account. Will attempt to acquire the sequence number from the network if not known. The next valid sequence number will be one higher than the result.- Returns:
- Sequence number as a Long value (zero or positive)
- Throws:
TimeoutException
IOException
-
getSequence
Gets the current sequence number for an account, which is the sequence number of the last transaction observed for the Account. Will attempt to acquire the sequence number from the network if not known.- Parameters:
addr
- Address for which to query the sequence number- Returns:
- Sequence number as a Long value (zero or positive)
- Throws:
IOException
- If an IO error occursTimeoutException
- If the request times out
-
lookupSequence
Look up the sequence number for an account- Parameters:
origin
-- Returns:
- Throws:
TimeoutException
IOException
-
maybeUpdateSequence
Called after a transaction is submitted to update sequence (if possible)- Parameters:
value
-
-
signData
Signs a value on behalf of this client, using the currently assigned keypair.- Type Parameters:
T
- Type of value to sign- Parameters:
value
- Value to sign- Returns:
- SignedData instance
-
createAccountSync
Creates a new account with the given public key- Parameters:
publicKey
- Public key to set for the new account- Returns:
- Address of account created
- Throws:
TimeoutException
- If attempt times outIOException
- If IO error occurs
-
createAccount
public CompletableFuture<Address> createAccount(AccountKey publicKey) throws TimeoutException, IOException Creates a new account with the given public key- Parameters:
publicKey
- Public key to set for the new account- Returns:
- Address of account created
- Throws:
TimeoutException
- If attempt times outIOException
- If IO error occurs
-
isConnected
public abstract boolean isConnected()Checks if this Convex client instance has an open connection.- Returns:
- true if connected, false otherwise
-
transact
public final CompletableFuture<Result> transact(ATransaction transaction) throws IOException, TimeoutException Submits a transaction to the Convex network, returning a future once the transaction has been successfully queued. Signs the transaction with the currently set key pair. Should be thread safe as long as multiple clients do not attempt to submit transactions for the same account concurrently. May block briefly if the send buffer is full.- Parameters:
transaction
- Transaction to execute- Returns:
- A Future for the result of the transaction
- Throws:
IOException
- If an IO Exception occurs (most likely the connection is broken)TimeoutException
-
prepareTransaction
public SignedData<ATransaction> prepareTransaction(ATransaction transaction) throws TimeoutException, IOException Prepares a transaction for network submission - Sets origin account if needed - Sets sequence number (if autosequencing is enabled) - Signs transaction with current key pair- Parameters:
transaction
- Transaction to prepare- Returns:
- Signed transaction ready to submit
- Throws:
IOException
- If an IO Exception occurs (most likely the connection is broken)TimeoutException
-
transact
Executes a transaction, compiling the given source code as an Invoke.- Parameters:
code
- Code to execute- Returns:
- A Future for the result of the transaction
- Throws:
IOException
- If the connection is broken, or the send buffer is fullTimeoutException
-
transact
Executes a transaction, compiling the given source code as an Invoke.- Parameters:
code
- Code to execute- Returns:
- A Future for the result of the transaction
- Throws:
IOException
- If the connection is broken, or the send buffer is fullTimeoutException
-
transactSync
Executes a transaction, compiling the given source code as an Invoke.- Parameters:
code
- Code to execute- Returns:
- A Future for the result of the transaction
- Throws:
IOException
- If the connection is broken, or the send buffer is fullTimeoutException
- If the transaction times out
-
transact
public abstract CompletableFuture<Result> transact(SignedData<ATransaction> signed) throws IOException Submits a signed transaction to the Convex network, returning a Future once the transaction has been successfully queued. Updates cached sequence number on best effort basis.- Parameters:
signed
- Signed transaction to execute- Returns:
- A Future for the result of the transaction
- Throws:
IOException
- If the connection is broken or send buffer is full
-
transfer
public CompletableFuture<Result> transfer(Address target, long amount) throws IOException, TimeoutException Submits a transfer transaction to the Convex network, returning a future once the transaction has been successfully queued.- Parameters:
target
- Destination address for transferamount
- Amount of Convex Coins to transfer- Returns:
- A Future for the result of the transaction
- Throws:
IOException
- If the connection is broken, or the send buffer is fullTimeoutException
-
transferSync
Submits a transfer transaction to the Convex network peer, and waits for confirmation of the result- Parameters:
target
- Destination address for transferamount
- Amount of Convex Coins to transfer- Returns:
- Result of the transaction
- Throws:
IOException
- If the connection is broken, or the send buffer is fullTimeoutException
- If the transaction times out
-
transactSync
public Result transactSync(SignedData<ATransaction> transaction) throws TimeoutException, IOException Submits a transaction synchronously to the Convex network, returning a Result- Parameters:
transaction
- Transaction to execute- Returns:
- The result of the transaction
- Throws:
IOException
- If the connection is brokenTimeoutException
- If the attempt to transact with the network is not confirmed within a reasonable time
-
transactSync
Submits a transaction synchronously to the Convex network, returning a Result- Parameters:
transaction
- Transaction to execute- Returns:
- The result of the transaction
- Throws:
IOException
- If the connection is brokenTimeoutException
- If the attempt to transact with the network is not confirmed within a reasonable time
-
transactSync
public Result transactSync(ATransaction transaction, long timeout) throws TimeoutException, IOException Submits a signed transaction synchronously to the Convex network, returning a Result- Parameters:
transaction
- Transaction to executetimeout
- Number of milliseconds for timeout- Returns:
- The result of the transaction
- Throws:
IOException
- If the connection is brokenTimeoutException
- If the attempt to transact with the network is not confirmed by the specified timeout
-
transactSync
public Result transactSync(SignedData<ATransaction> transaction, long timeout) throws TimeoutException, IOException Submits a signed transaction synchronously to the Convex network, returning a Result- Parameters:
transaction
- Transaction to executetimeout
- Number of milliseconds for timeout- Returns:
- The Result of the transaction, if received
- Throws:
IOException
- If the connection is brokenTimeoutException
- If the attempt to transact with the network is not confirmed by the specified timeout
-
query
Submits a query to the Convex network, returning a Future once the query has been successfully queued.- Parameters:
query
- Query to execute, as a Form or Op- Returns:
- A Future for the result of the query
- Throws:
IOException
- If the connection is broken, or the send buffer is full
-
query
Submits a query to the Convex network, returning a Future once the query has been successfully queued.- Parameters:
query
- Query to execute, as String containing one or more forms- Returns:
- A Future for the result of the query
- Throws:
IOException
- If the connection is broken, or the send buffer is full
-
resolve
Attempts to resolve a CNS name- Parameters:
cnsName
- CNS name to resolve- Returns:
- A Future for the resolved CNS value
- Throws:
IOException
- In case of IO failure
-
acquire
Attempts to asynchronously acquire a complete persistent data structure for the given hash from the remote peer. Uses the current store configured for the calling thread.- Parameters:
hash
- Hash of value to acquire.- Returns:
- Future for the cell being acquired
-
acquire
Attempts to acquire a complete persistent data structure for the given hash from the connected peer. Uses the store provided as a destination.- Parameters:
hash
- Hash of value to acquire.store
- Store to acquire the persistent data to.- Returns:
- Future for the Cell being acquired. May fail exceptionally or timeout if the given data cannot be acquired (most likely missing from the peer's store)
-
requestStatusSync
Request status using a sync operation. This request will automatically get any missing data with the status request- Parameters:
timeoutMillis
- Milliseconds to wait for request timeout- Returns:
- Status Vector from target Peer
- Throws:
IOException
- If an IO Error occursTimeoutException
- If operation times out
-
requestStatus
Submits a status request to the Convex network peer, returning a Future once the request has been successfully queued.- Returns:
- A Future for the result of the requestStatus
-
awaitResult
Method to start waiting for a complete result. Should be called with lock on `awaiting` map to prevent risk of missing results before it is called.- Parameters:
id
- ID of result message to await- Returns:
-
requestChallenge
public abstract CompletableFuture<Result> requestChallenge(SignedData<ACell> data) throws IOException Request a challenge. This is request is made by any peer that needs to find out if another peer can be trusted.- Parameters:
data
- Signed data to send to the peer for the challenge.- Returns:
- A Future for the result of the requestChallenge
- Throws:
IOException
- if the connection fails.
-
query
Submits a query to the Convex network, returning a Future once the query has been successfully queued.- Parameters:
query
- Query to execute, as a Form or Opaddress
- Address to use for the query- Returns:
- A Future for the result of the query
- Throws:
IOException
- In case of IO problem (connection is broken, send buffer full)
-
querySync
Executes a query synchronously and waits for the Result- Parameters:
query
- Query to execute. Map be a form or Op- Returns:
- Result of synchronous query
- Throws:
TimeoutException
- If the synchronous request timed outIOException
- In case of network error
-
querySync
Executes a query synchronously and waits for the Result- Parameters:
query
- Query to execute, as a String that contains one or more readable forms. Multiple forms will be wrapped in a `do` block- Returns:
- Result of synchronous query
- Throws:
TimeoutException
- If the synchronous request timed outIOException
- In case of network error
-
querySync
Executes a query synchronously and waits for the Result- Parameters:
query
- Query to execute, as a Form or OptimeoutMillis
- Timeout to wait for query result. Will throw TimeoutException if not received in this time- Returns:
- Result of query
- Throws:
TimeoutException
- If the synchronous request timed outIOException
- In case of network error
-
querySync
Executes a query synchronously and waits for the Result- Parameters:
query
- Query to execute, as a Form or Opaddress
- Address to use for the query- Returns:
- Result of query
- Throws:
TimeoutException
- If the synchronous request timed outIOException
- In case of network error
-
querySync
public Result querySync(ACell query, Address address, long timeoutMillis) throws TimeoutException, IOException Executes a query synchronously and waits for the Result- Parameters:
query
- Query to execute, as a Form or Opaddress
- Address to use for the querytimeoutMillis
- Timeout to wait for query result. Will throw TimeoutException if not received in this time- Returns:
- Result of query
- Throws:
TimeoutException
- If the synchronous request timed outIOException
- In case of network error
-
getAccountKey
Returns the current AccountKey for the client using the API.- Returns:
- AcountKey instance, or null if no keypair is set
-
getAccountKey
Returns the current AccountKey for the specified address. Performs a sync query- Returns:
- AcountKey instance, or null if unavailable
-
getAddress
Returns the current Address for the client using the API.- Returns:
- Address instance
-
close
public abstract void close()Disconnects the client from the network, releasing any connection resources. -
finalize
-
isAutoSequence
protected boolean isAutoSequence()Determines if this Client is configured to automatically generate sequence numbers- Returns:
-
setAutoSequence
protected void setAutoSequence(boolean autoSequence) Configures auto-generation of sequence numbers- Parameters:
autoSequence
- true to enable auto-sequencing, false otherwise
-
getBalance
Query the balance for the current account- Returns:
- Long balance in Convex coins,
- Throws:
IOException
- If balance query fails due to IO
-
getBalance
- Throws:
IOException
-
connect
Connect to a local Server, using given address and keypair- Parameters:
server
- Server to connect toaddress
- Address to usekeyPair
- Keypair to use- Returns:
- New Client Connection
-
connect
Connect to a local Server, with no address and keypair set- Parameters:
server
- Server to connect to- Returns:
- New Client Connection
-
acquireState
Gets the consensus state from the remote Peer- Returns:
- Future for consensus state
- Throws:
TimeoutException
- If initial status request times out
-
setTimeout
public void setTimeout(long timeout) Sets the default timeout for this Convex client instance.- Parameters:
timeout
- timeout in milliseconds
-
toString
-
getKeyPair
Get the keypair for this Convex connection.- Returns:
- Keypair or null if not set
-
getLocalServer
Gets the local Server instance, or null if not a local connection- Returns:
- Server instance (or null)
-
getHostAddress
Gets the remote address for this Convex client instance- Returns:
- Socket address
-