Go-Ethereum

Infura 節點在嘗試部署合約時似乎崩潰了

  • December 19, 2019

我正在嘗試部署契約並對其中的功能執行一些測試。

這是我的程式碼目前的樣子:

package main

import (
   "context"
   "crypto/rand"
   "encoding/hex"
   "flag"
   "math/big"
   "strings"
   "fmt"
   "log"
   "time"

   "github.com/HyperspaceApp/ed25519"
   "github.com/joho/godotenv"
   "github.com/ethereum/go-ethereum"
   "github.com/ethereum/go-ethereum/accounts/abi/bind"
   "github.com/ethereum/go-ethereum/common"
   "github.com/ethereum/go-ethereum/common/hexutil"
   "github.com/ethereum/go-ethereum/ethclient"
   solidityEd25519 "github.com/javgh/ed25519-solidity/contract"
)

var myenv map[string]string

const envLoc = ".env"

func loadEnv() {
   var err error
   if myenv, err = godotenv.Read(envLoc); err != nil {
       log.Printf("could not load env from %s: %v", envLoc, err)
   }
}

func toBigEndian(littleEndian []byte) []byte {
   bigEndian := make([]byte, len(littleEndian))
   for i := range littleEndian {
       bigEndian[i] = littleEndian[len(littleEndian)-1-i]
   }
   return bigEndian
}

func main() {

   var n = flag.Int("n", 3, "number of test cases to generate")
   flag.Parse()

   loadEnv()

   ctx := context.Background()

   // client, err := ethclient.Dial(os.Getenv("KOVAN"))
   client, err := ethclient.Dial("https://kovan.infura.io/v3/<!--SNIP-->")
   if err != nil {
       log.Fatalf("could not connect to Ethereum gateway: %v\n", err)
   }
   defer client.Close()


   account := common.HexToAddress("0x2D76d8729F9Cfa5bC3CEba8a504400FE30bb53D2")
   balance, err := client.BalanceAt(ctx, account, nil)
   if err != nil {
       log.Fatal(err)
   }

   fmt.Println(balance)


   auth, _ := bind.NewTransactor(strings.NewReader("keystore/UTC--2019-12-19T11-17-59.397909000Z--2d76d8729f9cfa5bc3ceba8a504400fe30bb53d2"), "Poppop12!")
   // auth := bind.NewKeyedTransactor(privateKey)
   auth.GasLimit = uint64(100000000)

   // address, _, instance, err := solidityEd25519.DeployEd25519(auth, client)
   address, tx, instance, err := solidityEd25519.DeployEd25519(auth, client)
   if err != nil {
       log.Fatal(err)
   }

   // Waiting for contract to be deployed.
   time.Sleep(10 * time.Second)
   fmt.Println(address.Hex()) // 0x147B8eb97fD247D06C4006D269c90C1908Fb5D54
   fmt.Println(tx.GasPrice())
   fmt.Println(tx.Hash().Hex())

   for i := 0; i < *n; i++ {
       // Create random keypair and compare results from Go and Solidity
       adaptor, adaptorPoint, err := ed25519.GenerateAdaptor(rand.Reader)
       if err != nil {
           log.Fatal(err)
       }

       publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
       if err != nil {
           log.Fatal(err)
       }

       fmt.Println(publicKey)
       fmt.Println(privateKey)

       adaptorBigInt := new(big.Int).SetBytes(toBigEndian(adaptor[:]))
       adaptorPointBytes := toBigEndian(adaptorPoint[:])
       adaptorPointBytes[0] &= 127 // clear sign bit
       adaptorPointBigInt := new(big.Int).SetBytes(adaptorPointBytes)

       _, adaptorPointSolidity, err := instance.ScalarMultBase(nil, adaptorBigInt)
       if err != nil {
           log.Fatal(err)
       }

       var estimateScalarMultBase []byte
       estimateScalarMultBase = append(estimateScalarMultBase, hexutil.MustDecode("0xc4f4912b")...) // scalarMultBase
       estimateScalarMultBase = append(estimateScalarMultBase, common.LeftPadBytes(adaptorBigInt.Bytes(), 32)...)

       gasEstimateestimateScalarMultBase, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
           To:   &address,
           Data: estimateScalarMultBase,
       })
       if err != nil {
           log.Fatal(err)
       }



       wasSuccessful := adaptorPointBigInt.Cmp(adaptorPointSolidity) == 0
       if wasSuccessful {
           fmt.Printf("\nTest successful:\n")
       } else {
           fmt.Printf("\nTest failed:\n")
       }
       fmt.Printf("  Adaptor (little endian)                   : %s\n",
           hex.EncodeToString(adaptor[:]))
       fmt.Printf("  Adaptor (big int, hex)                    : %s\n",
           adaptorBigInt.Text(16))
       fmt.Printf("  Adaptor (big int, decimal)                : %s\n",
           adaptorBigInt.Text(10))
       fmt.Printf("\n")
       fmt.Printf("  Adaptor point (big int, decimal, Go)      : %s\n",
           adaptorPointBigInt.Text(10))
       fmt.Printf("  Adaptor point (big int, decimal, Solidity): %s\n",
           adaptorPointSolidity.Text(10))
       fmt.Printf("\n")
       fmt.Printf("  Gas estimate for Scalar Multiplication: %d\n", gasEstimateestimateScalarMultBase)
       if !wasSuccessful {
           log.Fatal("last test failed")
       }
   }
}

不幸的是,我收到此錯誤:

11000000000000000000
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x455fe00]

goroutine 1 [running]:

我將不勝感激有關此的任何指示。

auth, _ := bind.NewTransactor(strings.NewReader("keystore/UTC--2019-12-19T11-17-59.397909000Z--2d76d8729f9cfa5bc3ceba8a504400fe30bb53d2"), "Poppop12!")

這里_auth大概是nil。嘗試_auth在此行之後記錄。

此外,strings.NewReader("...")創建從作為參數傳遞的字元串讀取的讀取器,而不是從傳遞名稱的文件中讀取。應該是os.Open("...")

引用自:https://ethereum.stackexchange.com/questions/78387