github.com/gochain-io/gochain/v3@v3.2.8/contracts/ens/ens_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 ens
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/gochain-io/gochain/v3/accounts/abi/bind"
    24  	"github.com/gochain-io/gochain/v3/accounts/abi/bind/backends"
    25  	"github.com/gochain-io/gochain/v3/contracts/ens/contract"
    26  	"github.com/gochain-io/gochain/v3/core"
    27  	"github.com/gochain-io/gochain/v3/crypto"
    28  )
    29  
    30  var (
    31  	key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    32  	name   = "my name on ENS"
    33  	hash   = crypto.Keccak256Hash([]byte("my content"))
    34  	addr   = crypto.PubkeyToAddress(key.PublicKey)
    35  )
    36  
    37  func TestENS(t *testing.T) {
    38  	contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
    39  	transactOpts := bind.NewKeyedTransactor(key)
    40  
    41  	ensAddr, ens, err := DeployENS(transactOpts, contractBackend)
    42  	if err != nil {
    43  		t.Fatalf("can't deploy root registry: %v", err)
    44  	}
    45  	contractBackend.Commit()
    46  
    47  	// Set ourself as the owner of the name.
    48  	if _, err := ens.Register(name); err != nil {
    49  		t.Fatalf("can't register: %v", err)
    50  	}
    51  	contractBackend.Commit()
    52  
    53  	// Deploy a resolver and make it responsible for the name.
    54  	resolverAddr, _, _, err := contract.DeployPublicResolver(transactOpts, contractBackend, ensAddr)
    55  	if err != nil {
    56  		t.Fatalf("can't deploy resolver: %v", err)
    57  	}
    58  	if _, err := ens.SetResolver(ensNode(name), resolverAddr); err != nil {
    59  		t.Fatalf("can't set resolver: %v", err)
    60  	}
    61  	contractBackend.Commit()
    62  
    63  	// Set the content hash for the name.
    64  	if _, err = ens.SetContentHash(name, hash); err != nil {
    65  		t.Fatalf("can't set content hash: %v", err)
    66  	}
    67  	contractBackend.Commit()
    68  
    69  	// Try to resolve the name.
    70  	vhost, err := ens.Resolve(name)
    71  	if err != nil {
    72  		t.Fatalf("expected no error, got %v", err)
    73  	}
    74  	if vhost != hash {
    75  		t.Fatalf("resolve error, expected %v, got %v", hash.Hex(), vhost.Hex())
    76  	}
    77  }