github.com/RobustRoundRobin/quorum@v20.10.0+incompatible/internal/ethapi/api_test.go (about)

     1  package ethapi
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/ethereum/go-ethereum/accounts"
    11  	"github.com/ethereum/go-ethereum/common"
    12  	"github.com/ethereum/go-ethereum/common/hexutil"
    13  	"github.com/ethereum/go-ethereum/core"
    14  	"github.com/ethereum/go-ethereum/core/bloombits"
    15  	"github.com/ethereum/go-ethereum/core/rawdb"
    16  	"github.com/ethereum/go-ethereum/core/state"
    17  	"github.com/ethereum/go-ethereum/core/types"
    18  	"github.com/ethereum/go-ethereum/core/vm"
    19  	"github.com/ethereum/go-ethereum/crypto"
    20  	"github.com/ethereum/go-ethereum/eth/downloader"
    21  	"github.com/ethereum/go-ethereum/ethdb"
    22  	"github.com/ethereum/go-ethereum/event"
    23  	"github.com/ethereum/go-ethereum/log"
    24  	"github.com/ethereum/go-ethereum/params"
    25  	"github.com/ethereum/go-ethereum/private"
    26  	"github.com/ethereum/go-ethereum/private/engine"
    27  	"github.com/ethereum/go-ethereum/private/engine/notinuse"
    28  	"github.com/ethereum/go-ethereum/rpc"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  var (
    33  	arbitraryCtx  = context.Background()
    34  	privateTxArgs = &PrivateTxArgs{
    35  		PrivateFor: []string{"arbitrary party 1", "arbitrary party 2"},
    36  	}
    37  	arbitraryFrom = common.BytesToAddress([]byte("arbitrary address"))
    38  
    39  	arbitrarySimpleStorageContractEncryptedPayloadHash = common.BytesToEncryptedPayloadHash([]byte("arbitrary payload hash"))
    40  
    41  	simpleStorageContractCreationTx = types.NewContractCreation(
    42  		0,
    43  		big.NewInt(0),
    44  		hexutil.MustDecodeUint64("0x47b760"),
    45  		big.NewInt(0),
    46  		hexutil.MustDecode("0x6060604052341561000f57600080fd5b604051602080610149833981016040528080519060200190919050505b806000819055505b505b610104806100456000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632a1afcd914605157806360fe47b11460775780636d4ce63c146097575b600080fd5b3415605b57600080fd5b606160bd565b6040518082815260200191505060405180910390f35b3415608157600080fd5b6095600480803590602001909190505060c3565b005b341560a157600080fd5b60a760ce565b6040518082815260200191505060405180910390f35b60005481565b806000819055505b50565b6000805490505b905600a165627a7a72305820d5851baab720bba574474de3d09dbeaabc674a15f4dd93b974908476542c23f00029"))
    47  
    48  	rawSimpleStorageContractCreationTx = types.NewContractCreation(
    49  		0,
    50  		big.NewInt(0),
    51  		hexutil.MustDecodeUint64("0x47b760"),
    52  		big.NewInt(0),
    53  		arbitrarySimpleStorageContractEncryptedPayloadHash.Bytes())
    54  
    55  	arbitrarySimpleStorageContractAddress                common.Address
    56  	arbitraryStandardPrivateSimpleStorageContractAddress common.Address
    57  
    58  	simpleStorageContractMessageCallTx                   *types.Transaction
    59  	standardPrivateSimpleStorageContractMessageCallTx    *types.Transaction
    60  	rawStandardPrivateSimpleStorageContractMessageCallTx *types.Transaction
    61  
    62  	arbitraryCurrentBlockNumber = big.NewInt(1)
    63  
    64  	publicStateDB  *state.StateDB
    65  	privateStateDB *state.StateDB
    66  )
    67  
    68  func TestMain(m *testing.M) {
    69  	setup()
    70  	retCode := m.Run()
    71  	teardown()
    72  	os.Exit(retCode)
    73  }
    74  
    75  func setup() {
    76  	log.Root().SetHandler(log.StreamHandler(os.Stdout, log.TerminalFormat(true)))
    77  	var err error
    78  
    79  	memdb := rawdb.NewMemoryDatabase()
    80  	db := state.NewDatabase(memdb)
    81  
    82  	publicStateDB, err = state.New(common.Hash{}, db)
    83  	if err != nil {
    84  		panic(err)
    85  	}
    86  	privateStateDB, err = state.New(common.Hash{}, db)
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  
    91  	private.P = &StubPrivateTransactionManager{}
    92  
    93  	key, _ := crypto.GenerateKey()
    94  	from := crypto.PubkeyToAddress(key.PublicKey)
    95  
    96  	arbitrarySimpleStorageContractAddress = crypto.CreateAddress(from, 0)
    97  
    98  	simpleStorageContractMessageCallTx = types.NewTransaction(
    99  		0,
   100  		arbitrarySimpleStorageContractAddress,
   101  		big.NewInt(0),
   102  		hexutil.MustDecodeUint64("0x47b760"),
   103  		big.NewInt(0),
   104  		hexutil.MustDecode("0x60fe47b1000000000000000000000000000000000000000000000000000000000000000d"))
   105  
   106  	arbitraryStandardPrivateSimpleStorageContractAddress = crypto.CreateAddress(from, 1)
   107  
   108  	standardPrivateSimpleStorageContractMessageCallTx = types.NewTransaction(
   109  		0,
   110  		arbitraryStandardPrivateSimpleStorageContractAddress,
   111  		big.NewInt(0),
   112  		hexutil.MustDecodeUint64("0x47b760"),
   113  		big.NewInt(0),
   114  		hexutil.MustDecode("0x60fe47b1000000000000000000000000000000000000000000000000000000000000000e"))
   115  
   116  	rawStandardPrivateSimpleStorageContractMessageCallTx = types.NewTransaction(
   117  		0,
   118  		arbitraryStandardPrivateSimpleStorageContractAddress,
   119  		big.NewInt(0),
   120  		hexutil.MustDecodeUint64("0x47b760"),
   121  		big.NewInt(0),
   122  		arbitrarySimpleStorageContractEncryptedPayloadHash.Bytes())
   123  
   124  }
   125  
   126  func teardown() {
   127  	log.Root().SetHandler(log.DiscardHandler())
   128  }
   129  
   130  func TestSimulateExecution_whenStandardPrivateCreation(t *testing.T) {
   131  	assert := assert.New(t)
   132  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagStandardPrivate
   133  
   134  	affectedCACreationTxHashes, merkleRoot, err := simulateExecution(arbitraryCtx, &StubBackend{}, arbitraryFrom, simpleStorageContractCreationTx, privateTxArgs)
   135  
   136  	assert.NoError(err, "simulate execution")
   137  	assert.Empty(affectedCACreationTxHashes, "creation tx should not have any affected contract creation tx hashes")
   138  	assert.Equal(common.Hash{}, merkleRoot, "no private state validation")
   139  }
   140  
   141  func TestSimulateExecution_whenPartyProtectionCreation(t *testing.T) {
   142  	assert := assert.New(t)
   143  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagPartyProtection
   144  
   145  	affectedCACreationTxHashes, merkleRoot, err := simulateExecution(arbitraryCtx, &StubBackend{}, arbitraryFrom, simpleStorageContractCreationTx, privateTxArgs)
   146  
   147  	assert.NoError(err, "simulation execution")
   148  	assert.Empty(affectedCACreationTxHashes, "creation tx should not have any affected contract creation tx hashes")
   149  	assert.Equal(common.Hash{}, merkleRoot, "no private state validation")
   150  }
   151  
   152  func TestSimulateExecution_whenCreationWithStateValidation(t *testing.T) {
   153  	assert := assert.New(t)
   154  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagStateValidation
   155  
   156  	affectedCACreationTxHashes, merkleRoot, err := simulateExecution(arbitraryCtx, &StubBackend{}, arbitraryFrom, simpleStorageContractCreationTx, privateTxArgs)
   157  
   158  	assert.NoError(err, "simulate execution")
   159  	assert.Empty(affectedCACreationTxHashes, "creation tx should not have any affected contract creation tx hashes")
   160  	assert.NotEqual(common.Hash{}, merkleRoot, "private state validation")
   161  }
   162  
   163  func TestSimulateExecution_whenStandardPrivateMessageCall(t *testing.T) {
   164  	assert := assert.New(t)
   165  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagStandardPrivate
   166  
   167  	privateStateDB.SetCode(arbitraryStandardPrivateSimpleStorageContractAddress, hexutil.MustDecode("0x608060405234801561001057600080fd5b506040516020806101618339810180604052602081101561003057600080fd5b81019080805190602001909291905050508060008190555050610109806100586000396000f3fe6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146099575b600080fd5b348015605957600080fd5b50608360048036036020811015606e57600080fd5b810190808035906020019092919050505060c1565b6040518082815260200191505060405180910390f35b34801560a457600080fd5b5060ab60d4565b6040518082815260200191505060405180910390f35b6000816000819055506000549050919050565b6000805490509056fea165627a7a723058203624ca2e3479d3fa5a12d97cf3dae0d9a6de3a3b8a53c8605b9cd398d9766b9f00290000000000000000000000000000000000000000000000000000000000000002"))
   168  	privateStateDB.SetState(arbitraryStandardPrivateSimpleStorageContractAddress, common.Hash{0}, common.Hash{100})
   169  	privateStateDB.Commit(true)
   170  
   171  	affectedCACreationTxHashes, merkleRoot, err := simulateExecution(arbitraryCtx, &StubBackend{}, arbitraryFrom, standardPrivateSimpleStorageContractMessageCallTx, privateTxArgs)
   172  
   173  	log.Debug("state", "state", privateStateDB.GetState(arbitraryStandardPrivateSimpleStorageContractAddress, common.Hash{0}))
   174  
   175  	assert.NoError(err, "simulate execution")
   176  	assert.Empty(affectedCACreationTxHashes, "standard private contract should not have any affected contract creation tx hashes")
   177  	assert.Equal(common.Hash{}, merkleRoot, "no private state validation")
   178  }
   179  
   180  func TestSimulateExecution_StandardPrivateMessageCallSucceedsWheContractNotAvailableLocally(t *testing.T) {
   181  	assert := assert.New(t)
   182  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagStandardPrivate
   183  
   184  	affectedCACreationTxHashes, merkleRoot, err := simulateExecution(arbitraryCtx, &StubBackend{}, arbitraryFrom, standardPrivateSimpleStorageContractMessageCallTx, privateTxArgs)
   185  
   186  	log.Debug("state", "state", privateStateDB.GetState(arbitraryStandardPrivateSimpleStorageContractAddress, common.Hash{0}))
   187  
   188  	assert.NoError(err, "simulate execution")
   189  	assert.Empty(affectedCACreationTxHashes, "standard private contract should not have any affected contract creation tx hashes")
   190  	assert.Equal(common.Hash{}, merkleRoot, "no private state validation")
   191  }
   192  
   193  func TestSimulateExecution_whenPartyProtectionMessageCall(t *testing.T) {
   194  	assert := assert.New(t)
   195  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagPartyProtection
   196  
   197  	privateStateDB.SetCode(arbitrarySimpleStorageContractAddress, hexutil.MustDecode("0x608060405234801561001057600080fd5b506040516020806101618339810180604052602081101561003057600080fd5b81019080805190602001909291905050508060008190555050610109806100586000396000f3fe6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146099575b600080fd5b348015605957600080fd5b50608360048036036020811015606e57600080fd5b810190808035906020019092919050505060c1565b6040518082815260200191505060405180910390f35b34801560a457600080fd5b5060ab60d4565b6040518082815260200191505060405180910390f35b6000816000819055506000549050919050565b6000805490509056fea165627a7a723058203624ca2e3479d3fa5a12d97cf3dae0d9a6de3a3b8a53c8605b9cd398d9766b9f00290000000000000000000000000000000000000000000000000000000000000001"))
   198  	privateStateDB.SetStatePrivacyMetadata(arbitrarySimpleStorageContractAddress, &state.PrivacyMetadata{
   199  		PrivacyFlag:    privateTxArgs.PrivacyFlag,
   200  		CreationTxHash: arbitrarySimpleStorageContractEncryptedPayloadHash,
   201  	})
   202  
   203  	privateStateDB.SetState(arbitrarySimpleStorageContractAddress, common.Hash{0}, common.Hash{100})
   204  	privateStateDB.Commit(true)
   205  
   206  	affectedCACreationTxHashes, merkleRoot, err := simulateExecution(arbitraryCtx, &StubBackend{}, arbitraryFrom, simpleStorageContractMessageCallTx, privateTxArgs)
   207  
   208  	expectedCACreationTxHashes := []common.EncryptedPayloadHash{arbitrarySimpleStorageContractEncryptedPayloadHash}
   209  
   210  	log.Debug("state", "state", privateStateDB.GetState(arbitrarySimpleStorageContractAddress, common.Hash{0}))
   211  
   212  	assert.NoError(err, "simulate execution")
   213  	assert.NotEmpty(affectedCACreationTxHashes, "affected contract accounts' creation transacton hashes")
   214  	assert.Equal(common.Hash{}, merkleRoot, "no private state validation")
   215  	assert.True(len(affectedCACreationTxHashes) == len(expectedCACreationTxHashes))
   216  }
   217  
   218  func TestSimulateExecution_whenPartyProtectionMessageCallAndPrivacyEnhancementsDisabled(t *testing.T) {
   219  	assert := assert.New(t)
   220  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagPartyProtection
   221  
   222  	params.QuorumTestChainConfig.PrivacyEnhancementsBlock = nil
   223  	defer func() { params.QuorumTestChainConfig.PrivacyEnhancementsBlock = big.NewInt(0) }()
   224  
   225  	stbBackend := &StubBackend{}
   226  	affectedCACreationTxHashes, merkleRoot, err := simulateExecution(arbitraryCtx, stbBackend, arbitraryFrom, simpleStorageContractMessageCallTx, privateTxArgs)
   227  
   228  	// the simulation returns early without executing the transaction
   229  	assert.False(stbBackend.getEVMCalled, "simulation is ended early - before getEVM is called")
   230  	assert.NoError(err, "simulate execution")
   231  	assert.Empty(affectedCACreationTxHashes, "affected contract accounts' creation transacton hashes")
   232  	assert.Equal(common.Hash{}, merkleRoot, "no private state validation")
   233  }
   234  
   235  func TestSimulateExecution_whenStateValidationMessageCall(t *testing.T) {
   236  	assert := assert.New(t)
   237  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagStateValidation
   238  
   239  	privateStateDB.SetCode(arbitrarySimpleStorageContractAddress, hexutil.MustDecode("0x608060405234801561001057600080fd5b506040516020806101618339810180604052602081101561003057600080fd5b81019080805190602001909291905050508060008190555050610109806100586000396000f3fe6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146099575b600080fd5b348015605957600080fd5b50608360048036036020811015606e57600080fd5b810190808035906020019092919050505060c1565b6040518082815260200191505060405180910390f35b34801560a457600080fd5b5060ab60d4565b6040518082815260200191505060405180910390f35b6000816000819055506000549050919050565b6000805490509056fea165627a7a723058203624ca2e3479d3fa5a12d97cf3dae0d9a6de3a3b8a53c8605b9cd398d9766b9f00290000000000000000000000000000000000000000000000000000000000000001"))
   240  	privateStateDB.SetStatePrivacyMetadata(arbitrarySimpleStorageContractAddress, &state.PrivacyMetadata{
   241  		PrivacyFlag:    privateTxArgs.PrivacyFlag,
   242  		CreationTxHash: arbitrarySimpleStorageContractEncryptedPayloadHash,
   243  	})
   244  
   245  	privateStateDB.SetState(arbitrarySimpleStorageContractAddress, common.Hash{0}, common.Hash{100})
   246  	privateStateDB.Commit(true)
   247  
   248  	affectedCACreationTxHashes, merkleRoot, err := simulateExecution(arbitraryCtx, &StubBackend{}, arbitraryFrom, simpleStorageContractMessageCallTx, privateTxArgs)
   249  
   250  	expectedCACreationTxHashes := []common.EncryptedPayloadHash{arbitrarySimpleStorageContractEncryptedPayloadHash}
   251  
   252  	log.Debug("state", "state", privateStateDB.GetState(arbitrarySimpleStorageContractAddress, common.Hash{0}))
   253  
   254  	assert.NoError(err, "simulate execution")
   255  	assert.NotEmpty(affectedCACreationTxHashes, "affected contract accounts' creation transacton hashes")
   256  	assert.NotEqual(common.Hash{}, merkleRoot, "private state validation")
   257  	assert.True(len(affectedCACreationTxHashes) == len(expectedCACreationTxHashes))
   258  }
   259  
   260  //mix and match flags
   261  func TestSimulateExecution_PrivacyFlagPartyProtectionCallingStandardPrivateContract_Error(t *testing.T) {
   262  	assert := assert.New(t)
   263  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagPartyProtection
   264  
   265  	privateStateDB.SetCode(arbitraryStandardPrivateSimpleStorageContractAddress, hexutil.MustDecode("0x608060405234801561001057600080fd5b506040516020806101618339810180604052602081101561003057600080fd5b81019080805190602001909291905050508060008190555050610109806100586000396000f3fe6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146099575b600080fd5b348015605957600080fd5b50608360048036036020811015606e57600080fd5b810190808035906020019092919050505060c1565b6040518082815260200191505060405180910390f35b34801560a457600080fd5b5060ab60d4565b6040518082815260200191505060405180910390f35b6000816000819055506000549050919050565b6000805490509056fea165627a7a723058203624ca2e3479d3fa5a12d97cf3dae0d9a6de3a3b8a53c8605b9cd398d9766b9f00290000000000000000000000000000000000000000000000000000000000000002"))
   266  	privateStateDB.SetState(arbitraryStandardPrivateSimpleStorageContractAddress, common.Hash{0}, common.Hash{100})
   267  	privateStateDB.Commit(true)
   268  
   269  	_, _, err := simulateExecution(arbitraryCtx, &StubBackend{}, arbitraryFrom, standardPrivateSimpleStorageContractMessageCallTx, privateTxArgs)
   270  
   271  	log.Debug("state", "state", privateStateDB.GetState(arbitraryStandardPrivateSimpleStorageContractAddress, common.Hash{0}))
   272  
   273  	assert.Error(err, "simulate execution")
   274  }
   275  
   276  func TestSimulateExecution_StandardPrivateFlagCallingPartyProtectionContract_Error(t *testing.T) {
   277  	assert := assert.New(t)
   278  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagStandardPrivate
   279  
   280  	privateStateDB.SetCode(arbitrarySimpleStorageContractAddress, hexutil.MustDecode("0x608060405234801561001057600080fd5b506040516020806101618339810180604052602081101561003057600080fd5b81019080805190602001909291905050508060008190555050610109806100586000396000f3fe6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146099575b600080fd5b348015605957600080fd5b50608360048036036020811015606e57600080fd5b810190808035906020019092919050505060c1565b6040518082815260200191505060405180910390f35b34801560a457600080fd5b5060ab60d4565b6040518082815260200191505060405180910390f35b6000816000819055506000549050919050565b6000805490509056fea165627a7a723058203624ca2e3479d3fa5a12d97cf3dae0d9a6de3a3b8a53c8605b9cd398d9766b9f00290000000000000000000000000000000000000000000000000000000000000001"))
   281  	privateStateDB.SetStatePrivacyMetadata(arbitrarySimpleStorageContractAddress, &state.PrivacyMetadata{
   282  		PrivacyFlag:    engine.PrivacyFlagPartyProtection,
   283  		CreationTxHash: arbitrarySimpleStorageContractEncryptedPayloadHash,
   284  	})
   285  
   286  	privateStateDB.SetState(arbitrarySimpleStorageContractAddress, common.Hash{0}, common.Hash{100})
   287  	privateStateDB.Commit(true)
   288  
   289  	_, _, err := simulateExecution(arbitraryCtx, &StubBackend{}, arbitraryFrom, simpleStorageContractMessageCallTx, privateTxArgs)
   290  
   291  	assert.Error(err, "simulate execution")
   292  }
   293  
   294  func TestSimulateExecution_StandardPrivateFlagCallingStateValidationContract_Error(t *testing.T) {
   295  	assert := assert.New(t)
   296  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagStandardPrivate
   297  
   298  	privateStateDB.SetCode(arbitrarySimpleStorageContractAddress, hexutil.MustDecode("0x608060405234801561001057600080fd5b506040516020806101618339810180604052602081101561003057600080fd5b81019080805190602001909291905050508060008190555050610109806100586000396000f3fe6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146099575b600080fd5b348015605957600080fd5b50608360048036036020811015606e57600080fd5b810190808035906020019092919050505060c1565b6040518082815260200191505060405180910390f35b34801560a457600080fd5b5060ab60d4565b6040518082815260200191505060405180910390f35b6000816000819055506000549050919050565b6000805490509056fea165627a7a723058203624ca2e3479d3fa5a12d97cf3dae0d9a6de3a3b8a53c8605b9cd398d9766b9f00290000000000000000000000000000000000000000000000000000000000000001"))
   299  	privateStateDB.SetStatePrivacyMetadata(arbitrarySimpleStorageContractAddress, &state.PrivacyMetadata{
   300  		PrivacyFlag:    engine.PrivacyFlagStateValidation,
   301  		CreationTxHash: arbitrarySimpleStorageContractEncryptedPayloadHash,
   302  	})
   303  
   304  	privateStateDB.SetState(arbitrarySimpleStorageContractAddress, common.Hash{0}, common.Hash{100})
   305  	privateStateDB.Commit(true)
   306  
   307  	_, _, err := simulateExecution(arbitraryCtx, &StubBackend{}, arbitraryFrom, simpleStorageContractMessageCallTx, privateTxArgs)
   308  
   309  	log.Debug("state", "state", privateStateDB.GetState(arbitrarySimpleStorageContractAddress, common.Hash{0}))
   310  
   311  	assert.Error(err, "simulate execution")
   312  }
   313  
   314  func TestSimulateExecution_PartyProtectionFlagCallingStateValidationContract_Error(t *testing.T) {
   315  	assert := assert.New(t)
   316  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagPartyProtection
   317  
   318  	privateStateDB.SetCode(arbitrarySimpleStorageContractAddress, hexutil.MustDecode("0x608060405234801561001057600080fd5b506040516020806101618339810180604052602081101561003057600080fd5b81019080805190602001909291905050508060008190555050610109806100586000396000f3fe6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146099575b600080fd5b348015605957600080fd5b50608360048036036020811015606e57600080fd5b810190808035906020019092919050505060c1565b6040518082815260200191505060405180910390f35b34801560a457600080fd5b5060ab60d4565b6040518082815260200191505060405180910390f35b6000816000819055506000549050919050565b6000805490509056fea165627a7a723058203624ca2e3479d3fa5a12d97cf3dae0d9a6de3a3b8a53c8605b9cd398d9766b9f00290000000000000000000000000000000000000000000000000000000000000001"))
   319  	privateStateDB.SetStatePrivacyMetadata(arbitrarySimpleStorageContractAddress, &state.PrivacyMetadata{
   320  		PrivacyFlag:    engine.PrivacyFlagStateValidation,
   321  		CreationTxHash: arbitrarySimpleStorageContractEncryptedPayloadHash,
   322  	})
   323  
   324  	privateStateDB.SetState(arbitrarySimpleStorageContractAddress, common.Hash{0}, common.Hash{100})
   325  	privateStateDB.Commit(true)
   326  
   327  	_, _, err := simulateExecution(arbitraryCtx, &StubBackend{}, arbitraryFrom, simpleStorageContractMessageCallTx, privateTxArgs)
   328  
   329  	log.Debug("state", "state", privateStateDB.GetState(arbitrarySimpleStorageContractAddress, common.Hash{0}))
   330  
   331  	assert.Error(err, "simulate execution")
   332  }
   333  
   334  func TestSimulateExecution_StateValidationFlagCallingPartyProtectionContract_Error(t *testing.T) {
   335  	assert := assert.New(t)
   336  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagStateValidation
   337  
   338  	privateStateDB.SetCode(arbitrarySimpleStorageContractAddress, hexutil.MustDecode("0x608060405234801561001057600080fd5b506040516020806101618339810180604052602081101561003057600080fd5b81019080805190602001909291905050508060008190555050610109806100586000396000f3fe6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146099575b600080fd5b348015605957600080fd5b50608360048036036020811015606e57600080fd5b810190808035906020019092919050505060c1565b6040518082815260200191505060405180910390f35b34801560a457600080fd5b5060ab60d4565b6040518082815260200191505060405180910390f35b6000816000819055506000549050919050565b6000805490509056fea165627a7a723058203624ca2e3479d3fa5a12d97cf3dae0d9a6de3a3b8a53c8605b9cd398d9766b9f00290000000000000000000000000000000000000000000000000000000000000001"))
   339  	privateStateDB.SetStatePrivacyMetadata(arbitrarySimpleStorageContractAddress, &state.PrivacyMetadata{
   340  		PrivacyFlag:    engine.PrivacyFlagPartyProtection,
   341  		CreationTxHash: arbitrarySimpleStorageContractEncryptedPayloadHash,
   342  	})
   343  
   344  	privateStateDB.SetState(arbitrarySimpleStorageContractAddress, common.Hash{0}, common.Hash{100})
   345  	privateStateDB.Commit(true)
   346  
   347  	_, _, err := simulateExecution(arbitraryCtx, &StubBackend{}, arbitraryFrom, simpleStorageContractMessageCallTx, privateTxArgs)
   348  
   349  	//expectedCACreationTxHashes := []common.EncryptedPayloadHash{arbitrarySimpleStorageContractEncryptedPayloadHash}
   350  
   351  	log.Debug("state", "state", privateStateDB.GetState(arbitrarySimpleStorageContractAddress, common.Hash{0}))
   352  
   353  	assert.Error(err, "simulate execution")
   354  }
   355  
   356  func TestHandlePrivateTransaction_whenInvalidFlag(t *testing.T) {
   357  	assert := assert.New(t)
   358  	privateTxArgs.PrivacyFlag = 4
   359  
   360  	_, _, err := checkAndHandlePrivateTransaction(arbitraryCtx, &StubBackend{}, simpleStorageContractCreationTx, privateTxArgs, arbitraryFrom, NormalTransaction)
   361  
   362  	assert.Error(err, "invalid privacyFlag")
   363  }
   364  
   365  func TestHandlePrivateTransaction_withPartyProtectionTxAndPrivacyEnhancementsIsDisabled(t *testing.T) {
   366  	assert := assert.New(t)
   367  	privateTxArgs.PrivacyFlag = 1
   368  	params.QuorumTestChainConfig.PrivacyEnhancementsBlock = nil
   369  	defer func() { params.QuorumTestChainConfig.PrivacyEnhancementsBlock = big.NewInt(0) }()
   370  
   371  	_, _, err := checkAndHandlePrivateTransaction(arbitraryCtx, &StubBackend{}, simpleStorageContractCreationTx, privateTxArgs, arbitraryFrom, NormalTransaction)
   372  
   373  	assert.Error(err, "PrivacyEnhancements are disabled. Can only accept transactions with PrivacyFlag=0(StandardPrivate).")
   374  }
   375  
   376  func TestHandlePrivateTransaction_whenStandardPrivateCreation(t *testing.T) {
   377  	assert := assert.New(t)
   378  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagStandardPrivate
   379  
   380  	isPrivate, _, err := checkAndHandlePrivateTransaction(arbitraryCtx, &StubBackend{}, simpleStorageContractCreationTx, privateTxArgs, arbitraryFrom, NormalTransaction)
   381  
   382  	if err != nil {
   383  		t.Fatalf("%s", err)
   384  	}
   385  
   386  	assert.True(isPrivate, "must be a private transaction")
   387  }
   388  
   389  func TestHandlePrivateTransaction_whenStandardPrivateCallingContractThatIsNotAvailableLocally(t *testing.T) {
   390  	assert := assert.New(t)
   391  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagStandardPrivate
   392  
   393  	isPrivate, _, err := checkAndHandlePrivateTransaction(arbitraryCtx, &StubBackend{}, standardPrivateSimpleStorageContractMessageCallTx, privateTxArgs, arbitraryFrom, NormalTransaction)
   394  
   395  	assert.NoError(err, "no error expected")
   396  
   397  	assert.True(isPrivate, "must be a private transaction")
   398  }
   399  
   400  func TestHandlePrivateTransaction_whenPartyProtectionCallingContractThatIsNotAvailableLocally(t *testing.T) {
   401  	assert := assert.New(t)
   402  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagPartyProtection
   403  
   404  	isPrivate, _, err := checkAndHandlePrivateTransaction(arbitraryCtx, &StubBackend{}, standardPrivateSimpleStorageContractMessageCallTx, privateTxArgs, arbitraryFrom, NormalTransaction)
   405  
   406  	assert.Error(err, "handle invalid message call")
   407  
   408  	assert.True(isPrivate, "must be a private transaction")
   409  }
   410  
   411  func TestHandlePrivateTransaction_whenPartyProtectionCallingStandardPrivate(t *testing.T) {
   412  	assert := assert.New(t)
   413  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagPartyProtection
   414  
   415  	isPrivate, _, err := checkAndHandlePrivateTransaction(arbitraryCtx, &StubBackend{}, standardPrivateSimpleStorageContractMessageCallTx, privateTxArgs, arbitraryFrom, NormalTransaction)
   416  
   417  	assert.Error(err, "handle invalid message call")
   418  
   419  	assert.True(isPrivate, "must be a private transaction")
   420  }
   421  
   422  func TestHandlePrivateTransaction_whenRawStandardPrivateCreation(t *testing.T) {
   423  	assert := assert.New(t)
   424  	private.P = &StubPrivateTransactionManager{creation: true}
   425  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagStandardPrivate
   426  
   427  	isPrivate, _, err := checkAndHandlePrivateTransaction(arbitraryCtx, &StubBackend{}, rawSimpleStorageContractCreationTx, privateTxArgs, arbitraryFrom, RawTransaction)
   428  
   429  	assert.NoError(err, "raw standard private creation succeeded")
   430  	assert.True(isPrivate, "must be a private transaction")
   431  }
   432  
   433  func TestHandlePrivateTransaction_whenRawStandardPrivateMessageCall(t *testing.T) {
   434  	assert := assert.New(t)
   435  	private.P = &StubPrivateTransactionManager{creation: false}
   436  	privateTxArgs.PrivacyFlag = engine.PrivacyFlagStandardPrivate
   437  
   438  	_, err := handlePrivateTransaction(arbitraryCtx, &StubBackend{}, rawStandardPrivateSimpleStorageContractMessageCallTx, privateTxArgs, arbitraryFrom, RawTransaction)
   439  
   440  	assert.NoError(err, "raw standard private msg call succeeded")
   441  
   442  }
   443  
   444  type StubBackend struct {
   445  	getEVMCalled bool
   446  }
   447  
   448  func (sb *StubBackend) GetEVM(ctx context.Context, msg core.Message, state vm.MinimalApiState, header *types.Header) (*vm.EVM, func() error, error) {
   449  	sb.getEVMCalled = true
   450  	vmCtx := core.NewEVMContext(msg, &types.Header{
   451  		Coinbase:   arbitraryFrom,
   452  		Number:     arbitraryCurrentBlockNumber,
   453  		Time:       0,
   454  		Difficulty: big.NewInt(0),
   455  		GasLimit:   0,
   456  	}, nil, &arbitraryFrom)
   457  	return vm.NewEVM(vmCtx, publicStateDB, privateStateDB, params.QuorumTestChainConfig, vm.Config{}), nil, nil
   458  }
   459  
   460  func (sb *StubBackend) CurrentBlock() *types.Block {
   461  	return types.NewBlock(&types.Header{
   462  		Number: arbitraryCurrentBlockNumber,
   463  	}, nil, nil, nil)
   464  }
   465  
   466  func (sb *StubBackend) Downloader() *downloader.Downloader {
   467  	panic("implement me")
   468  }
   469  
   470  func (sb *StubBackend) ProtocolVersion() int {
   471  	panic("implement me")
   472  }
   473  
   474  func (sb *StubBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   475  	panic("implement me")
   476  }
   477  
   478  func (sb *StubBackend) ChainDb() ethdb.Database {
   479  	panic("implement me")
   480  }
   481  
   482  func (sb *StubBackend) EventMux() *event.TypeMux {
   483  	panic("implement me")
   484  }
   485  
   486  func (sb *StubBackend) AccountManager() *accounts.Manager {
   487  	panic("implement me")
   488  }
   489  
   490  func (sb *StubBackend) ExtRPCEnabled() bool {
   491  	panic("implement me")
   492  }
   493  
   494  func (sb *StubBackend) CallTimeOut() time.Duration {
   495  	panic("implement me")
   496  }
   497  
   498  func (sb *StubBackend) RPCGasCap() *big.Int {
   499  	panic("implement me")
   500  }
   501  
   502  func (sb *StubBackend) SetHead(number uint64) {
   503  	panic("implement me")
   504  }
   505  
   506  func (sb *StubBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
   507  	panic("implement me")
   508  }
   509  
   510  func (sb *StubBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
   511  	panic("implement me")
   512  }
   513  
   514  func (sb *StubBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
   515  	panic("implement me")
   516  }
   517  
   518  func (sb *StubBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
   519  	panic("implement me")
   520  }
   521  
   522  func (sb *StubBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
   523  	panic("implement me")
   524  }
   525  
   526  func (sb *StubBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
   527  	panic("implement me")
   528  }
   529  
   530  func (sb *StubBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (vm.MinimalApiState, *types.Header, error) {
   531  	return &StubMinimalApiState{}, nil, nil
   532  }
   533  
   534  func (sb *StubBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (vm.MinimalApiState, *types.Header, error) {
   535  	return &StubMinimalApiState{}, nil, nil
   536  }
   537  
   538  func (sb *StubBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
   539  	panic("implement me")
   540  }
   541  
   542  func (sb *StubBackend) GetTd(blockHash common.Hash) *big.Int {
   543  	panic("implement me")
   544  }
   545  
   546  func (sb *StubBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   547  	panic("implement me")
   548  }
   549  
   550  func (sb *StubBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   551  	panic("implement me")
   552  }
   553  
   554  func (sb *StubBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   555  	panic("implement me")
   556  }
   557  
   558  func (sb *StubBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   559  	panic("implement me")
   560  }
   561  
   562  func (sb *StubBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   563  	panic("implement me")
   564  }
   565  
   566  func (sb *StubBackend) GetPoolTransactions() (types.Transactions, error) {
   567  	panic("implement me")
   568  }
   569  
   570  func (sb *StubBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
   571  	panic("implement me")
   572  }
   573  
   574  func (sb *StubBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   575  	panic("implement me")
   576  }
   577  
   578  func (sb *StubBackend) Stats() (pending int, queued int) {
   579  	panic("implement me")
   580  }
   581  
   582  func (sb *StubBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   583  	panic("implement me")
   584  }
   585  
   586  func (sb *StubBackend) SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription {
   587  	panic("implement me")
   588  }
   589  
   590  func (sb *StubBackend) BloomStatus() (uint64, uint64) {
   591  	panic("implement me")
   592  }
   593  
   594  func (sb *StubBackend) GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error) {
   595  	panic("implement me")
   596  }
   597  
   598  func (sb *StubBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   599  	panic("implement me")
   600  }
   601  
   602  func (sb *StubBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   603  	panic("implement me")
   604  }
   605  
   606  func (sb *StubBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   607  	panic("implement me")
   608  }
   609  
   610  func (sb *StubBackend) ChainConfig() *params.ChainConfig {
   611  	return params.QuorumTestChainConfig
   612  }
   613  
   614  type StubMinimalApiState struct {
   615  }
   616  
   617  func (StubMinimalApiState) GetBalance(addr common.Address) *big.Int {
   618  	panic("implement me")
   619  }
   620  
   621  func (StubMinimalApiState) SetBalance(addr common.Address, balance *big.Int) {
   622  	panic("implement me")
   623  }
   624  
   625  func (StubMinimalApiState) GetCode(addr common.Address) []byte {
   626  	return nil
   627  }
   628  
   629  func (StubMinimalApiState) GetState(a common.Address, b common.Hash) common.Hash {
   630  	panic("implement me")
   631  }
   632  
   633  func (StubMinimalApiState) GetNonce(addr common.Address) uint64 {
   634  	panic("implement me")
   635  }
   636  
   637  func (StubMinimalApiState) SetNonce(addr common.Address, nonce uint64) {
   638  	panic("implement me")
   639  }
   640  
   641  func (StubMinimalApiState) SetCode(common.Address, []byte) {
   642  	panic("implement me")
   643  }
   644  
   645  func (StubMinimalApiState) GetStatePrivacyMetadata(addr common.Address) (*state.PrivacyMetadata, error) {
   646  	panic("implement me")
   647  }
   648  
   649  func (StubMinimalApiState) GetRLPEncodedStateObject(addr common.Address) ([]byte, error) {
   650  	panic("implement me")
   651  }
   652  
   653  func (StubMinimalApiState) GetProof(common.Address) ([][]byte, error) {
   654  	panic("implement me")
   655  }
   656  
   657  func (StubMinimalApiState) GetStorageProof(common.Address, common.Hash) ([][]byte, error) {
   658  	panic("implement me")
   659  }
   660  
   661  func (StubMinimalApiState) StorageTrie(addr common.Address) state.Trie {
   662  	panic("implement me")
   663  }
   664  
   665  func (StubMinimalApiState) Error() error {
   666  	panic("implement me")
   667  }
   668  
   669  func (StubMinimalApiState) GetCodeHash(common.Address) common.Hash {
   670  	panic("implement me")
   671  }
   672  
   673  func (StubMinimalApiState) SetState(common.Address, common.Hash, common.Hash) {
   674  	panic("implement me")
   675  }
   676  
   677  func (StubMinimalApiState) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) {
   678  	panic("implement me")
   679  }
   680  
   681  type StubPrivateTransactionManager struct {
   682  	notinuse.PrivateTransactionManager
   683  	creation bool
   684  }
   685  
   686  func (sptm *StubPrivateTransactionManager) Send(data []byte, from string, to []string, extra *engine.ExtraMetadata) (common.EncryptedPayloadHash, error) {
   687  	return arbitrarySimpleStorageContractEncryptedPayloadHash, nil
   688  }
   689  
   690  func (sptm *StubPrivateTransactionManager) EncryptPayload(data []byte, from string, to []string, extra *engine.ExtraMetadata) ([]byte, error) {
   691  	return nil, engine.ErrPrivateTxManagerNotSupported
   692  }
   693  
   694  func (sptm *StubPrivateTransactionManager) DecryptPayload(payload common.DecryptRequest) ([]byte, *engine.ExtraMetadata, error) {
   695  	return nil, nil, engine.ErrPrivateTxManagerNotSupported
   696  }
   697  
   698  func (sptm *StubPrivateTransactionManager) StoreRaw(data []byte, from string) (common.EncryptedPayloadHash, error) {
   699  	return arbitrarySimpleStorageContractEncryptedPayloadHash, nil
   700  }
   701  
   702  func (sptm *StubPrivateTransactionManager) SendSignedTx(data common.EncryptedPayloadHash, to []string, extra *engine.ExtraMetadata) ([]byte, error) {
   703  	return arbitrarySimpleStorageContractEncryptedPayloadHash.Bytes(), nil
   704  }
   705  
   706  func (sptm *StubPrivateTransactionManager) ReceiveRaw(data common.EncryptedPayloadHash) ([]byte, *engine.ExtraMetadata, error) {
   707  	if sptm.creation {
   708  		return hexutil.MustDecode("0x6060604052341561000f57600080fd5b604051602080610149833981016040528080519060200190919050505b806000819055505b505b610104806100456000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632a1afcd914605157806360fe47b11460775780636d4ce63c146097575b600080fd5b3415605b57600080fd5b606160bd565b6040518082815260200191505060405180910390f35b3415608157600080fd5b6095600480803590602001909190505060c3565b005b341560a157600080fd5b60a760ce565b6040518082815260200191505060405180910390f35b60005481565b806000819055505b50565b6000805490505b905600a165627a7a72305820d5851baab720bba574474de3d09dbeaabc674a15f4dd93b974908476542c23f00029"), nil, nil
   709  	} else {
   710  		return hexutil.MustDecode("0x60fe47b1000000000000000000000000000000000000000000000000000000000000000e"), nil, nil
   711  	}
   712  }
   713  
   714  func (sptm *StubPrivateTransactionManager) HasFeature(f engine.PrivateTransactionManagerFeature) bool {
   715  	return true
   716  }