github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/rpc/backend/cache_lru_test.go (about)

     1  package backend
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/ethereum/go-ethereum/common"
     7  	"github.com/ethereum/go-ethereum/common/hexutil"
     8  	"github.com/fibonacci-chain/fbc/x/evm/watcher"
     9  	"github.com/spf13/viper"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestLruCache_AddOrUpdateBlock(t *testing.T) {
    14  	type args struct {
    15  		block *watcher.Block
    16  	}
    17  	type result struct {
    18  		blockCount int
    19  		block      *watcher.Block
    20  		txCount    int
    21  	}
    22  	tests := []struct {
    23  		name   string
    24  		args   args
    25  		result result
    26  	}{
    27  		{
    28  			name: "cache empty Block",
    29  			args: args{
    30  				block: &watcher.Block{
    31  					Number:       hexutil.Uint64(0x10),
    32  					Hash:         common.HexToHash("0x6b2cfa0a20e291ca0bb58b2112086f247026bb94a65133e87ee3aaa4658399e5"),
    33  					Transactions: []*watcher.Transaction{},
    34  				},
    35  			},
    36  			result: result{
    37  				blockCount: 1,
    38  				block: &watcher.Block{
    39  					Number:       hexutil.Uint64(0x10),
    40  					Hash:         common.HexToHash("0x6b2cfa0a20e291ca0bb58b2112086f247026bb94a65133e87ee3aaa4658399e5"),
    41  					Transactions: []*watcher.Transaction{},
    42  				},
    43  				txCount: 0,
    44  			},
    45  		},
    46  		{
    47  			name: "duplicate Block",
    48  			args: args{
    49  				block: &watcher.Block{
    50  					Number:       hexutil.Uint64(0x10),
    51  					Hash:         common.HexToHash("0x6b2cfa0a20e291ca0bb58b2112086f247026bb94a65133e87ee3aaa4658399e5"),
    52  					Transactions: []*watcher.Transaction{},
    53  				},
    54  			},
    55  			result: result{
    56  				blockCount: 1,
    57  				block: &watcher.Block{
    58  					Number:       hexutil.Uint64(0x10),
    59  					Hash:         common.HexToHash("0x6b2cfa0a20e291ca0bb58b2112086f247026bb94a65133e87ee3aaa4658399e5"),
    60  					Transactions: []*watcher.Transaction{},
    61  				},
    62  				txCount: 0,
    63  			},
    64  		},
    65  		{
    66  			name: "Block with txs",
    67  			args: args{
    68  				block: &watcher.Block{
    69  					Number: hexutil.Uint64(0x11),
    70  					Hash:   common.HexToHash("0x3bb254ed105476b94583eec8375c5d2fc0a5cf50047c5912b4337ba43a837b88"),
    71  					Transactions: []*watcher.Transaction{
    72  						{
    73  							From: common.HexToAddress("0xbbe4733d85bc2b90682147779da49cab38c0aa1f"),
    74  							Hash: common.HexToHash("0xb4a40e844ee4c012d4a6d9e16d4ee8dcf52ef5042da491dbc73574f6764e17d1"),
    75  						},
    76  					},
    77  				},
    78  			},
    79  			result: result{
    80  				blockCount: 2,
    81  				txCount:    1,
    82  				block: &watcher.Block{
    83  					Number: hexutil.Uint64(0x11),
    84  					Hash:   common.HexToHash("0x3bb254ed105476b94583eec8375c5d2fc0a5cf50047c5912b4337ba43a837b88"),
    85  					Transactions: []*watcher.Transaction{
    86  						{
    87  							From: common.HexToAddress("0xbbe4733d85bc2b90682147779da49cab38c0aa1f"),
    88  							Hash: common.HexToHash("0xb4a40e844ee4c012d4a6d9e16d4ee8dcf52ef5042da491dbc73574f6764e17d1"),
    89  						},
    90  					},
    91  				},
    92  			},
    93  		},
    94  	}
    95  	viper.Set(FlagApiBackendBlockLruCache, 100)
    96  	viper.Set(FlagApiBackendTxLruCache, 100)
    97  	alc := NewLruCache()
    98  
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			alc.AddOrUpdateBlock(tt.args.block.Hash, tt.args.block, true)
   102  			blockLru := alc.lruBlockWithFullTx
   103  			require.NotNil(t, blockLru)
   104  			require.Equal(t, tt.result.blockCount, blockLru.Len())
   105  
   106  			block, err := alc.GetBlockByHash(tt.result.block.Hash, true)
   107  			require.Nil(t, err)
   108  			require.NotNil(t, block)
   109  			require.Equal(t, tt.result.block.Hash, block.Hash)
   110  
   111  			//must update tx in block
   112  			txLru := alc.lruTx
   113  			require.NotNil(t, txLru)
   114  			require.Equal(t, tt.result.txCount, txLru.Len())
   115  		})
   116  	}
   117  }
   118  
   119  func TestLruCache_AddOrUpdateTransaction(t *testing.T) {
   120  	type result struct {
   121  		tx      *watcher.Transaction
   122  		txCount int
   123  	}
   124  	type args struct {
   125  		tx *watcher.Transaction
   126  	}
   127  	tests := []struct {
   128  		name   string
   129  		args   args
   130  		result result
   131  	}{
   132  		{
   133  			name: "cache tx",
   134  			args: args{
   135  				tx: &watcher.Transaction{
   136  					From: common.HexToAddress("0xbbe4733d85bc2b90682147779da49cab38c0aa1f"),
   137  					Hash: common.HexToHash("0xb4a40e844ee4c012d4a6d9e16d4ee8dcf52ef5042da491dbc73574f6764e17d1"),
   138  				},
   139  			},
   140  			result: result{
   141  				txCount: 1,
   142  				tx: &watcher.Transaction{
   143  					From: common.HexToAddress("0xbbe4733d85bc2b90682147779da49cab38c0aa1f"),
   144  					Hash: common.HexToHash("0xb4a40e844ee4c012d4a6d9e16d4ee8dcf52ef5042da491dbc73574f6764e17d1"),
   145  				},
   146  			},
   147  		},
   148  		{
   149  			name: "duplicate tx",
   150  			args: args{
   151  				tx: &watcher.Transaction{
   152  					From: common.HexToAddress("0xbbe4733d85bc2b90682147779da49cab38c0aa1f"),
   153  					Hash: common.HexToHash("0xb4a40e844ee4c012d4a6d9e16d4ee8dcf52ef5042da491dbc73574f6764e17d1"),
   154  				},
   155  			},
   156  			result: result{
   157  				txCount: 1,
   158  				tx: &watcher.Transaction{
   159  					From: common.HexToAddress("0xbbe4733d85bc2b90682147779da49cab38c0aa1f"),
   160  					Hash: common.HexToHash("0xb4a40e844ee4c012d4a6d9e16d4ee8dcf52ef5042da491dbc73574f6764e17d1"),
   161  				},
   162  			},
   163  		},
   164  	}
   165  	viper.Set(FlagApiBackendTxLruCache, 100)
   166  	alc := NewLruCache()
   167  	for _, tt := range tests {
   168  		t.Run(tt.name, func(t *testing.T) {
   169  			alc.AddOrUpdateTransaction(tt.args.tx.Hash, tt.args.tx)
   170  			txLru := alc.lruTx
   171  			require.NotNil(t, txLru)
   172  			require.Equal(t, tt.result.txCount, txLru.Len())
   173  
   174  			tx, err := alc.GetTransaction(tt.result.tx.Hash)
   175  			require.Nil(t, err)
   176  			require.NotNil(t, tx)
   177  			require.Equal(t, tt.result.tx.Hash, tx.Hash)
   178  		})
   179  	}
   180  }
   181  
   182  func TestLruCache_GetBlockByNumber(t *testing.T) {
   183  	type args struct {
   184  		block *watcher.Block
   185  	}
   186  	type result struct {
   187  		blockCount int
   188  		block      *watcher.Block
   189  		txCount    int
   190  	}
   191  	tests := []struct {
   192  		name   string
   193  		args   args
   194  		result result
   195  	}{
   196  		{
   197  			name: "Get Block by Number",
   198  			args: args{
   199  				block: &watcher.Block{
   200  					Number:       hexutil.Uint64(0x10),
   201  					Hash:         common.HexToHash("0x6b2cfa0a20e291ca0bb58b2112086f247026bb94a65133e87ee3aaa4658399e5"),
   202  					Transactions: []*watcher.Transaction{},
   203  				},
   204  			},
   205  			result: result{
   206  				blockCount: 1,
   207  				block: &watcher.Block{
   208  					Number:       hexutil.Uint64(0x10),
   209  					Hash:         common.HexToHash("0x6b2cfa0a20e291ca0bb58b2112086f247026bb94a65133e87ee3aaa4658399e5"),
   210  					Transactions: []*watcher.Transaction{},
   211  				},
   212  				txCount: 0,
   213  			},
   214  		},
   215  	}
   216  	viper.Set(FlagApiBackendBlockLruCache, 100) // must be 3
   217  	alc := NewLruCache()
   218  
   219  	for _, tt := range tests {
   220  		t.Run(tt.name, func(t *testing.T) {
   221  			alc.AddOrUpdateBlock(tt.args.block.Hash, tt.args.block, true)
   222  			alc.AddOrUpdateBlockHash(uint64(tt.args.block.Number), tt.args.block.Hash)
   223  
   224  			blockLru := alc.lruBlockWithFullTx
   225  			require.NotNil(t, blockLru)
   226  			require.Equal(t, tt.result.blockCount, blockLru.Len())
   227  
   228  			blockInfoLru := alc.lruBlockInfo
   229  			require.NotNil(t, blockInfoLru)
   230  			require.Equal(t, tt.result.blockCount, blockInfoLru.Len())
   231  
   232  			block, err := alc.GetBlockByNumber(uint64(tt.result.block.Number), true)
   233  			require.Nil(t, err)
   234  			require.NotNil(t, block)
   235  			require.Equal(t, tt.result.block.Hash, block.Hash)
   236  		})
   237  	}
   238  }