Solution - GAS Consumption Issue

Since it is assumed that all code is included in transactions when a contract is deployed, if more contracts require more logic, a larger amount of GAS will be consumed accordingly.

Therefore, if the field part is made as a structure independent to the Contract, the contract need be deployed just once.

An object is created as a structure object within the contract that is deployed, and it is referenced by mapping to it's id.

  • Class diagram

    Structure

  • Source code

    contract SomeThing {
    
        struct SomeThingStruct {
            bytes32 id;
            string name;
        }
        mapping (bytes32 => SomeThingStruct) someThings;
    
        function create(bytes32 _id, string _name) {
            someThings[_id] = SomeThingStruct(_id, _name);
        }
    
        function getId(bytes32 _id) constant returns(bytes32) {
            return someThings[_id].id;
        }
    
        function getName(bytes32 _id) constant returns(string) {
            return someThings[_id].name;
        }
    }
    

    Structure

This will require 275,234 GAS during deployment, but the cost for each subsequent object creation is reduced to 63,250 GAS (22.98%).

Note

Although we were able to reduce GAS, this method has one problem.

When Contract is deployed, an address that uniquely identifies the deployed Contract is assigned on Ethereum.

In the case of the structure, each instance address is not available, so it is necessary refer to the structure instance by a unique key (in this case, the id field).

If keys are duplicated, it is necessary to prevent duplicate registration by throwing an error with throw etc.

  • Duplicate check at registration

    contract SomeThing {
        struct SomeThingStruct {
            bytes32 id;
            string name;
        }
        mapping (bytes32 => SomeThingStruct) someThings;
    
        function create(bytes32 _id, string _name) {
            if (someThings[_id].id != "") throw;
            someThings[_id] = SomeThingStruct(_id, _name);
        }
        function getId(bytes32 _id) constant returns(bytes32) {
            return someThings[_id].id;
        }
    
        function getName(bytes32 _id) constant returns(string) {
            return someThings[_id].name;
        }
    }
    

Basically, in Ethrereum smart contract logic is processed serially, so if you create new objects as described above, duplicates can be reliably eliminated.

Supplement

Since the dependency between data part and the logic at the time of deployment is reason for this issue, it is conceivable to represent the data part as another contract. However the result is as follows.

  • Source code

    contract SomeThingFactory {
        mapping (bytes32 => SomeThing) someThings;
    
        function create(bytes32 _id, string _name) {
            someThings[_id] = new SomeThing(_id, _name);
        }
    
        function get(bytes32 _id) constant returns(address) {
            return address(someThings[_id]);
        }
    }
    contract SomeThing {
        bytes32 private id;
        string private name;
        function SomeThing(bytes32 _id, string _name) {
            id = _id;
            name = _name;
        }
        function getId() constant returns(bytes32) {
            return id;
        }
        function getName() constant returns(string) {
            return name;
        }
    }
    
  • Generation of SomeThingFactory and execution of SomeThingFactory # create

    Faculty execution result

Actually it consumed 200,247 GAS, and so it can be senn that this did not contribute to a reduction in GAS usage.

Even if you generate another contract (new) via another contract (SomeThingFactory), the same GAS as the standard deployment pattern is consumed.

results matching ""

    No results matching ""