Skip to content

Instantly share code, notes, and snippets.

@tpmccallum
Last active March 25, 2019 05:04
Show Gist options
  • Save tpmccallum/5851cc2d7047879a868da280b9378eb1 to your computer and use it in GitHub Desktop.
Save tpmccallum/5851cc2d7047879a868da280b9378eb1 to your computer and use it in GitHub Desktop.
A comparison of uint256 and bytes32 as well as draft code to research and test for the Chopsticks Challenge

uint256 code example

contract ChopsticksBytes {
    uint256 a = 0;
    
    constructor() public{
        a = 1;
    }
   
}

uint256 gas for the above code

{
    "Creation": {
        "codeDepositCost": "10600",
        "executionCost": "25086",
        "totalCost": "35686"
    }
}

bytes3 code example

contract ChopsticksBytes {
    bytes3 a = hex"000000";
    
    constructor() public{
        a = hex"000000";
    }
   
}

bytes3 gas for the above code

{
    "Creation": {
        "codeDepositCost": "10600",
        "executionCost": "40610",
        "totalCost": "51210"
    }
}

Interestingly the uint256 is cheaper in terms of gas.

More interestingly bytes32 is considerably cheaper than both bytes8 and uint256 //TODO explain this succinctly

contract ChopsticksBytes {
    bytes32 a;
    
    constructor() public{
        a = hex"000000";
    }
   
}
{
    "Creation": {
        "codeDepositCost": "10600",
        "executionCost": "5089",
        "totalCost": "15689"
    }
}

The cool part is that bytes32 actually allows us to play with a really huge number


contract ChopsticksBytes {
    bytes32 a;
    
    constructor() public{
        a = hex"0000000000000000000000000000000000000000000000000000000000000000";
    }
   
}

This is really interesting because even a simple boolen is twice as expensive as a bytes32

contract ChopsticksBytes {
    bool a;
    
    constructor() public{
        a = false;
    }
   
}

{
    "Creation": {
        "codeDepositCost": "10600",
        "executionCost": "20333",
        "totalCost": "30933"
    }
}

This boolean is almost the same gas as a bytes 1

contract ChopsticksBytes {
    bytes1 a;
    
    constructor() public{
        a = hex"00";
        }
   
}
{
    "Creation": {
        "codeDepositCost": "10600",
        "executionCost": "20338",
        "totalCost": "30938"
    }
}

Explicit converstion of a single digit int is also just as expensive, so its settled

contract ChopsticksBytes {
    bytes32 a; 

    
    constructor() public{
          a = bytes32(5);
    }
}
{
    "Creation": {
        "codeDepositCost": "10600",
        "executionCost": "20097",
        "totalCost": "30697"
    }
}

Rough outline of contract[s]

pragma lity ^1.2.3;

contract Hand {
    bytes32 a;
    constructor(bytes32 _initialHand) public {
        a = _initialHand;
    }

    function getHand() public view returns(bytes32) {
        return a;
    }

    function setHand(bytes32 _updatedHand) public {
        a = _updatedHand;
    }
}

contract Hands {
    event NewHand(address owner, string side, address newHand);
    constructor() public {
        Hand leftHand = new Hand(hex"000001");
        emit NewHand(msg.sender, "LeftHandSide", leftHand);
        Hand rightHand = new Hand(hex"100001");
        emit NewHand(msg.sender, "RightHandSide", rightHand);
    }

}

contract Games {
    event GamesContractDeployed(address addressOFGamesContract);
    event NewPlayerAccount(address player, address hands);
    mapping(address => address) private playerAccounts;

    constructor() public {
        emit GamesContractDeployed(this);
    }
    
    function joinAGame() public {
        Hands playersHands = new Hands();
        playerAccounts[msg.sender] = playersHands;
        emit NewPlayerAccount(msg.sender, playersHands);
    }
    

}

Instructions

    // INSTRUCTION
    // The giver is always on the left followed by which hand
    // 0 is playerA and 1 is playerB
    // 0 is leftHand and 1 is rightHand
    // 00000101 playerA's leftHand is giving to playerB's rightHand
    // 00000100 playerA's leftHand is giving to playerB's leftHand
    // 00010101 playerA's rightHand is giving to playerB's rightHand
    // 00010100 playerA's rightHand is giving to playerB's leftHand
   
    // 01000001 playerB's leftHand is giving to playerA's rightHand
    // 01000000 playerB's leftHand is giving to playerA's leftHand
    // 01010001 playerB's rightHand is giving to playerA's rightHand
    // 01010000 playerB's rightHand is giving to playerA's leftHand
    //bytes32 instructions = hex"0000";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment