-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2454 from rsksmart/juraj/eth_call_stableGasPrice_…
…multi_factory feat(smgp): implements eth_call exchange rate provider
- Loading branch information
Showing
10 changed files
with
266 additions
and
20 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
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
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
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
40 changes: 40 additions & 0 deletions
40
rskj-core/src/test/java/co/rsk/config/mining/OnChainMinGasPriceSystemConfigTest.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,40 @@ | ||
package co.rsk.config.mining; | ||
|
||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigFactory; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class OnChainMinGasPriceSystemConfigTest { | ||
private OnChainMinGasPriceSystemConfig config; | ||
private String address = "0x77045E71a7A2c50903d88e564cD72fab11e82051"; | ||
private String from = "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"; | ||
private String data = "0x98d5fdca"; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
Config testConfig = ConfigFactory.parseString( | ||
"address=\"" + address + "\"\n" + | ||
"from=\"" + from + "\"\n" + | ||
"data=\"" + data + "\"" | ||
); | ||
config = new OnChainMinGasPriceSystemConfig(testConfig); | ||
} | ||
|
||
@Test | ||
void testAddress() { | ||
assertEquals(address, config.address()); | ||
} | ||
|
||
@Test | ||
void testFrom() { | ||
assertEquals(from, config.from()); | ||
} | ||
|
||
@Test | ||
void testData() { | ||
assertEquals(data, config.data()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,4 +64,4 @@ public Long getStableMinGasPrice() { | |
return 1L; | ||
} | ||
} | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
rskj-core/src/test/java/co/rsk/mine/gas/provider/onchain/OnChainMinGasPriceProviderTest.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 @@ | ||
package co.rsk.mine.gas.provider.onchain; | ||
|
||
import co.rsk.config.mining.OnChainMinGasPriceSystemConfig; | ||
import co.rsk.mine.gas.provider.MinGasPriceProvider; | ||
import co.rsk.mine.gas.provider.MinGasPriceProviderType; | ||
import co.rsk.util.HexUtils; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import co.rsk.rpc.modules.eth.EthModule; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.NullSource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class OnChainMinGasPriceProviderTest { | ||
private final String oracle_address = "0xbffBD993FF1d229B0FfE55668F2009d20d4F7C5f"; | ||
private final String from_address = "0xbffBD993FF1d229B0FfE55668F2009d20d4F7C5f"; | ||
private final String data = "0x"; | ||
private final long fallback_minGasPrice_fake = 1234567890L; | ||
|
||
private EthModule ethModule_mock; | ||
private MinGasPriceProvider fallback_mock; | ||
private OnChainMinGasPriceSystemConfig onChainMinGasPriceSystemConfig_mock; | ||
|
||
private OnChainMinGasPriceProvider onChainMinGasPriceProvider; | ||
|
||
@BeforeEach | ||
public void beforeEach() { | ||
ethModule_mock = mock(EthModule.class); | ||
when(ethModule_mock.chainId()).thenReturn("0x21"); | ||
|
||
fallback_mock = mock(MinGasPriceProvider.class); | ||
when(fallback_mock.getType()).thenReturn(MinGasPriceProviderType.FIXED); | ||
when(fallback_mock.getMinGasPrice()).thenReturn(fallback_minGasPrice_fake); | ||
|
||
onChainMinGasPriceSystemConfig_mock = mock(OnChainMinGasPriceSystemConfig.class); | ||
when(onChainMinGasPriceSystemConfig_mock.address()).thenReturn(oracle_address); | ||
when(onChainMinGasPriceSystemConfig_mock.from()).thenReturn(from_address); | ||
when(onChainMinGasPriceSystemConfig_mock.data()).thenReturn(data); | ||
|
||
|
||
onChainMinGasPriceProvider = new OnChainMinGasPriceProvider( | ||
fallback_mock, | ||
onChainMinGasPriceSystemConfig_mock, | ||
() -> ethModule_mock | ||
); | ||
} | ||
|
||
@AfterEach | ||
public void afterEach() { | ||
ethModule_mock = null; | ||
fallback_mock = null; | ||
} | ||
|
||
|
||
|
||
@ParameterizedTest | ||
@NullSource | ||
@ValueSource(strings = {"0x123", "0xabc"}) | ||
void constructorSetsFieldsCorrectly(String data_input) { | ||
MinGasPriceProvider fallbackProvider = mock(MinGasPriceProvider.class); | ||
OnChainMinGasPriceSystemConfig config = mock(OnChainMinGasPriceSystemConfig.class); | ||
|
||
when(config.address()).thenReturn("0xaddress"); | ||
when(config.from()).thenReturn("0xfrom"); | ||
when(config.data()).thenReturn(data_input); | ||
|
||
OnChainMinGasPriceProvider provider = new OnChainMinGasPriceProvider(fallbackProvider, config, () -> ethModule_mock); | ||
|
||
Assertions.assertEquals("0xaddress", provider.getToAddress()); | ||
} | ||
|
||
@Test | ||
void constructorSetsFieldsToNullWhenConfigReturnsNull() { | ||
MinGasPriceProvider fallbackProvider = mock(MinGasPriceProvider.class); | ||
OnChainMinGasPriceSystemConfig config = mock(OnChainMinGasPriceSystemConfig.class); | ||
|
||
when(config.address()).thenReturn(null); | ||
when(config.from()).thenReturn(null); | ||
when(config.data()).thenReturn(null); | ||
|
||
OnChainMinGasPriceProvider provider = new OnChainMinGasPriceProvider(fallbackProvider, config, () -> ethModule_mock); | ||
|
||
Assertions.assertNull(provider.getToAddress()); | ||
Assertions.assertNull(provider.getFromAddress()); | ||
Assertions.assertNull(provider.getData()); | ||
} | ||
|
||
@Test | ||
void getStableMinGasPrice_callsEthModulesCallMethod() { | ||
String expectedPrice = "0x21"; | ||
when(ethModule_mock.call(any(), any())).thenReturn(expectedPrice); | ||
|
||
Assertions.assertEquals( | ||
HexUtils.jsonHexToLong(expectedPrice), | ||
onChainMinGasPriceProvider.getStableMinGasPrice() | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@NullSource | ||
@ValueSource(strings = {"", "0x"}) | ||
void getStableMinGasPrice_callsFallback_whenNoData(String data_input) { | ||
when(onChainMinGasPriceSystemConfig_mock.data()).thenReturn(data_input); | ||
|
||
Assertions.assertEquals( | ||
fallback_minGasPrice_fake, | ||
onChainMinGasPriceProvider.getStableMinGasPrice(), | ||
"For " + data_input + ": " | ||
); | ||
} | ||
|
||
|
||
@Test | ||
void getStableMinGasPrice_callsFallback_whenEthModuleIsNull() { | ||
Assertions.assertEquals( | ||
fallback_minGasPrice_fake, | ||
onChainMinGasPriceProvider.getStableMinGasPrice() | ||
); | ||
} | ||
|
||
@Test | ||
void getType_returnsOnChain() { | ||
Assertions.assertEquals(MinGasPriceProviderType.ON_CHAIN, onChainMinGasPriceProvider.getType()); | ||
} | ||
|
||
} |