Skip to content

Commit

Permalink
#97: Migrate set to list type
Browse files Browse the repository at this point in the history
The set type makes testing the serialization hell because the sets
deny a fixed ordering of some of the properties.
  • Loading branch information
csmuller committed Nov 30, 2019
1 parent 624378f commit 83f8fde
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
39 changes: 21 additions & 18 deletions core/src/main/java/io/neow3j/transaction/Cosigner.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.List;


/**
Expand All @@ -28,17 +27,17 @@ public class Cosigner extends NeoSerializable {
/**
* The scopes in which a transaction witness can be used. Multiple scopes can be combined.
*/
private Set<WitnessScope> scopes;
private List<WitnessScope> scopes;

/**
* The script hashes of contracts that are allowed to use the witness.
*/
private Set<ScriptHash> allowedContracts;
private List<ScriptHash> allowedContracts;

/**
* The group hashes of contracts that are allowed to use the witness.
*/
private Set<ECKeyPair.ECPublicKey> allowedGroups;
private List<ECKeyPair.ECPublicKey> allowedGroups;

private Cosigner(Builder builder) {
this.account = builder.account;
Expand Down Expand Up @@ -77,15 +76,15 @@ public ScriptHash getAccount() {
return account;
}

public Set<WitnessScope> getScopes() {
public List<WitnessScope> getScopes() {
return scopes;
}

public Set<ScriptHash> getAllowedContracts() {
public List<ScriptHash> getAllowedContracts() {
return allowedContracts;
}

public Set<ECKeyPair.ECPublicKey> getAllowedGroups() {
public List<ECKeyPair.ECPublicKey> getAllowedGroups() {
return allowedGroups;
}

Expand All @@ -99,24 +98,24 @@ public void serialize(BinaryWriter writer) throws IOException {
writer.write(account.toArray());
writer.writeByte(WitnessScope.getCombinedScope(this.scopes));
if (scopes.contains(WitnessScope.CUSTOM_CONSTRACTS)) {
writer.writeSerializableVariable(new ArrayList<>(this.allowedContracts));
writer.writeSerializableVariable(this.allowedContracts);
}
if (scopes.contains(WitnessScope.CUSTOM_GROUPS)) {
writer.writeSerializableVariable(new ArrayList<>(this.allowedGroups));
writer.writeSerializableVariable(this.allowedGroups);
}
}

public static class Builder {

private ScriptHash account;
private Set<WitnessScope> scopes;
private Set<ScriptHash> allowedContracts;
private Set<ECKeyPair.ECPublicKey> allowedGroups;
private List<WitnessScope> scopes;
private List<ScriptHash> allowedContracts;
private List<ECKeyPair.ECPublicKey> allowedGroups;

public Builder() {
this.scopes = new HashSet<>();
this.allowedContracts = new HashSet<>();
this.allowedGroups = new HashSet<>();
this.scopes = new ArrayList<>();
this.allowedContracts = new ArrayList<>();
this.allowedGroups = new ArrayList<>();
}

/**
Expand Down Expand Up @@ -154,7 +153,9 @@ public Builder scopes(WitnessScope... scopes) {
* @return this builder.
*/
public Builder allowedContracts(ScriptHash... contracts) {
this.scopes.add(WitnessScope.CUSTOM_CONSTRACTS);
if (!this.scopes.contains(WitnessScope.CUSTOM_CONSTRACTS)) {
this.scopes.add(WitnessScope.CUSTOM_CONSTRACTS);
}
this.allowedContracts.addAll(Arrays.asList(contracts));
return this;
}
Expand All @@ -169,7 +170,9 @@ public Builder allowedContracts(ScriptHash... contracts) {
* @return this builder.
*/
public Builder allowedGroups(ECKeyPair.ECPublicKey... groups) {
this.scopes.add(WitnessScope.CUSTOM_GROUPS);
if (!this.scopes.contains(WitnessScope.CUSTOM_GROUPS)) {
this.scopes.add(WitnessScope.CUSTOM_GROUPS);
}
this.allowedGroups.addAll(Arrays.asList(groups));
return this;
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/io/neow3j/transaction/WitnessScope.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.neow3j.transaction;

import java.util.Set;
import java.util.List;

public enum WitnessScope {

Expand Down Expand Up @@ -45,7 +45,7 @@ public static WitnessScope valueOf(byte byteValue) {
throw new IllegalArgumentException();
}

public static byte getCombinedScope(Set<WitnessScope> scopes) {
public static byte getCombinedScope(List<WitnessScope> scopes) {
byte combined = 0;
for (WitnessScope s : scopes) {
combined |= s.byteValue();
Expand Down
13 changes: 5 additions & 8 deletions core/src/test/java/io/neow3j/transaction/WitnessScopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.junit.Test;

import java.util.Arrays;
import java.util.HashSet;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
Expand All @@ -12,21 +11,19 @@ public class WitnessScopeTest {

@Test
public void getCombinedScopes() {
byte scope = WitnessScope.getCombinedScope(new HashSet<>(Arrays.asList(
byte scope = WitnessScope.getCombinedScope(Arrays.asList(
WitnessScope.CALLED_BY_ENTRY,
WitnessScope.CUSTOM_CONSTRACTS)));
WitnessScope.CUSTOM_CONSTRACTS));
assertThat(scope, is((byte) 0x11));

scope = WitnessScope.getCombinedScope(new HashSet<>(Arrays.asList(
scope = WitnessScope.getCombinedScope(Arrays.asList(
WitnessScope.CALLED_BY_ENTRY,
WitnessScope.CUSTOM_CONSTRACTS,
WitnessScope.CUSTOM_GROUPS)));
WitnessScope.CUSTOM_GROUPS));
assertThat(scope, is((byte) 0x31));

scope = WitnessScope.getCombinedScope(new HashSet<>(Arrays.asList(
WitnessScope.GLOBAL)));
scope = WitnessScope.getCombinedScope(Arrays.asList(WitnessScope.GLOBAL));
assertThat(scope, is((byte) 0x00));
}


}

0 comments on commit 83f8fde

Please sign in to comment.