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

Add paymaster contract + mandos test #797

Merged
merged 7 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
Binary file added test/paymaster/output/paymaster.wasm
Binary file not shown.
179 changes: 179 additions & 0 deletions test/paymaster/scenarios/test_forward_call_wegld.scen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
{
"name": "paymaster",
"comment": "add then check",
"gasSchedule": "v3",
"steps": [
{
"step": "setState",
"accounts": {
"address:user": {
"nonce": "1",
"balance": "0",
"esdt": {
"str:FEE-123456": "100,000,000",
"str:WEGLD-123456": "100,000,000"
}
},
"address:owner": {
"nonce": "1"
},
"address:relayer": {
"nonce": "1",
"balance": "0"
},
"sc:wegld": {
"nonce": "0",
"balance": "100,000,000",
"esdt": {
"str:WEGLD-123456": {
"instances": [
{
"nonce": "",
"balance": "0"
}
],
"roles": [
"ESDTRoleLocalMint",
"ESDTRoleLocalBurn"
]
}
},
"storage": {
"str:wrappedEgldTokenId": "str:WEGLD-123456"
},
"code": "file:../../wegld-swap/output/multiversx-wegld-swap-sc.wasm"
}
},
"newAddresses": [
{
"creatorAddress": "address:owner",
"creatorNonce": "1",
"newAddress": "sc:paymaster"
}
]
},
{
"step": "scDeploy",
"id": "",
"tx": {
"from": "address:owner",
"contractCode": "file:../output/paymaster.wasm",
"arguments": [],
"gasLimit": "5,000,000",
"gasPrice": ""
},
"expect": {
"out": [],
"status": "0"
}
},
{
"step": "checkState",
"accounts": {
"address:user": {
"esdt": {
"str:FEE-123456": "100,000,000",
"str:WEGLD-123456": "100,000,000"
},
"storage": "*",
"code": "*",
"owner": "*",
"nonce": "*",
"balance": "0"
},
"+": ""
}
},
{
"step": "scCall",
"id": "paymaster-forward-execution",
"tx": {
"from": "address:user",
"to": "sc:paymaster",
"esdtValue": [
{
"tokenIdentifier": "str:FEE-123456",
"value": "20,000"
},
{
"tokenIdentifier": "str:WEGLD-123456",
"value": "100,000,000"
}
],
"function": "forwardExecution",
"arguments": [
"0x72656c617965725f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f",
"0x000000000000000000007765676c645f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f",
"0x756e7772617045676c64"
],
"gasLimit": "100,000,000",
"gasPrice": ""
},
"expect": {
"out": [],
"status": "0",
"gas": "*",
"refund": "*"
}
},
{
"step": "checkState",
"accounts": {
"address:relayer": {
"esdt": {
"str:FEE-123456": "20,000",
"str:WEGLD-123456": "0"
},
"storage": "*",
"code": "*",
"owner": "*",
"nonce": "1"
},
"address:user": {
"esdt": {
"str:FEE-123456": "99,980,000"
},
"balance": "100,000,000",
"storage": "*",
"code": "*",
"owner": "*",
"nonce": "2"
},
"sc:paymaster": {
"esdt": {
"str:FEE-123456": "0",
"str:WEGLD-123456": "0"
},
"balance": "0",
"storage": "*",
"code": "*",
"owner": "address:owner",
"nonce": "0"
},
"sc:wegld": {
"esdt": {
"str:FEE-123456": "0",
"str:WEGLD-123456": {
"instances": [
{
"nonce": "",
"balance": "0"
}
],
"roles": [
"ESDTRoleLocalMint",
"ESDTRoleLocalBurn"
]
}
},
"balance": "0",
"storage": "*",
"owner": "*",
"nonce": "0",
"code": "file:../../wegld-swap/output/multiversx-wegld-swap-sc.wasm"
},
"+": ""
}
}
]
}
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 @@
"crypto/elliptic"
"encoding/binary"
"errors"
"github.com/multiversx/mx-chain-core-go/data/vm"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go imports

"io"
basicMath "math"
"math/big"
Expand Down Expand Up @@ -139,7 +140,7 @@
// 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 @@
})
}

// 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 @@

// 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 @@
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 @@
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
Loading