github.com/gochain-io/gochain@v2.2.26+incompatible/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  	"context"
    21  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/gochain-io/gochain/accounts/abi/bind"
    25  	"github.com/gochain-io/gochain/accounts/abi/bind/backends"
    26  	"github.com/gochain-io/gochain/contracts/ens/contract"
    27  	"github.com/gochain-io/gochain/core"
    28  	"github.com/gochain-io/gochain/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  )
    37  
    38  func TestENS(t *testing.T) {
    39  	ctx := context.Background()
    40  	contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
    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(ctx)
    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(ctx)
    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(ctx)
    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(ctx)
    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  }