github.com/XinFinOrg/xdcchain@v1.1.0/contracts/randomize/randomize_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 randomize
    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/contracts"
    28  	"github.com/ethereum/go-ethereum/core"
    29  	"github.com/ethereum/go-ethereum/core/types"
    30  	"github.com/ethereum/go-ethereum/crypto"
    31  )
    32  
    33  var (
    34  	epocNumber = int64(12)
    35  	key, _     = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    36  	addr       = crypto.PubkeyToAddress(key.PublicKey)
    37  	byte0      = make([][32]byte, epocNumber)
    38  	acc1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
    39  	acc1Addr   = crypto.PubkeyToAddress(acc1Key.PublicKey)
    40  )
    41  
    42  func TestRandomize(t *testing.T) {
    43  	contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(100000000000000)}}, 10000000)
    44  	transactOpts := bind.NewKeyedTransactor(key)
    45  	transactOpts.GasLimit = 1000000
    46  
    47  	randomizeAddress, randomize, err := DeployRandomize(transactOpts, contractBackend)
    48  	t.Log("contract address", randomizeAddress.String())
    49  	if err != nil {
    50  		t.Fatalf("can't deploy root registry: %v", err)
    51  	}
    52  	contractBackend.Commit()
    53  
    54  	d := time.Now().Add(1000 * time.Millisecond)
    55  	ctx, cancel := context.WithDeadline(context.Background(), d)
    56  	defer cancel()
    57  	code, _ := contractBackend.CodeAt(ctx, randomizeAddress, nil)
    58  	t.Log("contract code", common.ToHex(code))
    59  	f := func(key, val common.Hash) bool {
    60  		t.Log(key.Hex(), val.Hex())
    61  		return true
    62  	}
    63  	contractBackend.ForEachStorageAt(ctx, randomizeAddress, nil, f)
    64  	s, err := randomize.SetSecret(byte0)
    65  	if err != nil {
    66  		t.Fatalf("can't set secret: %v", err)
    67  	}
    68  	t.Log("tx data", s)
    69  	contractBackend.Commit()
    70  }
    71  
    72  func TestSendTxRandomizeSecretAndOpening(t *testing.T) {
    73  	genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000000)}}
    74  	backend := backends.NewSimulatedBackend(genesis, 10000000)
    75  	backend.Commit()
    76  	signer := types.HomesteadSigner{}
    77  	ctx := context.Background()
    78  
    79  	transactOpts := bind.NewKeyedTransactor(acc1Key)
    80  	transactOpts.GasLimit = 4200000
    81  	epocNumber := uint64(900)
    82  	randomizeAddr, randomizeContract, err := DeployRandomize(transactOpts, backend)
    83  	if err != nil {
    84  		t.Fatalf("Can't deploy randomize SC: %v", err)
    85  	}
    86  	backend.Commit()
    87  
    88  	randomizeKeyValue := contracts.RandStringByte(32)
    89  
    90  	for i := 1; i <= 900; i++ {
    91  		nonce := uint64(i)
    92  		switch i {
    93  		case 800:
    94  			tx, err := contracts.BuildTxSecretRandomize(nonce, randomizeAddr, epocNumber, randomizeKeyValue)
    95  			if err != nil {
    96  				t.Fatalf("Can't create tx randomize secret: %v", err)
    97  			}
    98  			tx, err = types.SignTx(tx, signer, acc1Key)
    99  			if err != nil {
   100  				t.Fatalf("Can't sign tx randomize secret: %v", err)
   101  			}
   102  			err = backend.SendTransaction(ctx, tx)
   103  			if err != nil {
   104  				t.Fatalf("Can't send tx for create randomize secret: %v", err)
   105  			}
   106  		case 850:
   107  			// Set opening.
   108  			tx, err := contracts.BuildTxOpeningRandomize(nonce, randomizeAddr, randomizeKeyValue)
   109  			if err != nil {
   110  				t.Fatalf("Can't create tx randomize opening: %v", err)
   111  			}
   112  			tx, err = types.SignTx(tx, signer, acc1Key)
   113  			if err != nil {
   114  				t.Fatalf("Can't sign tx randomize opening: %v", err)
   115  			}
   116  			err = backend.SendTransaction(ctx, tx)
   117  			if err != nil {
   118  				t.Fatalf("Can't send tx for create randomize opening: %v", err)
   119  			}
   120  
   121  		case 900:
   122  			// Get randomize secret from SC.
   123  			secrets, err := randomizeContract.GetSecret(acc1Addr)
   124  			if err != nil {
   125  				t.Error("Fail get secrets from randomize", err)
   126  			}
   127  			if len(secrets) <= 0 {
   128  				t.Error("Empty get secrets from SC", err)
   129  			}
   130  			// Decrypt randomize from SC.
   131  			opening, err := randomizeContract.GetOpening(acc1Addr)
   132  			if err != nil {
   133  				t.Fatalf("Can't get secret from SC: %v", err)
   134  			}
   135  			randomize, err := contracts.DecryptRandomizeFromSecretsAndOpening(secrets, opening)
   136  			t.Log("randomize", randomize)
   137  			if err != nil {
   138  				t.Error("Can't decrypt secret and opening", err)
   139  			}
   140  		default:
   141  			tx, err := types.SignTx(types.NewTransaction(nonce, common.Address{}, new(big.Int), 21000, new(big.Int), nil), signer, acc1Key)
   142  			if err != nil {
   143  				t.Fatalf("Can't sign tx randomize: %v", err)
   144  			}
   145  			err = backend.SendTransaction(ctx, tx)
   146  			if err != nil {
   147  				t.Fatalf("Can't send tx for create randomize: %v", err)
   148  			}
   149  		}
   150  		backend.Commit()
   151  	}
   152  }