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 all 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"
},
"+": ""
}
}
]
}
23 changes: 15 additions & 8 deletions vmhost/contexts/instanceTracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"bytes"
"errors"
"fmt"

"github.com/multiversx/mx-chain-core-go/core/check"
Expand All @@ -15,6 +16,8 @@

type instanceCacheLevel int

var errTooManyInstances = errors.New("too many instances")

const (
// Warm indicates that the instance to track is a warm instance
Warm instanceCacheLevel = iota
Expand Down Expand Up @@ -178,27 +181,27 @@

// UseWarmInstance attempts to retrieve a warm instance for the given codeHash
// and to set it as active; returns false if not possible
func (tracker *instanceTracker) UseWarmInstance(codeHash []byte, newCode bool) bool {
func (tracker *instanceTracker) UseWarmInstance(codeHash []byte, newCode bool) (bool, error) {
instance, ok := tracker.GetWarmInstance(codeHash)
if !ok {
return false
return false, nil
}

ok = instance.Reset()
if !ok {
tracker.warmInstanceCache.Remove(codeHash)
return false
return false, nil

Check warning on line 193 in vmhost/contexts/instanceTracker.go

View check run for this annotation

Codecov / codecov/patch

vmhost/contexts/instanceTracker.go#L193

Added line #L193 was not covered by tests
}

if newCode {
// A warm instance was found, but newCode == true, meaning this is an
// upgrade; the old warm instance must be cleaned
tracker.ForceCleanInstance(false)
return false
return false, nil

Check warning on line 200 in vmhost/contexts/instanceTracker.go

View check run for this annotation

Codecov / codecov/patch

vmhost/contexts/instanceTracker.go#L200

Added line #L200 was not covered by tests
}

tracker.SetNewInstance(instance, Warm)
return true
err := tracker.SetNewInstance(instance, Warm)
return true, err
}

// ForceCleanInstance cleans the active instance and evicts it from the
Expand Down Expand Up @@ -237,7 +240,6 @@
// SaveAsWarmInstance saves the active instance into the internal warm instance cache
func (tracker *instanceTracker) SaveAsWarmInstance() {
lenCacheBeforeSaving := tracker.warmInstanceCache.Len()

codeHashInWarmCache := tracker.warmInstanceCache.Has(tracker.codeHash)

if codeHashInWarmCache {
Expand Down Expand Up @@ -289,13 +291,18 @@
}

// SetNewInstance sets the given instance as active and tracks its creation
func (tracker *instanceTracker) SetNewInstance(instance executor.Instance, cacheLevel instanceCacheLevel) {
func (tracker *instanceTracker) SetNewInstance(instance executor.Instance, cacheLevel instanceCacheLevel) error {
tracker.ReplaceInstance(instance)
tracker.cacheLevel = cacheLevel
if cacheLevel != Warm {
tracker.updateNumRunningInstances(+1)
}
tracker.instances[instance.ID()] = instance

if len(tracker.instances) >= warmCacheSize-1 {
return errTooManyInstances

Check warning on line 303 in vmhost/contexts/instanceTracker.go

View check run for this annotation

Codecov / codecov/patch

vmhost/contexts/instanceTracker.go#L303

Added line #L303 was not covered by tests
}
return nil
}

// ReplaceInstance replaces the currently active instance with the given one
Expand Down
26 changes: 13 additions & 13 deletions vmhost/contexts/instanceTracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestInstanceTracker_TrackInstance(t *testing.T) {
AlreadyClean: false,
}

iTracker.SetNewInstance(newInstance, Bytecode)
_ = iTracker.SetNewInstance(newInstance, Bytecode)
iTracker.codeHash = []byte("testinst")

require.False(t, iTracker.IsCodeHashOnTheStack(iTracker.codeHash))
Expand All @@ -38,7 +38,7 @@ func TestInstanceTracker_InitState(t *testing.T) {
require.Equal(t, 0, iTracker.numRunningInstances)

for i := 0; i < 5; i++ {
iTracker.SetNewInstance(mock.NewInstanceMock(nil), Bytecode)
_ = iTracker.SetNewInstance(mock.NewInstanceMock(nil), Bytecode)
}

require.Equal(t, 5, iTracker.numRunningInstances)
Expand All @@ -63,7 +63,7 @@ func TestInstanceTracker_GetWarmInstance(t *testing.T) {
testData := []string{"warm1", "bytecode1", "bytecode2", "warm2"}

for _, codeHash := range testData {
iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
_ = iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
iTracker.codeHash = []byte(codeHash)
if strings.Contains(codeHash, "warm") {
iTracker.SaveAsWarmInstance()
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestInstanceTracker_UseWarmInstance(t *testing.T) {
testData := []string{"warm1", "bytecode1", "warm2", "bytecode2"}

for _, codeHash := range testData {
iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
_ = iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
iTracker.codeHash = []byte(codeHash)

if strings.Contains(codeHash, "warm") {
Expand All @@ -106,7 +106,7 @@ func TestInstanceTracker_UseWarmInstance(t *testing.T) {
require.Equal(t, []byte("bytecode2"), iTracker.CodeHash())

for _, codeHash := range testData {
ok := iTracker.UseWarmInstance([]byte(codeHash), false)
ok, _ := iTracker.UseWarmInstance([]byte(codeHash), false)

if strings.Contains(codeHash, "warm") {
require.True(t, ok)
Expand All @@ -124,7 +124,7 @@ func TestInstanceTracker_IsCodeHashOnStack_Ok(t *testing.T) {
testData := []string{"alpha", "beta", "alpha", "active"}

for i, codeHash := range testData {
iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
_ = iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
iTracker.codeHash = []byte(codeHash)
if i < 2 || codeHash == "active" {
iTracker.SaveAsWarmInstance()
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestInstanceTracker_PopSetActiveSelfScenario(t *testing.T) {
testData := []string{"alpha", "alpha", "alpha", "alpha", "active"}

for i, codeHash := range testData {
iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
_ = iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
iTracker.codeHash = []byte(codeHash)
if i == 0 || codeHash == "active" {
iTracker.SaveAsWarmInstance()
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestInstanceTracker_PopSetActiveSimpleScenario(t *testing.T) {
testData := []string{"alpha", "beta", "alpha", "beta", "active"}

for i, codeHash := range testData {
iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
_ = iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
iTracker.codeHash = []byte(codeHash)
if i < 2 || codeHash == "active" {
iTracker.SaveAsWarmInstance()
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestInstanceTracker_PopSetActiveComplexScenario(t *testing.T) {
testData := []string{"alpha", "beta", "gamma", "beta", "gamma", "delta", "alpha", "active"}

for i, codeHash := range testData {
iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
_ = iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
iTracker.codeHash = []byte(codeHash)
if i < 3 || codeHash == "delta" || codeHash == "active" {
iTracker.SaveAsWarmInstance()
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestInstanceTracker_PopSetActiveWarmOnlyScenario(t *testing.T) {
testData := []string{"alpha", "beta", "gamma", "delta", "active"}

for _, codeHash := range testData {
iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
_ = iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
iTracker.codeHash = []byte(codeHash)
iTracker.SaveAsWarmInstance()

Expand Down Expand Up @@ -283,7 +283,7 @@ func TestInstanceTracker_ForceCleanInstanceWithBypass(t *testing.T) {
testData := []string{"warm1", "bytecode1"}

for _, codeHash := range testData {
iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
_ = iTracker.SetNewInstance(mock.NewInstanceMock([]byte(codeHash)), Bytecode)
iTracker.codeHash = []byte(codeHash)

if strings.Contains(codeHash, "warm") {
Expand All @@ -298,7 +298,7 @@ func TestInstanceTracker_ForceCleanInstanceWithBypass(t *testing.T) {
iTracker.ForceCleanInstance(true)
require.Nil(t, iTracker.instance)

iTracker.UseWarmInstance([]byte("warm1"), false)
_, _ = iTracker.UseWarmInstance([]byte("warm1"), false)
require.NotNil(t, iTracker.instance)

iTracker.ForceCleanInstance(true)
Expand All @@ -312,7 +312,7 @@ func TestInstanceTracker_DoubleForceClean(t *testing.T) {
iTracker, err := NewInstanceTracker()
require.Nil(t, err)

iTracker.SetNewInstance(mock.NewInstanceMock(nil), Bytecode)
_ = iTracker.SetNewInstance(mock.NewInstanceMock(nil), Bytecode)
require.NotNil(t, iTracker.instance)
require.Equal(t, 1, iTracker.numRunningInstances)

Expand Down
Loading
Loading