Skip to content

Commit

Permalink
use local testnet for integration tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ketola committed Aug 4, 2023
1 parent ac97ed0 commit eb1e023
Show file tree
Hide file tree
Showing 23 changed files with 169 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ jobs:
java-version: 11
distribution: 'temurin'
cache: 'maven'
- name: Start containers
run: export IMAGE_TAG=release-1.1_639d5ab4 && docker-compose -f "docker/validator-testnet/docker-compose.yaml" up -d
- name: Build with Maven
run: mvn -B package --file pom.xml
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ JLibra simplifies integration to Diem but does not hide any features of the Diem
* Clone this repo
* Build project with `mvn install` (this is important because the project contains classes that will be created during the Maven build and simply checking the project out is not enough)

## Start a local Diem instance
A running local instance is required for the examples and the integration tests

* `cd docker/validator-testnet`
* define the Diem version to use `export IMAGE_TAG=release-1.1_639d5ab4`
* `docker-compose up`

## Try the examples

Start sample Main classes in `dev.jlibra.example` package for examples (for a complete example with creating accounts to moving coins between them check the [how to](https://github.com/ketola/jlibra/blob/master/docs/HOWTO.md))
Expand Down
87 changes: 87 additions & 0 deletions docker/validator-testnet/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# This compose file defines a single validator node test network,
# along with a faucet that connects to it over json-rpc.
# In addition, it offers the following facilities:
# * JSON-RPC endpoint at http://127.0.0.1:8080.
# * chain_id of 4 / TESTING
# * The faucet can be accessed at 127.0.0.1:8000

# Additional information:
# * If you use this compose for different Diem Networks, you will need remove the db volume first.
# * If you would like to use the current Diem version within this repository, execute the
# `build.sh` in `docker/validator` and change the image tag below to diem_e2e:latest
# * Validator images can be found at https://hub.docker.com/r/devnet/validator/tags
# * Faucet images can be found at https://hub.docker.com/r/devnet/faucet/tags

# Monitoring:
# If you want to install the monitoring components for your validator-testnet
# you can symlink the ../monitoring folder into this directory.
# Note that you will need to rename the monitoring docker-compose.yaml file to avoid duplication.
# e.g. rename it to docker-compose.mon.yaml
# You will also need to configure the network in the monitoring compose file,
# so that the container can join the same network and talk to each other.
# To start both validator and monitoring, run `docker-compose -f docker-compose.yaml -f docker-compose.mon.yaml up -d`

version: "3.8"
services:
validator:
# Note this image currently does not support this, will update to the appropriate minimum
# version shortly
image: "diem/validator:${IMAGE_TAG:-devnet}"
networks:
shared:
volumes:
- type: volume
source: diem-shared
target: /opt/diem/var
- type: bind
source: ./validator_node_template.yaml
target: /opt/diem/var/validator_node_template.yaml
command: ["/opt/diem/bin/diem-node", "--test", "--config", "/opt/diem/var"]
ports:
- "8080:8080"
expose:
- 9101

faucet:
image: "diem/faucet:${IMAGE_TAG:-devnet}"
depends_on:
- validator
networks:
shared:
volumes:
- type: volume
source: diem-shared
target: /opt/diem/var
command: >
/bin/bash -c "
for i in {1..10}
do
if [[ -s /opt/diem/var/mint.key ]]
then
/opt/diem/bin/diem-faucet \
--address 0.0.0.0 \
--port 8000 \
--chain-id TESTING \
--mint-key-file-path /opt/diem/var/mint.key \
--server-url http://validator:8080/v1
exit $$?
else
echo 'Validator has not populated mint.key yet. Is it running?'
sleep 1
fi
done
exit 1
"
ports:
- "8000:8000"

networks:
shared:
name: "diem-docker-compose-shared"
ipam:
config:
- subnet: 172.16.1.0/24

volumes:
diem-shared:
name: diem-shared
32 changes: 32 additions & 0 deletions docker/validator-testnet/validator_node_template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
consensus:
# The following slow consensus down when there are no active transactions to process
round_initial_timeout_ms: 20000
mempool_poll_count: 333

# The rest of this config is copy paste of config/src/config/test_data/validator.yaml
validator_network:
listen_address: "/ip4/0.0.0.0/tcp/6180"
identity:
type: "from_storage"
key_name: "validator_network"
peer_id_name: "owner_account"
backend:
type: "vault"
server: "https://127.0.0.1:8200"
ca_certificate: "/full/path/to/certificate"
token:
from_disk: "/full/path/to/token"

full_node_networks:
- listen_address: "/ip4/0.0.0.0/tcp/7180"
identity:
type: "from_storage"
key_name: "fullnode_network"
peer_id_name: "owner_account"
backend:
type: "vault"
server: "https://127.0.0.1:8200"
token:
from_disk: "/full/path/to/token"
network_id:
private: "vfn"
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,4 @@ public interface Account {
@JsonProperty("is_frozen")
Boolean isFrozen();

@JsonProperty("version")
Long version();

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void setUp() {
public void testGetAccount() throws Exception {
HttpResponse httpResponse = mock(HttpResponse.class);
when(httpResponse.body()).thenReturn(
"{\"diem_chain_id\":2,\"diem_ledger_version\":4867582,\"diem_ledger_timestampusec\":1604134778415882,\"jsonrpc\":\"2.0\",\"id\":\"5e0a1c17-d3ca-4813-817e-92f0680ccfdc\",\"result\":{\"address\":\"b3f7e8e38f8c8393f281a2f0792a2849\",\"authentication_key\":\"c0c19d6b1d48371ea28f0cdc5f74bba7b3f7e8e38f8c8393f281a2f0792a2849\",\"balances\":[{\"amount\":500000000,\"currency\":\"Coin1\"}],\"delegated_key_rotation_capability\":false,\"delegated_withdrawal_capability\":false,\"is_frozen\":false,\"received_events_key\":\"0200000000000000b3f7e8e38f8c8393f281a2f0792a2849\",\"role\":{\"base_url\":\"\",\"base_url_rotation_events_key\":\"0100000000000000b3f7e8e38f8c8393f281a2f0792a2849\",\"compliance_key\":\"\",\"compliance_key_rotation_events_key\":\"0000000000000000b3f7e8e38f8c8393f281a2f0792a2849\",\"expiration_time\":18446744073709551615,\"human_name\":\"No. 307\",\"num_children\":0,\"type\":\"parent_vasp\"},\"sent_events_key\":\"0300000000000000b3f7e8e38f8c8393f281a2f0792a2849\",\"sequence_number\":0,\"version\":1}}");
"{\"diem_chain_id\":2,\"diem_ledger_version\":4867582,\"diem_ledger_timestampusec\":1604134778415882,\"jsonrpc\":\"2.0\",\"id\":\"5e0a1c17-d3ca-4813-817e-92f0680ccfdc\",\"result\":{\"address\":\"b3f7e8e38f8c8393f281a2f0792a2849\",\"authentication_key\":\"c0c19d6b1d48371ea28f0cdc5f74bba7b3f7e8e38f8c8393f281a2f0792a2849\",\"balances\":[{\"amount\":500000000,\"currency\":\"Coin1\"}],\"delegated_key_rotation_capability\":false,\"delegated_withdrawal_capability\":false,\"is_frozen\":false,\"received_events_key\":\"0200000000000000b3f7e8e38f8c8393f281a2f0792a2849\",\"role\":{\"base_url\":\"\",\"base_url_rotation_events_key\":\"0100000000000000b3f7e8e38f8c8393f281a2f0792a2849\",\"compliance_key\":\"\",\"compliance_key_rotation_events_key\":\"0000000000000000b3f7e8e38f8c8393f281a2f0792a2849\",\"expiration_time\":18446744073709551615,\"human_name\":\"No. 307\",\"num_children\":0,\"type\":\"parent_vasp\"},\"sent_events_key\":\"0300000000000000b3f7e8e38f8c8393f281a2f0792a2849\",\"sequence_number\":0}}");
when(httpClient.sendAsync(Mockito.any(), Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(httpResponse));

Expand Down
3 changes: 1 addition & 2 deletions jlibra-core/src/test/resources/batch_response_ok.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@
"type": "parent_vasp"
},
"sent_events_key": "0300000000000000b3f7e8e38f8c8393f281a2f0792a2849",
"sequence_number": 0,
"version": 1
"sequence_number": 0
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());

DiemAsyncClient client = DiemAsyncClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();

// Note: minting and account creations is not done asynchronously, we just need
Expand Down Expand Up @@ -83,9 +83,11 @@ public static void main(String[] args) throws Exception {

private static void createAccount(AuthenticationKey authKey) {
DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();
Faucet faucet = Faucet.builder()
.withUrl("http://localhost:8000")
.build();
Faucet faucet = Faucet.builder().build();
faucet.mint(authKey, 100L * 1_000_000L, CURRENCY);
Wait.until(Conditions.accountExists(AccountAddress.fromAuthenticationKey(authKey), client));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void main(String[] args) throws Exception {
String address = "79153273a34e0aadf26c963367973760";

DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();

// 1. Create a new batch request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();
Faucet faucet = Faucet.builder()
.withUrl("http://localhost:8000")
.build();
Faucet faucet = Faucet.builder().build();

KeyPair parentVaspKeyPair = generateKeyPair();

Expand Down Expand Up @@ -100,7 +102,7 @@ public static void main(String[] args) throws Exception {
.addArguments(childAccountArgument, authKeyPrefixArgument, createAllCurrenciesArgument,
initialBalanceArgument)
.build())
.chainId(ChainId.TESTNET)
.chainId(ChainId.TESTING)
.build();

SignedTransaction signedTransaction = ImmutableSignedTransaction.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();
Faucet faucet = Faucet.builder()
.withUrl("http://localhost:8000")
.build();

KeyPairGenerator kpGen = KeyPairGenerator.getInstance("Ed25519", "BC");
Expand Down Expand Up @@ -141,7 +142,7 @@ private static void transferFromVaspToVasp(DiemClient client, KeyPair sourceKeyP
.typeArguments(asList(Struct.typeTagForCurrency(CURRENCY)))
.addArguments(addressArgument, amountArgument, metadataArgument, signatureArgument)
.build())
.chainId(ChainId.TESTNET)
.chainId(ChainId.TESTING)
.build();

Signature signature = Signature.signTransaction(transaction, sourceKeyPair.getPrivate());
Expand Down Expand Up @@ -184,7 +185,7 @@ private static void rotateDualAttestationInfoForVaspAccount(DiemClient client, K
.code(Move.rotateDualAttestationInfo())
.addArguments(newUrlArgument, compliancePublicKey)
.build())
.chainId(ChainId.TESTNET)
.chainId(ChainId.TESTING)
.build();

Signature signature = Signature.signTransaction(transaction, parentVaspKeyPair.getPrivate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void main(String[] args) throws Exception {
String address = "79153273a34e0aadf26c963367973760";

DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();

Account accountView = client.getAccount(AccountAddress.fromHexString(address));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void main(String[] args) {
int sequenceNumber = 0;

DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();

Transaction t = client.getAccountTransaction(AccountAddress.fromHexString(address), sequenceNumber, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void main(String[] args) {
String address = "9ed85589fb5617a18521636181a12b88";

DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();

List<Transaction> transactions = client.getAccountTransactions(AccountAddress.fromHexString(address), 0, 5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void main(String[] args) {
String eventKey = "00000000000000000000000000000000000000000a550c18";

DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();

List<Event> events = client.getEvents(eventKey, 0, 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class GetStateProofExample {

public static void main(String[] args) {
DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();

StateProof stateProof = client.getStateProof(324);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void main(String[] args) {
boolean fetchEvent = true;

DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();

List<Transaction> transactions = client.getTransactions(start, limit, fetchEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ public class KeyRotationExample {
public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();

Faucet faucet = Faucet.builder().build();
Faucet faucet = Faucet.builder()
.withUrl("http://localhost:8000")
.build();

/*
* Create a key pair, calculate the libra address and add some coins to the
Expand Down Expand Up @@ -157,7 +159,7 @@ private static void rotateAuthenticationKey(BCEdDSAPrivateKey privateKey,
.code(Move.rotateAuthenticationKey())
.addArguments(newAuthenticationKeyArgument)
.build())
.chainId(ChainId.TESTNET)
.chainId(ChainId.TESTING)
.build();

SignedTransaction signedTransaction = ImmutableSignedTransaction.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ public static void main(String[] args) {
AuthenticationKey authenticationKey = AuthenticationKey
.fromHexString("71967329878ac3bb3b8e30a0aa389761133b573e1450c9e05c31a9041a6fcc67");

Faucet faucet = Faucet.builder().build();
Faucet faucet = Faucet.builder()
.withUrl("http://localhost:8000")
.build();
faucet.mint(authenticationKey, 100L * 1_000_000L, CURRENCY);

DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();

Wait.until(accountHasPositiveBalance(AccountAddress.fromAuthenticationKey(authenticationKey), client));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void main(String[] args) {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();

PrivateKey privateKey = KeyUtils.privateKeyFromByteSequence(ByteArray.from(
Expand Down Expand Up @@ -99,7 +99,7 @@ public static void main(String[] args) {
.addArguments(addressArgument, amountArgument, metadataArgument,
signatureArgument)
.build())
.chainId(ChainId.TESTNET)
.chainId(ChainId.TESTING)
.build();

SignedTransaction signedTransaction = ImmutableSignedTransaction.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static void main(String[] args) throws Exception {
.typeArguments(asList(Struct.typeTagForCurrency(CURRENCY)))
.addArguments(addressArgument, amountArgument, metadataArgument, signatureArgument)
.build())
.chainId(ChainId.TESTNET)
.chainId(ChainId.TESTING)
.build();

Signature signature = Signature.newMultisignature();
Expand All @@ -105,7 +105,7 @@ public static void main(String[] args) throws Exception {
.build();

DiemClient client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();

client.submit(signedTransaction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class BatchRequestIntegrationTest {
public void setUp() {
Security.addProvider(new BouncyCastleProvider());
client = DiemClient.builder()
.withUrl("https://testnet.diem.com/v1")
.withUrl("http://localhost:8080")
.build();
}

Expand Down
Loading

0 comments on commit eb1e023

Please sign in to comment.