|
| 1 | +package convex.api; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.InetSocketAddress; |
| 5 | +import java.util.concurrent.CompletableFuture; |
| 6 | +import java.util.concurrent.TimeoutException; |
| 7 | + |
| 8 | +import convex.core.Result; |
| 9 | +import convex.core.ResultContext; |
| 10 | +import convex.core.cpos.Block; |
| 11 | +import convex.core.crypto.AKeyPair; |
| 12 | +import convex.core.cvm.Address; |
| 13 | +import convex.core.cvm.Peer; |
| 14 | +import convex.core.cvm.PeerStatus; |
| 15 | +import convex.core.cvm.State; |
| 16 | +import convex.core.cvm.transactions.ATransaction; |
| 17 | +import convex.core.data.ACell; |
| 18 | +import convex.core.data.AccountKey; |
| 19 | +import convex.core.data.Blob; |
| 20 | +import convex.core.data.Hash; |
| 21 | +import convex.core.data.SignedData; |
| 22 | +import convex.core.data.prim.CVMLong; |
| 23 | +import convex.core.message.Message; |
| 24 | +import convex.core.store.AStore; |
| 25 | +import convex.core.util.Utils; |
| 26 | + |
| 27 | +/** |
| 28 | + * Convex API instance that directly interacts with a Peer instance |
| 29 | + */ |
| 30 | +public class ConvexDirect extends Convex { |
| 31 | + |
| 32 | + protected Peer peer; |
| 33 | + private boolean isConnected=true; |
| 34 | + |
| 35 | + protected ConvexDirect(Address address, AKeyPair keyPair, Peer initial) { |
| 36 | + super(address, keyPair); |
| 37 | + this.peer=initial; |
| 38 | + } |
| 39 | + |
| 40 | + public static ConvexDirect create(AKeyPair peerKey,State state) { |
| 41 | + AccountKey key=peerKey.getAccountKey(); |
| 42 | + PeerStatus ps= state.getPeer(key); |
| 43 | + if (ps==null) throw new IllegalStateException("Peer does not exist in desired state"); |
| 44 | + Address cont=ps.getController(); |
| 45 | + return new ConvexDirect(cont,peerKey,Peer.create(peerKey, state)); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean isConnected() { |
| 50 | + return isConnected; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public synchronized CompletableFuture<Result> transact(SignedData<ATransaction> signedTransaction) { |
| 55 | + try { |
| 56 | + CVMLong id=CVMLong.create(getNextID()); |
| 57 | + Peer p=peer; |
| 58 | + Result failure=p.checkTransaction(signedTransaction); |
| 59 | + if (failure!=null) { |
| 60 | + return CompletableFuture.completedFuture(failure.withID(id)); |
| 61 | + } |
| 62 | + |
| 63 | + long ts=Utils.getCurrentTimestamp(); |
| 64 | + Block block=Block.of(ts, signedTransaction); |
| 65 | + |
| 66 | + // Peer updates |
| 67 | + p=p.updateTimestamp(ts); |
| 68 | + p=p.proposeBlock(block); |
| 69 | + p=p.mergeBeliefs(); |
| 70 | + p=p.updateState(); |
| 71 | + long blockNum=p.getPeerOrder().count()-1; |
| 72 | + peer=p; |
| 73 | + |
| 74 | + Result result= p.getResult(blockNum, 0); |
| 75 | + return CompletableFuture.completedFuture(result.withID(id)); |
| 76 | + } catch (Exception e) { |
| 77 | + return CompletableFuture.completedFuture(Result.fromException(e)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public CompletableFuture<Result> messageRaw(Blob message) { |
| 83 | + // TODO Auto-generated method stub |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public CompletableFuture<Result> message(Message message) { |
| 89 | + // TODO Auto-generated method stub |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public <T extends ACell> CompletableFuture<T> acquire(Hash hash, AStore store) { |
| 95 | + // TODO Auto-generated method stub |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public CompletableFuture<Result> requestStatus() { |
| 101 | + // TODO Auto-generated method stub |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public CompletableFuture<Result> requestChallenge(SignedData<ACell> data) { |
| 107 | + // TODO Auto-generated method stub |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public CompletableFuture<Result> query(ACell query, Address address) { |
| 113 | + CVMLong id=CVMLong.create(getNextID()); |
| 114 | + ResultContext rc= peer.executeQuery(query, address); |
| 115 | + return CompletableFuture.completedFuture(Result.fromContext(id,rc)); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void close() { |
| 120 | + peer=null; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public String toString() { |
| 125 | + return "Direct client with peer state: "+peer.getConsensusState().getHash(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public InetSocketAddress getHostAddress() { |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void reconnect() throws IOException, TimeoutException, InterruptedException { |
| 135 | + isConnected=true; |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +} |
0 commit comments