ProtoSolGenerator – 原生质体发生器区块链毕设代写

区块链毕设代写本文提供国外最新区块链项目源码下载,包括solidity,eth,fabric等blockchain区块链,ProtoSolGenerator – 原生质体发生器区块链毕设代写 是一篇很好的国外资料

ProtoSolGenerator

ProtoSolGenerator is a protocol buffer client generator for Solidity. Its main purpose is to allow Ethereum developers to define custom data structure using protocol buffer, and generates Solidity stubs for data serialization and deserialization.

ProtoSolGenerator is inspired by pb3sol with optimization on performance, flexibility and usability. We initial testing shows that the gas cost is reduced by 2/3 compared to the original Solidity implementation.

Introduction

Protocol buffer is a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more. It’s widely adopted as in various platforms, and it has several characteristics to make it particularly beneficial to Ethereum development.

Optimized Storage

The storage cost is significantly higher in Ethereum than compute. For example, an SSTORE operation costs 20,000 gas while a normal ADD costs only 3. It’s more reasonable to trade storage cost with computation in Ethereum.

However, Solidity in Ethereum is not storage-efficient. A single storage variable of type uint8 also takes the space of a whole word, identical to variables of type uint256. For a variable of type uint256, even if its value is relative small, (e.g it consumes only 4 bytes), it’s still stored as 32 bytes. Protocol buffer defines an encoding format which is highly optimized in storage.

Flexibility

Once a smart contract is deployed to Ethereum network, it’s not updatable. Even though it can be “upgraded” using proxies, it comes with constraints and is not easy to use. For example, the upgraded contract might need to use the same field declaration order as the old contract.

Such inflexibility in logic can be mitigated with flexibility in data definition, which is a core feature of protocol buffer. When implemented with separation of logic and data, the upgraded contracts can work with data from the old ones.

Cross-platform Support

Protocol buffer has rich support in languages including Java, JavaScript, Python and C++, so the function parameters and return values can be defined using the protocol buffer encoded strings. With this approach, users of smart contracts can use languages like JavaScript to construct function parameters to invoke Solidity functions.

Security

The binary of protocol buffer data pertains the data type definition as well as field number. This ensures type-safety of data either on-chain or passed-in externally.

Moreover, Protocol Buffer simplifies the usage of complex data structure in the paramters and return values of external functions. There might be still hidden risk in using ABIEncoderV2, especially with the known bug identified in March 2019. ProtoSolGen allows auto-serialization of the complex data types so that we could use string as paramter/return value types instead of relying on ABIEncoderV2.

Concept

Solidity has much more basic types than protocol buffer. For example, in Solidity there are 32 signed integer types, from int8 to int256. In order to address this type mismatch, we provide a custom type defintion for each Solidity types. For example, uint128 is defined as below:

message uint128 {bytes data = 1;} 

The data, which is of type bytes, contains the minimum number of bytes needed to hold the value. For example, if the uint128 variable has a value of 20,000, 2 bytes are sufficient to hold this number. Currently we only support length-delimited encoding for value data.

How to Use It

Installation

To install ProtoSolGenerator, first install protobuf compiler.

Then install python requirements.

pip install -r requirements.txt

Data Definition

Users can use the custom type definition to define their data structure. Below is an example:

syntax = "proto3";  import "SolidityTypes.proto";  package finance.nuts;  message SellerParameter {     .solidity.address seller_address = 1;     .solidity.uint256 start_date = 2;     .solidity.address collateral_token_address = 3;     .solidity.uint256 collateral_token_amount = 4;     .solidity.uint256 borrow_amount = 5;     .solidity.uint16 collateral_due_days = 6;     .solidity.uint16 engagement_due_days = 7;     .solidity.uint16 tenor_days = 8;     .solidity.uint16 interest_rate = 9;     .solidity.uint16 grace_period = 10; } 

ProtoSolGenerator generates a Solidity struct definition for this message as well as serialization/deserialization methods. Users can use the generated Solidity stub in their smart contracts.

Client Generation

Use run.sh with input proto files (--input) and output Solidity location(--output)

./run.sh --input <proto file path> --output <output directory> 

协议生成器

ProtoSolGenerator是一个协议缓冲区客户端生成器,用于实现稳固性。其主要目的是允许以太坊eth开发人员使用协议缓冲区定义自定义数据结构,并生成用于数据序列化和反序列化的稳定存根

ProtoSolGenerator以pb3sol为灵感,在性能、灵活性和可用性方面进行了优化。我们的初步测试表明,与原始的坚固性实现相比,气体成本降低了2/3

简介

协议缓冲区是一种语言中立、平台中立、可扩展的结构化数据序列化方式,用于通信协议、数据存储等。它在各种平台中被广泛采用,并且它有几个特点使它对以太坊eth的开发特别有利

优化存储

以太坊eth的存储成本明显高于计算机。例如,一个SSTORE操作需要20000汽油,而一个普通的ADD操作只需要3汽油。在以太坊eth中用存储成本和计算进行权衡更为合理

然而,以太坊eth的坚固性不利于存储。uint8类型的单个存储变量也占用整个字的空间,与uint256类型的变量相同。对于uint256类型的变量,即使其值相对较小(例如,它只消耗4个字节),它仍然存储为32个字节。协议缓冲区定义了在存储中高度优化的编码格式

灵活性

一旦智能合约部署到以太坊eth网络,它就不可更新。尽管它可以使用代理“升级”,但它有一些限制,不容易使用。例如,升级后的合同可能需要使用与旧合同相同的字段声明顺序

这种逻辑上的不灵活可以通过数据定义的灵活性来缓解,数据定义是协议缓冲区的核心特性。当实现逻辑和数据分离时,升级的合同可以使用旧合同中的数据

跨平台支持

协议缓冲区数据的二进制与数据类型定义以及字段编号有关。这确保了链上或外部传递的数据的类型安全

如何使用它

此外,协议缓冲区简化了外部函数的参数和返回值中复杂数据结构的使用。使用Abinecoderv2可能仍然存在潜在风险,特别是2019年3月发现的已知错误。ProtoSolGen允许复杂数据类型的自动序列化,因此我们可以使用字符串作为参数/返回值类型,而不是依赖Abinecoderv2

Solidity比协议缓冲区有更多的基本类型。例如,在Solidity中有32种有符号整数类型,从int8到int256。为了解决这种类型不匹配的问题,我们为每个实体类型提供了自定义类型定义。例如,uint128的定义如下:

安装

字节类型的数据包含保存该值所需的最小字节数。例如,如果uint128变量的值为20000,则2个字节足以容纳该数字。目前,我们只支持值数据的长度分隔编码

message uint128 {bytes data = 1;} 

要安装ProtoSolGenerator,首先安装protobuf编译器

How to Use It

Installation

然后根据需要安装python

pip install-r requirements.txt

用户可以使用自定义类型定义来定义其数据结构。下面是一个示例:

Data Definition

ProtoSolGenerator为该消息以及序列化/反序列化方法生成一个Solidity结构定义。用户可以在智能合约中使用生成的Solidity存根

syntax = "proto3";  import "SolidityTypes.proto";  package finance.nuts;  message SellerParameter {     .solidity.address seller_address = 1;     .solidity.uint256 start_date = 2;     .solidity.address collateral_token_address = 3;     .solidity.uint256 collateral_token_amount = 4;     .solidity.uint256 borrow_amount = 5;     .solidity.uint16 collateral_due_days = 6;     .solidity.uint16 engagement_due_days = 7;     .solidity.uint16 tenor_days = 8;     .solidity.uint16 interest_rate = 9;     .solidity.uint16 grace_period = 10; } 

将run.sh与输入协议文件(–input)和输出实体位置(–output)一起使用

Client Generation

Use run.sh with input proto files (--input) and output Solidity location(--output)

./run.sh --input <proto file path> --output <output directory> 

部分转自网络,侵权联系删除区块链源码网

www.interchains.cc

https://www.interchains.cc/23496.html

区块链毕设网(www.interchains.cc)全网最靠谱的原创区块链毕设代做网站 部分资料来自网络,侵权联系删除! 最全最大的区块链源码站 ! QQ3039046426
区块链知识分享网, 以太坊dapp资源网, 区块链教程, fabric教程下载, 区块链书籍下载, 区块链资料下载, 区块链视频教程下载, 区块链基础教程, 区块链入门教程, 区块链资源 » ProtoSolGenerator – 原生质体发生器区块链毕设代写

提供最优质的资源集合

立即查看 了解详情