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
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.casper.sdk.jackson.deserializer;

import com.casper.sdk.model.contract.entrypoint.EntryPointAccess;
import com.casper.sdk.model.contract.entrypoint.GroupsAccess;
import com.casper.sdk.model.contract.entrypoint.PublicAccess;
import com.casper.sdk.model.contract.entrypoint.TemplateAccess;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Custom deserializer for {@link EntryPointAccess}
* @author ian@meywood.com
*/
public class EntryPointAccessDeserializer extends JsonDeserializer<EntryPointAccess> {

@Override
public EntryPointAccess deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JacksonException {

if (p.getCurrentToken() == JsonToken.START_OBJECT) {
// Nested rust enums are represented as objects in JSON
return createNestedEntryPointAccess(p.readValueAsTree());
} else if (p.getCurrentToken() == JsonToken.VALUE_STRING) {
// Standard Rust enum types are read from string
return createSimpleEntryPointAccess(p.getValueAsString());
} else {
throw new IllegalArgumentException("Unknown scheduling type: " + p);
}
}

private EntryPointAccess createNestedEntryPointAccess(final ObjectNode treeNode) {
final ArrayNode groups = (ArrayNode) treeNode.get("Groups");
final List<String> groupNames = new ArrayList<>();
if (groups != null) {
groups.forEach(jsonNode -> groupNames.add(jsonNode.asText()));
}
return new GroupsAccess(groupNames);
}

private EntryPointAccess createSimpleEntryPointAccess(final String valueAsString) {
if ("Public".equals(valueAsString)) {
return new PublicAccess();
} else if ("Template".equals(valueAsString)) {
return new TemplateAccess();
} else {
throw new IllegalArgumentException("Unknown entry point access type: " + valueAsString);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.casper.sdk.jackson.deserializer;

import com.casper.sdk.model.clvalue.cltype.AbstractCLType;
import com.casper.sdk.model.contract.EntryPointArg;
import com.casper.sdk.model.contract.EntryPointType;
import com.casper.sdk.model.contract.EntryPointV1;
import com.casper.sdk.model.contract.entrypoint.EntryPointAccess;
import com.casper.sdk.model.contract.entrypoint.EntryPointArg;
import com.casper.sdk.model.contract.entrypoint.EntryPointType;
import com.casper.sdk.model.contract.entrypoint.EntryPointV1;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
Expand All @@ -27,7 +28,7 @@ public EntryPointV1 deserialize(final JsonParser p, final DeserializationContext

return EntryPointV1.builder()
.name(mapNode.get("name").asText())
.access(entryPointNode.get("access").asText())
.access(getNestedObject(entryPointNode, ctxt, "access", EntryPointAccess.class))
.ret(getNestedObject(entryPointNode, ctxt, "ret", AbstractCLType.class))
.entryPointType(getNestedObject(entryPointNode, ctxt, "entry_point_type", EntryPointType.class))
.args(getNestedObject(entryPointNode, ctxt, "args", new TypeReference<List<EntryPointArg>>() {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/casper/sdk/model/contract/Contract.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.casper.sdk.model.contract;

import com.casper.sdk.model.contract.entrypoint.EntryPointV1;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

import java.util.List;
import java.util.Optional;

/**
* Methods and type signatures supported by a contract.
Expand Down Expand Up @@ -38,4 +40,8 @@ public class Contract {
/** protocol_version(String) - ? */
@JsonProperty("protocol_version")
private String protocolVersion;

public Optional<EntryPointV1> getEntryPoint(String name) {
return entryPoints.stream().filter(entryPoint -> entryPoint.getName().equals(name)).findFirst();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.casper.sdk.model.contract;
package com.casper.sdk.model.contract.entrypoint;

import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport;
import com.casper.sdk.model.clvalue.cltype.AbstractCLType;
import com.casper.sdk.model.clvalue.cltype.AbstractCLTypeBasic;
import com.casper.sdk.model.contract.Parameter;
import com.fasterxml.jackson.annotation.*;
import lombok.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.casper.sdk.model.contract.entrypoint;

import com.casper.sdk.jackson.deserializer.EntryPointAccessDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

/**
* Interface describing the possible access control options for a contract entry point (method).
*
* @author ian@meywood.com
*/
@JsonDeserialize(using = EntryPointAccessDeserializer.class)
public interface EntryPointAccess {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.casper.sdk.model.contract;
package com.casper.sdk.model.contract.entrypoint;

import com.casper.sdk.model.clvalue.cltype.AbstractCLType;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.casper.sdk.model.contract;
package com.casper.sdk.model.contract.entrypoint;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.casper.sdk.model.contract;
package com.casper.sdk.model.contract.entrypoint;

import com.casper.sdk.jackson.deserializer.EntryPointV1Deserializer;
import com.casper.sdk.model.clvalue.cltype.AbstractCLType;
Expand All @@ -24,7 +24,7 @@
public class EntryPointV1 {

private String name;
private String access;
private EntryPointAccess access;
private AbstractCLType ret;
@JsonProperty("entry_point_type")
private EntryPointType entryPointType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.casper.sdk.model.contract;
package com.casper.sdk.model.contract.entrypoint;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

/**
* The entry point for the V2 Casper VM.
*
* TODO Delete this class there is no corresponding class in the Casper node
*
* @author carl@stormeye.co.uk
*/
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.casper.sdk.model.contract;
package com.casper.sdk.model.contract.entrypoint;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.casper.sdk.model.contract.entrypoint;

import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;

import java.util.Iterator;
import java.util.List;

/**
* Only users from the listed groups may call this method. Note: if the list is empty then this method is not callable
* from outside the contract.
*
* @author ian@meywood.com
*/
@JsonTypeName("Groups")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class GroupsAccess implements EntryPointAccess, Iterable<String> {
private List<String> groups;

@NotNull
@Override
public Iterator<String> iterator() {
return groups.iterator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.casper.sdk.model.contract.entrypoint;

import com.fasterxml.jackson.annotation.JsonTypeName;

/**
* Anyone can call this method (no access controls).
*
* @author ian@meywood.com
*/
@JsonTypeName("Public")
public class PublicAccess implements EntryPointAccess {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.casper.sdk.model.contract.entrypoint;

import com.fasterxml.jackson.annotation.JsonTypeName;

/**
* Can't be accessed directly but are kept in the derived wasm bytes.
*
* @author ian@meywood.com
*/
@JsonTypeName("Template")
public class TemplateAccess implements EntryPointAccess {
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,4 @@ public void serialize(SerializerBuffer ser, Target target) throws ValueSerializa
ser.writeU8(getOrder());
serializeNamedArgs(ser, target);
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.casper.sdk.model.entity;

import com.casper.sdk.model.contract.EntryPointValue;
import com.casper.sdk.model.contract.entrypoint.EntryPointValue;
import com.casper.sdk.model.contract.NamedKey;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.Setter;

/**
* The summary for a message topic in a message topic kind.
*
* @author ian@meywood.com
*/
@AllArgsConstructor
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/casper/sdk/model/key/BidAddr.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ public enum BidAddr {
/// Validator BidAddr.
VALIDATOR((byte) 1),
/// Delegator BidAddr.
DELEGATOR((byte) 2),
DELEGATOR_ACCOUNT((byte) 2),
DELEGATOR_PURSE((byte) 3),
/// Validator credit BidAddr.
CREDIT((byte) 4);
CREDIT((byte) 4),
RESERVED_DELEGATION_ACCOUNT((byte) 5),
RESERVED_DELEGATION_PURSE((byte) 6),
UNBOND_ACCOUNT((byte) 7),
UNBOND_PURSE((byte) 8);

private final byte byteTag;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/casper/sdk/model/key/BidAddrKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected void deserializeCustom(final DeserializerBuffer deser) throws Exceptio
this.bidAddr = BidAddr.getByTag(deser.readU8());

final int len;
if (BidAddr.DELEGATOR == bidAddr) {
if (BidAddr.DELEGATOR_ACCOUNT == bidAddr) {
len = 65;
} else if (BidAddr.CREDIT == bidAddr) {
len = 41;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public abstract class TransactionEntryPoint implements CasperSerializableObject,
protected static final int CHANGE_BID_PUBLIC_KEY_TAG = 8;
protected static final int CALL_TAG = 9;


private final byte tag;
private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.Map;

/**
* The fields of a transaction.
*
* @author ian@meywood.com
*/
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import lombok.Setter;

/**
* A StoredValue containing a ContractWasm.
*
* @author ian@meywood.com
*/
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.casper.sdk.model.transaction.kind;

import com.casper.sdk.model.contract.EntryPoint;
import com.casper.sdk.model.contract.EntryPointValue;
import com.casper.sdk.model.contract.entrypoint.EntryPoint;
import com.casper.sdk.model.contract.entrypoint.EntryPointValue;
import com.casper.sdk.model.storedvalue.StoredValue;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import lombok.Setter;

/**
* A StoredValue containing a MessageTopicSummary.
*
* @author ian@meywood.com
*/
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void clValueKeyBidAddrKeyDelegatorJsonRoundTrip() throws JsonProcessingException
assertThat(clValueKey.getParsed(), is("bid-addr-022f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c4580073099fa1fc0808d3a5b9ea9f3af4ca7c8c3655568fdf378d8afdf8a7e56e58abbfd4"));
assertThat(clValueKey.getBytes(), is("0f02022f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c4580073099fa1fc0808d3a5b9ea9f3af4ca7c8c3655568fdf378d8afdf8a7e56e58abbfd4"));
final BidAddrKey key = (BidAddrKey) clValueKey.getValue();
assertThat(key.getBidAddr(), is(BidAddr.DELEGATOR));
assertThat(key.getBidAddr(), is(BidAddr.DELEGATOR_ACCOUNT));

final String written = new ObjectMapper().writeValueAsString(clValueKey);
JSONAssert.assertEquals(json, written, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.casper.sdk.model.entity;

import com.casper.sdk.model.AbstractJsonTests;
import com.casper.sdk.model.contract.EntryPoint;
import com.casper.sdk.model.contract.EntryPointV2;
import com.casper.sdk.model.contract.EntryPointValue;
import com.casper.sdk.model.contract.entrypoint.EntryPoint;
import com.casper.sdk.model.contract.entrypoint.EntryPointV2;
import com.casper.sdk.model.contract.entrypoint.EntryPointValue;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand Down
17 changes: 14 additions & 3 deletions src/test/java/com/casper/sdk/model/key/KeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ void bidKeyFromKeyString() throws Exception {
assertThat(key.getAlgoTaggedHex(), is("070808080808080808080808080808080808080808080808080808080808080808"));
}

@Test
void longBidKeyFromKeyString() throws Exception {

final String strKey = "bid-addr-032f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c458007309de0ef381c12dc842e0872453d35aa4b49fa488c0427e68b7dd5ac63e151eae98";
final Key key = fromJson(strKey);
assertThat(key, is(notNullValue(Key.class)));
assertThat(key.getTag(), is(KeyTag.BID_ADDR));
assertThat(key.getKey(), is(Hex.decode("032f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c458007309de0ef381c12dc842e0872453d35aa4b49fa488c0427e68b7dd5ac63e151eae98")));
assertThat(toJson(key), is(strKey));
assertThat(key.getAlgoTaggedHex(), is("0f032f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c458007309de0ef381c12dc842e0872453d35aa4b49fa488c0427e68b7dd5ac63e151eae98"));
}

@Test
void withdrawKeyFromKeyString() throws Exception {

Expand Down Expand Up @@ -279,7 +291,7 @@ void bidAddrKeyDelegatorFromKeyString() throws Exception {
assertThat(key.getKey(), is(Hex.decode("022f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c4580073099fa1fc0808d3a5b9ea9f3af4ca7c8c3655568fdf378d8afdf8a7e56e58abbfd4")));
assertThat(toJson(key), is(strKey));
assertThat(key.getAlgoTaggedHex(), is("0f022f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c4580073099fa1fc0808d3a5b9ea9f3af4ca7c8c3655568fdf378d8afdf8a7e56e58abbfd4"));
assertThat(((BidAddrKey) key).getBidAddr(), is(BidAddr.DELEGATOR));
assertThat(((BidAddrKey) key).getBidAddr(), is(BidAddr.DELEGATOR_ACCOUNT));
}

@Test
Expand Down Expand Up @@ -391,7 +403,7 @@ void messageTopicKeyFromKeyString() throws Exception {
@Test
void basicMessageTopicKeyFromKeyString() throws Exception {

final String strKey ="message-topic-94b21891ae273b17eeb6a1899a52ab952bcc4da2e19626563f88d6cf7ab6a2bd-e0846cb0e9995ea903dd1b6a275f9c7d9f23e2f83b6918a8a01ba857ff7efdad";
final String strKey = "message-topic-94b21891ae273b17eeb6a1899a52ab952bcc4da2e19626563f88d6cf7ab6a2bd-e0846cb0e9995ea903dd1b6a275f9c7d9f23e2f83b6918a8a01ba857ff7efdad";
final Key key = fromJson(strKey);
assertThat(key, is(instanceOf(MessageKey.class)));
assertThat(key.getTag(), is(KeyTag.MESSAGE));
Expand Down Expand Up @@ -492,7 +504,6 @@ void nullKeyFromJson() throws Exception {
}

private Key fromJson(final String strKey) throws JsonProcessingException {
//noinspection VulnerableCodeUsages
return new ObjectMapper().readValue(strKey != null ? "\"" + strKey + "\"" : "null", Key.class);
}

Expand Down
Loading