github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/contracts/vns/vns_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package vns
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/vntchain/go-vnt/accounts/abi/bind"
    24  	"github.com/vntchain/go-vnt/accounts/abi/bind/backends"
    25  	"github.com/vntchain/go-vnt/common"
    26  	"github.com/vntchain/go-vnt/contracts/vns/contract"
    27  	"github.com/vntchain/go-vnt/core"
    28  	"github.com/vntchain/go-vnt/crypto"
    29  	"github.com/vntchain/go-vnt/params"
    30  )
    31  
    32  var (
    33  	chainID      = params.TestChainConfig.ChainID
    34  	key, _       = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    35  	name         = "my name on VNS"
    36  	hash         = crypto.Keccak256Hash([]byte("my content")).Hex()
    37  	addr         = crypto.PubkeyToAddress(key.PublicKey)
    38  	testAddr     = common.HexToAddress("0x1234123412341234123412341234123412341234")
    39  	activeKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f292")
    40  	activeAddr   = crypto.PubkeyToAddress(activeKey.PublicKey)
    41  )
    42  
    43  func TestVNS(t *testing.T) {
    44  	contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}, activeAddr: {Balance: big.NewInt(0).Mul(big.NewInt(1e9), big.NewInt(1e18))}})
    45  
    46  	transactOpts := bind.NewKeyedTransactor(key, chainID)
    47  
    48  	vnsAddr, vns, err := DeployVNS(transactOpts, contractBackend)
    49  	if err != nil {
    50  		t.Fatalf("can't deploy root registry: %v", err)
    51  	}
    52  	contractBackend.Commit()
    53  
    54  	// Set ourself as the owner of the name.
    55  	if _, err := vns.Register(name); err != nil {
    56  		t.Fatalf("can't register: %v", err)
    57  	}
    58  	contractBackend.Commit()
    59  
    60  	// Deploy a resolver and make it responsible for the name.
    61  	resolverAddr, _, _, err := contract.DeployPublicResolver(transactOpts, contractBackend, vnsAddr)
    62  	if err != nil {
    63  		t.Fatalf("can't deploy resolver: %v", err)
    64  	}
    65  	if _, err := vns.SetResolver(VnsNode(name), resolverAddr); err != nil {
    66  		t.Fatalf("can't set resolver: %v", err)
    67  	}
    68  	contractBackend.Commit()
    69  
    70  	// Set the content hash for the name.
    71  	if _, err = vns.SetContentHash(name, hash); err != nil {
    72  		t.Fatalf("can't set content hash: %v", err)
    73  	}
    74  	contractBackend.Commit()
    75  
    76  	// Try to resolve the name.
    77  	vhost, err := vns.Resolve(name)
    78  	if err != nil {
    79  		t.Fatalf("expected no error, got %v", err)
    80  	}
    81  	if vhost != hash {
    82  		t.Fatalf("resolve error, expected %v, got %v", hash, vhost)
    83  	}
    84  
    85  	// set the address for the name
    86  	if _, err = vns.SetAddr(name, testAddr); err != nil {
    87  		t.Fatalf("can't set address: %v", err)
    88  	}
    89  	contractBackend.Commit()
    90  
    91  	// Try to resolve the name to an address
    92  	recoveredAddr, err := vns.Addr(name)
    93  	if err != nil {
    94  		t.Fatalf("expected no error, got %v", err)
    95  	}
    96  	if vhost != hash {
    97  		t.Fatalf("resolve error, expected %v, got %v", testAddr.Hex(), recoveredAddr.Hex())
    98  	}
    99  }