Skip to content

Commit

Permalink
fix: do not use same machine in different executions
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone authored and gfyrag committed Oct 23, 2024
1 parent ec68658 commit 42ce894
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions internal/controller/ledger/numscript_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,32 @@ type NumscriptRuntime interface {

type MachineNumscriptRuntimeAdapter struct {
program program.Program
machine *vm.Machine
}

func (d *MachineNumscriptRuntimeAdapter) Execute(ctx context.Context, tx TX, vars map[string]string) (*NumscriptExecutionResult, error) {
store := newVmStoreAdapter(tx)

d.machine = vm.NewMachine(d.program)
machineInstance := vm.NewMachine(d.program)

// notes(gfyrag): machines modify the map, copy it to keep our original parameters unchanged
varsCopy := make(map[string]string)
for k, v := range vars {
varsCopy[k] = v
}

if err := d.machine.SetVarsFromJSON(varsCopy); err != nil {
if err := machineInstance.SetVarsFromJSON(varsCopy); err != nil {
return nil, fmt.Errorf("failed to set vars from JSON: %w", err)
}
err := d.machine.ResolveResources(ctx, store)
err := machineInstance.ResolveResources(ctx, store)
if err != nil {
return nil, fmt.Errorf("failed to resolve resources: %w", err)
}

if err := d.machine.ResolveBalances(ctx, store); err != nil {
if err := machineInstance.ResolveBalances(ctx, store); err != nil {
return nil, fmt.Errorf("failed to resolve balances: %w", err)
}

if err := d.machine.Execute(); err != nil {
if err := machineInstance.Execute(); err != nil {
switch {
case errors.Is(err, &machine.ErrMetadataOverride{}):
errMetadataOverride := &machine.ErrMetadataOverride{}
Expand All @@ -66,16 +65,16 @@ func (d *MachineNumscriptRuntimeAdapter) Execute(ctx context.Context, tx TX, var
}

return &NumscriptExecutionResult{
Postings: collectionutils.Map(d.machine.Postings, func(from vm.Posting) ledger.Posting {
Postings: collectionutils.Map(machineInstance.Postings, func(from vm.Posting) ledger.Posting {
return ledger.Posting{
Source: from.Source,
Destination: from.Destination,
Amount: from.Amount.ToBigInt(),
Asset: from.Asset,
}
}),
Metadata: d.machine.GetTxMetaJSON(),
AccountMetadata: d.machine.GetAccountsMetaJSON(),
Metadata: machineInstance.GetTxMetaJSON(),
AccountMetadata: machineInstance.GetAccountsMetaJSON(),
}, nil
}

Expand Down

0 comments on commit 42ce894

Please sign in to comment.