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 Object 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<Result>> Map of results awaiting completion.Result Consumer for messages received back from a client connectionprotected AKeyPair
Key pair for this Clientprotected 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, 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 AcountKey for the client using the API.Returns the current Address for the client using the API.getBalance
(Address address) long
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.protected void
maybeUpdateSequence
(SignedData<ATransaction> signed) Called after a transaction is submitted to update sequence (if possible)Submits 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.void
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) void
setKeyPair
(AKeyPair kp) void
setNextSequence
(long nextSequence) void
setTimeout
(long timeout) Sets the 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()
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.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. -
internalHandler
Result Consumer for messages received back from a client connection
-
-
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
Sets the Address and Keypair for this connection. This will be used for subsequent transactions and queries.- Parameters:
address
- Address to usekp
- Keypair to use for the given Address
-
setKeyPair
-
setNextSequence
public void setNextSequence(long nextSequence) -
setHandler
-
getSequence
public long 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)
-
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
-
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
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)
-
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 full
-
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
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 full
-
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
-
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
- If the connection is broken, or the send buffer is 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:
timeoutMillis
- Timeout to wait for query result. Will throw TimeoutException if not received in this timequery
- Query to execute, as a Form or Op- 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:
address
- Address to use for the queryquery
- Query to execute, as a Form or Op- 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:
timeoutMillis
- Timeout to wait for query result. Will throw TimeoutException if not received in this timeaddress
- Address to use for the queryquery
- Query to execute, as a Form or Op- Returns:
- Result of query
- Throws:
TimeoutException
- If the synchronous request timed outIOException
- In case of network error
-
getAccountKey
Returns the current AcountKey for the client using the API.- Returns:
- AcountKey instance
-
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
public void 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
- 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
-
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 timeout for this Convex client instance.- Parameters:
timeout
- timeout in milliseconds
-
toString
-