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

Name Service #78

Merged
merged 3 commits into from
Aug 5, 2024
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
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@koinos/sdk-as",
"version": "1.2.0",
"version": "1.3.0",
"main": "index.ts",
"license": "MIT",
"author": "Roamin Ro",
Expand All @@ -16,8 +16,9 @@
"@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": "1.2.0",
"@koinos/proto-as": "2.2.0",
"@koinos/sdk-as-cli": "^1.0.2",
"@roamin/protoc": "^2.4.0",
"as-bignum": "^0.2.18",
"as-proto": "npm:@koinos/[email protected]",
Expand Down
Loading
Loading