code.vegaprotocol.io/vega@v0.79.0/core/banking/deduplicate_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package banking_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/core/assets"
    24  	"code.vegaprotocol.io/vega/core/assets/erc20"
    25  	"code.vegaprotocol.io/vega/core/types"
    26  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    27  
    28  	"github.com/golang/mock/gomock"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestAssetActionDeduplication(t *testing.T) {
    33  	ctx := context.Background()
    34  
    35  	eng := getTestEngine(t)
    36  	eng.OnPrimaryEthChainIDUpdated("1", "hello")
    37  
    38  	id1 := vgrand.RandomStr(5)
    39  	txHash1 := vgrand.RandomStr(5)
    40  	assetID1 := vgrand.RandomStr(5)
    41  	assetList1 := &types.ERC20AssetList{
    42  		VegaAssetID: assetID1,
    43  	}
    44  	erc20Asset, err := erc20.New(assetID1, &types.AssetDetails{
    45  		Source: &types.AssetDetailsErc20{
    46  			ERC20: &types.ERC20{
    47  				ChainID:           "",
    48  				ContractAddress:   "",
    49  				LifetimeLimit:     nil,
    50  				WithdrawThreshold: nil,
    51  			},
    52  		},
    53  	}, nil, nil)
    54  	require.NoError(t, err)
    55  	asset1 := assets.NewAsset(erc20Asset)
    56  
    57  	t.Run("Generate asset list", func(t *testing.T) {
    58  		eng.assets.EXPECT().Get(assetID1).Times(1).Return(asset1, nil)
    59  		eng.ethSource.EXPECT().UpdateContractBlock(gomock.Any(), gomock.Any(), gomock.Any()).Times(1)
    60  		require.NoError(t, eng.EnableERC20(ctx, assetList1, id1, 1000, 1000, txHash1, "1"))
    61  
    62  		// Validate the asset list.
    63  		eng.witness.f(eng.witness.r, true)
    64  
    65  		// These expectations shows the asset action is processed.
    66  		eng.assets.EXPECT().Get(assetID1).Times(1).Return(asset1, nil)
    67  		eng.assets.EXPECT().Enable(ctx, assetID1).Times(1).Return(nil)
    68  		eng.col.EXPECT().EnableAsset(ctx, *asset1.ToAssetType()).Times(1).Return(nil)
    69  
    70  		// Trigger processing of asset actions, and deduplication.
    71  		eng.OnTick(ctx, time.Now())
    72  	})
    73  
    74  	t.Run("Generate duplicated asset list and ", func(t *testing.T) {
    75  		eng.assets.EXPECT().Get(assetID1).Times(1).Return(asset1, nil)
    76  		require.NoError(t, eng.EnableERC20(ctx, assetList1, id1, 1000, 1000, txHash1, "1"))
    77  
    78  		// Validate the asset list.
    79  		eng.witness.f(eng.witness.r, true)
    80  
    81  		// We expect nothing as the asset action should be deduplicated.
    82  
    83  		// Trigger processing of asset actions, and deduplication.
    84  		eng.OnTick(ctx, time.Now())
    85  	})
    86  
    87  	// This covers the scenario where the event is replayed but with the chain ID
    88  	// set, which might happen with the introduction of the second bridge. We have
    89  	// to ensure the event is acknowledge as a duplicate.
    90  	t.Run("Generate a duplicated event but updated with the chain ID", func(t *testing.T) {
    91  		eng.assets.EXPECT().Get(assetID1).Times(1).Return(asset1, nil)
    92  		require.NoError(t, eng.EnableERC20(ctx, assetList1, id1, 1000, 1000, txHash1, "1"))
    93  
    94  		// Validate the asset list.
    95  		eng.witness.f(eng.witness.r, true)
    96  
    97  		// We expect nothing as the asset action should be deduplicated.
    98  
    99  		// Trigger processing of asset actions, and deduplication.
   100  		eng.OnTick(ctx, time.Now())
   101  	})
   102  }