warp icon indicating copy to clipboard operation
warp copied to clipboard

Contract Swap Bug

Open piwonskp opened this issue 2 years ago • 0 comments

Contract Swap Bug

This bug is highly suspected to be a solc-typed-ast bug and not a Warp one.

The bug happens with user defined types defined in different source files

The bug starts happening just after StorageAllocator pass where the Contract Definitions are swapped by Cairo Contracts Definitions (basically a super set of Contract Definition, the same stuff plus other things)

Struct Bug

file1.sol

contract A {
	struct S { ...}
}

file2.sol

import "file1.sol";

contract B {
	 function getS() public pure returns (S memory) {
			return S(...);
   }
}

This would throw an error (after the StorageAllocator), because when transpiling B, getNodeType won’t be able to find the definition of S. After closer inspection the cause is that getNodeType will be using the old contract definiton (the one which was swapped during StorageAllocator) instead of the actual one on the AST 😱.

Remember this only happens if both contracts are on different source units.

The patch to this is linking the replaced node context to the current one, and it works as expected.

Address Bug

file1.sol

import "file2.sol";

contract A {
		B b;
}

file2.sol

import "file1.sol";

contract B {
   A a;
}

After Storage Allocator and this type of contracts where they both import each other, after replacing the Contract Definitions for the Cairo ones, the context get’s lost completely to what contracts have been defined and which haven’t.

This bug is unpatched, and how to fix it is not clear yet.

A proposed solution is check on solc-typed-ast library latest released and see if this kind of things are fixed.

If there is no replacement, no issues related to this arise.

piwonskp avatar Jan 15 '23 19:01 piwonskp