[Ethereum] Is it possible to insert some custom data in the DATA field of an Ethereum Transaction

contract-invocationremixsoliditystoragetransactions

i am working on trying to get some data encoded/inserted into an ethereum transaction.

Let me describe my environment first, i am using Web3py, Ganache and Remix IDE. I am able to successfully communicate between the three as i can write python code in Jupyter Notebook which successfully interacts with the contract deployed on Remix. And i can see each and every transaction in Ganache.

Now, my task is to store some data on the Ethereum Blockchain ( not necessarily the mainnet, if it can be done on a local RPC or even a test net, it will be enough. )

I am trying to send the following Raw transaction

tx = {
    'nonce': nonce,
    'to': address,
    'value': web3.toWei(1, 'ether'),
    'gas': 2000000,
    'gasPrice': web3.toWei('10', 'wei'),
}

to my smart-contract, using

web3.eth.sendRawTransaction

however, i get the following error in web3py ( this is a huge error, but this is the part that seems pertinent, i will post whole error if you need me to. )

'message': 'VM Exception while processing transaction: revert', 'code': -32000

Then is tried to use this command

web3.eth.sendTransaction({
    'nonce': nonce,
    'to': address_of_contract,
    'value': web3.toWei(1, 'ether'),
    'gas': 2000000,
    'data' : '7b224e616d65223a202',
    'gasPrice': web3.toWei('10', 'wei'),
})

Whether i include the data field or exclude it in this command, it gives the same error;

'message': 'VM Exception while processing transaction: revert', 'code': -32000

Please help me out.

I just want to find a way to send data(some hexadecimal string or hex bytes) in a transaction.

Let me know what possibilities exist for any such functionality in Ethereum.

Thank You.

EDIT : Contract Code as requested by @iamdefinitelyhuman

pragma solidity ^0.4.17;

contract Inbox {
    string public message;
    string[] hex_storage;
    address owner;

     event printHex(string x);

    function Inbox(string initialMessage) public payable {
        owner = msg.sender;
        message = initialMessage;
    }

    modifier isOwner {
        require(msg.sender == owner);
        _;
    }

    function setMessage(string newMessage) public 
    {
        message = newMessage;
    }
        function getMessage() public view returns (string) 
        {
            return message;
        }

    function storeMember(string hex_data) public 
    {
        hex_storage.push(hex_data);
    }

    function showMember() public 
    {
        for (uint i=0; i<hex_storage.length; i++)
        {
            printHex(hex_storage[i]);
        }
    }

    function cleanOutBalance() isOwner public  
    {
        uint bal = address(this).balance;
        address(owner).transfer(bal);
    }

    function displayBalance() public view returns (uint)
    {
        uint bal = address(this).balance;
        return bal;
    }


}

So i know the Contract code is not perfect, but most functions perform rudmentary stuff ( get, set, pushing data into array )

i would like to target the storeMember function with my transaction. But, from what i have learned on Solidity, is that when you invoke a smart contract function from a front-end, the transaction contains some data of the targeted function in it's data field, hence it is impossible to alter that field if i am calling a Smart Contract function from the front-end. ( please correct me if i am wrong with this concept )

Therefore, i am trying to send a custom transaction (not a raw transaction), without invoking any function, to the contract. In the hopes that it may allow me to modify the Data field without any restrictions. ( I have also tried raw transactions, but they show the same error as listed above )

Please let me know if i can take any additional steps or precautions to allow me to encode data in the Data field of the transaction.

Best Answer

There are two types of transaction in Ethereum, one is simple balance transfer and the other one is making any modification in the contract(either it could be contract deployment/contract's method invocation).

The 'data' field in the raw transaction json structure of Ethereum contains the code to execute a transaction in the EVM. In case of contract deployment, 'data' field contains the byte code of the contract along with the parameters to call the constructor(if any). In case of method invocation, 'data' contains the method signature along with the parameters. In case of 'balance transfer', it is of no use. If you provide, it will be left unused.

So you cannot modify any data in the blockchain (either balance or any field in the deployed contract) without issuing a transaction.If the field needed to modify belongs to the contract, you have to expose a method which will set the field and you have to invoke it through a transaction. In that case, in your raw transaction, 'data' field will contain the method's signature and parameter list encoded in the way Ethereum expects.

If you want to make own custom transaction, you have to change the source code of Ethereum.

Hope it will help you to understand.

Related Topic