github.com/MetalBlockchain/metalgo@v1.11.9/vms/avm/pubsub_filterer_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 avm
     5  
     6  import (
     7  	"bytes"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/MetalBlockchain/metalgo/ids"
    13  	"github.com/MetalBlockchain/metalgo/pubsub"
    14  	"github.com/MetalBlockchain/metalgo/vms/avm/txs"
    15  	"github.com/MetalBlockchain/metalgo/vms/components/avax"
    16  	"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
    17  )
    18  
    19  type mockFilter struct {
    20  	addr []byte
    21  }
    22  
    23  func (f *mockFilter) Check(addr []byte) bool {
    24  	return bytes.Equal(addr, f.addr)
    25  }
    26  
    27  func TestFilter(t *testing.T) {
    28  	require := require.New(t)
    29  
    30  	addrID := ids.ShortID{1}
    31  	tx := txs.Tx{Unsigned: &txs.BaseTx{BaseTx: avax.BaseTx{
    32  		Outs: []*avax.TransferableOutput{
    33  			{
    34  				Out: &secp256k1fx.TransferOutput{
    35  					OutputOwners: secp256k1fx.OutputOwners{
    36  						Addrs: []ids.ShortID{addrID},
    37  					},
    38  				},
    39  			},
    40  		},
    41  	}}}
    42  	addrBytes := addrID[:]
    43  
    44  	fp := pubsub.NewFilterParam()
    45  	require.NoError(fp.Add(addrBytes))
    46  
    47  	parser := NewPubSubFilterer(&tx)
    48  	fr, _ := parser.Filter([]pubsub.Filter{&mockFilter{addr: addrBytes}})
    49  	require.Equal([]bool{true}, fr)
    50  }