github.com/MetalBlockchain/metalgo@v1.11.9/vms/secp256k1fx/mint_operation_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package secp256k1fx
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/MetalBlockchain/metalgo/ids"
    12  	"github.com/MetalBlockchain/metalgo/vms/components/verify"
    13  )
    14  
    15  func TestMintOperationVerify(t *testing.T) {
    16  	var (
    17  		validOutputOwners = OutputOwners{
    18  			Threshold: 1,
    19  			Addrs:     []ids.ShortID{ids.GenerateTestShortID()},
    20  		}
    21  		validMintInput = Input{
    22  			SigIndices: []uint32{0},
    23  		}
    24  		validMintOutput = MintOutput{
    25  			OutputOwners: validOutputOwners,
    26  		}
    27  		validTransferOutput = TransferOutput{
    28  			Amt:          1,
    29  			OutputOwners: validOutputOwners,
    30  		}
    31  	)
    32  
    33  	tests := []struct {
    34  		name        string
    35  		op          *MintOperation
    36  		expectedErr error
    37  	}{
    38  		{
    39  			name:        "nil",
    40  			op:          nil,
    41  			expectedErr: errNilMintOperation,
    42  		},
    43  		{
    44  			name: "invalid mint input",
    45  			op: &MintOperation{
    46  				MintInput: Input{
    47  					SigIndices: []uint32{0, 0},
    48  				},
    49  				MintOutput:     validMintOutput,
    50  				TransferOutput: validTransferOutput,
    51  			},
    52  			expectedErr: ErrInputIndicesNotSortedUnique,
    53  		},
    54  		{
    55  			name: "invalid mint output",
    56  			op: &MintOperation{
    57  				MintInput: validMintInput,
    58  				MintOutput: MintOutput{
    59  					OutputOwners: OutputOwners{
    60  						Threshold: 2,
    61  						Addrs:     []ids.ShortID{ids.GenerateTestShortID()},
    62  					},
    63  				},
    64  				TransferOutput: validTransferOutput,
    65  			},
    66  			expectedErr: ErrOutputUnspendable,
    67  		},
    68  		{
    69  			name: "invalid transfer output",
    70  			op: &MintOperation{
    71  				MintInput:  validMintInput,
    72  				MintOutput: validMintOutput,
    73  				TransferOutput: TransferOutput{
    74  					Amt: 1,
    75  					OutputOwners: OutputOwners{
    76  						Threshold: 0,
    77  						Addrs:     []ids.ShortID{ids.GenerateTestShortID()},
    78  					},
    79  				},
    80  			},
    81  			expectedErr: ErrOutputUnoptimized,
    82  		},
    83  		{
    84  			name: "addresses not unique",
    85  			op: &MintOperation{
    86  				MintInput:  validMintInput,
    87  				MintOutput: validMintOutput,
    88  				TransferOutput: TransferOutput{
    89  					Amt: 1,
    90  					OutputOwners: OutputOwners{
    91  						Threshold: 1,
    92  						Addrs:     []ids.ShortID{ids.ShortEmpty, ids.ShortEmpty},
    93  					},
    94  				},
    95  			},
    96  			expectedErr: ErrAddrsNotSortedUnique,
    97  		},
    98  		{
    99  			name: "addresses not sorted",
   100  			op: &MintOperation{
   101  				MintInput:  validMintInput,
   102  				MintOutput: validMintOutput,
   103  				TransferOutput: TransferOutput{
   104  					Amt: 1,
   105  					OutputOwners: OutputOwners{
   106  						Threshold: 1,
   107  						Addrs:     []ids.ShortID{{2}, {1}},
   108  					},
   109  				},
   110  			},
   111  			expectedErr: ErrAddrsNotSortedUnique,
   112  		},
   113  		{
   114  			name: "passes verification",
   115  			op: &MintOperation{
   116  				MintInput:      validMintInput,
   117  				MintOutput:     validMintOutput,
   118  				TransferOutput: validTransferOutput,
   119  			},
   120  			expectedErr: nil,
   121  		},
   122  	}
   123  
   124  	for _, tt := range tests {
   125  		t.Run(tt.name, func(t *testing.T) {
   126  			err := tt.op.Verify()
   127  			require.ErrorIs(t, err, tt.expectedErr)
   128  		})
   129  	}
   130  }
   131  
   132  func TestMintOperationOuts(t *testing.T) {
   133  	require := require.New(t)
   134  	op := &MintOperation{
   135  		MintInput: Input{
   136  			SigIndices: []uint32{0},
   137  		},
   138  		MintOutput: MintOutput{
   139  			OutputOwners: OutputOwners{
   140  				Threshold: 1,
   141  				Addrs: []ids.ShortID{
   142  					addr,
   143  				},
   144  			},
   145  		},
   146  		TransferOutput: TransferOutput{
   147  			Amt: 1,
   148  			OutputOwners: OutputOwners{
   149  				Locktime:  0,
   150  				Threshold: 1,
   151  			},
   152  		},
   153  	}
   154  
   155  	require.Len(op.Outs(), 2)
   156  }
   157  
   158  func TestMintOperationState(t *testing.T) {
   159  	require := require.New(t)
   160  	intf := interface{}(&MintOperation{})
   161  	_, ok := intf.(verify.State)
   162  	require.False(ok)
   163  }