github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/extension/state_fetcher_test.go (about)

     1  package extension
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/kisexp/xdchain/common"
     8  	"github.com/kisexp/xdchain/core/rawdb"
     9  	"github.com/kisexp/xdchain/core/state"
    10  )
    11  
    12  func TestDumpAddressWhenFound(t *testing.T) {
    13  	//db := ethdb.NewMemDatabase()
    14  	db := rawdb.NewMemoryDatabase()
    15  
    16  	statedb, _ := state.New(common.Hash{}, state.NewDatabase(db), nil)
    17  	address := common.HexToAddress("0x2222222222222222222222222222222222222222")
    18  
    19  	stateFetcher := NewStateFetcher(nil)
    20  
    21  	// generate a few entries and write them out to the db
    22  	statedb.SetBalance(address, big.NewInt(22))
    23  	statedb.SetCode(address, []byte{3, 3, 3, 3, 3, 3, 3})
    24  	statedb.Commit(false)
    25  
    26  	out, _ := stateFetcher.addressStateAsJson(statedb, address)
    27  
    28  	want := `{"0x2222222222222222222222222222222222222222":{"state":{"balance":"22","nonce":0,"root":"56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","codeHash":"87874902497a5bb968da31a2998d8f22e949d1ef6214bcdedd8bae24cca4b9e3","code":"03030303030303"}}}`
    29  
    30  	if string(out) != want {
    31  		t.Errorf("dump mismatch:\ngot: %s\nwant: %s\n", string(out), want)
    32  	}
    33  }
    34  
    35  func TestDumpAddressWhenNotFound(t *testing.T) {
    36  	//db := ethdb.NewMemDatabase()
    37  	db := rawdb.NewMemoryDatabase()
    38  	statedb, _ := state.New(common.Hash{}, state.NewDatabase(db), nil)
    39  	statedb.Commit(false)
    40  
    41  	stateFetcher := NewStateFetcher(nil)
    42  
    43  	address := common.HexToAddress("0x2222222222222222222222222222222222222222")
    44  	out, _ := stateFetcher.addressStateAsJson(statedb, address)
    45  
    46  	if out != nil {
    47  		t.Errorf("dump mismatch:\ngot: %s\nwant: nil\n", string(out))
    48  	}
    49  }