Since Vitalik Buterin Ethereum has developed, are Smart Contracts without the block Chain Ecosystem. But what is behind the “smart contracts”? In five Parts, Ingo Rammer explored for us the implications of the technology. Today: How to program Smart Contracts?

part 1: Smart Contracts: bringer of salvation, or a better database?

part 2: More than just crypto-currencies: transactions and Smart Contracts

Solidity: step-by-step

How to program Smart Contracts? In the examples in this article, I’m going to use Solidity – the Contracts are, therefore, on any Ethereum derivative, both public and private Blockchains. The concepts presented can also be for the majority of other Blockchains apply. The reason for the choice of Solidity, the availability of easy-to-use development tools: So it is with the Open-Source development environment Remix is an opportunity to develop Solidity Code locally to test and debug without the need to connect with a real Blockchain network.

As a first step, we need to define a data model that Is the state of the data of the Smart Contract determines. In highly simplified terms, it consists of the ground state in our case example, the following three aspects:

determination of the identity of the regulator – the Federal network Agency. Transactions sent from there, also have the permission to associate a phone number for the first time with an operator.Definition of a key/value Association, similar to a Dictionary. This mapping allows the determination of the identity of the current operator for each registered phone number.Definition of a key/value mapping that for each phone number that is located in a port, keeps the identity of the porting destination (of the new mobile operator).

(In the above-described data structure is, of course, a strong simplification. For the purpose of readability I have allows me to have some aspects for this article, which would define, in practice, is essential. Thus, neither a list of approved identities, for example, is set (every node in this network could invent any number of mobile operators), escalation processes, or time limits at the port are taken into account. The underlying Considerations, and technologies that allow the seamless extension of these parameters.

addresses

these “identities” are technology-specific sender addresses. In practice, there are all values that correspond to the public key of an asymmetric key pair. This means that by checking the cryptographic signature of a transaction to determine which identity the transaction was signed. This allows us to ensure that, for example, only the currently for a phone number of a competent mobile operator is allowed to change the data set.

we See therefore, first, in Listing 1 the base code of the Smart Contract that implements the above-outlined data model.

Listing 1

contract phone number transfer {

mapping(string => address) allphone numbers;

mapping(string => address) requested transfers;

address regulator;

constructor() public {regulator = msg.sender;}

}

The conceptual persistence model of Solidity

The conceptual persistence model of Solidity is relatively simple: All Member-fields of a Smart Contract will be automatically stored in your persistent World State. In this case, the relates to the fields of allphone numbers, requested transfer and regulator.

The constructor in this code fragment a further feature of Ethereum is using derivatives: A Smart Contract will be installed in the network by the to be pre-compiled Bytecode is made with a special Blockchain transaction to the network. These Blockchain-transaction (just like any other transaction), the sender, and this can be used with msg.the transmitter and stored to be evaluated. Before a participant in our number porting network can use the Smart Contract would have to send the network Agency once this installation transaction to the network. As a response, the sender gets after that, an address that references the newly created instance of the Smart Contract. All other transactions (of all participants) are then sent to this particular instance of address.

As mentioned above, this is a technical feature of Blockchain networks, based on Ethereum. In networks based on other technologies, can be the Installation of Smart Contracts is different. Depending on the selected platform, for example, in the case of Hyper Ledger Fabric must be made of the identity of the regulator, the Contract may also explicitly, with a single function call after the Installation. (An Ethereum Implementation how Parity behaves to Hyper Ledger Fabric in roughly the same way as Microsoft SQL Server to MongoDB: Both are manifestations of a technology concept, “database”, or “Blockchain”– but are interpreted completely differently and implemented).

the function of The initial assignment of a telephone number to a provider could be implemented as in Listing 2.

Listing 2

contract phone number transfer {

/* … as above … */

function createAndAssignNumber(uint phoneNumber, address to) public {

require(regulator == msg.channel);

require(allphone numbers[phoneNumber] == 0);

allphone numbers[phoneNumber] = to;

}

}

in this Fragment, the function specified createAndAssignNumbers receives two parameters: the phone number and the identity of a mobile provider (in the Form of an Ethereum address, as described above), the phone number is to be assigned. The Code first checks whether the sender of the transaction is equal to the Regulator, because it is the only in our example, the justification for the initial creation and assignment of a telephone number. The function require executes in Solidity to the Equivalent of an Exception, so that the System stops in case of non-fulfilment of the condition, the processing of the rest of the code, the transaction immediately and marked with an error. After that, we check through the access to allphone numbers, [phoneNumber], if the desired phone number is already assigned. This access to a Mapping is defined in Solidity so that it is not filled, entries will be returned as 0. In the lastth row, we have otherwise simply passed to the phone number passed as a Parameter, address. As in the case of the TRANS-action processing in a Blockchain-an absolute temporal sequence of the individual transactions by their Position in a Block is fixed, we must pay attention not to concurrent write access by other transactions.

For the second type of transaction, the request to port a number, we proceed analogously. Such a call would be sent from the new mobile operators to the network and could be run as in Listing 3 is shown.

Listing 3

function request transfer(uint phoneNumber) public {

// number must exist and be assigned to

require(allphone numbers[phoneNumber] != 0);

// number must not already Requesting be assigned to

require(allphone numbers[phoneNumber] != msg.channel);

// number is not already in a porting process

require(requested transfers[phoneNumber] == 0);

the requested transfers[phoneNumber] = msg transmitter;

}

Also here is the Code of Smart Contracts first checks the two input conditions, and changes after a positive test, the data in the World State so that a record is kept of who has requested the porting of the given number. Typical would be released as the last line of the Smart Contracts now have a notification event (for example, an Event in Solidity), on any mobile operator with its own program code in its Blockchain Node is subscribed. Thus, the Smart Contract will start the local (block chain, independent) processing of porting the existing back-end systems of a participant, as soon as another operator requesting the porting of a number from his area of influence.

After the mobile phone provider, this number is currently allocated to its internal audit of the porting completed and the correctness of the porting itself has been validated (e.g., by a Declaration of consent of the customer via SMS), it can answer the request with a new transaction. The Smart-Contract Code for this transaction could be simplified as in Listing 4 as shown in the appearance.

Listing 4

function confirm transfer(uint phoneNumber) public {

// make Sure the number is to be ported

address destination = requested transfers[phoneNumber];

require (destination != 0);

// number must currently, the Sender of the transaction to be

require (allphone numbers[phoneNumber] is mapped to == msg.channel);

// update the mapping for the number

allphone numbers[phoneNumber] = destination;

// Delete the open transfer

delete requested transfers[phoneNumber];

}

similarly, one can make the rejection of a transfer. Here, the difference lies in the fact that the assignment of the telephone number updated, but only the porting request is in the requested transfers. In both cases, it would trigger at the end of the function an Event, so that the person concerned is informed of the opposite side, without the need of each and every transaction in the Blockchain have to constantly monitor.

Ingo Rammer is a Speaker at the Blockchain Technology Conference from November 19-21. November in Berlin. There you can collect practical experiences with international experts. In addition, there are Live Demos and case studies of real implementations, individual interaction with experts and Networking opportunities with people from various industries. It also has a variety of Sessions, Workshops and lectures of international speakers.

Exclusive discount Code for our readers: blockchain Easy-to-btc-15 input and 15 percent discount. Tickets can be purchased here.