Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
with:
distribution: 'temurin'
java-version: 21
- uses: actions/cache@v3
- uses: actions/cache@v4
with:
path: ~/.m2/repository
key: maven-${{ hashFiles('**/pom.xml') }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/mvn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ jobs:
strategy:
matrix:
os: [ubuntu-24.04, windows-2022, macos-15]
java: [11, 17]
java: [21]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: ${{ matrix.java }}
- uses: actions/cache@v3
- uses: actions/cache@v4
with:
path: ~/.m2/repository
key: ${{ runner.os }}-jdk-${{ matrix.java }}-maven-${{ hashFiles('**/pom.xml') }}
Expand Down
76 changes: 58 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<img src="https://www.zold.io/logo.svg" width="92px" height="92px"/>
# Zold Java API

![Zold logo](https://www.zold.io/logo.svg)

[![EO principles respected here](https://www.elegantobjects.org/badge.svg)](https://www.elegantobjects.org)
[![DevOps By Rultor.com](https://www.rultor.com/b/zold-io/java-api)](https://www.rultor.com/p/zold-io/java-api)
Expand Down Expand Up @@ -27,7 +29,9 @@ All you need is this:

Java version required: 1.8+.

**We recommend you read the Zold [whitepaper](https://papers.zold.io/wp.pdf) in order to understand the concepts behind this API.**
**We recommend you read the Zold
[whitepaper](https://papers.zold.io/wp.pdf) in order to
understand the concepts behind this API.**

First, you find a wallet in a directory of wallets:

Expand Down Expand Up @@ -69,17 +73,23 @@ Just fork the repo and send us a pull request.

Make sure your branch builds without any warnings/issues:

```
```bash
mvn clean install -Pqulice
```

## Requirements

These are the requirements for this API.

**Note:** The original whitepaper on *zold* can be found [here](https://www.zold.io/). The whitepaper and the documentation found at www.zold.io serve as the highest authority on the subject of *zold*. The following requirements are condensed versions of the points expressed in the aforementioned docs as they relate to the scope of this project:
**Note:** The original whitepaper on *zold* can be found in the
[Zold whitepaper](https://www.zold.io/). The whitepaper and the documentation
found at <https://www.zold.io> serve as the highest authority on the subject
of *zold*. The following requirements are condensed versions of the points
expressed in the aforementioned docs as they relate to the scope of this
project:

* Maintain a wallet in structured textual format, within which is a ledger that contains transactions for that wallet.
* Maintain a wallet in structured textual format, within which is a ledger
that contains transactions for that wallet.
* Make payments:
* Taxes according to fixed formula
* Payments to other wallets
Expand All @@ -88,36 +98,66 @@ These are the requirements for this API.
* Receive payments:
* Pull the paying wallet from the network
* Merge the copies of the paying wallet with our own copy
* We need to refresh our local database of network nodes by querying the network.
* We need to refresh our local database of network nodes by querying
the network.
* We must implement with Java 8
* Non-functional requirements were not made, but we expect
* Flawless concurrency
* "Decent" performance
* Design must respect the principles of [elegant objects](www.elegantobjects.org)
* Design must resemble the design of the original [ruby API](https://github.com/zold-io/zold)

* Design must respect the principles of
[elegant objects](https://www.elegantobjects.org)
* Design must resemble the design of the original
[ruby API](https://github.com/zold-io/zold)

## Decisions and Alternatives

* `javax-json` is a Java API that can parse and also write JSON. As part of the EE7 spec, it is stable, well established, and is the standard. We can use it to write to our local database file. Alternatives are google's [gson](https://github.com/google/gson), [JSON-java](https://github.com/stleary/JSON-java), [jackson-databind](https://github.com/FasterXML/jackson-databind/), and many others.
* The standard `java.security` package contains everything we need to import keystores and sign/encrypt messages with RSA keys. There are lots of examples on the internet on how to use it. I am not aware of any other popular alternative out there.
* `cactoos-http` is a new object-oriented HTTP API under current development as part of project `cactoos` in Zerocracy. It is expected to be ready by the time this API goes to production. Other alternatives are Apache's [http client](https://hc.apache.org/httpcomponents-client-4.5.x/index.html) (not object-oriented), `jcabi-http` (too many dependencies), and many others.
* Our API will consist of our core classes that will communicate with the network using cactoos-http + javax.json, a local storage (`nodes.json`) for persistence of remote node data, will use the `java.security` package when signing the transactions, and will expect wallet files to have the `.z` extension ([discuss](https://github.com/zold-io/zold/issues/164)) and conform to the format specified in the whitepaper:
* `javax-json` is a Java API that can parse and also write JSON. As part of
the EE7 spec, it is stable, well established, and is the standard. We can
use it to write to our local database file. Alternatives are Google's
[gson](https://github.com/google/gson),
[JSON-java](https://github.com/stleary/JSON-java),
[jackson-databind](https://github.com/FasterXML/jackson-databind/),
and many others.
* The standard `java.security` package contains everything we need to import
keystores and sign/encrypt messages with RSA keys. There are lots of
examples on the internet on how to use it. I am not aware of any other
popular alternative out there.
* `cactoos-http` is a new object-oriented HTTP API under current development
as part of project `cactoos` in Zerocracy. It is expected to be ready by
the time this API goes to production. Other alternatives are Apache's
[http client](https://hc.apache.org/httpcomponents-client-4.5.x/index.html)
(not object-oriented), `jcabi-http` (too many dependencies), and many
others.
* Our API will consist of our core classes that will communicate with
the network using cactoos-http + javax.json, a local storage
(`nodes.json`) for persistence of remote node data, will use the
`java.security` package when signing the transactions, and will expect
wallet files to have the `.z` extension
([discuss](https://github.com/zold-io/zold/issues/164)) and conform to
the format specified in the whitepaper:

![architecture](http://www.plantuml.com/plantuml/proxy?src=https://raw.githubusercontent.com/zold-io/java-api/master/src/site/resources/plantuml/architecture.plantuml)

## Concerns

* `cactoos-http` is designed according to the principles of EO, although it still has mayor hurdles to overcome (eg. see [#62](https://github.com/yegor256/cactoos-http/issues/62)).
* `javax.json` will parse data from the zold network and write our local database file in structured JSON format. Its usage is very simple, although we will probably have to be careful with regards to concurrent access to the file.
* `cactoos-http` is designed according to the principles of EO, although it
still has major hurdles to overcome (e.g. see
[#62](https://github.com/yegor256/cactoos-http/issues/62)).
* `javax.json` will parse data from the zold network and write our local
database file in structured JSON format. Its usage is very simple,
although we will probably have to be careful with regards to concurrent
access to the file.
* `java.security` runtimes can handle RSA and MD5 on all platforms.

## Assumptions

* `cactoos-http` will reach the maturity level necessary to support our requirements
* `cactoos-http` will reach the maturity level necessary to support our
requirements
* We can flawlessly manage synchronized access to our local database file

## Risks

* The `cactoos-http` project might not obtain the resources to reach maturity, or may not reach maturity for some other reason.
* Our bottleneck will be reading/writing our local database file. We might not be able to manage a "heavy" throughput.
* The `cactoos-http` project might not obtain the resources to reach
maturity, or may not reach maturity for some other reason.
* Our bottleneck will be reading/writing our local database file. We might
not be able to manage a "heavy" throughput.
24 changes: 15 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.jcabi</groupId>
<artifactId>parent</artifactId>
<version>0.66.0</version>
<version>0.73.1</version>
</parent>
<groupId>io.zold</groupId>
<artifactId>java-api</artifactId>
Expand Down Expand Up @@ -70,7 +70,7 @@
<plugin>
<groupId>com.qulice</groupId>
<artifactId>qulice-maven-plugin</artifactId>
<version>0.17.4</version>
<version>0.27.6</version>
<configuration>
<excludes combine.children="append">
<exclude>checkstyle:/src/site/resources/.*</exclude>
Expand All @@ -86,7 +86,7 @@
<dependency>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>0.35</version>
<version>0.61.0</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
Expand All @@ -95,37 +95,43 @@
<dependency>
<groupId>com.github.victornoel.eo</groupId>
<artifactId>eo-envelopes</artifactId>
<version>0.0.3</version>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.llorllale</groupId>
<artifactId>cactoos-matchers</artifactId>
<version>0.11</version>
<version>0.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>2.4.8</version>
<version>4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.9.3</version>
<version>5.14.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<artifactId>hamcrest</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
58 changes: 34 additions & 24 deletions src/main/java/io/zold/api/Copies.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.cactoos.collection.CollectionOf;
import org.cactoos.iterable.IterableEnvelope;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Joined;
import org.cactoos.iterable.Mapped;
import org.cactoos.list.ListOf;

/**
* Multiple copies of a Wallet.
Expand All @@ -22,11 +22,11 @@ public final class Copies extends IterableEnvelope<Copy> {

/**
* Ctor.
* @param id Id of the wallet to pull.
* @param remotes Remote nodes.
* @param id Id of the wallet to pull
* @param remotes Remote nodes
*/
Copies(final long id, final Iterable<Remote> remotes) {
super(() -> copies(id, remotes));
super(new IterableOf<>(() -> copies(id, remotes).iterator()));
}

/**
Expand All @@ -36,7 +36,6 @@ public final class Copies extends IterableEnvelope<Copy> {
* @return Iterable Iterable of Copy
* @throws IOException If fails
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static Iterable<Copy> copies(final long id,
final Iterable<Remote> remotes) throws IOException {
final List<Copy> copies = new ArrayList<>(10);
Expand All @@ -51,10 +50,10 @@ private static Iterable<Copy> copies(final long id,
}
}
if (!updated) {
copies.add(new Copy(wallet, remote));
copies.add(new Copies.Copy(wallet, remote));
}
}
return new IterableOf<>(copies);
return new IterableOf<>(copies.iterator());
}

/**
Expand All @@ -69,9 +68,9 @@ private static Iterable<Copy> copies(final long id,
*/
private static boolean equalWallets(final Wallet first,
final Wallet second) throws IOException {
return first.id() == second.id() && new CollectionOf<>(
return first.id() == second.id() && new ListOf<>(
first.ledger()
).size() == new CollectionOf<>(second.ledger()).size();
).size() == new ListOf<>(second.ledger()).size();
}

/**
Expand All @@ -92,51 +91,62 @@ static final class Copy implements Comparable<Copy> {

/**
* Ctor.
* @param wallet The wallet.
* @param remotes The remote nodes where the wallet was found.
* @param wallet The wallet
* @param remotes The remote nodes where the wallet was found
*/
Copy(final Wallet wallet, final Remote... remotes) {
this(wallet, new IterableOf<>(remotes));
}

/**
* Ctor.
* @param wallet The wallet.
* @param remotes The remote nodes where the wallet was found.
* @param wallet The wallet
* @param remotes The remote nodes where the wallet was found
*/
Copy(final Wallet wallet, final Iterable<Remote> remotes) {
this.wlt = wallet;
this.remotes = remotes;
}

@Override
public int compareTo(final Copy other) {
return this.score().compareTo(other.score());
}

@Override
public boolean equals(final Object obj) {
return obj instanceof Copy
&& this.compareTo((Copy) obj) == 0;
}

@Override
public int hashCode() {
return this.wlt.hashCode();
}

/**
* Creates new Copy instance with additional remote.
* @param remote Remote
* @return Copy Copy
*/
public Copy with(final Remote remote) {
return new Copy(this.wallet(), new Joined<>(remote, this.remotes));
Copies.Copy with(final Remote remote) {
return new Copies.Copy(this.wallet(), new Joined<>(remote, this.remotes));
}

/**
* The wallet.
* @return The wallet.
* @return The wallet
*/
public Wallet wallet() {
Wallet wallet() {
return this.wlt;
}

/**
* The summary of the score of all the remote nodes.
* @return The score.
* @return The score
*/
public Score score() {
Score score() {
return new Score.Summed(new Mapped<>(Remote::score, this.remotes));
}

@Override
public int compareTo(final Copy other) {
return this.score().compareTo(other.score());
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/io/zold/api/CpTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

/**
* Computed Transaction.
*
* @since 1.0
* @todo #54:30min Implement the computation of the transaction string
* based on the white paper. The unit tests should also be updated to
Expand All @@ -17,9 +16,9 @@ public final class CpTransaction extends TransactionEnvelope {

/**
* Ctor.
*
* @param amt Amount to pay in zents
* @param bnf Wallet ID of beneficiary
* @checkstyle ConstructorsCodeFreeCheck (3 lines)
*/
CpTransaction(final long amt, final long bnf) {
super(new RtTransaction(Long.toString(amt + bnf)));
Expand Down
Loading
Loading