Skip to content

Commit

Permalink
Add MockVM nameservice methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mvandeberg committed Jul 31, 2024
1 parent 39e66d9 commit 9ac9994
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 78 deletions.
8 changes: 8 additions & 0 deletions __tests__/mockVM.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,12 @@ describe('MockVM', () => {
MockVM.setEntryPoint(2);
expect(System.getArguments().entry_point).toBe(2);
});

it("should set contract address", () => {
MockVM.setContractName(mockAccount, "foobar");
expect(System.getContractName(mockAccount)).toBe("foobar");

MockVM.setContractAddress("foobar", mockAccount);
expect(Arrays.equal(System.getContractAddress("foobar"), mockAccount)).toBe(true)
})
});
44 changes: 43 additions & 1 deletion assembly/util/mockVM.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Protobuf } from "as-proto";
import { System } from "../systemCalls";
import { system_calls, chain, protocol, authority, value } from '@koinos/proto-as';
import { system_calls, chain, protocol, authority, value, name_service } from '@koinos/proto-as';
import { StringBytes } from "./stringBytes";


export namespace MockVM {
export const METADATA_SPACE = new chain.object_space(true);
export const CONTRACT_NAME_SPACE = new chain.object_space(true, new Uint8Array(0), 1);
export const CONTRACT_ADDRESS_SPACE = new chain.object_space(true, new Uint8Array(0), 2);

export class MockAuthority {
autorization_type: authority.authorization_type;
Expand Down Expand Up @@ -476,6 +478,46 @@ export namespace MockVM {
return events;
}

/**
* Set contract name for contract at address
* @param address The contract address
* @param name The desired contract name
* @example
* ```ts
* MockVM.setContractName(contractAddress, "foobar");
*
* System.get_contract_name(contractAddress); // returns "foobar";
* ```
*/
export function setContractName(address: Uint8Array, name: string): void {
System.putObject(
CONTRACT_NAME_SPACE,
address,
new name_service.name_record(name),
name_service.name_record.encode
);
}

/**
* Set contract address for contract with name
* @param name The contract name
* @param address The desired contract address
* @example
* ```ts
* MockVM.setContractAddress("foobar", contractAddress);
*
* System.get_contract_address("foobar"); // returns 'contract_address';
* ```
*/
export function setContractAddress(name: string, address: Uint8Array): void {
System.putObject(
CONTRACT_ADDRESS_SPACE,
StringBytes.stringToBytes(name),
new name_service.address_record(address),
name_service.address_record.encode
);
}

/**
* Reset the MockVM database
* @example
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@koinos/abi-proto-gen": "1.0.0",
"@koinos/as-gen": "1.0.0",
"@koinos/as-proto-gen": "1.0.0",
"@koinos/mock-vm": "1.1.0",
"@koinos/mock-vm": "https://github.com/koinos/koinos-mock-vm#22-nameservice",
"@koinos/proto-as": "2.2.0",
"@roamin/protoc": "^2.4.0",
"as-bignum": "^0.2.18",
Expand Down
Loading

0 comments on commit 9ac9994

Please sign in to comment.