github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/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/ethereum/go-ethereum/accounts/abi/bind"
    24  	"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/contracts/ens/contract"
    27  	"github.com/ethereum/go-ethereum/core"
    28  	"github.com/ethereum/go-ethereum/crypto"
    29  )
    30  
    31  var (
    32  	key, _   = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    33  	name     = "my name on ENS"
    34  	hash     = crypto.Keccak256Hash([]byte("my content"))
    35  	addr     = crypto.PubkeyToAddress(key.PublicKey)
    36  	testAddr = common.HexToAddress("0x1234123412341234123412341234123412341234")
    37  )
    38  
    39  func TestENS(t *testing.T) {
    40  	contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000)
    41  	transactOpts := bind.NewKeyedTransactor(key)
    42  
    43  	ensAddr, ens, err := DeployENS(transactOpts, contractBackend)
    44  	if err != nil {
    45  		t.Fatalf("can't deploy root registry: %v", err)
    46  	}
    47  	contractBackend.Commit()
    48  
    49  	// Set ourself as the owner of the name.
    50  	if _, err := ens.Register(name); err != nil {
    51  		t.Fatalf("can't register: %v", err)
    52  	}
    53  	contractBackend.Commit()
    54  
    55  	// Deploy a resolver and make it responsible for the name.
    56  	resolverAddr, _, _, err := contract.DeployPublicResolver(transactOpts, contractBackend, ensAddr)
    57  	if err != nil {
    58  		t.Fatalf("can't deploy resolver: %v", err)
    59  	}
    60  	if _, err := ens.SetResolver(EnsNode(name), resolverAddr); err != nil {
    61  		t.Fatalf("can't set resolver: %v", err)
    62  	}
    63  	contractBackend.Commit()
    64  
    65  	// Set the content hash for the name.
    66  	if _, err = ens.SetContentHash(name, hash); err != nil {
    67  		t.Fatalf("can't set content hash: %v", err)
    68  	}
    69  	contractBackend.Commit()
    70  
    71  	// Try to resolve the name.
    72  	vhost, err := ens.Resolve(name)
    73  	if err != nil {
    74  		t.Fatalf("expected no error, got %v", err)
    75  	}
    76  	if vhost != hash {
    77  		t.Fatalf("resolve error, expected %v, got %v", hash.Hex(), vhost.Hex())
    78  	}
    79  
    80  	// set the address for the name
    81  	if _, err = ens.SetAddr(name, testAddr); err != nil {
    82  		t.Fatalf("can't set address: %v", err)
    83  	}
    84  	contractBackend.Commit()
    85  
    86  	// Try to resolve the name to an address
    87  	recoveredAddr, err := ens.Addr(name)
    88  	if err != nil {
    89  		t.Fatalf("expected no error, got %v", err)
    90  	}
    91  	if vhost != hash {
    92  		t.Fatalf("resolve error, expected %v, got %v", testAddr.Hex(), recoveredAddr.Hex())
    93  	}
    94  }