Skip to content

Commit 3e808a9

Browse files
committed
Add sdk/ton/executor_test.go
1 parent ef225fb commit 3e808a9

File tree

8 files changed

+404
-31
lines changed

8 files changed

+404
-31
lines changed

sdk/evm/executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (e *Executor) ExecuteOperation(
3737
op types.Operation,
3838
) (types.TransactionResult, error) {
3939
if e.Encoder == nil {
40-
return types.TransactionResult{}, errors.New("Executor was created without an encoder")
40+
return types.TransactionResult{}, errors.New("failed to create sdk.Executor - encoder (sdk.Encoder) is nil")
4141
}
4242

4343
bindOp, err := e.ToGethOperation(nonce, metadata, op)
@@ -74,7 +74,7 @@ func (e *Executor) SetRoot(
7474
sortedSignatures []types.Signature,
7575
) (types.TransactionResult, error) {
7676
if e.Encoder == nil {
77-
return types.TransactionResult{}, errors.New("Executor was created without an encoder")
77+
return types.TransactionResult{}, errors.New("failed to create sdk.Executor - encoder (sdk.Encoder) is nil")
7878
}
7979

8080
bindMeta, err := e.ToGethRootMetadata(metadata)

sdk/evm/executor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func TestExecutor_ExecuteOperation(t *testing.T) {
152152
encoder: nil,
153153
mockSetup: func(m *evm_mocks.ContractDeployBackend) {},
154154
wantTxHash: "",
155-
wantErr: errors.New("Executor was created without an encoder"),
155+
wantErr: errors.New("failed to create sdk.Executor - encoder (sdk.Encoder) is nil"),
156156
},
157157
{
158158
name: "failure in geth operation conversion due to invalid chain ID",
@@ -311,7 +311,7 @@ func TestExecutor_SetRoot(t *testing.T) {
311311
encoder: nil,
312312
mockSetup: func(m *evm_mocks.ContractDeployBackend) {},
313313
wantTxHash: "",
314-
wantErr: errors.New("Executor was created without an encoder"),
314+
wantErr: errors.New("failed to create sdk.Executor - encoder (sdk.Encoder) is nil"),
315315
},
316316
{
317317
name: "failure in geth operation conversion due to invalid chain ID",

sdk/ton/configurer_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/mock"
1313
"github.com/stretchr/testify/require"
14+
15+
"github.com/smartcontractkit/mcms/internal/testutils/chaintest"
16+
"github.com/smartcontractkit/mcms/types"
17+
1418
"github.com/xssnick/tonutils-go/tlb"
1519
"github.com/xssnick/tonutils-go/ton"
1620
"github.com/xssnick/tonutils-go/ton/wallet"
1721

18-
"github.com/smartcontractkit/mcms/internal/testutils/chaintest"
1922
tonmcms "github.com/smartcontractkit/mcms/sdk/ton"
2023
ton_mocks "github.com/smartcontractkit/mcms/sdk/ton/mocks"
21-
"github.com/smartcontractkit/mcms/types"
2224
)
2325

2426
// TestConfigurer_SetConfig tests the SetConfig method of the Configurer.
@@ -127,7 +129,7 @@ func TestConfigurer_SetConfig(t *testing.T) {
127129

128130
// Mock SendTransaction to return an error
129131
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"))
132+
Return(&tlb.Transaction{Hash: []byte{1, 2, 3, 4, 14}}, &ton.BlockIDExt{}, []byte{}, errors.New("transaction failed"))
131133
},
132134
want: "",
133135
wantErr: errors.New("transaction failed"),

sdk/ton/executor.go

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"math/big"
99
"math/rand/v2"
10+
"reflect"
1011

1112
"github.com/ethereum/go-ethereum/common"
1213

@@ -38,13 +39,25 @@ type executor struct {
3839
}
3940

4041
// NewExecutor creates a new Executor for TON chains
41-
func NewExecutor(encoder sdk.Encoder, client *ton.APIClient, wallet *wallet.Wallet, amount tlb.Coins) sdk.Executor {
42+
func NewExecutor(encoder sdk.Encoder, client ton.APIClientWrapped, wallet *wallet.Wallet, amount tlb.Coins) (sdk.Executor, error) {
43+
if IsNil(encoder) {
44+
return nil, errors.New("failed to create sdk.Executor - encoder (sdk.Encoder) is nil")
45+
}
46+
47+
if IsNil(client) {
48+
return nil, errors.New("failed to create sdk.Executor - client (ton.APIClientWrapped) is nil")
49+
}
50+
51+
if wallet == nil {
52+
return nil, errors.New("failed to create sdk.Executor - wallet (*wallet.Wallet) is nil")
53+
}
54+
4255
return &executor{
4356
Encoder: encoder,
4457
Inspector: NewInspector(client, NewConfigTransformer()),
4558
wallet: wallet,
4659
amount: amount,
47-
}
60+
}, nil
4861
}
4962

5063
func (e *executor) ExecuteOperation(
@@ -54,10 +67,6 @@ func (e *executor) ExecuteOperation(
5467
proof []common.Hash,
5568
op types.Operation,
5669
) (types.TransactionResult, error) {
57-
if e.Encoder == nil {
58-
return types.TransactionResult{}, errors.New("executor was created without an encoder")
59-
}
60-
6170
oe, ok := e.Encoder.(OperationEncoder[mcms.Op])
6271
if !ok {
6372
return types.TransactionResult{}, fmt.Errorf("failed to assert OperationEncoder")
@@ -109,7 +118,7 @@ func (e *executor) ExecuteOperation(
109118
// TODO: do we wait for execution trace?
110119
tx, _, err := e.wallet.SendWaitTransaction(ctx, msg)
111120
if err != nil {
112-
return types.TransactionResult{}, fmt.Errorf("failed to set config: %w", err)
121+
return types.TransactionResult{}, fmt.Errorf("failed to execute op: %w", err)
113122
}
114123

115124
return types.TransactionResult{
@@ -127,16 +136,6 @@ func (e *executor) SetRoot(
127136
validUntil uint32,
128137
sortedSignatures []types.Signature,
129138
) (types.TransactionResult, error) {
130-
if e.Encoder == nil {
131-
return types.TransactionResult{}, errors.New("Executor was created without an encoder")
132-
}
133-
134-
// Map to Ton Address type
135-
dstAddr, err := address.ParseAddr(metadata.MCMAddress)
136-
if err != nil {
137-
return types.TransactionResult{}, fmt.Errorf("invalid timelock address: %w", err)
138-
}
139-
140139
rme, ok := e.Encoder.(RootMetadataEncoder[mcms.RootMetadata])
141140
if !ok {
142141
return types.TransactionResult{}, fmt.Errorf("failed to assert RootMetadataEncoder")
@@ -147,6 +146,12 @@ func (e *executor) SetRoot(
147146
return types.TransactionResult{}, fmt.Errorf("failed to convert to root metadata: %w", err)
148147
}
149148

149+
// Map to Ton Address type
150+
dstAddr, err := address.ParseAddr(metadata.MCMAddress)
151+
if err != nil {
152+
return types.TransactionResult{}, fmt.Errorf("invalid timelock address: %w", err)
153+
}
154+
150155
// Encode proofs
151156
pe, ok := e.Encoder.(ProofEncoder[mcms.Proof])
152157
if !ok {
@@ -197,7 +202,7 @@ func (e *executor) SetRoot(
197202
// TODO: do we wait for execution trace?
198203
tx, _, err := e.wallet.SendWaitTransaction(ctx, msg)
199204
if err != nil {
200-
return types.TransactionResult{}, fmt.Errorf("failed to set config: %w", err)
205+
return types.TransactionResult{}, fmt.Errorf("failed to set root: %w", err)
201206
}
202207

203208
return types.TransactionResult{
@@ -206,3 +211,18 @@ func (e *executor) SetRoot(
206211
RawData: tx,
207212
}, nil
208213
}
214+
215+
// IsNil checks if a value is nil or if it's a reference type with a nil underlying value.
216+
// Notice: vendoring github:samber/lo
217+
func IsNil(x any) bool {
218+
if x == nil {
219+
return true
220+
}
221+
v := reflect.ValueOf(x)
222+
switch v.Kind() {
223+
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
224+
return v.IsNil()
225+
default:
226+
return false
227+
}
228+
}

0 commit comments

Comments
 (0)