aelf Contract Code Generation Extension Based on GPRC Template

aelf Developer
aelf
Published in
4 min readMay 27, 2020

aelf’s contract code mainly consists of four parts: proto file, Generated Contract Code based on proto file, contract method implementation code, and contract state. Proto file defines the contract method and the data structure used. The Generated Contract Code is compiled by protoc using the aelf Contract Code Generation Extension. Contract method implementation code is the concrete logic to implement contract method. Contract State is used to store the data generated after calling the contract method.

aelf Contract Code Generation Extension uses the GPRC code generation template, and makes corresponding adjustments according to the contract needs. There are 10 types of generated code: Messages,Events,Marshallers,Methods,Descriptor,Descriptors,ContractBase,ReferenceState, Stub, and ServerServiceDefinition.

Messages are generated based on the message in the Proto file, which is the main data structure used by the contract method for input parameters, return values, and data storage.

Events are defined according to the Message in the Proto file. To distinguish them from the Message, we add a PROTOBUF custom option, which is ‘is_event’. When compiling, if the message contains the is_event option, an Event is generated. When the contract is invoked, the Event is used to generate the Event log, which is recorded in the result of the invoking the contract transaction. is_event is defined in Proto as follows:

Descriptor is generated based on contract proto file,which recorded the FileDescriptor of contract proto file.

Descriptors are generated from the current contract FileDescriptor and its dependent contract Proto file, which records the FileDescriptor of contract Proto file and its dependent contract proto file.

Marshalers are generated according to the message in the proto file. When compiling the generated code, a Marshaler will be generated for each message in the proto file for message serialization and deserialization.

Methods are generated from the RPC method in the Proto file and records the type of the contract method, Action/View, contract name, method name, Marshaller for the entry parameter, and Marshaller for the return value. The contract method type (Action/View) is set through the protobuf custom option is_view. If the contract method contains is_view, the contract method type is_view. Otherwise, it is Action. Is_view is defined as follows in proto:

Contractbase is generated according to the service in the proto file. It is a contract base that contains all contract methods.

TheReferenceState is generated according to the service in the proto file. It needs to be generated by using the custom option reference of protoc, which is used to invoke other contract methods across contracts.

The Stub is generated based on the service in the Proto file and needs to be generated by using protoc’s custom option Stub. Stubs are primarily used for unit testing.

The ServerServiceDefinition is generated from the service in the Proto file and is used to record all contract methods.

In addition to is_event and is_view, we have also added the identity, base, CSharp_state, is_indexed and other protobuf customization options.

Identity defines the identity of the contract, which can be used to query the identity content through FileDescriptor. In proto, it is defined as follows:

Base is used to specify the dependent contract, and CSharp_state is used to specify the contract state class. In proto, it is defined as follows:

Is_indexed is used to define the index field in the Event. In proto, it is defined as follows:

The generated code does not include all the types mentioned above. Depending on the usage, we add 5 custom options for protoc:

Nocontract:Generate Messages,Events

Stub: Generate Messages, Events, Marshallers, Methods, Descriptor, Descriptors,Stub

Noevent: Generate Messages, Marshallers, Methods, Descriptor, Descriptors, ContractBase, ServerServiceDefinition

Reference: Generate Messages, Events, Marshallers, Methods, Descriptor, Descriptors,ReferenceState

Internal_access: It mainly makes the generated Events, Marshalers, Methods, and ReferenceState call permission internal, which can be used together with other options.

— Join the Community:

· Get aelf News from our Telegram, Wechat and Kakao channels

· Follow us on Twitter, Instagram, Reddit and Facebook

· Read weekly articles on our aelf blog

· Catch up with our development progress on Github or Telegram

· Chat in our Telegram communities -English, 한국, 日本 語, русский, العربية, Deutsch, Italiano, Türk, Español, and Tiếng Việt

· YouTube Channel: aelf

For more information, visit aelf.io

--

--