### Solidity Contract Testing Source: https://github.com/ethereum/solidity-examples/blob/master/README.md Runs the contract test-suite for Solidity contracts. Requires solc and evm (go ethereum) to be installed and added to the $PATH. Test code is written in Solidity and executed directly in the evm. ```Shell npm run contracts-test ``` -------------------------------- ### TestBitsBitXor Example Source: https://github.com/ethereum/solidity-examples/blob/master/docs/testing.md An example test contract for the `Bits.bitXor` function. It iterates through different inputs and uses `assert` to verify the correctness of the XOR operation. ```solidity contract TestBitsBitXor is BitsTest { function testImpl() internal { for (uint8 i = 0; i < 12; i++) { assert(ONES.bitXor(ONES, i*20) == 0); assert(ONES.bitXor(ZERO, i*20) == 1); assert(ZERO.bitXor(ONES, i*20) == 1); assert(ZERO.bitXor(ZERO, i*20) == 0); } } } ``` -------------------------------- ### Solidity Bytes Padding Examples Source: https://github.com/ethereum/solidity-examples/blob/master/docs/bytes/Bytes.md Demonstrates how number literals and string literals are padded when assigned to fixed-size bytes32 variables in Solidity. Number literals are padded on the left, and string literals are padded on the right. ```solidity var numLit = bytes32(0x01020304); // 0x0000000000000000000000000000000000000000000000000000000001020304 var strLit = bytes32("01020304"); // 0x3031303230333034000000000000000000000000000000000000000000000000 ``` -------------------------------- ### Solidity Contract Linting Source: https://github.com/ethereum/solidity-examples/blob/master/README.md Applies solhint to all Solidity contracts in the library to enforce style guidelines and best practices. ```Shell npm run contracts-lint ``` -------------------------------- ### TypeScript Linting Source: https://github.com/ethereum/solidity-examples/blob/master/README.md Runs TS-lint on the entire TypeScript codebase of the library to ensure code quality and consistency. ```Shell npm run ts-lint ``` -------------------------------- ### Solidity Standard Library Commandline Tool Documentation Source: https://github.com/ethereum/solidity-examples/blob/master/README.md References the commandline tool documentation for the Solidity standard library, which covers functionalities like running tests and viewing documentation. ```Markdown For more information about the tests, such as the test file format, read the full [test documentation](./docs/testing.md). For running tests with the command-line tool, check the [CLI documentation](./docs/cli.md). For more information about perf read the full [perf documentation](./docs/perf.md). For running perf with the command-line tool, check the [CLI documentation](./docs/cli.md). The full docs can be found in the [commandline tool documentation](./docs/cli.md). ``` -------------------------------- ### Key Encoding Example in Patricia Trie Source: https://github.com/ethereum/solidity-examples/blob/master/docs/patricia_tree/PatriciaTree.md Illustrates the process of encoding a key (represented as a hash) into a path within the Merkle Patricia Trie. This example demonstrates how parts of the key hash are used to form edge labels and navigate the trie structure. ```solidity // Example of key encoding process: // Start with a 256-bit key hash. // Shared prefix with rootEdge label is removed (length becomes 252). // Most significant bit is checked (1 -> right), length becomes 251. // Next 3 bits shared with next label removed (length becomes 248). // Most significant bit checked (0 -> left), length becomes 247. // Next 7 bits shared with next label removed (length becomes 240). // Most significant bit checked (1 -> right), length becomes 239. // If no right edge exists, a new edge is created with the remaining key hash as data and the value's hash as the node. ``` -------------------------------- ### Solidity Contract Performance Testing Source: https://github.com/ethereum/solidity-examples/blob/master/README.md Runs the entire performance suite for Solidity contracts. Detailed information about performance testing can be found in the perf documentation. ```Shell npm run contracts-perf ``` -------------------------------- ### Solidity Bytes.equals Gas Measurement Source: https://github.com/ethereum/solidity-examples/blob/master/docs/perf.md An example of a performance functor that measures the gas cost of the Bytes.equals function when the lengths of the two byte arrays are different, causing an early exit. ```solidity contract PerfBytesEqualsDifferentLengthFail is BytesPerf { function perf() public payable returns (uint) { bytes memory bts1 = new bytes(0); bytes memory bts2 = new bytes(1); uint gasPre = msg.gas; Bytes.equals(bts1, bts2); uint gasPost = msg.gas; return gasPre - gasPost; } } ``` -------------------------------- ### Solidity Standard Library Documentation Links Source: https://github.com/ethereum/solidity-examples/blob/master/README.md Provides links to the documentation for various packages within the Solidity standard library, including bits, bytes, math, patricia_tree, strings, tokens, and unsafe operations. ```Markdown - [Bits](./docs/bits/Bits.md) - [Bytes](./docs/bytes/Bytes.md) - [ExactMath](./docs/math/ExactMath.md) - [PatriciaTree](./docs/patricia_tree/PatriciaTree.md) - [Data](./docs/patricia_tree/Data.md) - [Strings](./docs/strings/Strings.md) - [ERC20TokenFace](./docs/tokens/ERC20TokenFace.md) - [Memory](./docs/unsafe/Memory.md) ``` -------------------------------- ### BitsTest Contract with Constants Source: https://github.com/ethereum/solidity-examples/blob/master/docs/testing.md An abstract contract that extends `STLTest` and provides common constants and using directives for testing bitwise operations. ```solidity contract BitsTest is STLTest { using Bits for uint; uint constant ZERO = uint(0); uint constant ONE = uint(1); uint constant ONES = uint(~0); } ``` -------------------------------- ### STLTest Contract Source: https://github.com/ethereum/solidity-examples/blob/master/docs/testing.md The base contract for all tests. It defines the structure for a test, including a `test` method that executes the actual test logic via `testImpl`. ```solidity contract STLTest { function test() public payable returns (bool ret) { ret = true; testImpl(); } function testImpl() internal; } ``` -------------------------------- ### Inline Assembly: Extracting Byte to byte Type Source: https://github.com/ethereum/solidity-examples/blob/master/docs/bytes/Bytes.md Demonstrates extracting a byte from a `bytes32` variable into a `byte` type using inline assembly. It highlights potential issues with out-of-range values. ```assembly bytes32 b32 = "Terry A. Davis"; byte b; assembly { b := byte(0, b32) } // The value of b will be 0, but the internal representation of b32 is // 0x0000000000000000000000000000000000000000000000000000000000000054 // which is outside the allowed range for a byte type. ``` -------------------------------- ### Solidity byte (bytes1) Type Representation Source: https://github.com/ethereum/solidity-examples/blob/master/docs/bytes/Bytes.md Compares the internal representation of a `uint8` and a `byte` (alias for `bytes1`) in Solidity. They differ in padding, which is crucial for type conversions, especially in inline assembly. ```solidity // 0x0000000000000000000000000000000000000000000000000000000000000001 uint8 u8 = 1; // 0x0100000000000000000000000000000000000000000000000000000000000000 byte b = 1; ``` -------------------------------- ### Inline Assembly: Accessing Bytes Source: https://github.com/ethereum/solidity-examples/blob/master/docs/bytes/Bytes.md Shows how to use the `byte(index, data)` instruction in Solidity's inline assembly to read a specific byte from a data item. The index ranges from 0 to 31. ```assembly uint numLit_0; uint numLit_31; assembly { numLit_0 := byte(0, numLit) // Assuming numLit is a bytes32 variable numLit_31 := byte(31, numLit) } // numLit_0 == 0 // numLit_31 == 4 ``` -------------------------------- ### Solidity Perf Contract Base Source: https://github.com/ethereum/solidity-examples/blob/master/docs/perf.md Defines the basic structure for a Solidity contract used in performance testing. The 'perf' function is intended to return the gas spent during its execution. ```solidity contract STLPerf { function perf() public payable returns (uint); } ``` -------------------------------- ### Solidity Index Access in Fixed Size Bytes Source: https://github.com/ethereum/solidity-examples/blob/master/docs/bytes/Bytes.md Illustrates how to access individual bytes within fixed-size byte arrays (`bytesN`) in Solidity. The highest order byte is at index 0. ```solidity var numLit = bytes32(0x01020304); var numLit_0 = numLit[0]; // 0 var numLit_31 = numLit[31]; // 0x04 var strLit = bytes32("01020304"); var strLit_0 = strLit[0]; // 0x30 var strLit_31 = strLit[31]; // 0 ``` -------------------------------- ### Patricia Tree Structure Source: https://github.com/ethereum/solidity-examples/blob/master/docs/patricia_tree/PatriciaTree.md Describes the overall structure of the Merkle Patricia Trie, including the root, rootEdge, and the mapping of node hashes to Node objects. It clarifies how the root and rootEdge represent the entire tree. ```solidity contract PatriciaTree { bytes32 public root; Edge public rootEdge; mapping(bytes32 => Node) public nodes; // ... other functions } ``` -------------------------------- ### Generate Merkle Proof Source: https://github.com/ethereum/solidity-examples/blob/master/docs/patricia_tree/PatriciaTree.md Generates a Merkle proof for a given key in a Merkle Patricia Trie. It returns a branch mask and sibling hashes required for validation. ```solidity function getProof(bytes32 key) public view returns (bytes memory branchMask, bytes32[] memory siblings) { // Implementation details for generating Merkle proof // Requires access to the trie structure and hashing functions. // Returns a branch mask and sibling hashes. return (branchMask, siblings); } ``` -------------------------------- ### Bits Library Description Source: https://github.com/ethereum/solidity-examples/blob/master/docs/bits/Bits.md The `Bits` library is used for accessing and manipulating bits using numbers of type `uint` to represent bitfields. This library is essential for low-level bit manipulation within Solidity smart contracts. ```solidity The `Bits` library is used for accessing and manipulating bits using numbers of type `uint` to represent bitfields. ``` -------------------------------- ### Validate Merkle Proof Source: https://github.com/ethereum/solidity-examples/blob/master/docs/patricia_tree/PatriciaTree.md Validates a Merkle proof against a given root hash. It uses the key, value, branch mask, and sibling hashes to reconstruct the path to the root. ```solidity function validateProof(bytes32 key, bytes memory value, bytes memory branchMask, bytes32[] memory siblings, bytes32 rootHash) public view returns (bool) { // Implementation details for validating Merkle proof // Combines sibling hashes based on the branch mask to reconstruct the root. // Returns true if the proof is valid, false otherwise. return false; } ``` -------------------------------- ### UTF-8 String Validation Library Source: https://github.com/ethereum/solidity-examples/blob/master/docs/strings/Strings.md This Solidity library provides functions to validate if a string is a valid UTF-8 encoded string at runtime. It is useful for ensuring data integrity when dealing with string inputs in smart contracts. The library is inspired by Arachnid's string-utils. ```solidity library Strings { // Placeholder for actual library functions // Example: function isUTF8(string memory _str) internal pure returns (bool) { ... } } ``` -------------------------------- ### Patricia Tree Data Structures Source: https://github.com/ethereum/solidity-examples/blob/master/docs/patricia_tree/PatriciaTree.md Defines the fundamental data types used in the Merkle Patricia Trie implementation: Label, Edge, and Node. These structures are essential for representing the trie's nodes and the paths within it. ```solidity struct Label { bytes32 data; uint length; } struct Edge { Label label; bytes32 node; } struct Node { Edge[2] children; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.