Skip to content

Commit

Permalink
fix int encoding, handle negative and positive case
Browse files Browse the repository at this point in the history
  • Loading branch information
colindickson committed Dec 13, 2023
1 parent ecd06f3 commit 62a71ed
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 14 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.9.7](https://github.com/streamingfast/substreams-ethereum/releases/tag/v0.9.6)
## [0.9.8](https://github.com/streamingfast/substreams-ethereum/releases/tag/v0.9.8)
* Fix bug where Int was not encoded properly in ABI generator

## [0.9.7](https://github.com/streamingfast/substreams-ethereum/releases/tag/v0.9.7)
* Blocks with `DetailLevel` set to `Base` now have transaction receipt information. Transaction successfulness is now supported.

## [0.9.6](https://github.com/streamingfast/substreams-ethereum/releases/tag/v0.9.6)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [
]

[workspace.package]
version = "0.9.7"
version = "0.9.8"
edition = "2021"
description = "Substreams development kit for Ethereum chains, contains Firehose Block model and helpers as well as utilities for Ethereum ABI encoding/decoding."
homepage = "https://substreams.streamingfast.io/"
Expand Down
90 changes: 81 additions & 9 deletions abigen-tests/src/abi/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,15 @@
ethabi::Token::FixedBytes(self.param3.as_ref().to_vec()),
{
let non_full_signed_bytes = self.param4.to_signed_bytes_be();
let mut full_signed_bytes = [255i32 as u8; 32];
let full_signed_bytes_init = if non_full_signed_bytes[0]
& 0x80 == 0x80
{
0xff
} else {
0x00
};
let mut full_signed_bytes = [full_signed_bytes_init
as u8; 32];
non_full_signed_bytes
.into_iter()
.rev()
Expand Down Expand Up @@ -698,7 +706,15 @@
&[
{
let non_full_signed_bytes = self.arg0.to_signed_bytes_be();
let mut full_signed_bytes = [0i32 as u8; 32];
let full_signed_bytes_init = if non_full_signed_bytes[0]
& 0x80 == 0x80
{
0xff
} else {
0x00
};
let mut full_signed_bytes = [full_signed_bytes_init
as u8; 32];
non_full_signed_bytes
.into_iter()
.rev()
Expand Down Expand Up @@ -773,7 +789,15 @@
&[
{
let non_full_signed_bytes = self.param0.to_signed_bytes_be();
let mut full_signed_bytes = [255i32 as u8; 32];
let full_signed_bytes_init = if non_full_signed_bytes[0]
& 0x80 == 0x80
{
0xff
} else {
0x00
};
let mut full_signed_bytes = [full_signed_bytes_init
as u8; 32];
non_full_signed_bytes
.into_iter()
.rev()
Expand Down Expand Up @@ -848,7 +872,15 @@
&[
{
let non_full_signed_bytes = self.param0.to_signed_bytes_be();
let mut full_signed_bytes = [255i32 as u8; 32];
let full_signed_bytes_init = if non_full_signed_bytes[0]
& 0x80 == 0x80
{
0xff
} else {
0x00
};
let mut full_signed_bytes = [full_signed_bytes_init
as u8; 32];
non_full_signed_bytes
.into_iter()
.rev()
Expand Down Expand Up @@ -923,7 +955,15 @@
&[
{
let non_full_signed_bytes = self.param0.to_signed_bytes_be();
let mut full_signed_bytes = [255i32 as u8; 32];
let full_signed_bytes_init = if non_full_signed_bytes[0]
& 0x80 == 0x80
{
0xff
} else {
0x00
};
let mut full_signed_bytes = [full_signed_bytes_init
as u8; 32];
non_full_signed_bytes
.into_iter()
.rev()
Expand Down Expand Up @@ -1036,7 +1076,15 @@
&[
{
let non_full_signed_bytes = self.param0.to_signed_bytes_be();
let mut full_signed_bytes = [255i32 as u8; 32];
let full_signed_bytes_init = if non_full_signed_bytes[0]
& 0x80 == 0x80
{
0xff
} else {
0x00
};
let mut full_signed_bytes = [full_signed_bytes_init
as u8; 32];
non_full_signed_bytes
.into_iter()
.rev()
Expand All @@ -1048,7 +1096,15 @@
},
{
let non_full_signed_bytes = self.param1.to_signed_bytes_be();
let mut full_signed_bytes = [255i32 as u8; 32];
let full_signed_bytes_init = if non_full_signed_bytes[0]
& 0x80 == 0x80
{
0xff
} else {
0x00
};
let mut full_signed_bytes = [full_signed_bytes_init
as u8; 32];
non_full_signed_bytes
.into_iter()
.rev()
Expand All @@ -1060,7 +1116,15 @@
},
{
let non_full_signed_bytes = self.param2.to_signed_bytes_be();
let mut full_signed_bytes = [255i32 as u8; 32];
let full_signed_bytes_init = if non_full_signed_bytes[0]
& 0x80 == 0x80
{
0xff
} else {
0x00
};
let mut full_signed_bytes = [full_signed_bytes_init
as u8; 32];
non_full_signed_bytes
.into_iter()
.rev()
Expand All @@ -1072,7 +1136,15 @@
},
{
let non_full_signed_bytes = self.param3.to_signed_bytes_be();
let mut full_signed_bytes = [255i32 as u8; 32];
let full_signed_bytes_init = if non_full_signed_bytes[0]
& 0x80 == 0x80
{
0xff
} else {
0x00
};
let mut full_signed_bytes = [full_signed_bytes_init
as u8; 32];
non_full_signed_bytes
.into_iter()
.rev()
Expand Down
8 changes: 5 additions & 3 deletions abigen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,14 @@ fn to_token(name: &proc_macro2::TokenStream, kind: &ParamType) -> proc_macro2::T
}
ParamType::Bytes => quote! { ethabi::Token::Bytes(#name.clone()) },
ParamType::FixedBytes(_) => quote! { ethabi::Token::FixedBytes(#name.as_ref().to_vec()) },
ParamType::Int(size) => {
let full_signed_bytes_init = if size == 128 { 0x00 } else { 0xff };
ParamType::Int(_) => {
// The check non_full_signed_bytes[0] & 0x80 == 0x80 is checking if the leftmost bit of the first byte is set.
// If it is, the number is negative and full_signed_bytes_init is set to 0xff. Otherwise, it's set to 0x00.
quote! {
{
let non_full_signed_bytes = #name.to_signed_bytes_be();
let mut full_signed_bytes = [#full_signed_bytes_init as u8; 32];
let full_signed_bytes_init = if non_full_signed_bytes[0] & 0x80 == 0x80 { 0xff } else { 0x00 };
let mut full_signed_bytes = [full_signed_bytes_init as u8; 32];
non_full_signed_bytes.into_iter().rev().enumerate().for_each(|(i, byte)| full_signed_bytes[31 - i] = byte);

ethabi::Token::Int(ethabi::Int::from_big_endian(full_signed_bytes.as_ref()))
Expand Down

0 comments on commit 62a71ed

Please sign in to comment.