Skip to content

Commit

Permalink
zold-io#56 resolved conflicts from master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Vatavuk committed Aug 11, 2018
2 parents 8692bff + aef97b4 commit fafc2a8
Show file tree
Hide file tree
Showing 18 changed files with 554 additions and 111 deletions.
7 changes: 4 additions & 3 deletions src/main/java/io/zold/api/CpTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
* Computed Transaction.
*
* @since 1.0
* @todo #29:30min Implement the computation of the transaction string
* based on the white paper. The unit test should also be updated to
* ensure it works as expected.
* @todo #54:30min Implement the computation of the transaction string
* based on the white paper. The unit tests should also be updated to
* ensure it works as expected and test for
* returnSignatureForNegativeTransaction must be implemented.
*/
public final class CpTransaction extends TransactionEnvelope {

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/zold/api/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
* @since 0.1
*/
public interface Network extends Iterable<Remote> {

/**
* Push the wallet to the network. The network will select the
* remote node with the highest score (with a minimum of {@code 16}).
* Push the wallet to the network.
* @param wallet The wallet
*/
void push(Wallet wallet);
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/io/zold/api/Remote.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

package io.zold.api;

import org.cactoos.iterable.Repeated;
import org.cactoos.text.RandomText;

/**
* Remote node.
*
Expand All @@ -50,18 +53,28 @@ public interface Remote {
Wallet pull(long id);

/**
* Fake Remote.
* A Fake {@link Remote}.
*/
final class Fake implements Remote {

/**
* Score.
* The remote's score.
*/
private final Score score;

/**
* Ctor.
* @param score Score
* @param val The remote's score value
*/
public Fake(final int val) {
this(new RtScore(
new Repeated<>(val, new RandomText())
));
}

/**
* Ctor.
* @param score The remote's score
*/
public Fake(final Score score) {
this.score = score;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/io/zold/api/RtNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ public final class RtNetwork implements Network {
this.nodes = remotes;
}

// @todo #5:30min Implement scoring algorithm when paying taxes. Scoring
// algorithm must select the node with the highest score and with score
// >= 16. There are some tests for the scoring algorithm in NetworkTest:
// remove ignore tag from them after algorithm implementation.
@Override
public void push(final Wallet wallet) {
this.nodes.forEach(
Expand Down
31 changes: 23 additions & 8 deletions src/main/java/io/zold/api/RtTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ final class RtTransaction implements Transaction {
);

/**
* Pattern for amount String.
* Pattern for 16 symbol hex string.
*/
private static final Pattern AMT = Pattern.compile("[A-Fa-f0-9]{16}");
private static final Pattern HEX = Pattern.compile("[A-Fa-f0-9]{16}");

/**
* Pattern for parsing Signature.
Expand Down Expand Up @@ -138,7 +138,7 @@ public long amount() throws IOException {
)
).value()
).asString();
if (!RtTransaction.AMT.matcher(amnt).matches()) {
if (!RtTransaction.HEX.matcher(amnt).matches()) {
throw new IOException(
new UncheckedText(
new FormattedText(
Expand Down Expand Up @@ -183,12 +183,27 @@ public String prefix() throws IOException {
}
}

// @todo #15:30min Implement bnf() by parsing the string representation
// of transaction according to the pattern, described in the white
// paper. Replace relevant test case with actual tests.
@Override
public long bnf() {
throw new UnsupportedOperationException("bnf() not yet implemented");
public String bnf() throws IOException {
final String bnf =
new IoCheckedScalar<>(
() -> new ItemAt<>(
// @checkstyle MagicNumberCheck (1 line)
4, new SplitText(this.transaction, ";")
).value().asString()
).value();
if (!RtTransaction.HEX.matcher(bnf).matches()) {
throw new IOException(
new UncheckedText(
new FormattedText(
// @checkstyle LineLength (1 line)
"Invalid bnf string '%s', expecting hex string with 16 symbols",
bnf
)
).asString()
);
}
return bnf;
}

@Override
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/io/zold/api/TaxBeneficiaries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.zold.api;

import java.util.Comparator;
import org.cactoos.iterable.Filtered;
import org.cactoos.iterable.IterableEnvelope;
import org.cactoos.iterable.LengthOf;
import org.cactoos.iterable.Sorted;

/**
* {@link Remote} nodes that should receive taxes.
*
* @since 1.0
*/
public final class TaxBeneficiaries extends IterableEnvelope<Remote> {

/**
* Ctor.
*
* @param nodes Remote nodes to select from.
*/
public TaxBeneficiaries(final Iterable<Remote> nodes) {
super(() -> new Sorted<>(
Comparator.comparing(Remote::score),
new Filtered<>(
// @checkstyle MagicNumberCheck (1 line)
n -> new LengthOf(n.score().suffixes()).intValue() >= 16,
nodes
)
));
}
}
69 changes: 69 additions & 0 deletions src/main/java/io/zold/api/Taxes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.zold.api;

import org.cactoos.Proc;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Taxes payment algorithm.
*
* @since 1.0
* @todo #40:30min Implement tax payment to remote nodes.
* Payment should happen only if the wallet is in debt of more than
* 1 Zold. Debt is difference between current taxes that should have
* been paid by wallet (see whitepaper for formula) and how much it
* already paid in the past. A first algorithm could pay the
* max to each node until there is nothing else to pay. The test
* must also be updated.
*/
public final class Taxes implements Proc<Wallet> {

/**
* The beneficiary nodes.
*/
private final Iterable<Remote> bnfs;

/**
* Ctor.
*
* @param nodes Remote nodes.
*/
public Taxes(final Iterable<Remote> nodes) {
this.bnfs = new TaxBeneficiaries(nodes);
}

@Override
public void exec(final Wallet wallet) {
throw new UnsupportedOperationException(
new UncheckedText(
new FormattedText(
"paying taxes to %s not yet supported",
this.bnfs
)
).asString()
);
}
}
3 changes: 2 additions & 1 deletion src/main/java/io/zold/api/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ public interface Transaction {
/**
* Beneficiary.
* @return Beneficiary
* @throws IOException When something goes wrong
*/
long bnf();
String bnf() throws IOException;

/**
* Details.
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/io/zold/api/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public interface Wallet {
*/
Iterable<Transaction> ledger();

/**
* This wallet's RSA key.
* @return This wallet's RSA key.
*/
String key();

/**
* A Fake {@link Wallet}.
*
Expand Down Expand Up @@ -112,6 +118,11 @@ public Wallet merge(final Wallet other) {
public Iterable<Transaction> ledger() {
return new IterableOf<>();
}

@Override
public String key() {
return Long.toString(this.id);
}
}

/**
Expand Down Expand Up @@ -185,5 +196,15 @@ public Iterable<Transaction> ledger() {
)
);
}

// @todo #54:30min Implement key method. This should return the
// public RSA key of the wallet owner in Base64. Also add a unit test
// to replace WalletTest.keyIsNotYetImplemented().
@Override
public String key() {
throw new UnsupportedOperationException(
"key() not yet supported"
);
}
}
}
7 changes: 5 additions & 2 deletions src/main/java/io/zold/api/Wallets.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package io.zold.api;

import java.io.IOException;

/**
* Wallets.
*
Expand All @@ -32,7 +34,8 @@
public interface Wallets extends Iterable<Wallet> {
/**
* Create a wallet.
* @return The new wallet
* @return The new wallet.
* @throws IOException If an error occurs.
*/
Wallet create();
Wallet create() throws IOException;
}
Loading

0 comments on commit fafc2a8

Please sign in to comment.