Skip to content

Commit

Permalink
fixed back transfers on callBack.
Browse files Browse the repository at this point in the history
  • Loading branch information
sasurobert committed Nov 23, 2023
1 parent 55d946f commit 3475079
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
7 changes: 7 additions & 0 deletions integrationTests/json/scenariosContracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,10 @@ func TestCAttestation(t *testing.T) {
Run().
CheckNoError()
}

func TestRustPaymaster(t *testing.T) {
ScenariosTest(t).
Folder("paymaster/scenarios").
Run().
CheckNoError()
}
24 changes: 19 additions & 5 deletions vmhost/contexts/managedType.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/elliptic"
"encoding/binary"
"errors"
"github.com/multiversx/mx-chain-core-go/data/vm"
"io"
basicMath "math"
"math/big"
Expand Down Expand Up @@ -139,7 +140,7 @@ func (context *managedTypesContext) InitState() {
// PushState appends the values map to the state stack
func (context *managedTypesContext) PushState() {
newBigIntState, newBigFloatState, newEcState, newmBufferState, newmMapState := context.clone()
newTransfers := context.cloneBackTransfers()
newTransfers := cloneBackTransfers(context.managedTypesValues.backTransfers)
context.managedTypesStack = append(context.managedTypesStack, managedTypesState{
bigIntValues: newBigIntState,
bigFloatValues: newBigFloatState,
Expand All @@ -150,6 +151,21 @@ func (context *managedTypesContext) PushState() {
})
}

// PopBackTransferIfAsyncCallBack copies the back transfer from the top of the stack in case of callbacks
func (context *managedTypesContext) PopBackTransferIfAsyncCallBack(vmInput *vmcommon.ContractCallInput) {
if vmInput.CallType != vm.AsynchronousCallBack {
return

Check warning on line 157 in vmhost/contexts/managedType.go

View check run for this annotation

Codecov / codecov/patch

vmhost/contexts/managedType.go#L155-L157

Added lines #L155 - L157 were not covered by tests
}

managedTypesStackLen := len(context.managedTypesStack)
if managedTypesStackLen == 0 {
return

Check warning on line 162 in vmhost/contexts/managedType.go

View check run for this annotation

Codecov / codecov/patch

vmhost/contexts/managedType.go#L160-L162

Added lines #L160 - L162 were not covered by tests
}

prevState := context.managedTypesStack[managedTypesStackLen-1]
context.managedTypesValues.backTransfers = cloneBackTransfers(prevState.backTransfers)

Check warning on line 166 in vmhost/contexts/managedType.go

View check run for this annotation

Codecov / codecov/patch

vmhost/contexts/managedType.go#L165-L166

Added lines #L165 - L166 were not covered by tests
}

// PopSetActiveState removes the latest entry from the state stack and sets it as the current values map
func (context *managedTypesContext) PopSetActiveState() {
managedTypesStackLen := len(context.managedTypesStack)
Expand Down Expand Up @@ -797,7 +813,7 @@ func (context *managedTypesContext) AddValueOnlyBackTransfer(value *big.Int) {

// GetBackTransfers returns all ESDT transfers and accumulated value as well, will clean accumulated values
func (context *managedTypesContext) GetBackTransfers() ([]*vmcommon.ESDTTransfer, *big.Int) {
clonedTransfers := context.cloneBackTransfers()
clonedTransfers := cloneBackTransfers(context.managedTypesValues.backTransfers)

Check warning on line 816 in vmhost/contexts/managedType.go

View check run for this annotation

Codecov / codecov/patch

vmhost/contexts/managedType.go#L816

Added line #L816 was not covered by tests
context.managedTypesValues.backTransfers = backTransfers{
ESDTTransfers: make([]*vmcommon.ESDTTransfer, 0),
CallValue: big.NewInt(0),
Expand All @@ -806,9 +822,7 @@ func (context *managedTypesContext) GetBackTransfers() ([]*vmcommon.ESDTTransfer
return clonedTransfers.ESDTTransfers, clonedTransfers.CallValue
}

func (context *managedTypesContext) cloneBackTransfers() backTransfers {
currentBackTransfers := context.managedTypesValues.backTransfers

func cloneBackTransfers(currentBackTransfers backTransfers) backTransfers {
newBackTransfers := backTransfers{
ESDTTransfers: make([]*vmcommon.ESDTTransfer, len(currentBackTransfers.ESDTTransfers)),
CallValue: big.NewInt(0).Set(currentBackTransfers.CallValue),
Expand Down
1 change: 1 addition & 0 deletions vmhost/hostCore/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ func (host *vmHost) executeOnDestContextNoBuiltinFunction(input *vmcommon.Contra
managedTypes, _, metering, output, runtime, async, storage := host.GetContexts()
managedTypes.PushState()
managedTypes.InitState()
managedTypes.PopBackTransferIfAsyncCallBack(input)

Check warning on line 451 in vmhost/hostCore/execution.go

View check run for this annotation

Codecov / codecov/patch

vmhost/hostCore/execution.go#L451

Added line #L451 was not covered by tests

output.PushState()
output.CensorVMOutput()
Expand Down
1 change: 1 addition & 0 deletions vmhost/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ type ManagedTypesContext interface {
GetBackTransfers() ([]*vmcommon.ESDTTransfer, *big.Int)
AddValueOnlyBackTransfer(value *big.Int)
AddBackTransfers(transfers []*vmcommon.ESDTTransfer)
PopBackTransferIfAsyncCallBack(vmInput *vmcommon.ContractCallInput)
}

// OutputContext defines the functionality needed for interacting with the output context
Expand Down

0 comments on commit 3475079

Please sign in to comment.