Skip to content

Commit

Permalink
Have util package subsume rlp.util and abi.util; (#61)
Browse files Browse the repository at this point in the history
* remove FloatingPoint.putFloat and putDouble

* move FloatingPoint to util from rlp.util

* move abi.util classes to util
  • Loading branch information
esaulpaugh authored Jan 19, 2024
1 parent 1cf3ea4 commit 8981724
Show file tree
Hide file tree
Showing 27 changed files with 40 additions and 59 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/esaulpaugh/headlong/abi/ABIJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
import java.util.List;
import java.util.Set;

import static com.esaulpaugh.headlong.abi.util.JsonUtils.getArray;
import static com.esaulpaugh.headlong.abi.util.JsonUtils.getBoolean;
import static com.esaulpaugh.headlong.abi.util.JsonUtils.getString;
import static com.esaulpaugh.headlong.abi.util.JsonUtils.parseArray;
import static com.esaulpaugh.headlong.util.JsonUtils.getArray;
import static com.esaulpaugh.headlong.util.JsonUtils.getBoolean;
import static com.esaulpaugh.headlong.util.JsonUtils.getString;
import static com.esaulpaugh.headlong.util.JsonUtils.parseArray;

/** For parsing JSON representations of {@link ABIObject}s according to the ABI specification. */
public final class ABIJSON {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/esaulpaugh/headlong/abi/ABIObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.abi.util.JsonUtils;
import com.esaulpaugh.headlong.util.JsonUtils;
import com.google.gson.JsonObject;

/** Supertype of json-encodeable types {@link Function}, {@link Event}, and {@link ContractError}.*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.abi.util.JsonUtils;
import com.esaulpaugh.headlong.util.JsonUtils;
import com.google.gson.JsonObject;

import java.util.Objects;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/esaulpaugh/headlong/abi/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.abi.util.JsonUtils;
import com.esaulpaugh.headlong.util.JsonUtils;
import com.esaulpaugh.headlong.util.FastHex;
import com.esaulpaugh.headlong.util.Strings;
import com.google.gson.JsonObject;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/esaulpaugh/headlong/abi/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.abi.util.JsonUtils;
import com.esaulpaugh.headlong.util.JsonUtils;
import com.esaulpaugh.headlong.util.Integers;
import com.esaulpaugh.headlong.util.Strings;
import com.google.gson.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.abi.util.Uint;
import com.esaulpaugh.headlong.util.Uint;
import com.esaulpaugh.headlong.util.Integers;

import java.lang.reflect.Array;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/esaulpaugh/headlong/rlp/RLPItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.esaulpaugh.headlong.rlp;

import com.esaulpaugh.headlong.rlp.util.FloatingPoint;
import com.esaulpaugh.headlong.util.FloatingPoint;
import com.esaulpaugh.headlong.util.Integers;
import com.esaulpaugh.headlong.util.Strings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.esaulpaugh.headlong.rlp.util;
package com.esaulpaugh.headlong.util;

import com.esaulpaugh.headlong.util.Integers;

Expand All @@ -28,10 +28,6 @@ public static float getFloat(byte[] bytes, int i, int len, boolean lenient) {
return Float.intBitsToFloat(Integers.getInt(bytes, i, len, lenient));
}

public static int putFloat(float val, byte[] bytes, int i) {
return Integers.putLong(Float.floatToIntBits(val), bytes, i);
}

public static byte[] toBytes(float val) {
return Integers.toBytes(Float.floatToIntBits(val));
}
Expand All @@ -42,10 +38,6 @@ public static double getDouble(byte[] bytes, int i, int len, boolean lenient) {
return Double.longBitsToDouble(Integers.getLong(bytes, i, len, lenient));
}

public static int putDouble(double val, byte[] bytes, int i) {
return Integers.putLong(Double.doubleToLongBits(val), bytes, i);
}

public static byte[] toBytes(double val) {
return Integers.toBytes(Double.doubleToLongBits(val));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.esaulpaugh.headlong.abi.util;
package com.esaulpaugh.headlong.util;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.esaulpaugh.headlong.abi.util;
package com.esaulpaugh.headlong.util;

import com.esaulpaugh.headlong.util.Integers;

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.abi.util.JsonUtils;
import com.esaulpaugh.headlong.util.JsonUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.abi.util.Deserializer;
import com.esaulpaugh.headlong.util.Deserializer;
import com.esaulpaugh.headlong.util.FastHex;
import com.esaulpaugh.headlong.abi.util.JsonUtils;
import com.esaulpaugh.headlong.util.JsonUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.abi.util.JsonUtils;
import com.esaulpaugh.headlong.util.JsonUtils;
import com.esaulpaugh.headlong.util.Strings;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/esaulpaugh/headlong/abi/EncodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.abi.util.Uint;
import com.esaulpaugh.headlong.abi.util.WrappedKeccak;
import com.esaulpaugh.headlong.util.Uint;
import com.esaulpaugh.headlong.util.WrappedKeccak;
import com.esaulpaugh.headlong.util.Strings;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/esaulpaugh/headlong/abi/EqualsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.abi.util.WrappedKeccak;
import com.esaulpaugh.headlong.util.WrappedKeccak;
import com.esaulpaugh.headlong.util.FastHex;
import com.esaulpaugh.headlong.util.Strings;
import com.joemelsha.crypto.hash.Keccak;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.abi.util.WrappedKeccak;
import com.esaulpaugh.headlong.util.WrappedKeccak;
import com.esaulpaugh.headlong.util.Strings;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.esaulpaugh.headlong.abi;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.abi.util.BizarroInts;
import com.esaulpaugh.headlong.abi.util.Uint;
import com.esaulpaugh.headlong.util.BizarroInts;
import com.esaulpaugh.headlong.util.Uint;
import com.esaulpaugh.headlong.util.FastHex;
import com.esaulpaugh.headlong.util.Strings;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.esaulpaugh.headlong.rlp;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.rlp.util.FloatingPoint;
import com.esaulpaugh.headlong.util.FloatingPoint;
import com.esaulpaugh.headlong.util.Integers;
import com.esaulpaugh.headlong.util.Strings;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.esaulpaugh.headlong.rlp;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.abi.util.JsonUtils;
import com.esaulpaugh.headlong.util.JsonUtils;
import com.esaulpaugh.headlong.util.FastHex;
import com.esaulpaugh.headlong.util.Integers;
import com.esaulpaugh.headlong.util.Strings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.esaulpaugh.headlong.rlp.RLPEncoder;
import com.esaulpaugh.headlong.rlp.RLPItem;
import com.esaulpaugh.headlong.rlp.util.FloatingPoint;
import com.esaulpaugh.headlong.util.FloatingPoint;
import com.esaulpaugh.headlong.util.Integers;
import com.esaulpaugh.headlong.util.Strings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.esaulpaugh.headlong.abi.util;
package com.esaulpaugh.headlong.util;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.util.IntegersTest;
import com.esaulpaugh.headlong.util.Strings;
import org.junit.jupiter.api.Test;

import java.util.Random;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.esaulpaugh.headlong.abi.util;
package com.esaulpaugh.headlong.util;

import com.esaulpaugh.headlong.util.Integers;
import com.esaulpaugh.headlong.util.Strings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.esaulpaugh.headlong.abi.util;
package com.esaulpaugh.headlong.util;

import com.esaulpaugh.headlong.abi.ABIType;
import com.esaulpaugh.headlong.abi.Address;
Expand All @@ -23,6 +23,8 @@
import com.esaulpaugh.headlong.abi.Tuple;
import com.esaulpaugh.headlong.abi.TupleType;
import com.esaulpaugh.headlong.util.FastHex;
import com.esaulpaugh.headlong.util.JsonUtils;
import com.esaulpaugh.headlong.util.Uint;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.esaulpaugh.headlong.rlp.util;
package com.esaulpaugh.headlong.util;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.util.Integers;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Random;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class FloatingPointTest {
Expand All @@ -33,13 +31,8 @@ public void testFloat() {
Random r = TestUtils.seededRandom();
for (int i = 0; i < 20; i++) {
final float flo = r.nextFloat();
byte[] floBytes = FloatingPoint.toBytes(flo);
byte[] floPutted = new byte[floBytes.length];
int len = FloatingPoint.putFloat(flo, floPutted, 0);
assertEquals(floBytes.length, len);
assertArrayEquals(floBytes, floPutted);

float floGotten = FloatingPoint.getFloat(floBytes, 0, floBytes.length, false);
final byte[] floBytes = FloatingPoint.toBytes(flo);
final float floGotten = FloatingPoint.getFloat(floBytes, 0, floBytes.length, false);
assertEquals(flo, floGotten);
}
}
Expand All @@ -49,13 +42,8 @@ public void testDouble() {
Random r = TestUtils.seededRandom();
for (int i = 0; i < 20; i++) {
final double dub = r.nextDouble();
byte[] dubBytes = FloatingPoint.toBytes(dub);
byte[] dubPutted = new byte[dubBytes.length];
int len = FloatingPoint.putDouble(dub, dubPutted, 0);
assertEquals(dubBytes.length, len);
assertArrayEquals(dubBytes, dubPutted);

double dubGotten = FloatingPoint.getDouble(dubBytes, 0, dubBytes.length, false);
final byte[] dubBytes = FloatingPoint.toBytes(dub);
final double dubGotten = FloatingPoint.getDouble(dubBytes, 0, dubBytes.length, false);
assertEquals(dub, dubGotten);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.esaulpaugh.headlong.abi.util;
package com.esaulpaugh.headlong.util;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.abi.Tuple;
import com.esaulpaugh.headlong.abi.TupleType;
import com.esaulpaugh.headlong.util.Uint;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.esaulpaugh.headlong.abi.util;
package com.esaulpaugh.headlong.util;

import org.bouncycastle.jcajce.provider.digest.Keccak.DigestKeccak;

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/joemelsha/crypto/hash/KeccakTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.joemelsha.crypto.hash;

import com.esaulpaugh.headlong.TestUtils;
import com.esaulpaugh.headlong.abi.util.WrappedKeccak;
import com.esaulpaugh.headlong.util.WrappedKeccak;
import com.esaulpaugh.headlong.util.Integers;
import com.esaulpaugh.headlong.util.Strings;
import org.junit.jupiter.api.Disabled;
Expand Down

0 comments on commit 8981724

Please sign in to comment.