Skip to content

Toolchain stack

IosifKoen edited this page Dec 17, 2023 · 1 revision

Toolchain

In this page we will discuss the the toolchain which is used to build ItemBlocks. We will see the tools that are used for development, testing and static analysis tools.

Foundry

For the most part of the contract building authors used Foundry an open source modular and very fast tool kit for Ethereum application development. Find more information here

Contract development

Foundry provides a full smart-contract project starting point, where developers can start to create their smart-contract. It is very easy to initialize a project the command is forge init in a directory where you want to store your project. Then Foundry will create multiple directories and files where you can work with.

The most important directories are lib, src and test.

  • lib Under this directory are all the dependencies for the smart contract. The command to add a dependency is: forge install <name-of-module>
  • src Under this directory should be all the source code of the project related to smart contract.
  • test Under this directory developers can run all the tests for their smart contract.

Testing

Authors used the forge tool from Foundry for testing purposes. And also to see the coverage of their tests.

As mentioned before the tests are under the test directory. Authors decided to create four different files for the test purposes. In this way they manage to develop more readable, manageable and more maintainable tests.

The structure is: 🚧

├── ItemBlocksTest.t.sol
├── TestConstructor.t.sol
├── TestCreatePassport.t.sol
└── TestGetters.t.sol

As the names of the files shows each of these files contains tests for specific functions of the smart contract. For example:

In TestGetters.t.sol there are all the test for the getters function of the smart contract.

    function testGetPassport() public {
        ItemBlocks.Passport memory p = itemBlocks.getPassport(7);
        assertEq(p.name, testPassport.name);
        assertEq(p.desc, testPassport.desc);
        assertEq(p.family, testPassport.family);
        assertEq(p.url, testPassport.url);
        assertEq(p.img, testPassport.img);
    }

In this test authors test the testGetPassport function if it returns the item passport correctly.

In TestCreatePassport.t.sol there are all the tests for update and create the passport of an item.

   function testUpdatePassportEligibileCreator() public {
        ItemBlocks.Passport memory testPassport = ItemBlocks.Passport({
            name: "TestItemName",
            desc: "TestItemDesc",
            family: "TestItemFamily",
            url: "TestItemUrl",
            img: "TestItemImg"
        });

        vm.prank(address(42));
        uint256 tokenId = itemBlocks.createPassport(7, testPassport.name, testPassport.desc, testPassport.family, testPassport.url, testPassport.img);
       
        vm.prank(address(42));
        itemBlocks.updateOwnership(address(42), address(43), 7);

        ItemBlocks.Passport memory updatedPassport = ItemBlocks.Passport({
            name: "UpdatedItemName",
            desc: "UpdatedItemDesc",
            family: "UpdatedItemFamily",
            url: "UpdatedItemUrl",
            img: "UpdatedItemImg"
        });

        //Eligible as the creator
        vm.prank(address(42));
        itemBlocks.updatePassport(tokenId, updatedPassport.name, updatedPassport.desc, updatedPassport.family, updatedPassport.url, updatedPassport.img);
    }

}

In this test the authors check if the user who wants to update the information of the item is eligible creator of the item.

And goes on...

After the testing development authors run these tests using the forge test -vvv.

For the ItemBlocks project to run the test needs to be in the test directory.

Stating from the root directory of the project to run the test needs to run this command: cd contract/ & forge test -vvv

And to take the statics of the test coverage used the command forge statistics.

Similar as before to see the test statistics and coverage for the ItemBlocks you need to run this command form the root directory of the project: cd contract/ && forge statistics.

Fuzzing Testing

Authors used Echidna that is a tool for fuzzing/property-based testing of Ethereum smart contracts. To run fuzzing tests in ItemBlocks project you need this command: cd contract/ && echidna ./echidna/PropertyTest.sol --contract ItemBlocksPropertyTest.

You can read more about Echidna tool and fuzzing tests here.

Statistic analysis

Authors used Slither a static analysis framework. So to check the vulnerabilities of their contract. And have better and more comprehensive coverage coverage and analysis for their contract. To run a statistic analysis to ItemBlocks you need to run this command: slither --checklist src/ItemBlocks.sol > Slither-Report.md this command not only will run a statistic analysis but also will write this analysis to a markdown file for better readability.

You can find more information for Slither here.

Clone this wiki locally