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: add /vm.m_run message handler #281

Merged
merged 4 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions packages/adena-extension/src/common/utils/client-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export function parseTxsEachDate(txs: object[]) {
txDesc = `pkg: ${cur.pkg_path}`;
} else if (cur.type === '/vm.m_addpkg') {
txFunc = 'AddPkg';
} else if (cur.type === '/vm.m_run') {
txDesc = `Run`;
} else {
txDesc = '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,36 @@ export const validateTrasactionMessageOfAddPkg = (message: { [key in string]: an
}
return true;
};

export const validateTrasactionMessageOfRun = (message: { [key in string]: any }) => {
if (!message.type || !message.value) {
return false;
}
if (message.type !== '/vm.m_run') {
return false;
}
if (typeof message.value !== 'object') {
return false;
}
if (typeof message.value.caller !== 'string') {
return false;
}
if (typeof message.value.send !== 'string') {
return false;
}
if (typeof message.value.package !== 'object') {
return false;
}

const packageValue = message.value.package;
if (typeof packageValue?.Name !== 'string') {
return false;
}
if (typeof packageValue?.Path !== 'string') {
return false;
}
if (!Array.isArray(packageValue?.Files)) {
return false;
}
return true;
};
6 changes: 6 additions & 0 deletions packages/adena-extension/src/inject/executor/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
validateDoContractRequest,
validateTrasactionMessageOfAddPkg,
validateTrasactionMessageOfBankSend,
validateTrasactionMessageOfRun,
validateTrasactionMessageOfVmCall,
} from '@common/validation/validation-message';

Expand Down Expand Up @@ -116,6 +117,11 @@ export class AdenaExecutor {
return InjectionMessageInstance.failure('INVALID_FORMAT');
}
break;
case '/vm.m_run':
if (!validateTrasactionMessageOfRun(message)) {
return InjectionMessageInstance.failure('INVALID_FORMAT');
}
break;
default:
return InjectionMessageInstance.failure('UNSUPPORTED_TYPE');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const validateInjectionAddress = (currentAccountAddress: string) => {
};

export const validateInjectionTransactionType = (requestData: InjectionMessage) => {
const messageTypes = ['/bank.MsgSend', '/vm.m_call', '/vm.m_addpkg'];
const messageTypes = ['/bank.MsgSend', '/vm.m_call', '/vm.m_addpkg', '/vm.m_run'];
return requestData.data?.messages.every((message: any) => messageTypes.includes(message?.type));
};

Expand All @@ -103,6 +103,11 @@ export const validateInjectionTransactionMessage = (
return false;
}
break;
case '/vm.m_run':
if (currentAccountAddress !== message.value.caller) {
return false;
}
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface TxProps {
txType: string;
}

type TxTypeValue = '/bank.MsgSend' | '/vm.m_call' | '/vm.m_addpkg';
type TxTypeValue = '/bank.MsgSend' | '/vm.m_call' | '/vm.m_addpkg' | '/vm.m_run';

export interface ResultTxStateType {
txStatusStyle: { [key in string]: any };
Expand All @@ -48,6 +48,7 @@ const statusSymbol = {
'/bank.MsgSend': gnot,
'/vm.m_call': contract,
'/vm.m_addpkg': addpkg,
'/vm.m_run': gnot,
};

const useStatus = () => {
Expand Down Expand Up @@ -91,6 +92,13 @@ const useStatus = () => {
txSend: 'AddPkg',
};
}
if ((state.txType as TxTypeValue) === '/vm.m_run') {
result = {
...result,
txTypeDesc: 'Run',
txSend: 'Run',
};
}
return result;
};

Expand Down
16 changes: 16 additions & 0 deletions packages/adena-extension/src/services/transaction/message/vm/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,19 @@ export const createMessageOfVmCall = (info: {
},
};
};

export const createMessageOfVmRun = (info: {
caller: string;
send: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
packageInfo: any;
}) => {
return {
type: '/vm.m_run',
value: {
caller: info.caller,
send: info.send,
package: info.packageInfo
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export interface VmAddPackage extends InfoType {
deposit: string;
}

export interface VmRun extends InfoType {
caller: string;
send: string;
package?: Package;
}

export const encodeVmCall = (writer: protobuf.Writer, messageInfo: VmCall) => {
if (messageInfo.caller !== '') {
writer.uint32(10).string(messageInfo.caller);
Expand Down Expand Up @@ -80,3 +86,16 @@ export const encodeVmAddPackage = (writer: protobuf.Writer, messageInfo: VmAddPa
}
return writer;
};

export const encodeVmRun = (writer: protobuf.Writer, messageInfo: VmAddPackage) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't it be messageInfo: VmRun ??

if (messageInfo.caller !== '') {
writer.uint32(10).string(messageInfo.caller);
}
if (messageInfo.send !== '') {
writer.uint32(18).string(messageInfo.send);
}
if (messageInfo.package !== undefined) {
encodePackage(writer.uint32(18).fork(), messageInfo.package).ldelim();
Copy link
Member

Choose a reason for hiding this comment

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

This simply seems to be the problem.
Change the size of the buffer in the 3rd argument.
18 -> 26

Copy link
Contributor

Choose a reason for hiding this comment

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

@luciorubeens seems like protobuf has certain buf size value for nth args.
https://www.tabnine.com/code/javascript/functions/protobufjs/Writer/string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perfect, thank you guys, now it works

CleanShot 2023-10-20 at 10 00 54@2x

}
return writer;
};
5 changes: 5 additions & 0 deletions packages/adena-module/src/transaction/transaction-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
encodeBankSend,
encodeVmAddPackage,
encodeVmCall,
encodeVmRun,
VmAddPackage,
VmCall,
} from './message-info';
Expand Down Expand Up @@ -112,6 +113,10 @@ export class TransactionBuilder {
messageInfo = message.value as VmAddPackage;
encodeVmAddPackage(writer, messageInfo);
break;
case '/vm.m_run':
messageInfo = message.value as VmAddPackage;
encodeVmRun(writer, messageInfo);
break;
default:
throw new Error('Not Found Transaction Message Type');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/adena-module/src/transaction/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import protobuf from 'protobufjs/minimal';
import { StdSignDoc } from '..';
import { TransactionEncode } from './transaction-encode';

export type MessageTypeUrl = '/vm.m_call' | '/vm.m_addpkg' | '/bank.MsgSend';
export type MessageTypeUrl = '/vm.m_call' | '/vm.m_addpkg' | '/vm.m_run' | '/bank.MsgSend';

export interface TransactionMessage {
type: MessageTypeUrl;
Expand Down