Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ABI coder #3402

Open
wants to merge 14 commits into
base: ns/feat/abi-parser
Choose a base branch
from
Open

Conversation

petertonysmith94
Copy link
Contributor

@petertonysmith94 petertonysmith94 commented Nov 18, 2024

Summary

  • This PR encompasses the refactoring and revamping of the encoding.
  • The following diagram outlines all the different encoding types and how the encoded types look in bytes.

image

Breaking Changes

Note

These will need to be combined into the final PR (#3085)

  • Accessing underlying coders is now achieved through a convenient class, AbiEncoding.
// Before
import { NumberCoder, BigNumberCoder, B256Coder, B512Coder, VoidCoder, BooleanCoder, RawSliceCoder, StrSliceCoder, StdStringCoder } from '@fuel-ts/abi-coder';

const u8Coder = new NumberCoder('u8');
const u16Coder = new NumberCoder('u16');
const u32Coder = new NumberCoder('u32');
const u64Coder = new BigNumberCoder('u64');
const u256Coder = new BigNumberCoder('u256');
const b256Coder = new B256Coder();
const b512Coder = new B512Coder();
const boolCoder = new BooleanCoder();
const voidCoder = new VoidCoder();

const byteCoder = new ByteCoder();
const rawSliceCoder = new RawSliceCoder();
const strSliceCoder = new StrSliceCoder();
const stdStringCoder = new StdStringCoder();
const stringCoder = new StringCoder(4);

const arrayCoder = new ArrayCoder(u8Coder, 3);
const tupleCoder = new TupleCoder([u8Coder, u8Coder]);
const vectorCoder = new VecCoder(u8Coder);

const enumCoder = new EnumCoder('name of enum', { u8: u8Coder });
const optionCoder = new OptionCoder('name of option', { None: voidCoder, Some: voidCoder });
const structCoder = new StructCoder('name of struct', { u8: u8Coder });
// After
import { encoding } from '@fuel-ts/abi';

const u8Coder = encoding.u8;
const u16Coder = encoding.u16;
const u32Coder = encoding.u32;
const u64Coder = encoding.u64;
const u256Coder = encoding.u256;
const b256Coder = encoding.b256;
const b512Coder = encoding.b512;
const boolCoder = encoding.bool;
const voidCoder = encoding.void;

const byteCoder = encoding.byte;
const rawSliceCoder = encoding.rawSlice;
const strSliceCoder = encoding.str;
const stdStringCoder = encoding.stdString;
const stringCoder = encoding.string(4);

const arrayCoder = encoding.array(u8Coder, 3);
const tupleCoder = encoding.tuple([u8Coder, u8Coder]);
const vectorCoder = encoding.vector(u8Coder);

const enumCoder = encoding.enum({ u8: u8Coder });
const optionCoder = encoding.option({ None: voidCoder, Some: voidCoder });
const structCoder = encoding.struct({ u8: u8Coder });
  • Removal of the constant INPUT_COIN_FIXED_SIZE
import { INPUT_COIN_FIXED_SIZE } from 'fuels';
  • Renamed Encoding from the @fuel-ts/crypto package to BufferEncoding.
// Before
import { Encoding } from '@fuel-ts/crypto';

// After
import { BufferEncoding } from '@fuel-ts/crypto';

In progress

Ref

Todo tests

  • types_struct_with_complex_nested_struct
  • types_struct_with_array

Document

  • createFunctionSelector what is the selector used for and how is it used?
  • Breakdown the specification + parsing of the ABI to allow for easier to consume encoding + decoding.
    • What is a concrete and metadata type, what are the differences?
    • How does the ABI specification concretely tell us how to encode/decode a type?
    • General flow of the abi package.

Refactor:

  • Separate logic of the native and enum coders.
  • Separate the fromAbi functionationlity away from the coders.
  • Abstract the assertions outside of the coders.
  • Remove utils from the AbiEncoding class.
  • Remove static accessors for coders; this is not tree-shakable.

Checklist

  • All changes are covered by tests (or not applicable)
  • All changes are documented (or not applicable)
  • I reviewed the entire PR myself (preferably, on GH UI)
  • I described all Breaking Changes (or there's none)

@petertonysmith94 petertonysmith94 added the feat Issue is a feature label Nov 18, 2024
@petertonysmith94 petertonysmith94 self-assigned this Nov 18, 2024
Copy link

vercel bot commented Nov 18, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
fuels-template ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 2:16pm
ts-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 2:16pm
ts-docs-api 🛑 Canceled (Inspect) Dec 17, 2024 2:16pm

Copy link

codspeed-hq bot commented Nov 25, 2024

CodSpeed Performance Report

Merging #3402 will degrade performances by 14.08%

Comparing ps/feat/abi-coder (3a6b861) with ns/feat/abi-parser (b6c5936)

Summary

⚡ 1 improvements
❌ 3 regressions
✅ 14 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark ns/feat/abi-parser ps/feat/abi-coder Change
should successfully get transaction cost estimate for a batch transfer (x20 times) 24.6 ms 28.7 ms -14.08%
should successfully get transaction cost estimate for a mint (x20 times) 21.9 ms 24.8 ms -11.61%
should successfully get transaction cost estimate for a single transfer (x20 times) 18.3 ms 21.1 ms -13.47%
should successfully conduct a custom transfer between wallets (x20 times) 96.9 ms 54.4 ms +78.02%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Abi - Refactor / Coder
6 participants