0.2.2 • Published 6 years ago

soltype-pb v0.2.2

Weekly downloads
4
License
ISC
Repository
github
Last release
6 years ago

pb3sol

  • protobuf3 solidity code generator, inpired by https://github.com/shmookey/solpb (only supports pb2) enhanced with:
  • unimplemented protobuf feature
    • true enum generated by protobuf enum declaration. because solidity does not allow us to set arbiter values to enum declaration.
    • float/double. no floating type in solidity.

Motivation

  • recently, some kind of dapp like game need to treat complex, unstable data structure.
  • but for now, only solidity code can describe data structure and solidity code is immutable if it once deployed.
  • actually, code can be replaced by zos or similar technology, but data related with old contract cannot move to new one automatically
  • thus migrate data schema in smart contract is often with full database copy. its un-acceptable from the view point of performance and cost.
  • storing data as bytes and parse with protobuf could solve this problem with small increase of gas comsumption for each contract call, by separating actual data storage and its schema definition (replacable as solidity library)
  • also, if we describe data as protobuf encoded byte array, contract only need to return byte array and parsing can be done on each client, which is huge save for contract code size and execution fee

Try it out

Caveat with solidity <= 0.4.24

  • at the time I wrote this (2018/06/05), latest released solidity version is 0.4.24, which cannot allow us to:
    • pass/return arbiter struct value to/from non-internal contract call
  • so still we cannot use generated library for linking, same reason of 0.4.20.

Caveat with solidity <= 0.4.20

  • at the time I wrote this (2018/03/03), latest released solidity version is 0.4.20, which cannot allow us to:
    • return bytes from non-internal contract call
    • pass arbiter struct value to non-internal contract call
  • as a result, read and parse protobuf encoded value from storage contract will be more expensive than it should be, because we cannot avoid do like this to read arbiter length bytes from external contract for now.
  • also this problem prevent our contract from linking with library generated by pb3sol, because we have to declare internal modifier for all encoder/decoder, which seems to prevent solc from generating delegatecall instruction.

Basic Usage

  • simplest way is using docker. for example, under truffle project do like following.
mkdir -p `pwd`/contracts/libs/pb 		# output directory for pb3sol
mkdir -p `pwd`/proto 					# input directory (proto files)
touch /proto/test.proto 				# create protofile (and edit) 

# compile test.proto as test_pb.sol with dependent library, runtime.sol and put into `pwd`/contracts/libs/pb 
docker run --rm -ti -v `pwd`/contracts/libs/pb:/out -v `pwd`/proto:/in umegaya/pb3sol protoc -I/ -I/protoc/include --plugin=protoc-gen-sol=gen_runtime=runtime.sol:/out /in/test.proto
  • for actually usage in solidity codes, see here and here

Handling native solidity type

  • if you use docker image, built-in proto file "Solidity.proto" can import straight forward like import "Solidity.proto". then you can use .solidity.$typename to declare variable which directly convert to $typename variable in solidity codes.

    • if you don't want to use docker, copy above file into somewhere in your proto compiler $path.
  • basically these solidity type is bytes variable boxed with message. but convert these bytes into correct number or bigint, is not trivial work.

    • we create node module soltype-pb to add small support for handling these native solidity types with protobufjs.
    • installation to your project: npm install soltype-pb
    • see here and here for usage.
0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago