NVM - Nebulas Virtual Machine¶
NVM is one of the most important components in Nebulas. As the name implies, it provides managed virtual machine execution environments for Smart Contract and Protocol Code.
go-nebulas now support two kinds of Virtual Machines:
- V8: Chrome V8
- LLVM: Low-Level Virtual Machine
Nebulas V8 Engine¶
In go-nebulas, we designed and implemented the Nebulas V8 Engine based on Chrome V8.
The Nebulas V8 Engine provides a high performance sandbox for Smart Contract execution. It guarantees user deployed code is running in a managed environment, and prevents massive resource consumption on hosts. Owing to the use of Chrome V8, JavaScript and TypeScript are first-class languages for Nebulas Smart Contracts. Anyone familiar with JavaScript or TypeScript can write their own Smart Contract and run it in Nebulas V8.
The following content is an example of Smart Contract written in JavaScript:
"use strict";
var BankVaultContract = function() {
LocalContractStorage.defineMapProperty(this, "bankVault");
};
// save value to contract, only after height of block, users can takeout
BankVaultContract.prototype = {
init:function() {},
save:function(height) {
var deposit = this.bankVault.get(Blockchain.transaction.from);
var value = new BigNumber(Blockchain.transaction.value);
if (deposit != null && deposit.balance.length > 0) {
var balance = new BigNumber(deposit.balance);
value = value.plus(balance);
}
var content = {
balance:value.toString(),
height:Blockchain.block.height + height
};
this.bankVault.put(Blockchain.transaction.from, content);
},
takeout:function(amount) {
var deposit = this.bankVault.get(Blockchain.transaction.from);
if (deposit == null) {
return 0;
}
if (Blockchain.block.height < deposit.height) {
return 0;
}
var balance = new BigNumber(deposit.balance);
var value = new BigNumber(amount);
if (balance.lessThan(value)) {
return 0;
}
var result = Blockchain.transfer(Blockchain.transaction.from, value);
if (result > 0) {
deposit.balance = balance.dividedBy(value).toString();
this.bankVault.put(Blockchain.transaction.from, deposit);
}
return result;
}
};
module.exports = BankVaultContract;
For more information about smart contracts in Nebulas, please go to Smart Contract.
For more information about the design of the Nebulas V8 Engine, please go to Nebulas V8 Engine.
LLVM¶
TBD.