Skip to content

Commit

Permalink
fix: set default threshold to 1 && rf: relayers add and remove
Browse files Browse the repository at this point in the history
  • Loading branch information
sherpalden committed Oct 7, 2024
1 parent 7d7031a commit 08508f5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
import score.annotation.External;
import score.ObjectReader;
import scorex.util.ArrayList;
import scorex.util.HashMap;

public class RelayAggregator {
private final Integer DEFAULT_SIGNATURE_THRESHOLD = 2;
private final Integer DEFAULT_SIGNATURE_THRESHOLD = 1;

private final VarDB<Integer> signatureThreshold = Context.newVarDB("signatureThreshold", Integer.class);

Expand All @@ -50,12 +49,20 @@ public RelayAggregator(Address _admin) {
if (admin.get() == null) {
admin.set(_admin);
signatureThreshold.set(DEFAULT_SIGNATURE_THRESHOLD);
addRelayer(_admin);
}
}

@External
public void setAdmin(Address _admin) {
adminOnly();

// add new admin as relayer
addRelayer(_admin);

// remove old admin from relayer list
removeRelayer(admin.get());

admin.set(_admin);
}

Expand Down Expand Up @@ -94,8 +101,7 @@ public void addRelayers(Address[] newRelayers) {
for (Address newRelayer : newRelayers) {
Boolean exits = relayersLookup.get(newRelayer);
if (exits == null) {
relayers.add(newRelayer);
relayersLookup.set(newRelayer, true);
addRelayer(newRelayer);
}
}
}
Expand All @@ -107,22 +113,9 @@ public void removeRelayers(Address[] relayersToBeRemoved) {
Context.require(relayersToBeRemoved != null && relayersToBeRemoved.length != 0,
"relayers to be removed cannot be empty");

HashMap<Address, Integer> existingRelayers = new HashMap<Address, Integer>();
for (int i = 0; i < relayers.size(); i++) {
Address relayer = relayers.get(i);
existingRelayers.put(relayer, i);
}

for (Address relayerToBeRemoved : relayersToBeRemoved) {
if (existingRelayers.containsKey(relayerToBeRemoved)) {
relayersLookup.set(relayerToBeRemoved, null);
Address top = relayers.pop();
if (!top.equals(relayerToBeRemoved)) {
Integer pos = existingRelayers.get(relayerToBeRemoved);
relayers.set(pos, top);
}
existingRelayers.remove(relayerToBeRemoved);
}
Context.require(relayerToBeRemoved != admin.get(), "admin cannot be removed from relayers list");
removeRelayer(relayerToBeRemoved);
}
}

Expand Down Expand Up @@ -254,6 +247,22 @@ private void relayersOnly() {
Context.require(isRelayer != null && isRelayer, "Unauthorized: caller is not a registered relayer");
}

private void addRelayer(Address newRelayer) {
relayers.add(newRelayer);
relayersLookup.set(newRelayer, true);
}

private void removeRelayer(Address oldRelayer) {
relayersLookup.set(oldRelayer, null);
Address top = relayers.pop();
for (int i = 0; i < relayers.size(); i++) {
if (oldRelayer.equals(relayers.get(i))) {
relayers.set(i, top);
break;
}
}
}

private Boolean signatureThresholdReached(String pktID) {
int noOfSignatures = 0;
for (int i = 0; i < relayers.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package relay.aggregator;

import java.math.BigInteger;
import java.util.Arrays;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -19,6 +20,7 @@

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -75,11 +77,21 @@ void setup() throws Exception {

@Test
public void testSetAdmin() {
Address oldAdmin = (Address) aggregator.call("getAdmin");

Account newAdminAc = sm.createAccount();
aggregator.invoke(adminAc, "setAdmin", newAdminAc.getAddress());

Address result = (Address) aggregator.call("getAdmin");
assertEquals(newAdminAc.getAddress(), result);
Address newAdmin = (Address) aggregator.call("getAdmin");
assertEquals(newAdminAc.getAddress(), newAdmin);

Address[] relayers = (Address[]) aggregator.call("getRelayers");

boolean containsNewAdmin = Arrays.asList(relayers).contains(newAdmin);
boolean containsOldAdmin = Arrays.asList(relayers).contains(oldAdmin);

assertTrue(containsNewAdmin);
assertFalse(containsOldAdmin);
}

@Test
Expand Down Expand Up @@ -180,6 +192,8 @@ public void testPacketSubmitted_true() throws Exception {
String dstContractAddress = "hxjuiod";
byte[] data = new byte[] { 0x01, 0x02 };

aggregator.invoke(adminAc, "setSignatureThreshold", 2);

byte[] dataHash = Context.hash("sha-256", data);
byte[] sign = relayerOne.sign(dataHash);

Expand Down Expand Up @@ -213,6 +227,8 @@ public void testSubmitPacket() throws Exception {
String dstContractAddress = "hxjuiod";
byte[] data = new byte[] { 0x01, 0x02 };

aggregator.invoke(adminAc, "setSignatureThreshold", 2);

byte[] dataHash = Context.hash("sha-256", data);
byte[] sign = relayerOne.sign(dataHash);

Expand All @@ -236,6 +252,8 @@ public void testSubmitPacket_thresholdReached() throws Exception {
String dstContractAddress = "hxjuiod";
byte[] data = new byte[] { 0x01, 0x02 };

aggregator.invoke(adminAc, "setSignatureThreshold", 2);

byte[] dataHash = Context.hash("sha-256", data);

byte[] signAdmin = admin.sign(dataHash);
Expand Down Expand Up @@ -296,6 +314,8 @@ public void testSubmitPacket_duplicate() throws Exception {
String dstContractAddress = "hxjuiod";
byte[] data = new byte[] { 0x01, 0x02 };

aggregator.invoke(adminAc, "setSignatureThreshold", 2);

byte[] dataHash = Context.hash("sha-256", data);
byte[] sign = relayerOne.sign(dataHash);

Expand Down

0 comments on commit 08508f5

Please sign in to comment.