transpiler-solidity – 透明度区块链毕设代写

区块链毕设代写本文提供国外最新区块链项目源码下载,包括solidity,eth,fabric等blockchain区块链,transpiler-solidity – 透明度区块链毕设代写 是一篇很好的国外资料

transpiler-solidity

transpiler-solidity - 透明度

A transpiler to Solidity that adds GCC macros and helper functions.

Why does this exist?

Solidity codes runs on the Etheruem Virtual Machine, an environment where every operation has a fixed cost. To keep processing costs low, it often makes sense to use Solidity Assembly to manually optimize storage (memory) operations. This transpiler provides tooling and language features to make Solidity Assembly more managable.

Runtime Requirements

  • gcc
  • npm & node

Usage

npm i -g transpiler-solidity transpiler-solidity file.sol > output.sol 

Added Assembly Functions

pointer(type, array_start_pointer, index)

Gives the storage address for an item in an array.

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64[8] numbers;   }    Item[1024] items;    function foo() public {     assembly {       // let item_ptr := add(items_slot, mul(2, 10))       let item_ptr := pointer(Item, items_slot, 10)     }   } }

pointer_attr(type, object_pointer, attribute_name)

Gives the storage address for an object’s attribute.

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64[8] numbers;     uint64 a;     uint64 b;     uint64 c;     uint64 d;     uint256 e;   }    Item[1024] items;    function foo() public {     assembly {       // let item_ptr := add(items_slot, mul(4, 10))       let item_ptr := pointer(Item, items_slot, 10)       // let e_ptr := add(item_ptr, 3)       let e_ptr := pointer_attr(Item, item_ptr, e)        let e := sload(e_ptr)       sstore(e_ptr, add(e, 1))     }   } }

byte_offset(type, attribute_name)

Byte offset of a attribute inside of a type.

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64[8] numbers;     uint64 a;     uint64 b;     uint64 c;     uint64 d;     uint256 e;   }    uint64 foo;    function foo(bytes memory input) public {     assembly {       // let b_offset := 72       let b_offset := byte_offset(Item, b)       let b := mload(add(add(input, 32), b_offset))        sstore(foo_slot, b)     }   } }

build(type, word, attributes…) | build_with_mask(type, word, attributes…)

Packs the data of an object. build_with_mask will mask each attribute to ensure attributes are not corrupted if the attribute is larger than allowed size.

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64 a;     uint64 b;     uint64 c;     uint64 d;     uint128 e;     uint128 f;   }    Item item;    function foo() public {     assembly {       // let data_0 := or(or(or(       //   /* a */ mul(1, 0x1000000000000000000000000000000000000000000000000),       //   /* b */ mul(2, 0x100000000000000000000000000000000)),       //   /* c */ mul(3, 0x10000000000000000)),       //   /* d */ 4)        let data_0 := build(Item, 0,         /* a */ 1,         /* b */ 2,         /* c */ 3,         /* d */ 4       )        // let data_1 := or(       //     /* e */ mul(5, 0x100000000000000000000000000000000),       //     /* f */ 6)        let data_1 := build(Item, 1,         /* e */ 5,         /* f */ 6       )        sstore(item_slot, data_0)       sstore(add(item_slot, 1), data_1)     }   } }

attr(type, word, object_data, attribute_name)

Extracts an attribute from an object.

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64 a;     uint64 b;     uint64 c;     uint64 d;     uint128 e;     uint128 f;   }    Item item;    function foo() public {     assembly {       let item_data_0 := sload(item_slot)       let item_data_1 := sload(add(item_slot, 1))        // let b := and(div(item_data_0, 0x100000000000000000000000000000000), 0xffffffffffffffff)       let b := attr(Item, 0, item_data_0, b)       // let f := and(item_data_1, 0xffffffffffffffffffffffffffffffff)       let f := attr(Item, 1, item_data_1, f)     }   } }

fn_hash(fn_signature)

Hashes a function signture for Solidity’s calling semantics.

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   function foo() public {     assembly {      // let function_hash := /* fn_hash("transfer(address,uint256)") */ 0xa9059cbb00000000000000000000000000000000000000000000000000000000       let function_hash := fn_hash("transfer(address,uint256)")       // ...     }   } }

log_event(event_type, memory_pointer, arguments…)

Logs an event

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   event UserCreated(uint64 user_id);    function foo() public {     uint256[2] memory log_data_mem;      assembly {       let user_id := 5        // /* Log event: UserCreated */       // mstore(log_data_mem, user_id)       // log1(log_data_mem, 32, /* UserCreated */ 0x654a5f371dd267582fdba132709448f256a549360e2ce54ccb3699d3b8ed2394)       log_event(UserCreated, log_data_mem, user_id)     }   } }

mask_out(type, word, attributes…)

Mask out the data occupied by a set of attributes. Often used to update a single attribute in an object.

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64 a;     uint64 b;     uint64 c;     uint64 d;   }    Item item;    function foo() public {     uint256[2] memory log_data_mem;      assembly {       let item_data_0 := sload(item_slot)        // let a := and(div(item_data_0, 0x1000000000000000000000000000000000000000000000000), 0xffffffffffffffff)       let a := attr(Item, 0, item_data_0, a)       // let c := and(div(item_data_0, 0x10000000000000000), 0xffffffffffffffff)       let c := attr(Item, 0, item_data_0, c)        a := add(a, 20)       c := add(c, 10)        item_data_0 := or(         // and(item_data_0, 0xffffffffffffffff0000000000000000ffffffffffffffff)         and(item_data_0, mask_out(Item, 0, c, a)),         // see build example         build(Item, 0,           /* a */ a,           /* b */ 0,           /* c */ c,           /* d */ 0       ))        sloat(item_slot, item_data_0)     }   } }

sizeof(type)

Returns the byte size of a struct type.

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64 a;     uint64 b;     uint64 c;   }    Item item;    function foo(bytes memory input) public {     assembly {       let input_len := mload(input)        // if iszero(eq(input_len, 24)) {       if iszero(eq(input_len, sizeof(Item))) {         revert(0, 0)       }     }   } }

const_add(items…) | const_sub(a, b)

Adds or substracts constant numbers

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   function foo(bytes memory input) public {     assembly {       // let a := 26       let a := const_add(1, 3, 4, 5, 6, 7)       // let b := 4       let b := const_sub(5, 1)       // let c := -3       let c := const_sub(5, 8)     }   } }

Acknowledgements

SEE LICENSE

License

SPDX-License-Identifier: MIT

Release 1.1.2


transpiler solidity

transpiler-solidity - 透明度

向Solidity添加GCC宏和助手函数的transpiler。

为什么会存在这种情况?

Solidity代码在Etheruem虚拟机上运行,在这个环境中,每个操作都有固定的成本。为了保持低处理成本,使用Solidity Assembly手动优化存储(内存)操作通常是有意义的。这个transpiler提供了工具和语言特性,使Solidity程序集更易于管理。

运行时要求

  • gcc
  • npm&节点

用法

npm i -g transpiler-solidity transpiler-solidity file.sol > output.sol 

添加的汇编函数

指针(类型、数组开始指针、索引)

给出数组中项目的存储地址。

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64[8] numbers;   }    Item[1024] items;    function foo() public {     assembly {       // let item_ptr := add(items_slot, mul(2, 10))       let item_ptr := pointer(Item, items_slot, 10)     }   } }

指针属性属性属性属性(类型、对象指针、属性名称)

给出对象属性的存储地址。

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64[8] numbers;     uint64 a;     uint64 b;     uint64 c;     uint64 d;     uint256 e;   }    Item[1024] items;    function foo() public {     assembly {       // let item_ptr := add(items_slot, mul(4, 10))       let item_ptr := pointer(Item, items_slot, 10)       // let e_ptr := add(item_ptr, 3)       let e_ptr := pointer_attr(Item, item_ptr, e)        let e := sload(e_ptr)       sstore(e_ptr, add(e, 1))     }   } }

字节偏移量(类型、属性名称)

类型内部属性的字节偏移量。

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64[8] numbers;     uint64 a;     uint64 b;     uint64 c;     uint64 d;     uint256 e;   }    uint64 foo;    function foo(bytes memory input) public {     assembly {       // let b_offset := 72       let b_offset := byte_offset(Item, b)       let b := mload(add(add(input, 32), b_offset))        sstore(foo_slot, b)     }   } }

build(type,word,attributes…)| build | with | mask(type,word,attributes…)

打包对象的数据。如果属性大于允许的大小,build u with u mask将屏蔽每个属性,以确保属性未损坏。

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64 a;     uint64 b;     uint64 c;     uint64 d;     uint128 e;     uint128 f;   }    Item item;    function foo() public {     assembly {       // let data_0 := or(or(or(       //   /* a */ mul(1, 0x1000000000000000000000000000000000000000000000000),       //   /* b */ mul(2, 0x100000000000000000000000000000000)),       //   /* c */ mul(3, 0x10000000000000000)),       //   /* d */ 4)        let data_0 := build(Item, 0,         /* a */ 1,         /* b */ 2,         /* c */ 3,         /* d */ 4       )        // let data_1 := or(       //     /* e */ mul(5, 0x100000000000000000000000000000000),       //     /* f */ 6)        let data_1 := build(Item, 1,         /* e */ 5,         /* f */ 6       )        sstore(item_slot, data_0)       sstore(add(item_slot, 1), data_1)     }   } }

attr(type,word,object | data,attribute | name)

从对象中提取属性。

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64 a;     uint64 b;     uint64 c;     uint64 d;     uint128 e;     uint128 f;   }    Item item;    function foo() public {     assembly {       let item_data_0 := sload(item_slot)       let item_data_1 := sload(add(item_slot, 1))        // let b := and(div(item_data_0, 0x100000000000000000000000000000000), 0xffffffffffffffff)       let b := attr(Item, 0, item_data_0, b)       // let f := and(item_data_1, 0xffffffffffffffffffffffffffffffff)       let f := attr(Item, 1, item_data_1, f)     }   } }

fn | hash(fn | signature)

散列函数符号以获得Solidity的调用语义。

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   function foo() public {     assembly {      // let function_hash := /* fn_hash("transfer(address,uint256)") */ 0xa9059cbb00000000000000000000000000000000000000000000000000000000       let function_hash := fn_hash("transfer(address,uint256)")       // ...     }   } }

log | event(event | type,memory | pointer,arguments…)

记录事件

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   event UserCreated(uint64 user_id);    function foo() public {     uint256[2] memory log_data_mem;      assembly {       let user_id := 5        // /* Log event: UserCreated */       // mstore(log_data_mem, user_id)       // log1(log_data_mem, 32, /* UserCreated */ 0x654a5f371dd267582fdba132709448f256a549360e2ce54ccb3699d3b8ed2394)       log_event(UserCreated, log_data_mem, user_id)     }   } }

mask | out,属性…

屏蔽一组属性占用的数据。通常用于更新对象中的单个属性。

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64 a;     uint64 b;     uint64 c;     uint64 d;   }    Item item;    function foo() public {     uint256[2] memory log_data_mem;      assembly {       let item_data_0 := sload(item_slot)        // let a := and(div(item_data_0, 0x1000000000000000000000000000000000000000000000000), 0xffffffffffffffff)       let a := attr(Item, 0, item_data_0, a)       // let c := and(div(item_data_0, 0x10000000000000000), 0xffffffffffffffff)       let c := attr(Item, 0, item_data_0, c)        a := add(a, 20)       c := add(c, 10)        item_data_0 := or(         // and(item_data_0, 0xffffffffffffffff0000000000000000ffffffffffffffff)         and(item_data_0, mask_out(Item, 0, c, a)),         // see build example         build(Item, 0,           /* a */ a,           /* b */ 0,           /* c */ c,           /* d */ 0       ))        sloat(item_slot, item_data_0)     }   } }

大小(类型)

返回结构类型的字节大小。

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   struct Item {     uint64 a;     uint64 b;     uint64 c;   }    Item item;    function foo(bytes memory input) public {     assembly {       let input_len := mload(input)        // if iszero(eq(input_len, 24)) {       if iszero(eq(input_len, sizeof(Item))) {         revert(0, 0)       }     }   } }

常量添加(项…);常量子项(a,b)

添加或减除常量

pragma solidity 0.5.7;  #define TRANSPILE  contract Ex {   function foo(bytes memory input) public {     assembly {       // let a := 26       let a := const_add(1, 3, 4, 5, 6, 7)       // let b := 4       let b := const_sub(5, 1)       // let c := -3       let c := const_sub(5, 8)     }   } }

确认

请参阅许可证

许可证

SPDX许可证标识符:MIT

Release 1.1.2

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

www.interchains.cc

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

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

提供最优质的资源集合

立即查看 了解详情