github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/cmd/util/ledger/migrations/transaction_migration.go (about)

     1  package migrations
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rs/zerolog"
     7  
     8  	"github.com/onflow/flow-go/cmd/util/ledger/util/registers"
     9  	"github.com/onflow/flow-go/engine/execution/computation"
    10  	"github.com/onflow/flow-go/fvm"
    11  	"github.com/onflow/flow-go/model/flow"
    12  )
    13  
    14  func NewTransactionBasedMigration(
    15  	tx *flow.TransactionBody,
    16  	chainID flow.ChainID,
    17  	logger zerolog.Logger,
    18  	expectedWriteAddresses map[flow.Address]struct{},
    19  ) RegistersMigration {
    20  	return func(registersByAccount *registers.ByAccount) error {
    21  
    22  		options := computation.DefaultFVMOptions(chainID, false, false)
    23  		options = append(options,
    24  			fvm.WithContractDeploymentRestricted(false),
    25  			fvm.WithContractRemovalRestricted(false),
    26  			fvm.WithAuthorizationChecksEnabled(false),
    27  			fvm.WithSequenceNumberCheckAndIncrementEnabled(false),
    28  			fvm.WithTransactionFeesEnabled(false))
    29  		ctx := fvm.NewContext(options...)
    30  
    31  		storageSnapshot := registers.StorageSnapshot{
    32  			Registers: registersByAccount,
    33  		}
    34  
    35  		vm := fvm.NewVirtualMachine()
    36  
    37  		executionSnapshot, res, err := vm.Run(
    38  			ctx,
    39  			fvm.Transaction(tx, 0),
    40  			storageSnapshot,
    41  		)
    42  		if err != nil {
    43  			return fmt.Errorf("failed to run transaction: %w", err)
    44  		}
    45  
    46  		if res.Err != nil {
    47  			return fmt.Errorf("transaction failed: %w", res.Err)
    48  		}
    49  
    50  		err = registers.ApplyChanges(
    51  			registersByAccount,
    52  			executionSnapshot.WriteSet,
    53  			expectedWriteAddresses,
    54  			logger,
    55  		)
    56  		if err != nil {
    57  			return fmt.Errorf("failed to apply changes: %w", err)
    58  		}
    59  
    60  		return nil
    61  	}
    62  }