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

Possibility to print initial state and final state #2

Open
0x090909 opened this issue Nov 22, 2023 · 5 comments
Open

Possibility to print initial state and final state #2

0x090909 opened this issue Nov 22, 2023 · 5 comments

Comments

@0x090909
Copy link

Hello is it possible to print in the final high level output the state at the start of the transaction and the state at the end?

For example initial balance and final balance

@aj3423
Copy link
Owner

aj3423 commented Nov 22, 2023

Hi, I see in your previous issue you mentioned more examples, actually all my use cases were about predicting nft, tracing the sha3 parameters, they are all the same, I guess your use case is a bit different. In most cases, the balance is not used in sha3, so it's not handled at all, see:

edb/opcode.go

Line 881 in 5a4f5d3

// when input is empty, it's transfer: `addr.call{value:xxx}("")`

But maybe add some lines to make it work (not tested):

func opCall(ctx *Context) error {
	stack := ctx.Stack()

	gas, addr, value, inOffset, inSize, retOffset, retSize :=
		stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop()

	// when input is empty, it's transfer: `addr.call{value:xxx}("")`
	if inSize.IsZero() {
		fromAddr := ctx.Call().This
		toAddr := common.Address(addr.Bytes20())
		fromBalance := ctx.Contracts[fromAddr].Balance
		toBalance := ctx.Contracts[toAddr].Balance

		fromBalance.Sub(fromBalance, value.ToBig())
		toBalance.Sub(toBalance, value.ToBig())

		// just assume it succeeded
		stack.Push(*uint256.NewInt(1))

		return nil
	}

	return do_opcall(ctx,
		gas, addr, value, inOffset, inSize, retOffset, retSize)
}

And modify

edb/opcode.go

Line 448 in 5a4f5d3

func opBalance(ctx *Context) error {
to:

func opBalance(ctx *Context) error {
	slot := ctx.Stack().Peek()
	address := common.Address(slot.Bytes20())
	bal, e := ensure_balance(ctx, address)
	if e != nil {
		return e
	}
	slot.SetFromBig(bal)
	return nil
}

About printing, there is command ctx or context, it prints everything in json format, including the balance, you can call it before and after the execution.

@0x090909
Copy link
Author

my use case is the following: Im tracing a contract call, specifically Im after the call trace, my problem is that the trace call results are different than the result you get after the transaction is mined, so I want to figure out why is that

@0x090909
Copy link
Author

@aj3423 thank you for tips! I actually need selfbalance which I see is already implemented correct? I only have to edit the opCall procedure?

@aj3423
Copy link
Owner

aj3423 commented Nov 22, 2023

The opBalance handles the selfbalance.
The opCall handles transfering, eg: addr.call{value:xxx}("")

@0x090909
Copy link
Author

Thank you, your tool is super cool

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

2 participants