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

Add ability to modify and retrieve the resource footprint #680

Merged
merged 5 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/sorobandata_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ export class SorobanDataBuilder {
constructor(sorobanData) {
let data;

if (typeof sorobanData === 'string') {
data = SorobanDataBuilder.fromXDR(sorobanData, 'base64');
} else if (ArrayBuffer.isView(sorobanData)) {
data = SorobanDataBuilder.fromXDR(sorobanData, 'raw');
if (typeof sorobanData === 'string' || ArrayBuffer.isView(sorobanData)) {
data = SorobanDataBuilder.fromXDR(sorobanData);
} else if (!sorobanData) {
data = new xdr.SorobanTransactionData({
resources: new xdr.SorobanResources({
Expand Down Expand Up @@ -102,22 +100,37 @@ export class SorobanDataBuilder {
return this;
}

/**
* Appends the given ledger keys to the existing storage access footprint.
* @param {xdr.LedgerKey[]} readOnly read-only keys to add
* @param {xdr.LedgerKey[]} readWrite read-write keys to add
* @returns {SorobanDataBuilder} this builder instance
*/
appendFootprint(readOnly, readWrite) {

Choose a reason for hiding this comment

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

is it worth here to check if the readOnly/readWrite are actually LedgerKeys?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nah, we pretty much assume sanity in many, many places in this codebase. the TypeScript should do a good enough job enforcing this

return this.setFootprint(
this.getReadOnly().concat(readOnly),
this.getReadWrite().concat(readWrite)
);
}

/**
* Sets the storage access footprint to be a certain set of ledger keys.
*
* You can also set each field explicitly via
* {@link SorobanDataBuilder.setReadOnly} and
* {@link SorobanDataBuilder.setReadWrite}.
* {@link SorobanDataBuilder.setReadWrite} or add to the existing footprint
* via {@link SorobanDataBuilder.appendFootprint}.
*
* Passing `null|undefined` to either parameter will IGNORE the existing
* values. If you want to clear them, pass `[]`, instead.
*
* @param {xdr.LedgerKey[]|null} [readOnly] the set of ledger keys to set in
* the read-only portion of the transaction's `sorobanData`
* the read-only portion of the transaction's `sorobanData`, or `null |
* undefined` to keep the existing keys
* @param {xdr.LedgerKey[]|null} [readWrite] the set of ledger keys to set in
* the read-write portion of the transaction's `sorobanData`
*
* @returns {SorobanDataBuilder}
* the read-write portion of the transaction's `sorobanData`, or `null |
* undefined` to keep the existing keys
* @returns {SorobanDataBuilder} this builder instance
*/
setFootprint(readOnly, readWrite) {
if (readOnly !== null) {
Expand Down Expand Up @@ -160,4 +173,18 @@ export class SorobanDataBuilder {
build() {
return xdr.SorobanTransactionData.fromXDR(this._data.toXDR()); // clone
}

//
// getters follow
//

/** @returns {xdr.LedgerKey[]} the read-only storage access pattern */
getReadOnly() {
return this._data.resources().footprint().readOnly();
}

/** @returns {xdr.LedgerKey[]} the read-write storage access pattern */
getReadWrite() {
return this._data.resources().footprint().readWrite();
}
}
14 changes: 14 additions & 0 deletions test/unit/sorobandata_builder_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ describe('SorobanTransactionData can be built', function () {
expect(data2.resources().footprint().readWrite()).to.eql([]);
});

it('appends footprints', function () {
const builder = new dataBuilder();

const data = builder
.setFootprint([key], [key])
.appendFootprint([key, key], []);
const built = data.build();

expect(data.getReadOnly()).to.eql([key, key, key]);
expect(data.getReadWrite()).to.eql([key]);
expect(built.resources().footprint().readOnly()).to.eql([key, key, key]);
expect(built.resources().footprint().readWrite()).to.eql([key]);
});

it('makes copies on build()', function () {
const builder = new dataBuilder();
const first = builder.build();
Expand Down
14 changes: 11 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,8 @@ export function humanizeEvents(
): SorobanEvent[];

export class SorobanDataBuilder {
constructor(data?: string | xdr.SorobanTransactionData);
constructor(data?: string | Uint8Array | Buffer | xdr.SorobanTransactionData);
static fromXDR(data: Uint8Array | Buffer | string): SorobanDataBuilder;

setRefundableFee(fee: IntLike): SorobanDataBuilder;
setResources(
Expand All @@ -1153,9 +1154,16 @@ export class SorobanDataBuilder {
readOnly?: xdr.LedgerKey[] | null,
readWrite?: xdr.LedgerKey[] | null
): SorobanDataBuilder;
appendFootprint(
readOnly: xdr.LedgerKey[],
readWrite: xdr.LedgerKey[]
): SorobanDataBuilder;

setReadOnly(keys: xdr.LedgerKey[]): SorobanDataBuilder;
setReadWrite(keys: xdr.LedgerKey[]): SorobanDataBuilder;

setReadOnly(keys?: xdr.LedgerKey[]): SorobanDataBuilder;
setReadWrite(keys?: xdr.LedgerKey[]): SorobanDataBuilder;
getReadOnly(): xdr.LedgerKey[];
getReadWrite(): xdr.LedgerKey[];

build(): xdr.SorobanTransactionData;
}
Expand Down
Loading