Skip to content

Commit

Permalink
vm-mock: add colored error message
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Feb 29, 2024
1 parent 4ffdaf0 commit ac3c14e
Showing 1 changed file with 38 additions and 48 deletions.
86 changes: 38 additions & 48 deletions vm-mock/vm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable new-cap */

const { createHash } = await import('node:crypto');
import { SigningKey, hashMessage } from 'ethers';
import sha3 from 'js-sha3';
Expand All @@ -12,6 +14,17 @@ let contractAddress = 'AS12BqZEQ6sByhRLyEuf0YbQmcF2PsDdkNNG1akBJu9XcjZA1eT';

let mockedOriginOpId = '';

/**
* log error and throw
*
* @param {string} msg error message
*/
function ERROR(msg) {
console.log('\x1b[31m%s\x1b[0m','Vm-mock error: ' + msg);
throw new Error(msg);
}


/**
* return a random string
*
Expand Down Expand Up @@ -67,7 +80,6 @@ let callStack = callerAddress + ' , ' + contractAddress;
*/
let ledger;
let adminContext = false;
let deployContext = false;

/**
* Reset the ledger
Expand Down Expand Up @@ -268,12 +280,10 @@ export default function createMockedABI(
if (addressStorage.has(key)) {
return newArrayBuffer(addressStorage.get(key));
} else {
throw new Error('Runtime error: data entry not found');
ERROR('data entry not found');
}
} else {
throw new Error(
'Runtime error: address parsing error: ' + contractAddress,
);
ERROR(`Address ${contractAddress} does not exist in ledger.`);
}
},

Expand Down Expand Up @@ -317,10 +327,10 @@ export default function createMockedABI(
if (addressStorage.has(key)) {
return newArrayBuffer(addressStorage.get(key));
} else {
throw new Error('Runtime error: data entry not found');
ERROR('data entry not found');
}
} else {
throw new Error('Runtime error: address parsing error: ' + a);
ERROR(`Address ${a} does not exist in ledger.`);
}
},

Expand All @@ -346,13 +356,11 @@ export default function createMockedABI(
if (ledger.has(contractAddress)) {
const addressStorage = ledger.get(contractAddress).storage;
if (!addressStorage.has(key)) {
throw new Error('Runtime error: data entry not found');
ERROR('data entry not found');
}
addressStorage.delete(key);
} else {
throw new Error(
'Runtime error: address parsing error: ' + contractAddress,
);
ERROR(`Address ${contractAddress} does not exist in ledger.`);
}
},

Expand All @@ -361,13 +369,13 @@ export default function createMockedABI(
const key = ptrToUint8ArrayString(kPtr);

if (!ledger.has(address)) {
throw new Error('Runtime error: address parsing error: ' + address);
ERROR(`Address ${address} does not exist in ledger.`);
}

const addressStorage = ledger.get(address).storage;

if (!addressStorage.has(key)) {
throw new Error('Runtime error: data entry not found');
ERROR('data entry not found');
}

addressStorage.delete(key);
Expand All @@ -379,13 +387,13 @@ export default function createMockedABI(
const newValue = getArrayBuffer(valuePtr);

if (!ledger.has(address)) {
throw new Error('Runtime error: address parsing error: ' + address);
ERROR(`Address ${address} does not exist in ledger.`);
}

const addressStorage = ledger.get(address).storage;

if (!addressStorage.has(key)) {
throw new Error('Runtime error: data entry not found');
ERROR('data entry not found');
}

const oldValue = addressStorage.get(key);
Expand All @@ -400,13 +408,13 @@ export default function createMockedABI(
const newValue = getArrayBuffer(valuePtr);

if (!ledger.has(address)) {
throw new Error('Runtime error: address parsing error: ' + address);
ERROR(`Address ${address} does not exist in ledger.`);
}

const addressStorage = ledger.get(address).storage;

if (!addressStorage.has(key)) {
throw new Error('Runtime error: data entry not found');
ERROR('data entry not found');
}

const oldValue = addressStorage.get(key);
Expand Down Expand Up @@ -448,18 +456,14 @@ export default function createMockedABI(
if (scCallMockStack.length) {
return newArrayBuffer(scCallMockStack.shift());
}
throw new Error(
`No mock defined for sc call on "${ptrToString(method)}".`,
);
ERROR('No mock defined for sc call on ' + ptrToString(method));
},

assembly_script_local_call(_address, method, _param) {
if (scCallMockStack.length) {
return newArrayBuffer(scCallMockStack.shift());
}
throw new Error(
`No mock defined for sc call on "${ptrToString(method)}".`,
);
ERROR('No mock defined for sc call on ' + ptrToString(method));
},

assembly_script_mock_admin_context(isAdmin) {
Expand Down Expand Up @@ -629,7 +633,7 @@ export default function createMockedABI(
const calledFunction = ptrToString(handlerPtr);

if (!ledger.has(address)) {
throw new Error(`Address ${ptrToString(addressPtr)} does not exist.`);
ERROR(`Address ${address} does not exist.`);
}
console.log(
`sendMessage: function ${calledFunction} will be called in ${address}`,
Expand All @@ -656,7 +660,7 @@ export default function createMockedABI(
const bytecode = getArrayBuffer(bytecodePtr);

if (!ledger.has(a)) {
throw new Error('Runtime error: address parsing error: ' + a);
ERROR(`Address ${a} does not exist in ledger.`);
}

const addressLedger = ledger.get(a);
Expand All @@ -671,7 +675,7 @@ export default function createMockedABI(
assembly_script_get_bytecode_for(aPtr) {
const a = ptrToString(aPtr);
if (!ledger.has(a)) {
throw new Error('Runtime error: address parsing error: ' + a);
ERROR(`Address ${a} does not exist in ledger.`);
}

const addressLedger = ledger.get(a);
Expand All @@ -688,10 +692,9 @@ export default function createMockedABI(
});
}
const callerBalance = ledger.get(callerAddress).balance;

if (callerBalance < BigInt(_coinsAmount)) {
throw new Error(
`Runtime error: not enough balance to transfer ${_coinsAmount} coins.`,
);
ERROR('not enough balance to transfer ' + _coinsAmount + ' coins.');
}
ledger.get(callerAddress).balance -= BigInt(_coinsAmount);
ledger.get(address).balance += BigInt(_coinsAmount);
Expand All @@ -706,9 +709,7 @@ export default function createMockedABI(
const addressTo = ptrToString(_addressToPtr);

if (!ledger.has(addressFrom)) {
throw new Error(
'Runtime error: address parsing error: ' + addressFrom,
);
ERROR(`Sending address ${addressFrom} does not exist in ledger.`);
}

if (!ledger.has(addressTo)) {
Expand All @@ -722,9 +723,7 @@ export default function createMockedABI(
const addressFromBalance = ledger.get(addressFrom).balance;

if (addressFromBalance < BigInt(_coinsAmount)) {
throw new Error(
`Runtime error: not enough balance to transfer ${_coinsAmount} coins.`,
);
ERROR(`not enough balance to transfer ${_coinsAmount} coins.`);
}

ledger.get(addressFrom).balance -= BigInt(_coinsAmount);
Expand All @@ -741,11 +740,7 @@ export default function createMockedABI(
});
return;
}
ledger.set(addr, {
storage: ledger.get(addr).storage,
contract: ledger.get(addr).contract,
balance: BigInt(amount),
});
ledger.get(addr).balance = BigInt(amount);
},

assembly_script_get_balance() {
Expand All @@ -770,16 +765,12 @@ export default function createMockedABI(
) {
const signatureBuf = getArrayBuffer(signaturePtr);
if (signatureBuf.byteLength !== 65) {
console.log('Invalid signature length. Expected 65 bytes');
throw new Error();
ERROR('Invalid signature length. Expected 65 bytes');
}

const pubKeyBuf = getArrayBuffer(publicKeyPtr);
if (pubKeyBuf.byteLength !== 64) {
console.log(
'Invalid public key length. Expected 64 bytes uncompressed secp256k1 public key',
);
throw new Error();
ERROR('Invalid public key length. Expected 64 bytes uncompressed secp256k1 public key');
}

const digest = hashMessage(new Uint8Array(getArrayBuffer(dataPtr)));
Expand All @@ -796,8 +787,7 @@ export default function createMockedABI(
assembly_script_evm_get_pubkey_from_signature(dataPtr, signaturePtr) {
const signatureBuf = getArrayBuffer(signaturePtr);
if (signatureBuf.byteLength !== 65) {
console.log('Invalid signature length. Expected 65 bytes');
throw new Error();
ERROR('Invalid signature length. Expected 65 bytes');
}
const digest =
'0x' + Buffer.from(getArrayBuffer(dataPtr)).toString('hex');
Expand Down

0 comments on commit ac3c14e

Please sign in to comment.