I have a problem with adding new opcode to solidity. I'm using solc (on C++) and geth(ethereum on Go). I want to add new opcode, that takes address payable, uint256, uint256, bytes memory and returns bytes memory. So I have a problem with return value.
Some peaces of code below, I will skip some files, to make question shorter.
Solc
libsolidity/codegen/ExpressionCompiler.cpp
// ExpressionCompiler::visit(FunctionCall const& _functionCall)
case FunctionType::Kind::MyOpcode:
{
acceptAndConvert(*arguments[0], *function.parameterTypes()[0], true);
acceptAndConvert(*arguments[1], *function.parameterTypes()[1], true);
acceptAndConvert(*arguments[2], *function.parameterTypes()[2], true);
arguments[3]->accept(*this);
utils().fetchFreeMemoryPointer();
utils().packedEncode(
{arguments[3]->annotation().type},
{TypeProvider::array(DataLocation::Memory, true)}
);
utils().toSizeAfterFreeMemoryPointer();
m_context << Instruction::MYOPCODE;
}
libsolidity/analysis/GlobalContext.cpp
// inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariables()
magicVarDecl("myopcode", TypeProvider::function(strings{"address payable", "uint256", "uint256", "bytes memory"}, strings{"bytes memory"}, FunctionType::Kind::MyOpcode, false, StateMutability::Payable)),
libevmasm/Instruction.cpp
// static std::map<Instruction, InstructionInfo> const c_instructionInfo =
{ Instruction::MYOPCODE, { "MYOPCODE", 0, 5, 1, true, Tier::Base } }
Geth
core/vm/jump_table.go
// func newFrontierInstructionSet() JumpTable {
CALLACTOR: {
execute: opMyOpCode,
dynamicGas: gasCallActor,
minStack: minStack(5, 1),
maxStack: maxStack(5, 1),
memorySize: memoryReturn,
writes: true,
returns: true,
},
core/vm/instructions.go
func opMyOpcode(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
inoffset, insize := callContext.stack.pop(), callContext.stack.pop()
params := callContext.memory.GetPtr(int64(inoffset.Uint64()), int64(insize.Uint64()))
secondValue := callContext.stack.pop()
firstValue := callContext.stack.pop()
addr := callContext.stack.pop()
// ... Do smth with input ...
outoffset := inoffset.Uint64() + insize.Uint64()
callContext.memory.Set(outoffset, 1, []byte{0x2})
tmp := make([]byte, 1)
tmp[0] = 0x98
callContext.memory.Set(outoffset + 1, 1, tmp)
callContext.stack.push(uint256.NewInt().SetUint64(outoffset))
return tmp, nil
}
Smart contract
pragma solidity >=0.6.0; // SPDX-License-Identifier: GPL-3
contract test {
event ReturnValue(address payable _from, bytes data);
function f() public returns(bytes memory){
address payable addr1 = payable(msg.sender);
bytes memory params = new bytes(2);
params[0] = 0x21;
params[1] = 0x22;
bytes memory result = myopcode(addr1, 0x11, 0x12, params);
emit ReturnValue(addr1, result);
return result;
}
}
When I run that code I get invalid jump destination. So, what I need to do, to get my code work correctly?
I found a solution, it certainly looks stupid, but this case is already provided by the developers. All you need is utils().returnDataToArray().
Related
I'm making an IsPrime function using Solidity Assembly code.
The function takes a number as parameter and returns true for prime, and false otherwise.
I've gotten my function to work, but I had to do a workaround outside of the assembly code.
See the code below:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.4.26;
contract PrimeNumber{
function isPrimeNumber(uint num1) public view returns(bool) {
uint result = 20; // bool result = true;
assembly{
for {let i := 2} lt(i, num1) {i := add(i, 1)}{
if eq(mod(num1, i), 0) {result := 10} // result := false
}
}
if(result == 10){
return false;
}
return true; // return result;
}
}
So the function works, but I can't for the life of me, get it to work properly using only BOOL and assembly.
I had to add a normal if/else statement after the assembly because I could only get the result to work properly as a UINT type.
I've tried switch statements but I get the error "true and false are not valid literals"
See comments for what I want to do.
Solidity assembly is not able to work with true and false literals until Solidity version 0.6. I haven't found any info about this in the docs - only validated by trying different versions.
You can bypass this limitation by using false-like value, e.g. 0.
pragma solidity ^0.4.26;
contract PrimeNumber{
function isPrimeNumber(uint num1) public view returns(bool) {
bool result = true;
assembly{
for {let i := 2} lt(i, num1) {i := add(i, 1)}{
if eq(mod(num1, i), 0) {
result := 0
}
}
}
return result;
}
}
Note: Latest version of Solidity (November 2022) is 0.8.17. So if your use case allows it, I'd recommend you to use the latest version as it contains bug fixes and security improvements.
I'm practicing on an ERC_721 contract and would like to know if there's a way to store JSON data directly on the contract, take a look at my approach and give your thoughts:
The variation1.json file uploaded to IPFS:
{
"attributes": [
{
"trait_type": "Rank",
"value": "1"
}
],
"description": "A simple NFT",
"image": "Qmdasifhw89rv92enfkq128re3",
"name": "NFT#1"
}
Then I would use an object like the one below to go with the contract that references the file to the urlOfTokenURI
[
{
"file": "variation1.json", "urlOfTokenURI": https://gateway.pinata.cloud/ipfs/Qmdasifhw89rv92enfkqeqe23f"
},
{
"file": "variation2.json", "urlOfTokenURI": "https://gateway.pinata.cloud/ipfs/Qmgvfrg34vt4u785ygbmasdsa"
},
...
]
Then get a random number, retrieve a random urlOfTokenURI and mint the token to the desired address, excluding that token from the list of available tokens:
function mintNFT(address recipient, string memory tokenURI)
public onlyOwner
returns (uint256)
{
_tokenIds.increment();
//retrieve object, get random number and mint with the urlOfTokenURI
//from random number index
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
What's missing here for this to work?
Can someone guide me how to proceed or have a suggestion for another approach? I'm kind of lost.
Don't understand how to mint a random NFT and instantly revealing it to the user.
Sorry in advance if it's a stupid question. I'm learning, btw.
Appreciate the help!
If you need a real random number fn, you need to look for oracle service (e.g. Chainlink)
Yes, it is possible to generate an uri without publishing to IPFS. You can generate a base64 data uri instead. Check out Example from wizard and dragon game and their contract
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/utils/Strings.sol";
...
using Strings for uint256;
using Base64 for bytes;
...
function tokenURI(uint256 id) public view returns (string memory) {
string memory metadata = string(abi.encodePacked(
"{\"name\": \"NFT#",
id.toString(),
"\", \"description\": \"A simple NFT\", \"attributes\":",
"[{\"key\":\"trait_type\",\"value\":\"1\"}]",
"}"
));
return string(abi.encodePacked(
"data:application/json;base64,",
bytes(metadata).base64()
));
}
Base64 lib
pragma solidity ^0.8.0;
library Base64 {
string internal constant TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function base64(bytes memory data) internal pure returns (string memory) {
if (data.length == 0) return "";
// load the table into memory
string memory table = TABLE;
// multiply by 4/3 rounded up
uint256 encodedLen = 4 * ((data.length + 2) / 3);
// add some extra buffer at the end required for the writing
string memory result = new string(encodedLen + 32);
assembly {
// set the actual output length
mstore(result, encodedLen)
// prepare the lookup table
let tablePtr := add(table, 1)
// input ptr
let dataPtr := data
let endPtr := add(dataPtr, mload(data))
// result ptr, jump over length
let resultPtr := add(result, 32)
// run over the input, 3 bytes at a time
for {} lt(dataPtr, endPtr) {}
{
dataPtr := add(dataPtr, 3)
// read 3 bytes
let input := mload(dataPtr)
// write 4 characters
mstore(resultPtr, shl(248, mload(add(tablePtr, and(shr(18, input), 0x3F)))))
resultPtr := add(resultPtr, 1)
mstore(resultPtr, shl(248, mload(add(tablePtr, and(shr(12, input), 0x3F)))))
resultPtr := add(resultPtr, 1)
mstore(resultPtr, shl(248, mload(add(tablePtr, and(shr( 6, input), 0x3F)))))
resultPtr := add(resultPtr, 1)
mstore(resultPtr, shl(248, mload(add(tablePtr, and( input, 0x3F)))))
resultPtr := add(resultPtr, 1)
}
// padding with '='
switch mod(mload(data), 3)
case 1 { mstore(sub(resultPtr, 2), shl(240, 0x3d3d)) }
case 2 { mstore(sub(resultPtr, 1), shl(248, 0x3d)) }
}
return result;
}
}
I am trying to follow this tutorial here:
But in the tutorial he doesnt specify how to implement the contract. So I tried to do it using truffle and ganache-cli. In a truffle test I have tried using the following code:
const amount = web3.toWei(5, 'ether');
const Contract = await GmsPay.new({from : Sender, value : web3.toWei(10, 'ether')});
const hash = Web3Beta.utils.soliditySha3(
{t : 'address', v : Recipient},
{t : 'uint256', v : amount},
{t : 'uint256', v : 1},
{t : 'address', v : Contract.address}
);
const sig = await Web3Beta.eth.sign(hash, Sender);
const res = await Contract.claimPayment(amount, 1, sig, {from : Recipient});
But I just keep getting, "Error: VM Exception while processing transaction: revert". Using the debuger I see that my code executes down to:
require(recoverSigner(message, sig) == owner);
Even if I take that line out the last line still doesnt work. What am I doing wrong? Any help would be greatly appreciated.
Ran into similar challenges in my truffle tests with the 'recoverSigner(message, sig) == owner' check. After comparing R,S,V, values produced in the solidity recoverSigner() function vs. the same values generated on the test side using the ethereumjs-util's fromRpcSig() function, I realized that recoverSigner is returning V as a 0, whereas fromRpcSig has this value at 27. This line provided the working answer.
Final splitSignature() function included below if you run into a similar issue.
function splitSignature(bytes memory sig)
internal
pure
returns (uint8, bytes32, bytes32)
{
require(sig.length == 65);
bytes32 r;
bytes32 s;
uint8 v;
assembly {
// first 32 bytes, after the length prefix
r := mload(add(sig, 32))
// second 32 bytes
s := mload(add(sig, 64))
// final byte (first byte of the next 32 bytes)
v := byte(0, mload(add(sig, 96)))
}
// support both versions of `eth_sign` responses
if (v < 27)
v += 27;
return (v, r, s);
}
I have a contract A and a contract B.
Contract A declares this function:
function getIntValue() constant returns (uint);
What would be the appropriate assembly code to delegatecall contract A's getIntValue function from B? I'm not yet very experienced with assembly so I only have this so far which doesn't work:
function getContractAIntValue() constant returns (uint c) {
address addr = address(contractA); // contract A is stored in B.
bytes4 sig = bytes4(sha3("getIntValue()")); // function signature
assembly {
let x := mload(0x40) // find empty storage location using "free memory pointer"
mstore(x,sig) // attach function signature
let status := delegatecall(sub(gas, 10000), addr, add(x, 0x04), 0, x, 0x20)
jumpi(invalidJumpLabel, iszero(status)) // error out if unsuccessful delegatecall
c := mload(x)
}
}
Maybe you have solved it cause was asked more than one year ago, but in case some is still looking for it...
address addr = address(contractA); // contract A is stored in B.
bytes memory sig = abi.encodeWithSignature("getIntValue()"); // function signature
// solium-disable-next-line security/no-inline-assembly
assembly {
let result := delegatecall(sub(gas, 10000), addr, add(sig, 0x20), mload(sig), 0, 0)
let size := returndatasize
let ptr := mload(0x40)
returndatacopy(ptr, 0, size)
// revert instead of invalid() bc if the underlying call failed with invalid() it already wasted gas.
// if the call returned error data, forward it
switch result case 0 { revert(ptr, size) }
default { return(ptr, size) }
}
The contract code:
pragma solidity ^0.4.10;
contract Test {
mapping (bytes32 => uint8) private dict;
function Test() {}
function Set(bytes32 key, uint8 val) returns (uint8) {
dict[key] = val;
return dict[key];
}
function Get(bytes32 key) returns (uint8) {
return dict[key];
}
}
and I run on testrpc:
contract_file = 'test/test.sol'
contract_name = ':Test'
Solc = require('solc')
Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
source_code = fs.readFileSync(contract_file).toString()
compiledContract = Solc.compile(source_code)
abi = compiledContract.contracts[contract_name].interface
bytecode = compiledContract.contracts[contract_name].bytecode;
ContractClass = web3.eth.contract(JSON.parse(abi))
contract_init_data = {
data: bytecode,
from: web3.eth.accounts[0],
gas: 1000000,
}
deployed_contract = ContractClass.new(contract_init_data)
deployed_contract.Set.call("akey", 5)
deployed_contract.Get.call("akey")
bizarrely, this is the output I get in the node terminal:
> deployed_contract.Set.call("akey", 5)
{ [String: '5'] s: 1, e: 0, c: [ 5 ] }
> deployed_contract.Get.call("akey")
{ [String: '0'] s: 1, e: 0, c: [ 0 ] }
This result is the outcome of a long debugging session... what is going on? It seems distinctly like something is broken here, but I followed a tutorial which did something very similar which seems to work...
also:
> Solc.version()
'0.4.11+commit.68ef5810.Emscripten.clang'
Try this deployed_contract.Set("akey", 5) without .call
Because .call on your setter method
executes a message call transaction, which is directly executed in the
VM of the node, but never mined into the blockchain.
doc
The value of the map does not change. I bet 0 is the default value when nothing is set
By the way try using the online compiler you will quickly see if the problem is on the contract or the way you interact with it with web3.