github.com/bencicandrej/quorum@v2.2.6-0.20190909091323-878cab86f711+incompatible/core/state_transition_test.go (about)

     1  package core
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/ethereum/go-ethereum/private"
     9  
    10  	"github.com/ethereum/go-ethereum/common"
    11  	"github.com/ethereum/go-ethereum/core/state"
    12  	"github.com/ethereum/go-ethereum/core/vm"
    13  	"github.com/ethereum/go-ethereum/ethdb"
    14  	"github.com/ethereum/go-ethereum/params"
    15  
    16  	testifyassert "github.com/stretchr/testify/assert"
    17  )
    18  
    19  func verifyGasPoolCalculation(t *testing.T, pm private.PrivateTransactionManager) {
    20  	assert := testifyassert.New(t)
    21  	saved := private.P
    22  	defer func() {
    23  		private.P = saved
    24  	}()
    25  	private.P = pm
    26  
    27  	txGasLimit := uint64(100000)
    28  	gasPool := new(GasPool).AddGas(200000)
    29  	// this payload would give us 25288 intrinsic gas
    30  	arbitraryEncryptedPayload := "4ab80888354582b92ab442a317828386e4bf21ea4a38d1a9183fbb715f199475269d7686939017f4a6b28310d5003ebd8e012eade530b79e157657ce8dd9692a"
    31  	expectedGasPool := new(GasPool).AddGas(174712) // only intrinsic gas is deducted
    32  
    33  	db := ethdb.NewMemDatabase()
    34  	privateState, _ := state.New(common.Hash{}, state.NewDatabase(db))
    35  	publicState, _ := state.New(common.Hash{}, state.NewDatabase(db))
    36  	msg := privateCallMsg{
    37  		callmsg: callmsg{
    38  			addr:     common.Address{2},
    39  			to:       &common.Address{},
    40  			value:    new(big.Int),
    41  			gas:      txGasLimit,
    42  			gasPrice: big.NewInt(0),
    43  			data:     common.Hex2Bytes(arbitraryEncryptedPayload),
    44  		},
    45  	}
    46  	ctx := NewEVMContext(msg, &dualStateTestHeader, nil, &common.Address{})
    47  	evm := vm.NewEVM(ctx, publicState, privateState, params.QuorumTestChainConfig, vm.Config{})
    48  	arbitraryBalance := big.NewInt(100000000)
    49  	publicState.SetBalance(evm.Coinbase, arbitraryBalance)
    50  	publicState.SetBalance(msg.From(), arbitraryBalance)
    51  
    52  	testObject := NewStateTransition(evm, msg, gasPool)
    53  
    54  	_, _, failed, err := testObject.TransitionDb()
    55  
    56  	assert.NoError(err)
    57  	assert.False(failed)
    58  
    59  	assert.Equal(new(big.Int).SetUint64(expectedGasPool.Gas()), new(big.Int).SetUint64(gasPool.Gas()), "gas pool must be calculated correctly")
    60  	assert.Equal(arbitraryBalance, publicState.GetBalance(evm.Coinbase), "balance must not be changed")
    61  	assert.Equal(arbitraryBalance, publicState.GetBalance(msg.From()), "balance must not be changed")
    62  }
    63  
    64  func TestStateTransition_TransitionDb_GasPoolCalculation_whenNonPartyNodeProcessingPrivateTransactions(t *testing.T) {
    65  	stubPTM := &StubPrivateTransactionManager{
    66  		responses: map[string][]interface{}{
    67  			"Receive": {
    68  				[]byte{},
    69  				nil,
    70  			},
    71  		},
    72  	}
    73  	verifyGasPoolCalculation(t, stubPTM)
    74  }
    75  
    76  func TestStateTransition_TransitionDb_GasPoolCalculation_whenPartyNodeProcessingPrivateTransactions(t *testing.T) {
    77  	stubPTM := &StubPrivateTransactionManager{
    78  		responses: map[string][]interface{}{
    79  			"Receive": {
    80  				common.Hex2Bytes("600a6000526001601ff300"),
    81  				nil,
    82  			},
    83  		},
    84  	}
    85  	verifyGasPoolCalculation(t, stubPTM)
    86  }
    87  
    88  type privateCallMsg struct {
    89  	callmsg
    90  }
    91  
    92  func (pm privateCallMsg) IsPrivate() bool { return true }
    93  
    94  type StubPrivateTransactionManager struct {
    95  	responses map[string][]interface{}
    96  }
    97  
    98  func (spm *StubPrivateTransactionManager) Send(data []byte, from string, to []string) ([]byte, error) {
    99  	return nil, fmt.Errorf("to be implemented")
   100  }
   101  
   102  func (spm *StubPrivateTransactionManager) SendSignedTx(data []byte, to []string) ([]byte, error) {
   103  	return nil, fmt.Errorf("to be implemented")
   104  }
   105  
   106  func (spm *StubPrivateTransactionManager) Receive(data []byte) ([]byte, error) {
   107  	res := spm.responses["Receive"]
   108  	if err, ok := res[1].(error); ok {
   109  		return nil, err
   110  	}
   111  	if ret, ok := res[0].([]byte); ok {
   112  		return ret, nil
   113  	}
   114  	return nil, nil
   115  }