-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ConvexDirect API class for testing / local usage
- Loading branch information
Showing
7 changed files
with
224 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package convex.api; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import convex.core.Result; | ||
import convex.core.ResultContext; | ||
import convex.core.cpos.Block; | ||
import convex.core.crypto.AKeyPair; | ||
import convex.core.cvm.Address; | ||
import convex.core.cvm.Peer; | ||
import convex.core.cvm.PeerStatus; | ||
import convex.core.cvm.State; | ||
import convex.core.cvm.transactions.ATransaction; | ||
import convex.core.data.ACell; | ||
import convex.core.data.AccountKey; | ||
import convex.core.data.Blob; | ||
import convex.core.data.Hash; | ||
import convex.core.data.SignedData; | ||
import convex.core.data.prim.CVMLong; | ||
import convex.core.message.Message; | ||
import convex.core.store.AStore; | ||
import convex.core.util.Utils; | ||
|
||
/** | ||
* Convex API instance that directly interacts with a Peer instance | ||
*/ | ||
public class ConvexDirect extends Convex { | ||
|
||
protected Peer peer; | ||
private boolean isConnected=true; | ||
|
||
protected ConvexDirect(Address address, AKeyPair keyPair, Peer initial) { | ||
super(address, keyPair); | ||
this.peer=initial; | ||
} | ||
|
||
public static ConvexDirect create(AKeyPair peerKey,State state) { | ||
AccountKey key=peerKey.getAccountKey(); | ||
PeerStatus ps= state.getPeer(key); | ||
if (ps==null) throw new IllegalStateException("Peer does not exist in desired state"); | ||
Address cont=ps.getController(); | ||
return new ConvexDirect(cont,peerKey,Peer.create(peerKey, state)); | ||
} | ||
|
||
@Override | ||
public boolean isConnected() { | ||
return isConnected; | ||
} | ||
|
||
@Override | ||
public synchronized CompletableFuture<Result> transact(SignedData<ATransaction> signedTransaction) { | ||
try { | ||
CVMLong id=CVMLong.create(getNextID()); | ||
Peer p=peer; | ||
Result failure=p.checkTransaction(signedTransaction); | ||
if (failure!=null) { | ||
return CompletableFuture.completedFuture(failure.withID(id)); | ||
} | ||
|
||
long ts=Utils.getCurrentTimestamp(); | ||
Block block=Block.of(ts, signedTransaction); | ||
|
||
// Peer updates | ||
p=p.updateTimestamp(ts); | ||
p=p.proposeBlock(block); | ||
p=p.mergeBeliefs(); | ||
p=p.updateState(); | ||
long blockNum=p.getPeerOrder().count()-1; | ||
peer=p; | ||
|
||
Result result= p.getResult(blockNum, 0); | ||
return CompletableFuture.completedFuture(result.withID(id)); | ||
} catch (Exception e) { | ||
return CompletableFuture.completedFuture(Result.fromException(e)); | ||
} | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Result> messageRaw(Blob message) { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Result> message(Message message) { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public <T extends ACell> CompletableFuture<T> acquire(Hash hash, AStore store) { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Result> requestStatus() { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Result> requestChallenge(SignedData<ACell> data) { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Result> query(ACell query, Address address) { | ||
CVMLong id=CVMLong.create(getNextID()); | ||
ResultContext rc= peer.executeQuery(query, address); | ||
return CompletableFuture.completedFuture(Result.fromContext(id,rc)); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
peer=null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Direct client with peer state: "+peer.getConsensusState().getHash(); | ||
} | ||
|
||
@Override | ||
public InetSocketAddress getHostAddress() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void reconnect() throws IOException, TimeoutException, InterruptedException { | ||
isConnected=true; | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
convex-peer/src/test/java/convex/api/ConvexDirectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package convex.api; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import convex.core.Result; | ||
import convex.core.crypto.AKeyPair; | ||
import convex.core.cvm.Address; | ||
import convex.core.cvm.State; | ||
import convex.core.data.prim.CVMLong; | ||
import convex.core.init.Init; | ||
import convex.core.init.InitTest; | ||
import convex.core.lang.ACVMTest; | ||
|
||
/** | ||
* Tests for Convex Direct client | ||
*/ | ||
public class ConvexDirectTest extends ACVMTest { | ||
static final AKeyPair peerKey=AKeyPair.createSeeded(5675675); | ||
|
||
@Test public void testSetup() throws InterruptedException { | ||
State state=Init.createTestState(List.of(peerKey.getAccountKey())); | ||
ConvexDirect convex=ConvexDirect.create(peerKey,state); | ||
Address addr=convex.getAddress(); | ||
|
||
assertTrue(convex.isConnected()); | ||
assertEquals(InitTest.FIRST_PEER_ADDRESS,addr); | ||
|
||
assertEquals(addr,convex.query("*address*").join().getValue()); | ||
|
||
Result r=convex.transactSync("(+ 1 2)"); | ||
assertFalse(r.isError(),()->"Expected ed error: "+r); | ||
assertEquals(CVMLong.create(3),r.getValue()); | ||
} | ||
} |