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!: remove offset from decodeLog and decodeFunctionResult methods #2514

Closed
wants to merge 16 commits into from

Conversation

nedsalk
Copy link
Contributor

@nedsalk nedsalk commented Jun 13, 2024

Interface.decodeLog and Interface.decodeFunctionResult are top-level functions that don't need to return a data offset because you pass in all the data and the returned offset is always the length of that data. If somebody wants to get the offset value after this change, they can just call data.length.

const theInterface = new Interface(...);

// previously
const [decodedLog, offset] = theInterface.decodeLog(data, logId);
// now
const decodedLog = theInterface.decodeLog(data, logId);

// previously
const [decodedFnResult, offset] = theInterface.decodeFunctionResult(fnName, data);
// now
const decodedFnResult = theInterface.decodeFunctionResult(fnName, data);

I tagged it as a feat because it improves the DX of the methods.

Supersedes #2308

@fuel-service-user
Copy link
Contributor

fuel-service-user commented Jun 13, 2024

✨ A PR has been created under the rc-2514 tag on fuels-wallet repo.
FuelLabs/fuels-wallet#1374

@arboleya arboleya added this to the 0.x post-launch milestone Jun 13, 2024
Copy link
Contributor

@petertonysmith94 petertonysmith94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we highlight the breaking changes in the description please?

@petertonysmith94 petertonysmith94 dismissed their stale review June 14, 2024 11:33

Wrong checkbox

Copy link
Contributor

Coverage Report:

Lines Branches Functions Statements
45.59%(+0%) 38.88%(+0%) 42.23%(+0%) 45.37%(+0%)
Changed Files:

Coverage values did not change👌.

Copy link
Member

@arboleya arboleya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there downsides to removing the offset property?

Can someone be looking for the offset in the future?

@nedsalk
Copy link
Contributor Author

nedsalk commented Jun 14, 2024

@arboleya the offset value is equal to data.length so people can always get it that way. I updated the description to be more explicit about this.

@nedsalk nedsalk requested a review from arboleya June 14, 2024 13:54
Copy link
Member

@arboleya arboleya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pattern

Could we be breaking the patterns already in use?

let decoded;
let o = offset;
[decoded, o] = new BigNumberCoder('u64').decode(data, o);
const scriptGasLimit = decoded;
[decoded, o] = new B256Coder().decode(data, o);
const receiptsRoot = decoded;
[decoded, o] = new BigNumberCoder('u64').decode(data, o);
const scriptLength = decoded;
[decoded, o] = new BigNumberCoder('u64').decode(data, o);
const scriptDataLength = decoded;
[decoded, o] = new NumberCoder('u32', { padToWordSize: true }).decode(data, o);
const policyTypes = decoded;
[decoded, o] = new NumberCoder('u16', { padToWordSize: true }).decode(data, o);
const inputsCount = decoded;
[decoded, o] = new NumberCoder('u16', { padToWordSize: true }).decode(data, o);
const outputsCount = decoded;
[decoded, o] = new NumberCoder('u16', { padToWordSize: true }).decode(data, o);
const witnessesCount = decoded;
[decoded, o] = new ByteArrayCoder(scriptLength.toNumber()).decode(data, o);
const script = decoded;
[decoded, o] = new ByteArrayCoder(scriptDataLength.toNumber()).decode(data, o);
const scriptData = decoded;
[decoded, o] = new PoliciesCoder().decode(data, o, policyTypes);
const policies = decoded;
[decoded, o] = new ArrayCoder(new InputCoder(), inputsCount).decode(data, o);
const inputs = decoded;
[decoded, o] = new ArrayCoder(new OutputCoder(), outputsCount).decode(data, o);
const outputs = decoded;
[decoded, o] = new ArrayCoder(new WitnessCoder(), witnessesCount).decode(data, o);
const witnesses = decoded;

Exceptions

Also, it seems some Coders have non-obvious ways of computing the offset:

return [toHex(bytes, 32), offset + 32];

let newOffset = offsetAndLength;
const chunks = [];
for (let i = 0; i < length; i++) {
const [decoded, optionOffset] = this.coder.decode(data, newOffset);
chunks.push(decoded);
newOffset = optionOffset;
}

API Preferences

Regarding personal preference, I'd go with Objects instead of Arrays.

But only if we'd change the pattern everywhere, which is not the case.

const { decodedLog, offset } = theInterface.decodeLog(data, logId);
// vs
const [decodedLog, offset] = theInterface.decodeLog(data, logId);

Conclusion

I'm not sure if the breaking change is worth it.

The change is subtle, but the latter hides information.

const [decodedLog] = theInterface.decodeLog(data, logId);
// vs
const decodedLog = theInterface.decodeLog(data, logId);

As for if this is a good or a bad thing, I'll let others chime in.

@nedsalk
Copy link
Contributor Author

nedsalk commented Jun 17, 2024

@arboleya you're right, introducing this would break the pattern present in other coders; I didn't think of that. If I remember correctly, there was talk on the offsite around not needing the offset value across the board on all our coders and that this would be considered in #1795.

The argument for merging it right now is that this is a high level API relative to the coders and we can get the DX benefits of it right now because the offset value is never needed when using these methods. The counter-argument is that API consistency is more important and that it should be done in tandem with #1795.

In the end, I agree with your uncertainty

I'm not sure if the breaking change is worth it.

and I'll close this PR. I've added a comment on #1795 linking to this PR.

@nedsalk nedsalk closed this Jun 17, 2024
@nedsalk nedsalk deleted the ns/fix/remove-offset-from-decode-functions branch June 17, 2024 09:38
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.

None yet

6 participants