github.com/annchain/OG@v0.0.9/arefactor_core/core/processor_vm.go (about)

     1  package core
     2  
     3  import (
     4  	"fmt"
     5  	ogTypes "github.com/annchain/OG/arefactor/og_interface"
     6  	"github.com/annchain/OG/arefactor/types"
     7  	"github.com/annchain/OG/common/math"
     8  	evm "github.com/annchain/OG/vm/eth/core/vm"
     9  	"github.com/annchain/OG/vm/ovm"
    10  	vmtypes "github.com/annchain/OG/vm/types"
    11  	log "github.com/sirupsen/logrus"
    12  )
    13  
    14  type OvmProcessor struct{}
    15  
    16  func (op *OvmProcessor) CanProcess(txi types.Txi) bool {
    17  	tx, ok := txi.(*types.Tx)
    18  	if !ok {
    19  		return false
    20  	}
    21  
    22  	_, okFrom := tx.From.(*ogTypes.Address20)
    23  	_, okTo := tx.To.(*ogTypes.Address20)
    24  	if !okFrom || !okTo {
    25  		return false
    26  	}
    27  	return true
    28  }
    29  
    30  func (op *OvmProcessor) Process(statedb VmStateDB, txi types.Txi, height uint64) (*Receipt, error) {
    31  	tx := txi.(*types.Tx)
    32  
    33  	from20 := tx.From.(*ogTypes.Address20)
    34  	to20 := tx.To.(*ogTypes.Address20)
    35  	vmContext := ovm.NewOVMContext(&ovm.DefaultChainContext{}, DefaultCoinbase, statedb)
    36  
    37  	txContext := &ovm.TxContext{
    38  		From:       from20,
    39  		Value:      tx.Value,
    40  		Data:       tx.Data,
    41  		GasPrice:   math.NewBigInt(0),
    42  		GasLimit:   DefaultGasLimit,
    43  		Coinbase:   DefaultCoinbase,
    44  		SequenceID: height,
    45  	}
    46  
    47  	evmInterpreter := evm.NewEVMInterpreter(vmContext, txContext,
    48  		&evm.InterpreterConfig{
    49  			Debug: false,
    50  		})
    51  	ovmconf := &ovm.OVMConfig{
    52  		NoRecursion: false,
    53  	}
    54  	ogvm := ovm.NewOVM(vmContext, []ovm.Interpreter{evmInterpreter}, ovmconf)
    55  
    56  	var ret []byte
    57  	//var leftOverGas uint64
    58  	var contractAddress ogTypes.Address20
    59  	var err error
    60  	var receipt *Receipt
    61  	if tx.To.Cmp(emptyAddress) == 0 {
    62  		ret, contractAddress, _, err = ogvm.Create(vmtypes.AccountRef(*txContext.From), txContext.Data, txContext.GasLimit, txContext.Value.Value, true)
    63  	} else {
    64  		ret, _, err = ogvm.Call(vmtypes.AccountRef(*txContext.From), *to20, txContext.Data, txContext.GasLimit, txContext.Value.Value, true)
    65  	}
    66  	if err != nil {
    67  		receipt := NewReceipt(tx.GetTxHash(), ReceiptStatusVMFailed, err.Error(), emptyAddress)
    68  		log.WithError(err).Warn("vm processing error")
    69  		return receipt, fmt.Errorf("vm processing error: %v", err)
    70  	}
    71  	if tx.To.Cmp(emptyAddress) == 0 {
    72  		receipt = NewReceipt(tx.GetTxHash(), ReceiptStatusSuccess, contractAddress.Hex(), &contractAddress)
    73  	} else {
    74  		receipt = NewReceipt(tx.GetTxHash(), ReceiptStatusSuccess, fmt.Sprintf("%x", ret), emptyAddress)
    75  	}
    76  	return receipt, nil
    77  }