Skip to content

Commit

Permalink
Merge pull request #38 from icon-project/bgkwon-BDT-829
Browse files Browse the repository at this point in the history
Add SetFeeTable event log in bmc
  • Loading branch information
kwon528 authored Dec 6, 2023
2 parents f52acad + 6a72f0d commit 0027797
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import java.util.List;
import java.util.Map;

import static foundation.icon.score.util.Encode.encode;

public class BTPMessageCenter implements BMC, ICONSpecific, OwnerManager {
private static final Logger logger = Logger.getLogger(BTPMessageCenter.class);

Expand Down Expand Up @@ -359,6 +361,7 @@ public void setFeeTable(String[] _dst, BigInteger[][] _value) {
fees.remove(dstNet);
}
}
SetFeeTable(encode(_dst), encode(_value));
}

private BigInteger[] getFeeList(String net, boolean includeBackward) {
Expand Down Expand Up @@ -1189,4 +1192,8 @@ private void requireNormalMode() {
@EventLog
public void RelayMessage(String _prev, BigInteger _count) {
}

@EventLog
public void SetFeeTable(byte[] _dst, byte[] _value) {
}
}
12 changes: 12 additions & 0 deletions bmc/src/main/java/foundation/icon/btp/bmc/ICONSpecific.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,16 @@ public interface ICONSpecific {
*/
@EventLog
void RelayMessage(String _prev, BigInteger _count);

/**
* (EventLog) Logs the event that handle the fee table
* The tracker monitors this event.
* <p>
* indexed: 0
*
* @param _dst String[] ( Rlp encoded of list of BTP Network Address of the destination BMC )
* @param _value Integer[][] ( Rlp encoded of lists of relay fees in the path including return path )
*/
@EventLog
void SetFeeTable(byte[] _dst, byte[] _value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,12 @@ static Consumer<TransactionResult> relayMessageEvent(
ICONSpecificScoreClient.RelayMessage::eventLogs,
consumer, null);
}

static Consumer<TransactionResult> setFeeTableEvent(
Consumer<ICONSpecificScoreClient.SetFeeTable> consumer) {
return eventLogChecker(
ICONSpecificScoreClient.SetFeeTable::eventLogs,
consumer, null);
}

}
21 changes: 20 additions & 1 deletion bmc/src/test/java/foundation/icon/btp/bmc/FeeManagementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@
import foundation.icon.btp.test.BTPIntegrationTest;
import foundation.icon.btp.test.MockBMVIntegrationTest;
import foundation.icon.jsonrpc.Address;
import foundation.icon.jsonrpc.model.TransactionResult;
import foundation.icon.score.test.ScoreIntegrationTest;
import foundation.icon.score.util.ArrayUtil;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import score.ByteArrayObjectWriter;
import score.Context;
import score.ObjectReader;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import static foundation.icon.score.util.Encode.encode;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -108,8 +114,10 @@ static void setFeeTable(List<FeeInfo> fees) {
BigInteger[][] _value = fees.stream()
.map(FeeInfo::getValues)
.toArray(BigInteger[][]::new);
bmc.setFeeTable(_dst, _value);


Consumer<TransactionResult> checker = setFeeTableEventChecker(_dst, _value);
bmc.setFeeTable(checker, _dst, _value);
BigInteger[][] ret = bmc.getFeeTable(_dst);
assertArrayEquals(_value, ret);
}
Expand Down Expand Up @@ -151,4 +159,15 @@ void getFeeShouldReturnsSumOfFee() {
assertEquals(ArrayUtil.sum(forward(reachableFee.getValues())), bmc.getFee(reachable.net(), false));
assertEquals(ArrayUtil.sum(reachableFee.getValues()), bmc.getFee(reachable.net(), true));
}

static Consumer<TransactionResult> setFeeTableEventChecker(
String[] _dst, BigInteger[][] _value) {

return BMCIntegrationTest.setFeeTableEvent(
(el) -> {
assertArrayEquals(encode(_dst), el.get_dst());
assertArrayEquals(encode(_value), el.get_value());
});
}

}
43 changes: 43 additions & 0 deletions lib/src/main/java/foundation/icon/score/util/Encode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package foundation.icon.score.util;

import score.ByteArrayObjectWriter;
import score.Context;

import java.math.BigInteger;

public class Encode {
public static byte[] encode(BigInteger[] arr) {
ByteArrayObjectWriter w = Context.newByteArrayObjectWriter("RLPn");
w.beginList(arr.length);
for (BigInteger v : arr) {
w.write(v);
}
w.end();
return w.toByteArray();
}

public static byte[] encode(BigInteger[][] twoDimArr) {
ByteArrayObjectWriter w = Context.newByteArrayObjectWriter("RLPn");
w.beginList(twoDimArr.length);
for (BigInteger[] arr : twoDimArr) {
w.beginList(arr.length);
for (BigInteger v : arr) {
w.write(v);
}
w.end();
}
w.end();
return w.toByteArray();
}

public static byte[] encode(String[] arr) {
ByteArrayObjectWriter w = Context.newByteArrayObjectWriter("RLPn");
w.beginList(arr.length);
for (String v : arr) {
w.write(v);
}
w.end();
return w.toByteArray();
}

}

0 comments on commit 0027797

Please sign in to comment.