github.com/annchain/OG@v0.0.9/og/txmaker/ogtx_creator_test.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package txmaker
    15  
    16  import (
    17  	"fmt"
    18  	types2 "github.com/annchain/OG/arefactor/og/types"
    19  	"github.com/annchain/OG/arefactor/og_interface"
    20  	"github.com/annchain/OG/common/math"
    21  	ogcrypto2 "github.com/annchain/OG/deprecated/ogcrypto"
    22  	"github.com/annchain/OG/og/types"
    23  
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/annchain/OG/common"
    28  	"github.com/annchain/OG/ogcore/miner"
    29  	"github.com/sirupsen/logrus"
    30  )
    31  
    32  type AllOkVerifier struct{}
    33  
    34  func (AllOkVerifier) Verify(t types.Txi) bool {
    35  	return true
    36  }
    37  
    38  func (AllOkVerifier) Name() string {
    39  	return "AllOkVerifier"
    40  }
    41  
    42  func (a *AllOkVerifier) String() string {
    43  	return a.Name()
    44  }
    45  
    46  func (a *AllOkVerifier) Independent() bool {
    47  	return false
    48  }
    49  
    50  func Init() *OGTxCreator {
    51  	og_interface.Signer = &ogcrypto2.SignerEd25519{}
    52  	txc := OGTxCreator{
    53  		TipGenerator:       &dummyTxPoolRandomTx{},
    54  		Miner:              &miner.PoWMiner{},
    55  		MaxConnectingTries: 100,
    56  		MaxTxHash:          types2.HexToHash("0x0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
    57  		MaxMinedHash:       types2.HexToHash("0x00000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
    58  		GraphVerifier:      &AllOkVerifier{},
    59  	}
    60  	return &txc
    61  }
    62  
    63  func TestTxCreator(t *testing.T) {
    64  	logrus.SetLevel(logrus.DebugLevel)
    65  	txc := Init()
    66  	tx := txc.TipGenerator.GetRandomTips(1)[0].(*types.Tx)
    67  	_, priv := og_interface.Signer.RandomKeyPair()
    68  	time1 := time.Now()
    69  
    70  	txSigned := txc.NewSignedTx(SignedTxBuildRequest{
    71  		UnsignedTxBuildRequest: UnsignedTxBuildRequest{
    72  			From:         *tx.From,
    73  			To:           tx.To,
    74  			Value:        tx.Value,
    75  			AccountNonce: tx.AccountNonce,
    76  			TokenId:      0,
    77  		},
    78  		PrivateKey: priv,
    79  	})
    80  
    81  	logrus.Infof("total time for Signing: %d ns", time.Since(time1).Nanoseconds())
    82  	ok := txc.SealTx(txSigned, &priv)
    83  	logrus.Infof("result: %t %v", ok, txSigned)
    84  	txdata, _ := tx.MarshalMsg(nil)
    85  	rawtx := tx.RawTx()
    86  	rawTxData, _ := rawtx.MarshalMsg(nil)
    87  	msg := archive.MessageNewTx{
    88  		RawTx: rawtx,
    89  	}
    90  	msgData, _ := msg.MarshalMsg(nil)
    91  	logrus.WithField("len tx ", len(txdata)).WithField("len raw tx ", len(rawTxData)).WithField(
    92  		"len msg", len(msgData)).Debug("encode msg")
    93  	logrus.Debug(txSigned.Dump())
    94  }
    95  
    96  func TestSequencerCreator(t *testing.T) {
    97  	logrus.SetLevel(logrus.DebugLevel)
    98  	txc := Init()
    99  	_, priv := og_interface.Signer.RandomKeyPair()
   100  	time1 := time.Now()
   101  
   102  	// for copy
   103  	randomSeq := types.RandomSequencer()
   104  
   105  	txSigned := txc.NewSignedSequencer(SignedSequencerBuildRequest{
   106  		UnsignedSequencerBuildRequest: UnsignedSequencerBuildRequest{
   107  			Height:       randomSeq.Height,
   108  			AccountNonce: randomSeq.AccountNonce,
   109  			Issuer:       common.Address{},
   110  		},
   111  		PrivateKey: priv,
   112  	})
   113  	logrus.Infof("total time for Signing: %d ns", time.Since(time1).Nanoseconds())
   114  	ok := txc.SealTx(txSigned, &priv)
   115  	logrus.Infof("result: %t %v", ok, txSigned)
   116  }
   117  
   118  func sampleTxi(selfHash string, parentsHash []string, baseType types.TxBaseType) types.Txi {
   119  
   120  	tx := &types.Tx{TxBase: types.TxBase{
   121  		ParentsHash: types2.Hashes{},
   122  		Type:        types.TxBaseTypeTx,
   123  		Hash:        types2.HexToHash(selfHash),
   124  	},
   125  	}
   126  	for _, h := range parentsHash {
   127  		tx.ParentsHash = append(tx.ParentsHash, types2.HexToHash(h))
   128  	}
   129  	return tx
   130  }
   131  
   132  func TestBuildDag(t *testing.T) {
   133  	og_interface.Signer = &ogcrypto2.SignerEd25519{}
   134  	logrus.SetLevel(logrus.DebugLevel)
   135  	pool := &dummyTxPoolMiniTx{}
   136  	pool.Init()
   137  	txc := OGTxCreator{
   138  		TipGenerator:       pool,
   139  		Miner:              &miner.PoWMiner{},
   140  		MaxConnectingTries: 10,
   141  		MaxTxHash:          types2.HexToHash("0x0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
   142  		MaxMinedHash:       types2.HexToHash("0x000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
   143  		GraphVerifier:      &AllOkVerifier{},
   144  	}
   145  
   146  	_, privateKey := og_interface.Signer.RandomKeyPair()
   147  
   148  	txs := []types.Txi{
   149  		txc.NewSignedSequencer(SignedSequencerBuildRequest{
   150  			UnsignedSequencerBuildRequest: UnsignedSequencerBuildRequest{
   151  				Issuer:       common.Address{},
   152  				Height:       0,
   153  				AccountNonce: 0,
   154  			},
   155  			PrivateKey: privateKey,
   156  		}),
   157  
   158  		txc.NewSignedTx(SignedTxBuildRequest{
   159  			UnsignedTxBuildRequest: UnsignedTxBuildRequest{
   160  				From:         common.HexToAddress("0x01"),
   161  				To:           common.HexToAddress("0x02"),
   162  				Value:        math.NewBigInt(10),
   163  				AccountNonce: 0,
   164  				TokenId:      0,
   165  			},
   166  			PrivateKey: privateKey,
   167  		}),
   168  		txc.NewSignedSequencer(SignedSequencerBuildRequest{
   169  			UnsignedSequencerBuildRequest: UnsignedSequencerBuildRequest{
   170  				Issuer:       common.Address{},
   171  				Height:       1,
   172  				AccountNonce: 1,
   173  			},
   174  			PrivateKey: privateKey,
   175  		}),
   176  		txc.NewSignedTx(SignedTxBuildRequest{
   177  			UnsignedTxBuildRequest: UnsignedTxBuildRequest{
   178  				From:         common.HexToAddress("0x02"),
   179  				To:           common.HexToAddress("0x03"),
   180  				Value:        math.NewBigInt(9),
   181  				AccountNonce: 0,
   182  				TokenId:      0,
   183  			},
   184  			PrivateKey: privateKey,
   185  		}),
   186  		txc.NewSignedTx(SignedTxBuildRequest{
   187  			UnsignedTxBuildRequest: UnsignedTxBuildRequest{
   188  				From:         common.HexToAddress("0x03"),
   189  				To:           common.HexToAddress("0x04"),
   190  				Value:        math.NewBigInt(8),
   191  				AccountNonce: 0,
   192  				TokenId:      0,
   193  			},
   194  			PrivateKey: privateKey,
   195  		}),
   196  		txc.NewSignedSequencer(SignedSequencerBuildRequest{
   197  			UnsignedSequencerBuildRequest: UnsignedSequencerBuildRequest{
   198  				Issuer:       common.Address{},
   199  				Height:       2,
   200  				AccountNonce: 2,
   201  			},
   202  			PrivateKey: privateKey,
   203  		}),
   204  	}
   205  
   206  	txs[0].GetBase().Hash = txs[0].CalcTxHash()
   207  	pool.Add(txs[0])
   208  	for i := 1; i < len(txs); i++ {
   209  		if ok := txc.SealTx(txs[i], &privateKey); ok {
   210  			pool.Add(txs[i])
   211  		}
   212  	}
   213  }
   214  
   215  func TestNewFIFOTIpGenerator(t *testing.T) {
   216  	logrus.SetLevel(logrus.TraceLevel)
   217  	f := NewFIFOTIpGenerator(&dummyTxPoolRandomTx{}, 15)
   218  	fmt.Println(f.fifoRing)
   219  	f.GetRandomTips(3)
   220  	fmt.Println(f.fifoRing)
   221  	fmt.Println(f.fifoRingPos)
   222  	f.fifoRing[2].SetValid(false)
   223  	f.validation()
   224  	fmt.Println(f.fifoRingPos)
   225  	fmt.Println(f.fifoRing)
   226  	f.GetRandomTips(2)
   227  	fmt.Println(f.fifoRing)
   228  }
   229  
   230  func TestSlice(t *testing.T) {
   231  	var parents types.Txis
   232  	parentHashes := make(types2.Hashes, len(parents))
   233  	for i, parent := range parents {
   234  		parentHashes[i] = parent.GetHash()
   235  	}
   236  }