Skip to content

Commit

Permalink
eth/abi: fix negative integer *coding (#279)
Browse files Browse the repository at this point in the history
* eth/abi: fix negative integer *coding

* uncomment test cases :)
  • Loading branch information
q9f authored Jun 23, 2024
1 parent a89936e commit c80d7f9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/eth/abi/encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions spec/eth/abi/encoder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion spec/eth/abi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 6 additions & 7 deletions spec/eth/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c80d7f9

Please sign in to comment.