Page 42 - MSDN Magazine, July 2018
P. 42
eth.coinbase -- address of the coinbase account personal.unlockAccount('address', 'passphrase', 'duration') --
unlocking the account for a longer time period
If you’re wondering what the “geth” command stands for, it’s a multipurpose command-line tool that runs a full Ethereum node implemented in Go.
Once the Coinbase account is unlocked, this represents time zero, when the network starts. After this point, nodes can accept transactions. Transactions could be in the form of creation of accounts, movement of ether, creation of smart contracts, or any change to the state of the blockchain. Then, at a periodic time con- figured for the network, the network mines the next block. This block is a hash calculated by combining hashes of the transactions executed between the last block and now, plus the hash from the previous block and a nonce—a sequence of bits in a block that can be adjusted in order to try to satisfy the proof-of-work condition.
This is the essence of mining. This value makes satisfying “proof of work” a difficult computational task that depends on luck or brute force. The block is then accepted by the network by consen- sus, and so you have the first two blocks in your chain, and so on.
Figure 6: Starting the Deployment
Developing Smart Contracts
It would take a whole book to go through the details of develop- ing smart contracts in Ethereum. In this article, I want to offer pointers to start with and understand the landscape of technologies and frameworks in use.
To write and deploy smart contracts in Ethereum, you can use any of the development environment available in Azure, or access a completely external browser-based IDE like Ether Camp (ether.camp) or Ethereum Remix (remix.ethereum.org).
In terms of programming languages, Solidity (solidity.readthedocs.io) is a popular contract-oriented language for blockchain program- ming, with a JavaScript-like syntax.
On the client side, programming languages that support inter- acting with an Ethereum node include C#, C++, JavaScript and more. It’s possible to write C# code using a library like NEthereum (nethereum.com), a fully managed .NET integration library for Ethereum that allows interaction with Ethereum clients like geth, eth or parity using RPC. The library has very similar functionality to the JavaScript Ethereum Web3 RPC Client Library, which is the de-facto standard for blockchain client interoperability.
For example, to make a Smart Contract call via NEthereum, I need to do the following:
• Obtain the smart contract address and Application Binary Interface (ABI). An ABI is the interface to call functions in a smart contract and get data back from an Ethereum node.
• Obtainthefunctionsignatureonthesmartcontracttobeinvoked. • Unlock the Ethereum account making the call with the
account’s passphrase.
• Make the call to the smart contract.
The code snippet in Figure 7 shows a few very basic steps, using the NEthereum library.
The invoked smart contract, written in Solidity, would look like a ballot contract that exposes a vote method, which accepts a pro- posal number in input. When a vote is cast, the voted flag on the voter (the message sender) is set to true to prevent double voting,
Figure 7 Calling a Smart Contract
# sign in
Write-Host "Logging in..."; Login-AzureRmAccount;
# select subscription
Write-Host "Selecting subscription '$subscriptionId'"; Select-AzureRmSubscription -SubscriptionID $subscriptionId;
# Register RPs
$resourceProviders = @("microsoft.compute","microsoft. resources","microsoft.network"); if($resourceProviders.length) {
Write-Host "Registering resource providers" foreach($resourceProvider in $resourceProviders) {
RegisterRP($resourceProvider); }
}
#Create or check for existing resource group
$resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
if(!$resourceGroup)
{
Write-Host "Resource group '$resourceGroupName' does not exist. To create a new resource
group, please enter a location.";
if(!$resourceGroupLocation) {
$resourceGroupLocation = Read-Host "resourceGroupLocation";
}
Write-Host "Creating resource group '$resourceGroupName' in location
'$resourceGroupLocation'"; New-AzureRmResourceGroup -Name $resourceGroupName
-Location $resourceGroupLocation }
else{
Write-Host "Using existing resource group '$resourceGroupName'";
}
# Start the deployment
Write-Host "Starting deployment..."; if(Test-Path $parametersFilePath) {
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $templateFilePath -TemplateParameterFile
$parametersFilePath; } else {
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $templateFilePath;
}
// Obtain the contract ABI
abi = db.GetContract(ballot.ContractID);
// Get the function address to call on the smart contract var func = web3.Eth.GetContract(
abi, ballot.ContractID).GetFunction("vote");
// Unlock the account so you can call the smart contract
string passphrase = db.GetAccountPassphrase(agreement.OriginatorAccount); bool success = await web3.Personal.UnlockAccount.SendRequestAsync(
ballot.OriginatorAccount, passphrase,
120);
// Make the smart contract call if (success)
{
object[] args = new object[] { id,
ballot.OriginatorAccount, ballot.CounterSigAccount,
123 /* sample proposal number to vote for */ };
// Call the "vote" function on the smart contract
await func.SendTransactionAsync(ballot.OriginatorAccount, args); }
36 msdn magazine
Microsoft Azure