From 08508f562d07c76316a986334b7403e9a49851c0 Mon Sep 17 00:00:00 2001 From: sherpalden Date: Mon, 7 Oct 2024 17:46:27 +0545 Subject: [PATCH] fix: set default threshold to 1 && rf: relayers add and remove --- .../relay/aggregator/RelayAggregator.java | 47 +++++++++++-------- .../relay/aggregator/RelayAggregatorTest.java | 24 +++++++++- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/contracts/javascore/aggregator/src/main/java/relay/aggregator/RelayAggregator.java b/contracts/javascore/aggregator/src/main/java/relay/aggregator/RelayAggregator.java index 12203c70..f4f41542 100644 --- a/contracts/javascore/aggregator/src/main/java/relay/aggregator/RelayAggregator.java +++ b/contracts/javascore/aggregator/src/main/java/relay/aggregator/RelayAggregator.java @@ -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 signatureThreshold = Context.newVarDB("signatureThreshold", Integer.class); @@ -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); } @@ -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); } } } @@ -107,22 +113,9 @@ public void removeRelayers(Address[] relayersToBeRemoved) { Context.require(relayersToBeRemoved != null && relayersToBeRemoved.length != 0, "relayers to be removed cannot be empty"); - HashMap existingRelayers = new HashMap(); - 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); } } @@ -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++) { diff --git a/contracts/javascore/aggregator/src/test/java/relay/aggregator/RelayAggregatorTest.java b/contracts/javascore/aggregator/src/test/java/relay/aggregator/RelayAggregatorTest.java index f06fae8d..d44e84a2 100644 --- a/contracts/javascore/aggregator/src/test/java/relay/aggregator/RelayAggregatorTest.java +++ b/contracts/javascore/aggregator/src/test/java/relay/aggregator/RelayAggregatorTest.java @@ -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; @@ -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; @@ -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 @@ -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); @@ -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); @@ -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); @@ -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);