Package convex.api
Class Convex
java.lang.Object
convex.api.Convex
Class representing the client API to the Convex network when connected
directly using the binary protocol. This can be more efficient than using a
REST API.
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 Connection
Current Connection to a Peer, may be null or a closed connection.protected AKeyPair
Key pair for this Clientprotected Long
Sequence number for this client, or null if not yet known -
Method Summary
Modifier and TypeMethodDescriptionAttempts to acquire a complete persistent data structure for the given hash from the remote peer.Attempts to acquire a complete persistent data structure for the given hash from the remote peer.void
close()
Disconnects the client from the network.void
Close without affecting the connectionstatic Convex
Connect to a local Server, using the Peer's address and keypairstatic Convex
connect(InetSocketAddress hostAddress)
Creates an anonymous connection to a Peer, suitable for queriesstatic Convex
connect(InetSocketAddress peerAddress, Address address, AKeyPair keyPair)
Create a Convex client by connecting to the specified Peer using the given key pairstatic Convex
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)
Gets the underlying Connection instance for this Client.Gets the Internet address of the currently connected remotelong
Gets the current sequence number for this Client, which is the sequence number of the last transaction observed for the current client's Account.protected boolean
Determines if this Client is configured to automatically generate sequence numbersboolean
Checks if this Convex client instance has an open connection.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 ResultrequestChallenge(SignedData<ACell> data)
Request a challenge.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 addr, AKeyPair kp)
protected void
setAutoSequence(boolean autoSequence)
Configures auto-generation of sequence numbersvoid
setHandler(Consumer<Message> handler)
void
setKeyPair(AKeyPair kp)
void
setNextSequence(long nextSequence)
<T extends ACell>
SignedData<T>signData(T value)
Signs a value on behalf of this client.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.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 ResultSubmits 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 resultstatic Convex
wrap(Connection c)
Wraps a connection as a Convex client instance
-
Field Details
-
keyPair
Key pair for this Client -
address
Current address for this Client -
connection
Current Connection to a Peer, may be null or a closed connection. -
sequence
Sequence number for this client, or null if not yet known
-
-
Method Details
-
connect
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 Convex connect(InetSocketAddress peerAddress, Address address, AKeyPair keyPair) throws IOException, TimeoutExceptionCreate 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 failsTimeoutException
- If connection attempt times out
-
connect
public static Convex connect(InetSocketAddress peerAddress, Address address, AKeyPair keyPair, AStore store) throws IOException, TimeoutExceptionCreate 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 failsTimeoutException
- If connection attempt times out
-
setAddress
Sets the Address for this connection. This will be used by default for subsequent transactions and queries- Parameters:
address
- Address to use
-
setAddress
-
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.- Returns:
- Sequence number as a Long value, zero or positive
-
signData
Signs a value on behalf of this client.- Type Parameters:
T
- Type of value to sign- Parameters:
value
- Value to sign- Returns:
- SignedData instance
-
getRemoteAddress
Gets the Internet address of the currently connected remote- Returns:
- Remote socket address
-
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, IOExceptionCreates 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 boolean isConnected()Checks if this Convex client instance has an open connection.- Returns:
- true if connected, false otherwise
-
getConnection
Gets the underlying Connection instance for this Client. May be null if not connected.- Returns:
- Connection instance or null
-
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.- Parameters:
transaction
- Transaction to execute- Returns:
- A Future for the result of the transaction
- Throws:
IOException
- If the connection is broken, or the send buffer is full
-
transact
Submits a signed transaction to the Convex network, returning a future once the transaction has been successfully queued.- Parameters:
signed
- Signed transaction to execute- Returns:
- A Future for the result of the transaction
- Throws:
IOException
- If the connection is broken
-
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, IOExceptionSubmits 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, IOExceptionSubmits 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, IOExceptionSubmits 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
-
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
-
acquire
Attempts to acquire a complete persistent data structure for the given hash from the remote peer. Uses the 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 remote 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
-
requestStatusSync
public AVector<ACell> requestStatusSync(long timeoutMillis) throws IOException, InterruptedException, ExecutionException, TimeoutExceptionRequest 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 occursInterruptedException
- If execution is interruptedExecutionException
- If a concurrent execution failure 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
- Throws:
IOException
- If the connection is broken, or the send buffer is full
-
requestChallenge
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:
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, IOExceptionExecutes 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 void close()Disconnects the client from the network. -
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 the Peer's address and keypair- Parameters:
server
- Server to connect to- Returns:
- New Client Connection
- Throws:
TimeoutException
- If connection attempt times outIOException
- If IO error occurs
-
wrap
Wraps a connection as a Convex client instance- Parameters:
c
- Connection to wrap- Returns:
- New Convex client instance using underlying connection
-
acquireState
- Throws:
TimeoutException
-
closeButMaintainConnection
public void closeButMaintainConnection()Close without affecting the connection
-