From c80d7f933daa92b6fa705b4ec0a989e32fc7da35 Mon Sep 17 00:00:00 2001 From: Afri <58883403+q9f@users.noreply.github.com> Date: Sun, 23 Jun 2024 22:53:10 +0200 Subject: [PATCH] eth/abi: fix negative integer *coding (#279) * eth/abi: fix negative integer *coding * uncomment test cases :) --- lib/eth/abi/encoder.rb | 2 +- spec/eth/abi/encoder_spec.rb | 4 ++-- spec/eth/abi_spec.rb | 18 +++++++++++++++++- spec/eth/client_spec.rb | 13 ++++++------- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/eth/abi/encoder.rb b/lib/eth/abi/encoder.rb index 4b4247ec..f2ee06d8 100644 --- a/lib/eth/abi/encoder.rb +++ b/lib/eth/abi/encoder.rb @@ -138,7 +138,7 @@ def int(arg, type) real_size = type.sub_type.to_i i = arg.to_i raise ValueOutOfBounds, arg unless i >= -2 ** (real_size - 1) and i < 2 ** (real_size - 1) - Util.zpad_int(i < 0 ? (i + 2 ** 256) : i % 2 ** type.sub_type.to_i) + Util.zpad_int(i % 2 ** 256) end # Properly encodes booleans. diff --git a/spec/eth/abi/encoder_spec.rb b/spec/eth/abi/encoder_spec.rb index 79e68d66..910e0d56 100644 --- a/spec/eth/abi/encoder_spec.rb +++ b/spec/eth/abi/encoder_spec.rb @@ -23,7 +23,7 @@ expect(Abi::Encoder.type t_uint_8, 255).to eq Util.zpad_int 255 expect { Abi::Encoder.type t_uint_8, 256 }.to raise_error Abi::ValueOutOfBounds - expect(Abi::Encoder.type t_int_8, -128).to eq Util.zpad "\x80", 32 + expect(Abi::Encoder.type t_int_8, -128).to eq Util.zpad "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x80", 32 expect(Abi::Encoder.type t_int_8, 127).to eq Util.zpad "\x7f", 32 expect { Abi::Encoder.type t_int_8, -129 }.to raise_error Abi::ValueOutOfBounds expect { Abi::Encoder.type t_int_8, 128 }.to raise_error Abi::ValueOutOfBounds @@ -62,7 +62,7 @@ expect(Abi::Encoder.primitive_type t_uint_8, 255).to eq Util.zpad_int 255 expect { Abi::Encoder.primitive_type t_uint_8, 256 }.to raise_error Abi::ValueOutOfBounds - expect(Abi::Encoder.primitive_type t_int_8, -128).to eq Util.zpad "\x80", 32 + expect(Abi::Encoder.primitive_type t_int_8, -128).to eq Util.zpad "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x80", 32 expect(Abi::Encoder.primitive_type t_int_8, 127).to eq Util.zpad "\x7f", 32 expect { Abi::Encoder.primitive_type t_int_8, -129 }.to raise_error Abi::ValueOutOfBounds expect { Abi::Encoder.primitive_type t_int_8, 128 }.to raise_error Abi::ValueOutOfBounds diff --git a/spec/eth/abi_spec.rb b/spec/eth/abi_spec.rb index 044b2c1a..cf188649 100644 --- a/spec/eth/abi_spec.rb +++ b/spec/eth/abi_spec.rb @@ -387,12 +387,28 @@ def assert(data, types, args) pending("https://github.com/q9f/eth.rb/issues/102") assert(data, types, args) end + end + describe "edge cases" do it "test negative number" do types = ["int24"] args = [-887220] data = Util.hex_to_bin "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2764c" - assert(data, types, args) + expect(data).to eq Abi.encode(types, args) + expect(args).to eq Abi.decode(types, data) + expect(args).to eq Abi.decode(types, Abi.encode(types, args)) + + expect(Abi.encode(["int8"], [0])).to eq Util.hex_to_bin "0000000000000000000000000000000000000000000000000000000000000000" + expect(Abi.encode(["int8"], [1])).to eq Util.hex_to_bin "0000000000000000000000000000000000000000000000000000000000000001" + expect(Abi.encode(["int8"], [-1])).to eq Util.hex_to_bin "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + expect(Abi.encode(["int24"], [887220])).to eq Util.hex_to_bin "00000000000000000000000000000000000000000000000000000000000d89b4" + expect(Abi.encode(["int24"], [-887220])).to eq Util.hex_to_bin "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2764c" + + expect(Abi.decode(["int8"], "0000000000000000000000000000000000000000000000000000000000000000")).to eq [0] + expect(Abi.decode(["int8"], "0000000000000000000000000000000000000000000000000000000000000001")).to eq [1] + expect(Abi.decode(["int8"], "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")).to eq [-1] + expect(Abi.decode(["int24"], "00000000000000000000000000000000000000000000000000000000000d89b4")).to eq [887220] + expect(Abi.decode(["int24"], "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2764c")).to eq [-887220] end end end diff --git a/spec/eth/client_spec.rb b/spec/eth/client_spec.rb index c9a6a2c5..d5fe029d 100644 --- a/spec/eth/client_spec.rb +++ b/spec/eth/client_spec.rb @@ -230,21 +230,20 @@ method: "eth_call", params: [{ data: "0x70a08231000000000000000000000000d496b23d61f88a8c7758fca7560dcfac7b3b01f9", - to: "0xD496b23D61F88A8C7758fca7560dCFac7b3b01F9" + to: "0xD496b23D61F88A8C7758fca7560dCFac7b3b01F9", }, "0x#{block_number.to_s(16)}"], - id: 1 + id: 1, }.to_json mock_response = { jsonrpc: "2.0", id: 1, - result: "0x0000000000000000000000000000000000000000000000000000000000000000" + result: "0x0000000000000000000000000000000000000000000000000000000000000000", } - expect_any_instance_of(Eth::Client::Http) - .to receive(:send_request) - .with(expected_payload) - .and_return(mock_response.to_json) + expect_any_instance_of(Eth::Client::Http).to receive(:send_request) + .with(expected_payload) + .and_return(mock_response.to_json) geth_http.call(erc20_contract, "balanceOf", address) end