Skip to content

Commit

Permalink
test serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsobries committed Nov 18, 2024
1 parent 18497ff commit 5f45d12
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,50 @@ public abstract class AbstractTransaction {
public String validatorPublicKey;
public String vote;

public AbstractTransaction() {

}

public AbstractTransaction(Map<String, Object> data) {
if (data.containsKey("network")) {
this.network = ((Number) data.get("network")).intValue();
}
if (data.containsKey("nonce")) {
this.nonce = Long.parseLong(data.get("nonce").toString());
}
if (data.containsKey("gasPrice")) {
this.gasPrice = ((Number) data.get("gasPrice")).intValue();
}
if (data.containsKey("gasLimit")) {
this.gasLimit = ((Number) data.get("gasLimit")).intValue();
}
if (data.containsKey("recipientAddress")) {
this.recipientAddress = (String) data.get("recipientAddress");
}
if (data.containsKey("value")) {
this.value = data.get("value").toString();
}
if (data.containsKey("data")) {
this.data = (String) data.get("data");
}
if (data.containsKey("signature")) {
this.signature = (String) data.get("signature");
}
if (data.containsKey("senderPublicKey")) {
this.senderPublicKey = (String) data.get("senderPublicKey");
}
if (data.containsKey("id")) {
this.id = (String) data.get("id");
}
if (data.containsKey("validatorPublicKey")) {
this.validatorPublicKey = (String) data.get("validatorPublicKey");
}
if (data.containsKey("vote")) {
this.vote = (String) data.get("vote");
}

}

public String getPayload() {
return this.data != null ? this.data : "";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package org.arkecosystem.crypto.transactions.types;

import java.util.Map;

public class EvmCall extends AbstractTransaction {
public EvmCall() {
super();
}

public EvmCall(Map<String, Object> data) {
super(data);
}

@Override
public String getPayload() {
return this.data != null ? this.data : "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package org.arkecosystem.crypto.transactions.types;

import java.util.Map;

public class Transfer extends AbstractTransaction {
public Transfer() {
super();
}

public Transfer(Map<String, Object> data) {
super(data);
}

@Override
public String getPayload() {
return "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package org.arkecosystem.crypto.transactions.types;

import java.util.Map;

import org.arkecosystem.crypto.enums.AbiFunction;
import org.arkecosystem.crypto.utils.AbiEncoder;

public class Unvote extends AbstractTransaction {

public Unvote() {
super();
}

public Unvote(Map<String, Object> data) {
super(data);
}

@Override
public String getPayload() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ public ValidatorRegistration() {
}

public ValidatorRegistration(Map<String, Object> data) {
super(); // Call the default constructor of AbstractTransaction
// Handle 'data' within this class

super(data);

// Use a local decodePayload method since we can't rely on AbstractTransaction's data field
List<Object> payload = decodePayload(data);
if (payload != null && !payload.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package org.arkecosystem.crypto.transactions.types;

import java.util.Map;

import org.arkecosystem.crypto.enums.AbiFunction;
import org.arkecosystem.crypto.utils.AbiEncoder;

public class ValidatorResignation extends AbstractTransaction {
public ValidatorResignation() {
super();
}

public ValidatorResignation(Map<String, Object> data) {
super(data);
}

@Override
public String getPayload() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public Vote() {
}

public Vote(Map<String, Object> data) {
super();
super(data);

List<Object> payload = decodePayload(data);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package org.arkecosystem.crypto.transactions.builder;
package org.arkecosystem.crypto;

import com.google.gson.Gson;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;

public abstract class AbstractTransactionBuilderTest {
protected String passphrase;
public abstract class AbstractTest {
public String passphrase;

@BeforeEach
public void setUp() {
this.passphrase = "my super secret passphrase";
}

protected Map<String, Object> loadFixture(String name) throws Exception {

String resourcePath = "/transactions/" + name + ".json";
InputStream inputStream = getClass().getResourceAsStream(resourcePath);
if (inputStream == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.arkecosystem.crypto.transactions;

import static org.junit.jupiter.api.Assertions.*;

import org.arkecosystem.crypto.AbstractTest;
import org.arkecosystem.crypto.encoding.Hex;
import org.arkecosystem.crypto.transactions.builder.TransferBuilder;
import org.arkecosystem.crypto.transactions.types.*;
import org.junit.jupiter.api.Test;

import java.util.Map;

public class SerializerTest extends AbstractTest {

@Test
public void it_should_serialize_a_transfer_transaction() throws Exception {
Map<String, Object> fixture = loadFixture("transfer");

Map<String, Object> data = (Map<String, Object>) fixture.get("data");

Transfer transaction = new Transfer(data);

byte[] serializedBytes = transaction.serialize(false);
String serializedHex = Hex.encode(serializedBytes);

assertEquals(fixture.get("serialized"), serializedHex);
}

@Test
public void it_should_serialize_a_vote_transaction() throws Exception {
Map<String, Object> fixture = loadFixture("vote");

Map<String, Object> data = (Map<String, Object>) fixture.get("data");

Vote transaction = new Vote(data);

byte[] serializedBytes = transaction.serialize(false);
String serializedHex = Hex.encode(serializedBytes);

assertEquals(fixture.get("serialized"), serializedHex);
}

@Test
public void it_should_serialize_a_unvote_transaction() throws Exception {
Map<String, Object> fixture = loadFixture("unvote");

Map<String, Object> data = (Map<String, Object>) fixture.get("data");

Unvote transaction = new Unvote(data);

byte[] serializedBytes = transaction.serialize(false);
String serializedHex = Hex.encode(serializedBytes);

assertEquals(fixture.get("serialized"), serializedHex);
}

@Test
public void it_should_serialize_a_validator_registration_transaction() throws Exception {
Map<String, Object> fixture = loadFixture("validator-registration");

Map<String, Object> data = (Map<String, Object>) fixture.get("data");

ValidatorRegistration transaction = new ValidatorRegistration(data);

byte[] serializedBytes = transaction.serialize(false);
String serializedHex = Hex.encode(serializedBytes);

assertEquals(fixture.get("serialized"), serializedHex);
}

@Test
public void it_should_serialize_a_validator_resignation_transaction() throws Exception {
Map<String, Object> fixture = loadFixture("validator-resignation");

Map<String, Object> data = (Map<String, Object>) fixture.get("data");

ValidatorResignation transaction = new ValidatorResignation(data);

byte[] serializedBytes = transaction.serialize(false);
String serializedHex = Hex.encode(serializedBytes);

assertEquals(fixture.get("serialized"), serializedHex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import static org.junit.jupiter.api.Assertions.*;

import java.util.Map;

import org.arkecosystem.crypto.AbstractTest;
import org.junit.jupiter.api.Test;

public class EvmCallBuilderTest extends AbstractTransactionBuilderTest {
public class EvmCallBuilderTest extends AbstractTest {

@Test
public void it_should_sign_it_with_a_passphrase() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static org.junit.jupiter.api.Assertions.*;

import java.util.Map;

import org.arkecosystem.crypto.AbstractTest;
import org.arkecosystem.crypto.encoding.Hex;
import org.junit.jupiter.api.Test;

public class TransferBuilderTest extends AbstractTransactionBuilderTest {
public class TransferBuilderTest extends AbstractTest {

@Test
public void it_should_sign_it_with_a_passphrase() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static org.junit.jupiter.api.Assertions.*;

import java.util.Map;

import org.arkecosystem.crypto.AbstractTest;
import org.arkecosystem.crypto.encoding.Hex;
import org.junit.jupiter.api.Test;

public class UnvoteBuilderTest extends AbstractTransactionBuilderTest {
public class UnvoteBuilderTest extends AbstractTest {

@Test
public void it_should_sign_it_with_a_passphrase() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static org.junit.jupiter.api.Assertions.*;

import java.util.Map;

import org.arkecosystem.crypto.AbstractTest;
import org.arkecosystem.crypto.encoding.Hex;
import org.junit.jupiter.api.Test;

public class ValidatorRegistrationBuilderTest extends AbstractTransactionBuilderTest {
public class ValidatorRegistrationBuilderTest extends AbstractTest {

@Test
public void it_should_sign_it_with_a_passphrase() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static org.junit.jupiter.api.Assertions.*;

import java.util.Map;

import org.arkecosystem.crypto.AbstractTest;
import org.arkecosystem.crypto.encoding.Hex;
import org.junit.jupiter.api.Test;

public class ValidatorResignationBuilderTest extends AbstractTransactionBuilderTest {
public class ValidatorResignationBuilderTest extends AbstractTest {

@Test
public void it_should_sign_it_with_a_passphrase() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static org.junit.jupiter.api.Assertions.*;

import java.util.Map;

import org.arkecosystem.crypto.AbstractTest;
import org.arkecosystem.crypto.encoding.Hex;
import org.junit.jupiter.api.Test;

public class VoteBuilderTest extends AbstractTransactionBuilderTest {
public class VoteBuilderTest extends AbstractTest {

@Test
public void it_should_sign_it_with_a_passphrase() throws Exception {
Expand Down

0 comments on commit 5f45d12

Please sign in to comment.