|
| 1 | +package ton_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "math/big" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/ethereum/go-ethereum/common" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/mock" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "github.com/xssnick/tonutils-go/tlb" |
| 15 | + "github.com/xssnick/tonutils-go/ton" |
| 16 | + "github.com/xssnick/tonutils-go/ton/wallet" |
| 17 | + |
| 18 | + "github.com/smartcontractkit/mcms/internal/testutils/chaintest" |
| 19 | + tonmcms "github.com/smartcontractkit/mcms/sdk/ton" |
| 20 | + ton_mocks "github.com/smartcontractkit/mcms/sdk/ton/mocks" |
| 21 | + "github.com/smartcontractkit/mcms/types" |
| 22 | +) |
| 23 | + |
| 24 | +// TestConfigurer_SetConfig tests the SetConfig method of the Configurer. |
| 25 | +func TestConfigurer_SetConfig(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + |
| 28 | + ctx := context.Background() |
| 29 | + |
| 30 | + // Initialize the mock |
| 31 | + chainID := chaintest.Chain7ToniID |
| 32 | + api := ton_mocks.NewTonAPI(t) |
| 33 | + wallets := []*wallet.Wallet{ |
| 34 | + must(makeRandomTestWallet(api, chainID)), |
| 35 | + must(makeRandomTestWallet(api, chainID)), |
| 36 | + must(makeRandomTestWallet(api, chainID)), |
| 37 | + must(makeRandomTestWallet(api, chainID)), |
| 38 | + } |
| 39 | + |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + mcmAddr string |
| 43 | + cfg *types.Config |
| 44 | + clearRoot bool |
| 45 | + mockSetup func(m *ton_mocks.TonAPI) |
| 46 | + want string |
| 47 | + wantErr error |
| 48 | + }{ |
| 49 | + { |
| 50 | + name: "success", |
| 51 | + mcmAddr: "EQADa3W6G0nSiTV4a6euRA42fU9QxSEnb-WeDpcrtWzA2jM8", |
| 52 | + cfg: &types.Config{ |
| 53 | + Quorum: 2, |
| 54 | + Signers: []common.Address{ |
| 55 | + common.Address(mustKey(wallets[1]).Bytes()), |
| 56 | + common.Address(mustKey(wallets[2]).Bytes()), |
| 57 | + }, |
| 58 | + GroupSigners: []types.Config{ |
| 59 | + { |
| 60 | + Quorum: 1, |
| 61 | + Signers: []common.Address{ |
| 62 | + common.Address(mustKey(wallets[3]).Bytes()), |
| 63 | + }, |
| 64 | + GroupSigners: nil, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + clearRoot: true, |
| 69 | + mockSetup: func(m *ton_mocks.TonAPI) { |
| 70 | + // Mock CurrentMasterchainInfo |
| 71 | + m.EXPECT().CurrentMasterchainInfo(mock.Anything). |
| 72 | + Return(&ton.BlockIDExt{}, nil) |
| 73 | + |
| 74 | + // Mock WaitForBlock |
| 75 | + apiw := ton_mocks.NewAPIClientWrapped(t) |
| 76 | + apiw.EXPECT().GetAccount(mock.Anything, mock.Anything, mock.Anything). |
| 77 | + Return(&tlb.Account{}, nil) |
| 78 | + |
| 79 | + apiw.EXPECT().RunGetMethod(mock.Anything, mock.Anything, mock.Anything, mock.Anything). |
| 80 | + Return(ton.NewExecutionResult([]any{big.NewInt(5)}), nil) |
| 81 | + |
| 82 | + m.EXPECT().WaitForBlock(mock.Anything). |
| 83 | + Return(apiw) |
| 84 | + |
| 85 | + // Mock SendTransaction to return an error |
| 86 | + m.EXPECT().SendExternalMessageWaitTransaction(mock.Anything, mock.Anything). |
| 87 | + Return(&tlb.Transaction{Hash: []byte{1, 2, 3, 4, 14}}, &ton.BlockIDExt{}, []byte{}, nil) |
| 88 | + }, |
| 89 | + want: "010203040e", |
| 90 | + wantErr: nil, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "failure - SendTransaction fails", |
| 94 | + mcmAddr: "EQADa3W6G0nSiTV4a6euRA42fU9QxSEnb-WeDpcrtWzA2jM8", |
| 95 | + cfg: &types.Config{ |
| 96 | + Quorum: 2, |
| 97 | + Signers: []common.Address{ |
| 98 | + common.Address(mustKey(wallets[1]).Bytes()), |
| 99 | + common.Address(mustKey(wallets[2]).Bytes()), |
| 100 | + }, |
| 101 | + GroupSigners: []types.Config{ |
| 102 | + { |
| 103 | + Quorum: 1, |
| 104 | + Signers: []common.Address{ |
| 105 | + common.Address(mustKey(wallets[3]).Bytes()), |
| 106 | + }, |
| 107 | + GroupSigners: nil, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + clearRoot: false, |
| 112 | + mockSetup: func(m *ton_mocks.TonAPI) { |
| 113 | + // Mock CurrentMasterchainInfo |
| 114 | + m.EXPECT().CurrentMasterchainInfo(mock.Anything). |
| 115 | + Return(&ton.BlockIDExt{}, nil) |
| 116 | + |
| 117 | + // Mock WaitForBlock |
| 118 | + apiw := ton_mocks.NewAPIClientWrapped(t) |
| 119 | + apiw.EXPECT().GetAccount(mock.Anything, mock.Anything, mock.Anything). |
| 120 | + Return(&tlb.Account{}, nil) |
| 121 | + |
| 122 | + apiw.EXPECT().RunGetMethod(mock.Anything, mock.Anything, mock.Anything, mock.Anything). |
| 123 | + Return(ton.NewExecutionResult([]any{big.NewInt(5)}), nil) |
| 124 | + |
| 125 | + m.EXPECT().WaitForBlock(mock.Anything). |
| 126 | + Return(apiw) |
| 127 | + |
| 128 | + // Mock SendTransaction to return an error |
| 129 | + m.EXPECT().SendExternalMessageWaitTransaction(mock.Anything, mock.Anything). |
| 130 | + Return(&tlb.Transaction{Hash: []byte{1, 2, 3, 4, 15}}, &ton.BlockIDExt{}, []byte{}, errors.New("transaction failed")) |
| 131 | + }, |
| 132 | + want: "", |
| 133 | + wantErr: errors.New("transaction failed"), |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + for _, tt := range tests { |
| 138 | + t.Run(tt.name, func(t *testing.T) { |
| 139 | + t.Parallel() |
| 140 | + |
| 141 | + _api := ton_mocks.NewTonAPI(t) |
| 142 | + walletOperator := must(makeRandomTestWallet(_api, chainID)) |
| 143 | + |
| 144 | + // Apply the mock setup for the ContractDeployBackend |
| 145 | + if tt.mockSetup != nil { |
| 146 | + tt.mockSetup(_api) |
| 147 | + } |
| 148 | + |
| 149 | + // Create the Configurer instance |
| 150 | + configurer, err := tonmcms.NewConfigurer(walletOperator, tlb.MustFromTON("0")) |
| 151 | + require.NoError(t, err) |
| 152 | + |
| 153 | + // Call SetConfig |
| 154 | + tx, err := configurer.SetConfig(ctx, tt.mcmAddr, tt.cfg, tt.clearRoot) |
| 155 | + |
| 156 | + // Assert the results |
| 157 | + if tt.wantErr != nil { |
| 158 | + require.Error(t, err) |
| 159 | + assert.Contains(t, err.Error(), tt.wantErr.Error()) |
| 160 | + assert.Empty(t, tx.Hash) |
| 161 | + } else { |
| 162 | + require.NoError(t, err) |
| 163 | + assert.Equal(t, tt.want, tx.Hash) |
| 164 | + } |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
0 commit comments