github.com/iotexproject/iotex-core@v1.14.1-rc1/e2etest/util.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package e2etest
     7  
     8  import (
     9  	"context"
    10  	"encoding/hex"
    11  	"math/big"
    12  	"os"
    13  	"path/filepath"
    14  
    15  	"github.com/iotexproject/go-pkgs/hash"
    16  	"github.com/pkg/errors"
    17  
    18  	"github.com/iotexproject/iotex-core/action"
    19  	"github.com/iotexproject/iotex-core/actpool"
    20  	"github.com/iotexproject/iotex-core/blockchain"
    21  	"github.com/iotexproject/iotex-core/pkg/unit"
    22  	"github.com/iotexproject/iotex-core/test/identityset"
    23  	"github.com/iotexproject/iotex-core/testutil"
    24  )
    25  
    26  func addTestingTsfBlocks(bc blockchain.Blockchain, ap actpool.ActPool) error {
    27  	ctx := context.Background()
    28  	addOneTx := func(tx *action.SealedEnvelope, err error) error {
    29  		if err != nil {
    30  			return err
    31  		}
    32  		if err := ap.Add(ctx, tx); err != nil {
    33  			return err
    34  		}
    35  		return nil
    36  	}
    37  	// Add block 1
    38  	// deploy simple smart contract
    39  	sk0 := identityset.PrivateKey(0)
    40  	data, _ := hex.DecodeString("608060405234801561001057600080fd5b50610233806100206000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635bec9e671461005c57806360fe47b114610073578063c2bc2efc146100a0575b600080fd5b34801561006857600080fd5b506100716100f7565b005b34801561007f57600080fd5b5061009e60048036038101908080359060200190929190505050610143565b005b3480156100ac57600080fd5b506100e1600480360381019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061017a565b6040518082815260200191505060405180910390f35b5b6001156101155760008081548092919060010191905055506100f8565b7f8bfaa460932ccf8751604dd60efa3eafa220ec358fccb32ef703f91c509bc3ea60405160405180910390a1565b80600081905550807fdf7a95aebff315db1b7716215d602ab537373cdb769232aae6055c06e798425b60405160405180910390a250565b60008073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16141515156101b757600080fd5b6000548273ffffffffffffffffffffffffffffffffffffffff167fbde7a70c2261170a87678200113c8e12f82f63d0a1d1cfa45681cbac328e87e360405160405180910390a360005490509190505600a165627a7a723058203198d0390613dab2dff2fa053c1865e802618d628429b01ab05b8458afc347eb0029")
    41  	if err := addOneTx(action.SignedExecution(action.EmptyAddress, sk0, 1, big.NewInt(0), 200000, big.NewInt(testutil.TestGasPriceInt64), data)); err != nil {
    42  		return err
    43  	}
    44  	if err := addOneTx(action.SignedTransfer(identityset.Address(27).String(), sk0, 2, unit.ConvertIotxToRau(90000000), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
    45  		return err
    46  	}
    47  	blk, err := bc.MintNewBlock(testutil.TimestampNow())
    48  	if err != nil {
    49  		return err
    50  	}
    51  	if err := bc.CommitBlock(blk); err != nil {
    52  		return err
    53  	}
    54  
    55  	addr0 := identityset.Address(27).String()
    56  	priKey0 := identityset.PrivateKey(27)
    57  	addr1 := identityset.Address(28).String()
    58  	addr2 := identityset.Address(29).String()
    59  	addr3 := identityset.Address(30).String()
    60  	priKey3 := identityset.PrivateKey(30)
    61  	addr4 := identityset.Address(31).String()
    62  	priKey4 := identityset.PrivateKey(31)
    63  	addr5 := identityset.Address(32).String()
    64  	priKey5 := identityset.PrivateKey(32)
    65  	addr6 := identityset.Address(33).String()
    66  	// Add block 2
    67  	// test --> A, B, C, D, E, F
    68  	if err = addOneTx(action.SignedTransfer(addr1, priKey0, 1, big.NewInt(20), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
    69  		return err
    70  	}
    71  	if err = addOneTx(action.SignedTransfer(addr2, priKey0, 2, big.NewInt(30), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
    72  		return err
    73  	}
    74  	if err = addOneTx(action.SignedTransfer(addr3, priKey0, 3, big.NewInt(50), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
    75  		return err
    76  	}
    77  	if err = addOneTx(action.SignedTransfer(addr4, priKey0, 4, big.NewInt(70), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
    78  		return err
    79  	}
    80  	if err = addOneTx(action.SignedTransfer(addr5, priKey0, 5, big.NewInt(110), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
    81  		return err
    82  	}
    83  	if err = addOneTx(action.SignedTransfer(addr6, priKey0, 6, big.NewInt(5<<20), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
    84  		return err
    85  	}
    86  	// call set()
    87  	data, _ = hex.DecodeString("60fe47b1")
    88  	data = append(data, hash.ZeroHash256[:]...)
    89  	if err := addOneTx(action.SignedExecution("io18vlvlj0v02yye70kpqtzhu4uek3qqz27zm7g42", sk0, 3, big.NewInt(0), testutil.TestGasLimit*5, big.NewInt(testutil.TestGasPriceInt64), data)); err != nil {
    90  		return err
    91  	}
    92  	blk, err = bc.MintNewBlock(testutil.TimestampNow())
    93  	if err != nil {
    94  		return err
    95  	}
    96  	if err := bc.CommitBlock(blk); err != nil {
    97  		return err
    98  	}
    99  
   100  	// Add block 3
   101  	// Charlie --> A, B, D, E, test
   102  	if err = addOneTx(action.SignedTransfer(addr1, priKey3, 1, big.NewInt(1), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   103  		return err
   104  	}
   105  	if err = addOneTx(action.SignedTransfer(addr2, priKey3, 2, big.NewInt(1), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   106  		return err
   107  	}
   108  	if err = addOneTx(action.SignedTransfer(addr4, priKey3, 3, big.NewInt(1), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   109  		return err
   110  	}
   111  	if err = addOneTx(action.SignedTransfer(addr5, priKey3, 4, big.NewInt(1), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   112  		return err
   113  	}
   114  	if err = addOneTx(action.SignedTransfer(addr0, priKey3, 5, big.NewInt(1), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   115  		return err
   116  	}
   117  	// call get()
   118  	getData, _ := hex.DecodeString("c2bc2efc")
   119  	getData = append(getData, hash.ZeroHash256[:]...)
   120  	if err = addOneTx(action.SignedExecution("io18vlvlj0v02yye70kpqtzhu4uek3qqz27zm7g42", sk0, 4, big.NewInt(0), testutil.TestGasLimit*5, big.NewInt(testutil.TestGasPriceInt64), getData)); err != nil {
   121  		return err
   122  	}
   123  	// call set()
   124  	if err = addOneTx(action.SignedExecution("io18vlvlj0v02yye70kpqtzhu4uek3qqz27zm7g42", sk0, 5, big.NewInt(0), testutil.TestGasLimit*5, big.NewInt(testutil.TestGasPriceInt64), data)); err != nil {
   125  		return err
   126  	}
   127  	blk, err = bc.MintNewBlock(testutil.TimestampNow())
   128  	if err != nil {
   129  		return err
   130  	}
   131  	if err := bc.CommitBlock(blk); err != nil {
   132  		return err
   133  	}
   134  
   135  	// Add block 4
   136  	// Delta --> B, E, F, test
   137  	if err = addOneTx(action.SignedTransfer(addr2, priKey4, 1, big.NewInt(1), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   138  		return err
   139  	}
   140  	if err = addOneTx(action.SignedTransfer(addr5, priKey4, 2, big.NewInt(1), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   141  		return err
   142  	}
   143  	if err = addOneTx(action.SignedTransfer(addr6, priKey4, 3, big.NewInt(1), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   144  		return err
   145  	}
   146  	if err = addOneTx(action.SignedTransfer(addr0, priKey4, 4, big.NewInt(1), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   147  		return err
   148  	}
   149  	// call get()
   150  	if err = addOneTx(action.SignedExecution("io18vlvlj0v02yye70kpqtzhu4uek3qqz27zm7g42", sk0, 6, big.NewInt(0), testutil.TestGasLimit*5, big.NewInt(testutil.TestGasPriceInt64), getData)); err != nil {
   151  		return err
   152  	}
   153  	// call set()
   154  	if err = addOneTx(action.SignedExecution("io18vlvlj0v02yye70kpqtzhu4uek3qqz27zm7g42", sk0, 7, big.NewInt(0), testutil.TestGasLimit*5, big.NewInt(testutil.TestGasPriceInt64), data)); err != nil {
   155  		return err
   156  	}
   157  	// call get()
   158  	if err = addOneTx(action.SignedExecution("io18vlvlj0v02yye70kpqtzhu4uek3qqz27zm7g42", sk0, 8, big.NewInt(0), testutil.TestGasLimit*5, big.NewInt(testutil.TestGasPriceInt64), getData)); err != nil {
   159  		return err
   160  	}
   161  	blk, err = bc.MintNewBlock(testutil.TimestampNow())
   162  	if err != nil {
   163  		return err
   164  	}
   165  	if err := bc.CommitBlock(blk); err != nil {
   166  		return err
   167  	}
   168  
   169  	// Add block 5
   170  	// Delta --> A, B, C, D, F, test
   171  	if err = addOneTx(action.SignedTransfer(addr1, priKey5, 1, big.NewInt(2), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   172  		return err
   173  	}
   174  	if err = addOneTx(action.SignedTransfer(addr2, priKey5, 2, big.NewInt(2), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   175  		return err
   176  	}
   177  	if err = addOneTx(action.SignedTransfer(addr3, priKey5, 3, big.NewInt(2), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   178  		return err
   179  	}
   180  	if err = addOneTx(action.SignedTransfer(addr4, priKey5, 4, big.NewInt(2), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   181  		return err
   182  	}
   183  	if err = addOneTx(action.SignedTransfer(addr6, priKey5, 5, big.NewInt(2), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   184  		return err
   185  	}
   186  	if err = addOneTx(action.SignedTransfer(addr0, priKey5, 6, big.NewInt(2), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64))); err != nil {
   187  		return err
   188  	}
   189  	blk, err = bc.MintNewBlock(testutil.TimestampNow())
   190  	if err != nil {
   191  		return err
   192  	}
   193  	return bc.CommitBlock(blk)
   194  }
   195  
   196  func copyDB(srcDB, dstDB string) error {
   197  	input, err := os.ReadFile(filepath.Clean(srcDB))
   198  	if err != nil {
   199  		return errors.Wrap(err, "failed to read source db file")
   200  	}
   201  	if err := os.WriteFile(dstDB, input, 0644); err != nil {
   202  		return errors.Wrap(err, "failed to copy db file")
   203  	}
   204  	return nil
   205  }