github.com/XinFinOrg/xdcchain@v1.1.0/contracts/blocksigner/blocksigner_test.go (about)

     1  // Copyright (c) 2018 XDCchain
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Lesser General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    11  // GNU Lesser General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Lesser General Public License
    14  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package blocksigner
    17  
    18  import (
    19  	"context"
    20  	"math/big"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    25  	"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/core"
    28  	"github.com/ethereum/go-ethereum/crypto"
    29  	"math/rand"
    30  )
    31  
    32  var (
    33  	key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    34  	addr   = crypto.PubkeyToAddress(key.PublicKey)
    35  )
    36  
    37  func TestBlockSigner(t *testing.T) {
    38  	contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000)
    39  	transactOpts := bind.NewKeyedTransactor(key)
    40  
    41  	blockSignerAddress, blockSigner, err := DeployBlockSigner(transactOpts, contractBackend, big.NewInt(99))
    42  	if err != nil {
    43  		t.Fatalf("can't deploy root registry: %v", err)
    44  	}
    45  	contractBackend.Commit()
    46  
    47  	d := time.Now().Add(1000 * time.Millisecond)
    48  	ctx, cancel := context.WithDeadline(context.Background(), d)
    49  	defer cancel()
    50  	code, _ := contractBackend.CodeAt(ctx, blockSignerAddress, nil)
    51  	t.Log("contract code", common.ToHex(code))
    52  	f := func(key, val common.Hash) bool {
    53  		t.Log(key.Hex(), val.Hex())
    54  		return true
    55  	}
    56  	contractBackend.ForEachStorageAt(ctx, blockSignerAddress, nil, f)
    57  
    58  	byte0 := randomHash()
    59  
    60  	// Test sign.
    61  	tx, err := blockSigner.Sign(big.NewInt(2), byte0)
    62  	if err != nil {
    63  		t.Fatalf("can't sign: %v", err)
    64  	}
    65  	contractBackend.Commit()
    66  	t.Log("tx", tx)
    67  
    68  	signers, err := blockSigner.GetSigners(byte0)
    69  	if err != nil {
    70  		t.Fatalf("can't get candidates: %v", err)
    71  	}
    72  	for _, it := range signers {
    73  		t.Log("signer", it.String())
    74  	}
    75  }
    76  
    77  // Generate random string.
    78  func randomHash() common.Hash {
    79  	letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
    80  	var b common.Hash
    81  	for i := range b {
    82  		rand.Seed(time.Now().UnixNano())
    83  		b[i] = letterBytes[rand.Intn(len(letterBytes))]
    84  	}
    85  	return b
    86  }