Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strings.slice stored in structs when retrieved and converted into slice yields gibberish when console.logged #60

Open
0xJamesBong opened this issue Feb 6, 2023 · 0 comments

Comments

@0xJamesBong
Copy link

0xJamesBong commented Feb 6, 2023

I have a struct like this

struct Question {
        strings.slice question;
        strings.slice answer;
    }

And a mapping mapping(uint256 => QA) public questions; and a proposeQuestion function that looks like this

        public
        returns (bool done)
    {
        questions[questionId].question = _question.toSlice();
        questions[questionId].answer = _answer.toSlice();
        questionId++;
        return true;
    }

When I retrieve the question and the answer stored in the struct which is stored in a mapping, and console log it, using foundry

function testQuestion() public {

        string memory q = "What?";
        console.log(q, q.toSlice().toString());
        myContract.proposeQuestion(q, unicode"すみません");
        assertTrue(
            keccak256(abi.encodePacked(q)) ==
                keccak256(abi.encodePacked(q.toSlice().toString()))
        );
        (          
            strings.slice memory question,
            strings.slice memory answer
        ) = myContract.questions(0);
        console.log(question.toString());

The console prints out gibberish ,.��

However, if I just do this console.log(q, q.toSlice().toString()) they print out the same thing! What's going on here?

PS:

Also, when I tried this out, this test fails

 assertEq(
            keccak256(abi.encodePacked(q)),
            keccak256(abi.encodePacked(question.toString()))
        );

and yields this error

    Expected: 0xc649b6993c4fe2cdb223c885f0145101f4d5ef046eebceed76b6431c34626612
      Actual: 0xf8a49a8b34d42855db8ed1c016fa2fe0354abc43323b8d67be08e3837aae8b60

Test result: FAILED. 0 passed; 1 failed; finished in 5.19ms```
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant