Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SetFeeTable event log in bmc #38

Merged
merged 1 commit into from
Dec 6, 2023
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
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 )
Comment on lines +170 to +171
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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 )
* @param _dst byte[] ( Rlp encoded of list of BTP Network Address of the destination BMC )
* @param _value byte[] ( 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();
}

}