code.vegaprotocol.io/vega@v0.79.0/core/client/eth/client_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 eth_test
    17  
    18  import (
    19  	"context"
    20  	"encoding/hex"
    21  	"math/big"
    22  	"testing"
    23  
    24  	"code.vegaprotocol.io/vega/core/client/eth"
    25  	"code.vegaprotocol.io/vega/core/client/eth/mocks"
    26  	vgcrypto "code.vegaprotocol.io/vega/libs/crypto"
    27  
    28  	ethcommon "github.com/ethereum/go-ethereum/common"
    29  	"github.com/ethereum/go-ethereum/core/types"
    30  	"github.com/golang/mock/gomock"
    31  	"github.com/stretchr/testify/assert"
    32  )
    33  
    34  func TestNullChain(t *testing.T) {
    35  	t.Run("test valid hash", testValidHash)
    36  	t.Run("test mismatch hash", testMismatchHash)
    37  	t.Run("test current block", testCurrentBlock)
    38  }
    39  
    40  func testValidHash(t *testing.T) {
    41  	contractAddress := "0xBC944ba38753A6fCAdd634Be98379330dbaB3Eb8"
    42  	byteCode := "BC944ba38753A6fCAdd634Be98379330dbaB3Eb8"
    43  	contractCode, _ := hex.DecodeString(byteCode + "a2640033")
    44  	ethAddress := ethcommon.HexToAddress(contractAddress)
    45  
    46  	c := getTestClient(t)
    47  	defer c.ctrl.Finish()
    48  
    49  	c.mockEthClient.EXPECT().CodeAt(gomock.Any(), ethAddress, gomock.Any()).Times(1).Return(contractCode, nil)
    50  
    51  	// get expected hash
    52  	asBytes, _ := hex.DecodeString(byteCode)
    53  	err := c.client.VerifyContract(context.Background(), ethAddress, hex.EncodeToString(vgcrypto.Hash(asBytes)))
    54  	assert.NoError(t, err)
    55  }
    56  
    57  func testMismatchHash(t *testing.T) {
    58  	contractAddress := "0xBC944ba38753A6fCAdd634Be98379330dbaB3Eb8"
    59  	byteCode := "BC944ba38753A6fCAdd634Be98379330dbaB3Eb8"
    60  	contractCode, _ := hex.DecodeString(byteCode + "a2640033")
    61  
    62  	c := getTestClient(t)
    63  	defer c.ctrl.Finish()
    64  
    65  	c.mockEthClient.EXPECT().CodeAt(gomock.Any(), ethcommon.HexToAddress(contractAddress), gomock.Any()).Times(1).Return(contractCode, nil)
    66  
    67  	err := c.client.VerifyContract(context.Background(), ethcommon.HexToAddress(contractAddress), "iamnotthehashyouarelookingfor")
    68  	assert.ErrorIs(t, err, eth.ErrUnexpectedContractHash)
    69  }
    70  
    71  func testCurrentBlock(t *testing.T) {
    72  	number := big.NewInt(19)
    73  	c := getTestClient(t)
    74  	c.mockEthClient.EXPECT().HeaderByNumber(gomock.Any(), gomock.Any()).Return(&types.Header{Number: number}, nil).AnyTimes()
    75  
    76  	defer c.ctrl.Finish()
    77  
    78  	got, err := c.client.CurrentHeight(context.Background())
    79  	if !assert.NoError(t, err, "CurrentHeight()") {
    80  		return
    81  	}
    82  	assert.Equal(t, number.Uint64(), got, "CurrentHeight()")
    83  }
    84  
    85  type testClient struct {
    86  	ctrl          *gomock.Controller
    87  	client        *eth.PrimaryClient
    88  	mockEthClient *mocks.MockETHClient
    89  }
    90  
    91  func getTestClient(t *testing.T) *testClient {
    92  	t.Helper()
    93  	ctrl := gomock.NewController(t)
    94  	mockEthClient := mocks.NewMockETHClient(ctrl)
    95  	c := &eth.PrimaryClient{ETHClient: mockEthClient}
    96  	mockEthClient.EXPECT().ChainID(gomock.Any()).Return(big.NewInt(1), nil).AnyTimes()
    97  
    98  	return &testClient{
    99  		ctrl:          ctrl,
   100  		client:        c,
   101  		mockEthClient: mockEthClient,
   102  	}
   103  }