-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
207 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
rskj-core/src/main/java/co/rsk/rpc/modules/debug/TraceOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2017 RSK Labs Ltd. | ||
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package co.rsk.rpc.modules.debug; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class TraceOptions { | ||
|
||
private static final String[] SUPPORTED_OPTIONS = { | ||
"disableStorage", "disableMemory", "disableStack"}; | ||
private static final String[] OPTIONAL_FIELDS = { | ||
"disableStorage", "disableMemory", "disableStack"}; | ||
|
||
private Set<String> disabledFields; | ||
private Set<String> unsupportedOptions; | ||
|
||
private TraceOptions() {} | ||
|
||
public TraceOptions(Map<String, String> traceOptions) { | ||
disabledFields = Arrays | ||
.stream(OPTIONAL_FIELDS) | ||
.filter(field -> traceOptions | ||
.containsKey(field) && Boolean.parseBoolean(traceOptions.get(field))) | ||
.collect(Collectors.toSet()); | ||
unsupportedOptions = traceOptions | ||
.keySet() | ||
.stream() | ||
.filter(key -> Arrays.stream(SUPPORTED_OPTIONS) | ||
.noneMatch(option -> option.equals(key))) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
public Set<String> getDisabledFields() { | ||
return disabledFields; | ||
} | ||
|
||
public Set<String> getUnsupportedOptions() { | ||
return unsupportedOptions; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
rskj-core/src/test/java/co/rsk/rpc/modules/debug/TraceOptionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2017 RSK Labs Ltd. | ||
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package co.rsk.rpc.modules.debug; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class TraceOptionsTest { | ||
|
||
@Test | ||
public void testTraceOptions_constructedOnlyWithSupportedOptions_unsupportedOptionsMustBeEmpty() { | ||
// Given | ||
Map<String, String> traceOptions = new HashMap<>(); | ||
traceOptions.put("disableStorage", "false"); | ||
traceOptions.put("disableMemory", "false"); | ||
traceOptions.put("disableStack", "false"); | ||
|
||
// When | ||
TraceOptions options = new TraceOptions(traceOptions); | ||
|
||
// Then | ||
Assert.assertEquals(0, options.getUnsupportedOptions().size()); | ||
} | ||
|
||
@Test | ||
public void testTraceOptions_constructedWithSomeUnsupportedOptions_unsupportedOptionsMustContainTheUnsupportedOptions() { | ||
// Given | ||
Map<String, String> traceOptions = new HashMap<>(); | ||
traceOptions.put("disableStorages", "false"); | ||
traceOptions.put("disablesMemory", "false"); | ||
traceOptions.put("disableStack", "false"); | ||
|
||
// When | ||
TraceOptions options = new TraceOptions(traceOptions); | ||
|
||
// Then | ||
Assert.assertTrue(options.getUnsupportedOptions().contains("disableStorages")); | ||
Assert.assertTrue(options.getUnsupportedOptions().contains("disablesMemory")); | ||
} | ||
|
||
@Test | ||
public void testTraceOptions_optionalFieldsSetToTrue_disabledFieldsMustContainGivenFields() { | ||
// Given | ||
Map<String, String> traceOptions = new HashMap<>(); | ||
traceOptions.put("disableStorage", "true"); | ||
traceOptions.put("disableMemory", "true"); | ||
traceOptions.put("disableStack", "true"); | ||
|
||
// When | ||
TraceOptions options = new TraceOptions(traceOptions); | ||
|
||
// Then | ||
Assert.assertEquals(0, options.getUnsupportedOptions().size()); | ||
Assert.assertTrue(options.getDisabledFields().contains("disableStorage")); | ||
Assert.assertTrue(options.getDisabledFields().contains("disableMemory")); | ||
Assert.assertTrue(options.getDisabledFields().contains("disableStack")); | ||
} | ||
|
||
@Test | ||
public void testTraceOptions_optionalFieldsSetToFalse_disabledFieldsMustNotContainGivenFields() { | ||
// Given | ||
Map<String, String> traceOptions = new HashMap<>(); | ||
traceOptions.put("disableStorage", "false"); | ||
traceOptions.put("disableMemory", "false"); | ||
traceOptions.put("disableStack", "false"); | ||
|
||
// When | ||
TraceOptions options = new TraceOptions(traceOptions); | ||
|
||
// Then | ||
Assert.assertEquals(0, options.getUnsupportedOptions().size()); | ||
Assert.assertFalse(options.getDisabledFields().contains("disableStorages")); | ||
Assert.assertFalse(options.getDisabledFields().contains("disableMemory")); | ||
Assert.assertFalse(options.getDisabledFields().contains("disableStack")); | ||
} | ||
|
||
@Test | ||
public void testTraceOptions_mixedCase_OK() { | ||
// Given | ||
Map<String, String> traceOptions = new HashMap<>(); | ||
traceOptions.put("disableStorage", "false"); // Valid optional field set to false (Must not be added to the disabled fields). | ||
traceOptions.put("disableStack", "true"); // Valid optional field set to true (Must be added to the disabled fields). | ||
traceOptions.put("unsupported_1", "false"); // Unsupported field (Must be added to the unsupported options). | ||
traceOptions.put("unsupported_2", "4"); // Unsupported field (Must be added to the unsupported options). | ||
|
||
// When | ||
TraceOptions options = new TraceOptions(traceOptions); | ||
|
||
// Then | ||
Assert.assertFalse(options.getDisabledFields().contains("disableStorage")); | ||
Assert.assertTrue(options.getDisabledFields().contains("disableStack")); | ||
Assert.assertTrue(options.getUnsupportedOptions().contains("unsupported_1")); | ||
Assert.assertTrue(options.getUnsupportedOptions().contains("unsupported_2")); | ||
} | ||
|
||
} |