-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: add support for dynamic configurables #3702
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
Draft
petertonysmith94
wants to merge
29
commits into
master
Choose a base branch
from
ps/feat/add-support-for-dynamic-configurables
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
6a0c413
chore: update version
petertonysmith94 cc2afe9
chore: updated templates
petertonysmith94 30d9b6e
chore: update types
petertonysmith94 b31a523
chore: added dynamic configurable test for predicates
petertonysmith94 4017959
chore: enable backwards compatible ABI
petertonysmith94 7d8c7a6
chore: added method to Interface to getCoder by concreteTypeId
petertonysmith94 e00ef7e
feat: added function for reading configurables
petertonysmith94 babab58
feat: added writer for direct configurables
petertonysmith94 749e5e1
feat: added configurables encapulation
petertonysmith94 97e1f67
feat: use configurables with predicate
petertonysmith94 71dc830
chore: shuffle around configurable wrapper
petertonysmith94 f1c7c9e
feat: implement contract configurables
petertonysmith94 89b2c89
feat: implement script configurables
petertonysmith94 38c6ce6
chore: added `isBytecodeLoader`
petertonysmith94 f7d0798
chore: refactor the loader instruction set
petertonysmith94 ea690e8
feat: added method to extract the correct data offset and blob ID
petertonysmith94 63e50ac
feat: use the correct data offset for configurables
petertonysmith94 105b0c7
chore: reintroduce configurable constant error handling
petertonysmith94 712e61d
WIP changes
petertonysmith94 b845d63
Merge branch 'master' of github.com:FuelLabs/fuels-ts into ps/feat/ad…
petertonysmith94 901f529
Removed sway patch
petertonysmith94 ca2a9d5
Updated typegen templates
petertonysmith94 bac6e16
Skip reverts
petertonysmith94 5454174
Fixing test cases
petertonysmith94 c3a1632
Moving tests to correct place
petertonysmith94 557f941
Added the ability to get all configurables from bytecode
petertonysmith94 4d7a490
lintfix
petertonysmith94 38dc85a
Removed empty test
petertonysmith94 819aa35
Use correct configurable constants
petertonysmith94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.66.6 | ||
git:xunilrj/dynamic-types-configurables |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,7 @@ export function getNewAbiTypegen( | |
typeArguments: null, | ||
}, | ||
offset: 120, | ||
indirect: false, | ||
}, | ||
]; | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { BigNumberCoder } from '@fuel-ts/abi-coder'; | ||
import type { InputValue, Interface } from '@fuel-ts/abi-coder'; | ||
import type { Configurable } from '@fuel-ts/abi-coder/dist/types/JsonAbiNew'; | ||
import { ErrorCode, FuelError } from '@fuel-ts/errors'; | ||
|
||
import { getBytecodeDataOffset } from '../utils/predicate-script-loader-instructions'; | ||
|
||
export const createConfigurables = (opts: { bytecode: Uint8Array; abi: Interface }) => { | ||
const { abi } = opts; | ||
let bytecode = new Uint8Array(opts.bytecode); | ||
const configurables = Object.values(abi.configurables); | ||
const bytecodeDataOffset = getBytecodeDataOffset(bytecode); | ||
const dynamicOffsetCoder = new BigNumberCoder('u64'); | ||
|
||
const getConfigurable = (name: string) => { | ||
const configurable = configurables.find((conf) => conf.name === name); | ||
if (!configurable) { | ||
throw new FuelError( | ||
ErrorCode.CONFIGURABLE_NOT_FOUND, | ||
`A configurable with the '${name}' was not found in the ABI.` | ||
); | ||
} | ||
return configurable; | ||
}; | ||
|
||
const readIndirectOffset = ({ offset }: Pick<Configurable, 'offset'>) => { | ||
const [dynamicOffsetBn] = dynamicOffsetCoder.decode(bytecode, offset); | ||
const dynamicOffset = bytecodeDataOffset + dynamicOffsetBn.toNumber(); | ||
return dynamicOffset; | ||
}; | ||
|
||
/** | ||
* Readers | ||
*/ | ||
const readDirect = ({ name, concreteTypeId, offset }: Configurable) => { | ||
const coder = abi.getCoder(concreteTypeId); | ||
const [value] = coder.decode(bytecode, offset); | ||
return { name, value }; | ||
}; | ||
|
||
const readIndirect = ({ name, concreteTypeId, offset }: Configurable) => { | ||
const dynamicOffset = readIndirectOffset({ offset }); | ||
const coder = abi.getCoder(concreteTypeId); | ||
const [value] = coder.decode(bytecode, dynamicOffset); | ||
return { name, value }; | ||
}; | ||
|
||
const read = (configurable: Configurable) => { | ||
const reader = configurable.indirect ? readIndirect : readDirect; | ||
return reader(configurable); | ||
}; | ||
|
||
/** | ||
* Writers | ||
*/ | ||
const writeDirect = ({ name, offset }: Configurable, value: InputValue) => { | ||
const encodedValue = abi.encodeConfigurable(name, value); | ||
bytecode.set(encodedValue, offset); | ||
}; | ||
|
||
const writeIndirect = ({ concreteTypeId, offset }: Configurable, value: InputValue) => { | ||
const dynamicOffset = readIndirectOffset({ offset }); | ||
|
||
// Read the original value | ||
const coder = abi.getCoder(concreteTypeId); | ||
const [, originalOffset] = coder.decode(bytecode, dynamicOffset); | ||
const originalLength = originalOffset - dynamicOffset; | ||
|
||
// Encode the new value | ||
const encodedValue = coder.encode(value); | ||
const newLength = encodedValue.length; | ||
|
||
// Update the bytecode | ||
bytecode = new Uint8Array([ | ||
...bytecode.slice(0, dynamicOffset), | ||
...encodedValue, | ||
...bytecode.slice(dynamicOffset + originalLength), | ||
]); | ||
|
||
const additionalOffset = newLength - originalLength; | ||
|
||
// Update the other dynamic configurable offsets | ||
configurables | ||
.filter((configurable) => configurable.indirect && configurable.offset > offset) | ||
.forEach((configurable) => { | ||
const newDynamicOffset = readIndirectOffset({ offset: configurable.offset }); | ||
const newOffset = newDynamicOffset + additionalOffset - bytecodeDataOffset; | ||
|
||
const encodedOffset = dynamicOffsetCoder.encode(newOffset); | ||
bytecode.set(encodedOffset, configurable.offset); | ||
}); | ||
}; | ||
|
||
const write = (configurable: Configurable, value: InputValue) => { | ||
const writer = configurable.indirect ? writeIndirect : writeDirect; | ||
return writer(configurable, value); | ||
}; | ||
|
||
return { | ||
/** | ||
* Reads the value of a configurable. | ||
* | ||
* @param name - The name of the configurable to read. | ||
* @returns The value of the configurable. | ||
*/ | ||
read: (name: string) => read(getConfigurable(name)), | ||
/** | ||
* Reads all the configurables. | ||
* | ||
* @returns An array of all the configurables. | ||
*/ | ||
all: () => configurables.map(read), | ||
/** | ||
* Updates the bytecode with the new configurable values. | ||
* | ||
* @param configurableValues - The new configurable values to set. | ||
* @returns The mutated bytecode. | ||
*/ | ||
set: (configurableValues: { [name: string]: unknown }) => { | ||
// TODO: add assertions for no configurables | ||
|
||
configurables | ||
.sort((a, b) => b.offset - a.offset) | ||
.filter((configurable) => Object.hasOwn(configurableValues, configurable.name)) | ||
.forEach((configurable) => { | ||
const value = configurableValues[configurable.name]; | ||
write(configurable, value as InputValue); | ||
}); | ||
|
||
return bytecode; | ||
}, | ||
}; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO