Customized SumActuator
Having a tailored actuator is key to building a java-tron-based customized public chain. This article illustrates how to develop a java-tron-based `SumActuator
`.
Actuator module is divided into 4 different methods that are defined in the `Actuator
` interface:
`
execute
`: execute specific actions of transactions, such as state modification, communication between modules, logic execution, etc.`
validate
`: define the validation logic of transactions.`
getOwnerAddress
`: acquire the address of transaction initiator.`
calcFee
`: define the logic for calculating transaction fees.
# Define and register the contract
Currently, contracts supported by java-tron are defined under `src/main/protos/core/contract
` directory in protocol module. First creating a `math_contract.proto
` file under this directory and declaring `SumContract
`. You can also implement any mathematical calculation you want, such as `Subtraction
`.
The logic for `SumContract
` is the summation of two numerical values:
Meanwhile, register the new contract type in `Transaction.Contract.ContractType
` emuneration within the `src/main/protos/core/Tron.proto file
`. Important data structures, such as transactions, accounts and blocks, are defined in the `Tron.proto
` file:
Then register a function to ensure that gRPC can receive and identify the requests of this contract. Currently, gRPC protocols are all defined in `src/main/protos/api/api.proto
`. To add an `InvokeSum
` interface in Wallet Service:
At last, recompile the modified proto files. Compiling the java-tron project directly will compile the proto files as well, `protoc
` command is also supported.
Currently, java-tron uses protoc v3.4.0. Please keep the same version when compiling by `protoc
` command.
After compilation, the corresponding .class under the java_out directory will be updated.
# Implement SumActuator
For now, the default Actuator supported by java-tron is located in `org.tron.core.actuator
`. Creating `SumActuator
` under this directory:
For simplicity, the above implementation prints the output of SumActuator directly to a log file. If there is any information that need to be stored, consider creating a new chainbase to store the data (guidance on how to create a chainbase will be revealed soon).
As `SumActuator
` finished, `invokeSum(MathContract.SumContract req, StreamObserver<Transaction> responseObserver)
` function in RpcApiService's sub-class `WalletApi
` need to be implemented to receive and process `SumContract
`.
# Validate SumActuator
At last, run a test class to validate whether the above steps are correct:
Running SumActuatorTest and the log will print outputs like this: `SumActuator: param1 = 1, param2 = 2, sum = 3
`. Here is the output:
At this point, SumActuator is finished. It is a simple case. In real business scenarios, there are much extra work to do, such as wallet-cli supportation or customizing a chainbase for storing data.