-
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
240 additions
and
14 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
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
/* | ||
* 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.*; | ||
|
||
public class TraceOptions { | ||
|
||
private Set<String> disabledFields; | ||
private Set<String> unsupportedOptions; | ||
|
||
private TraceOptions() {} | ||
|
||
public TraceOptions(Map<String, String> traceOptions) { | ||
disabledFields = new HashSet<>(); | ||
unsupportedOptions = new HashSet<>(); | ||
if (traceOptions != null) { | ||
if (traceOptions.containsKey("disableStorage")) { | ||
if (Boolean.parseBoolean(traceOptions.get("disableStorage"))) { | ||
disabledFields.add("storage"); | ||
} | ||
traceOptions.remove("disableStorage"); | ||
} | ||
if (traceOptions.containsKey("disableMemory")) { | ||
if (Boolean.parseBoolean(traceOptions.get("disableMemory"))) { | ||
disabledFields.add("memory"); | ||
} | ||
traceOptions.remove("disableMemory"); | ||
} | ||
if (traceOptions.containsKey("disableStack")) { | ||
if (Boolean.parseBoolean(traceOptions.get("disableStack"))) { | ||
disabledFields.add("stack"); | ||
} | ||
traceOptions.remove("disableStack"); | ||
} | ||
unsupportedOptions = traceOptions.keySet(); | ||
} | ||
} | ||
|
||
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
132 changes: 132 additions & 0 deletions
132
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,132 @@ | ||
/* | ||
* 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_allFieldsSetAsDisabled_disabledFieldsShouldReturnAllFields() { | ||
// 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(3, options.getDisabledFields().size()); | ||
Assert.assertTrue(options.getDisabledFields().contains("storage")); | ||
Assert.assertTrue(options.getDisabledFields().contains("memory")); | ||
Assert.assertTrue(options.getDisabledFields().contains("stack")); | ||
} | ||
|
||
@Test | ||
public void testTraceOptions_anyFieldsSetAsDisabled_disabledFieldsShouldReturnEmptySet() { | ||
// 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.assertEquals(0, options.getDisabledFields().size()); | ||
Assert.assertFalse(options.getDisabledFields().contains("storage")); | ||
Assert.assertFalse(options.getDisabledFields().contains("memory")); | ||
Assert.assertFalse(options.getDisabledFields().contains("stack")); | ||
} | ||
|
||
@Test | ||
public void testTraceOptions_someFieldsSetAsDisabled_disabledFieldsShouldReturnDisabledOnes() { | ||
// Given | ||
Map<String, String> traceOptions = new HashMap<>(); | ||
traceOptions.put("disableStorage", "true"); | ||
traceOptions.put("disableMemory", "false"); | ||
traceOptions.put("disableStack", "true"); | ||
|
||
// When | ||
TraceOptions options = new TraceOptions(traceOptions); | ||
|
||
// Then | ||
Assert.assertEquals(2, options.getDisabledFields().size()); | ||
Assert.assertTrue(options.getDisabledFields().contains("storage")); | ||
Assert.assertFalse(options.getDisabledFields().contains("memory")); | ||
Assert.assertTrue(options.getDisabledFields().contains("stack")); | ||
} | ||
|
||
@Test | ||
public void testTraceOptions_nullTraceOptionsGiven_disabledFieldsAndUnsupportedOptionsShouldReturnEmptySet() { | ||
// When | ||
TraceOptions options = new TraceOptions(null); | ||
|
||
// Then | ||
Assert.assertEquals(0, options.getDisabledFields().size()); | ||
Assert.assertEquals(0, options.getUnsupportedOptions().size()); | ||
} | ||
|
||
@Test | ||
public void testTraceOptions_unsupportedOptionsGiven_unsupportedOptionsShouldReturnAllOfThem() { | ||
// Given | ||
Map<String, String> traceOptions = new HashMap<>(); | ||
traceOptions.put("unsupportedOption.1", "1"); | ||
traceOptions.put("unsupportedOption.2", null); | ||
|
||
// When | ||
TraceOptions options = new TraceOptions(traceOptions); | ||
|
||
// Then | ||
Assert.assertEquals(2, options.getUnsupportedOptions().size()); | ||
Assert.assertTrue(options.getUnsupportedOptions().contains("unsupportedOption.1")); | ||
Assert.assertTrue(options.getUnsupportedOptions().contains("unsupportedOption.2")); | ||
} | ||
|
||
@Test | ||
public void testTraceOptions_mixOfSupportedAndUnsupportedOptionsGiven_disabledFieldsAndUnsupportedOptionsShouldReturnOK() { | ||
// Given | ||
Map<String, String> traceOptions = new HashMap<>(); | ||
traceOptions.put("disableMemory", "true"); | ||
traceOptions.put("disableStorage", "True"); // True != true but should also work | ||
traceOptions.put("unsupportedOption.1", "1"); | ||
traceOptions.put("unsupportedOption.2", null); | ||
|
||
// When | ||
TraceOptions options = new TraceOptions(traceOptions); | ||
|
||
// Then | ||
Assert.assertEquals(2, options.getDisabledFields().size()); | ||
Assert.assertTrue(options.getDisabledFields().contains("storage")); | ||
Assert.assertTrue(options.getDisabledFields().contains("memory")); | ||
|
||
Assert.assertEquals(2, options.getUnsupportedOptions().size()); | ||
Assert.assertTrue(options.getUnsupportedOptions().contains("unsupportedOption.1")); | ||
Assert.assertTrue(options.getUnsupportedOptions().contains("unsupportedOption.2")); | ||
} | ||
|
||
} |