github.com/klaytn/klaytn@v1.12.1/node/sc/bridge_addr_journal_test.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package sc
    18  
    19  import (
    20  	"os"
    21  	"path"
    22  	"testing"
    23  
    24  	"github.com/klaytn/klaytn/common"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestBridgeJournal(t *testing.T) {
    29  	defer func() {
    30  		if err := os.Remove(path.Join(os.TempDir(), "test.rlp")); err != nil {
    31  			t.Fatalf("fail to delete file %v", err)
    32  		}
    33  	}()
    34  
    35  	journal := newBridgeAddrJournal(path.Join(os.TempDir(), "test.rlp"))
    36  	if err := journal.load(func(journal BridgeJournal) error {
    37  		t.Log("Local address ", journal.ChildAddress.Hex())
    38  		t.Log("Remote address ", journal.ParentAddress.Hex())
    39  		t.Log("Subscribed", journal.Subscribed)
    40  		return nil
    41  	}); err != nil {
    42  		t.Fatalf("fail to load journal %v", err)
    43  	}
    44  	if err := journal.rotate([]*BridgeJournal{}); err != nil {
    45  		t.Fatalf("fail to rotate journal %v", err)
    46  	}
    47  
    48  	err := journal.insert("", common.BytesToAddress([]byte("test1")), common.BytesToAddress([]byte("test2")))
    49  	if err != nil {
    50  		t.Fatalf("fail to insert address %v", err)
    51  	}
    52  	err = journal.insert("", common.BytesToAddress([]byte("test2")), common.BytesToAddress([]byte("test3")))
    53  	if err != nil {
    54  		t.Fatalf("fail to insert address %v", err)
    55  	}
    56  	err = journal.insert("", common.BytesToAddress([]byte("test3")), common.BytesToAddress([]byte("test1")))
    57  	if err != nil {
    58  		t.Fatalf("fail to insert address %v", err)
    59  	}
    60  
    61  	if err := journal.close(); err != nil {
    62  		t.Fatalf("fail to close file %v", err)
    63  	}
    64  
    65  	journal = newBridgeAddrJournal(path.Join(os.TempDir(), "test.rlp"))
    66  
    67  	if err := journal.load(func(journal BridgeJournal) error {
    68  		switch address := journal.ChildAddress.Hex(); address {
    69  		case "0x0000000000000000000000000000007465737431":
    70  			if journal.ParentAddress.Hex() != "0x0000000000000000000000000000007465737432" {
    71  				t.Fatalf("unknown remoteAddress")
    72  			}
    73  		case "0x0000000000000000000000000000007465737432":
    74  			if journal.ParentAddress.Hex() != "0x0000000000000000000000000000007465737433" {
    75  				t.Fatalf("unknown remoteAddress")
    76  			}
    77  		case "0x0000000000000000000000000000007465737433":
    78  			if journal.ParentAddress.Hex() != "0x0000000000000000000000000000007465737431" {
    79  				t.Fatalf("unknown remoteAddress")
    80  			}
    81  		default:
    82  			t.Fatalf("unknown localAddress")
    83  		}
    84  		return nil
    85  	}); err != nil {
    86  		t.Fatalf("fail to load journal %v", err)
    87  	}
    88  }
    89  
    90  // TestBridgeJournalCache tests the journal cache.
    91  func TestBridgeJournalCache(t *testing.T) {
    92  	defer func() {
    93  		if err := os.Remove(path.Join(os.TempDir(), "test.rlp")); err != nil {
    94  			t.Fatalf("fail to delete file %v", err)
    95  		}
    96  	}()
    97  
    98  	// Step 1: Make new journal
    99  	journals := newBridgeAddrJournal(path.Join(os.TempDir(), "test.rlp"))
   100  
   101  	if err := journals.load(func(journal BridgeJournal) error { return nil }); err != nil {
   102  		t.Fatalf("fail to load journal %v", err)
   103  	}
   104  	if err := journals.rotate([]*BridgeJournal{}); err != nil {
   105  		t.Fatalf("fail to rotate journal %v", err)
   106  	}
   107  
   108  	localAddr := common.BytesToAddress([]byte("test1"))
   109  	remoteAddr := common.BytesToAddress([]byte("test2"))
   110  
   111  	err := journals.insert("", localAddr, remoteAddr)
   112  	if err != nil {
   113  		t.Fatalf("fail to insert address %v", err)
   114  	}
   115  
   116  	assert.Equal(t, 1, len(journals.cache))
   117  	for _, journal := range journals.cache {
   118  		assert.Equal(t, localAddr, journal.ChildAddress)
   119  		assert.Equal(t, remoteAddr, journal.ParentAddress)
   120  	}
   121  }