ens_test.go 4.2 KB

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