github.com/annchain/OG@v0.0.9/og/verifier/graph_verifier_test.go (about)

     1  package verifier
     2  
     3  import (
     4  	types2 "github.com/annchain/OG/arefactor/og/types"
     5  	"github.com/annchain/OG/common"
     6  	"github.com/annchain/OG/og/dummy"
     7  	"github.com/annchain/OG/og/protocol/ogmessage/archive"
     8  	"github.com/annchain/OG/og/types"
     9  
    10  	"github.com/sirupsen/logrus"
    11  	"testing"
    12  )
    13  
    14  func buildTx(from common.Address, accountNonce uint64) *types.Tx {
    15  	tx := archive.RandomTx()
    16  	tx.AccountNonce = accountNonce
    17  	tx.From = &from
    18  	return tx
    19  }
    20  
    21  func buildSeq(from common.Address, accountNonce uint64, id uint64) *types.Sequencer {
    22  	tx := types.RandomSequencer()
    23  	tx.AccountNonce = accountNonce
    24  	tx.Issuer = &from
    25  	return tx
    26  }
    27  
    28  func setParents(tx types.Txi, parents []types.Txi) {
    29  	tx.GetBase().ParentsHash = types2.Hashes{}
    30  	for _, parent := range parents {
    31  		tx.GetBase().ParentsHash = append(tx.GetBase().ParentsHash, parent.GetHash())
    32  	}
    33  }
    34  
    35  func judge(t *testing.T, actual interface{}, want interface{}, id int) {
    36  	assert.Equal(t, actual, want)
    37  	if actual != want {
    38  		logrus.Warnf("Assert %d, expected %t, actual %t.", id, want, actual)
    39  	}
    40  }
    41  
    42  // A3: Nodes produced by same source must be sequential (tx nonce ++).
    43  func TestA3(t *testing.T) {
    44  	pool := &DummyTxPoolParents{}
    45  	pool.Init()
    46  	dag := &dummyDag{}
    47  	dag.init()
    48  
    49  	addr1 := common.HexToAddress("0x0001")
    50  	addr2 := common.HexToAddress("0x0002")
    51  
    52  	txs := []types.Txi{
    53  		buildSeq(addr1, 1, 1),
    54  		buildTx(addr1, 2),
    55  		buildTx(addr2, 0),
    56  		buildTx(addr1, 3),
    57  		buildTx(addr2, 1),
    58  
    59  		buildTx(addr2, 2),
    60  		buildTx(addr1, 4),
    61  		buildTx(addr2, 3),
    62  		buildTx(addr1, 2),
    63  		buildTx(addr1, 4),
    64  
    65  		buildTx(addr2, 4),
    66  		buildTx(addr1, 3),
    67  	}
    68  
    69  	setParents(txs[0], []types.Txi{})
    70  	setParents(txs[1], []types.Txi{txs[0]})
    71  	setParents(txs[2], []types.Txi{txs[1], txs[7]})
    72  	setParents(txs[3], []types.Txi{txs[2], txs[8]})
    73  	setParents(txs[4], []types.Txi{txs[3], txs[9]})
    74  
    75  	setParents(txs[5], []types.Txi{txs[4], txs[10]})
    76  	setParents(txs[6], []types.Txi{txs[5]})
    77  	setParents(txs[7], []types.Txi{txs[0]})
    78  	setParents(txs[8], []types.Txi{txs[7]})
    79  	setParents(txs[9], []types.Txi{txs[8]})
    80  
    81  	setParents(txs[10], []types.Txi{txs[9]})
    82  	setParents(txs[11], []types.Txi{txs[10]})
    83  
    84  	for _, tx := range txs {
    85  		pool.AddRemoteTx(tx, true)
    86  	}
    87  
    88  	// tx2 is bad, others are good
    89  	verifier := GraphVerifier{
    90  		Dag:    dag,
    91  		TxPool: pool,
    92  	}
    93  
    94  	truth := []int{
    95  		0, 1, 1, 1, 1,
    96  		1, 1, 0, 1, 0,
    97  		1, 1}
    98  
    99  	for i, tx := range txs {
   100  		judge(t, verifier.verifyA3(tx), truth[i] == 1, i)
   101  	}
   102  
   103  }
   104  
   105  // A6: [My job] Node cannot reference two un-ordered nodes as its parents
   106  func TestA6(t *testing.T) {
   107  	pool := &dummy.DummyTxPoolParents{}
   108  	pool.Init()
   109  	dag := &dummyDag{}
   110  	dag.init()
   111  
   112  	addr1 := common.HexToAddress("0x0001")
   113  	addr2 := common.HexToAddress("0x0002")
   114  	addr3 := common.HexToAddress("0x0003")
   115  
   116  	txs := []types.Txi{
   117  		buildSeq(addr1, 1, 1),
   118  		buildTx(addr1, 2),
   119  		buildTx(addr1, 0),
   120  		buildTx(addr2, 3),
   121  		buildTx(addr3, 1),
   122  
   123  		buildTx(addr2, 2),
   124  		buildTx(addr1, 4),
   125  		buildTx(addr3, 3),
   126  		buildTx(addr2, 2),
   127  	}
   128  
   129  	setParents(txs[0], []types.Txi{})
   130  	setParents(txs[1], []types.Txi{txs[0]})
   131  	setParents(txs[2], []types.Txi{txs[1], txs[6]})
   132  	setParents(txs[3], []types.Txi{txs[2], txs[7]})
   133  	setParents(txs[4], []types.Txi{txs[3], txs[8]})
   134  
   135  	setParents(txs[5], []types.Txi{txs[4]})
   136  	setParents(txs[6], []types.Txi{txs[0]})
   137  	setParents(txs[7], []types.Txi{txs[2]})
   138  	setParents(txs[8], []types.Txi{txs[7]})
   139  
   140  	for _, tx := range txs {
   141  		pool.AddRemoteTx(tx, true)
   142  	}
   143  
   144  	// tx2 is bad, others are good
   145  	verifier := GraphVerifier{
   146  		Dag:    dag,
   147  		TxPool: pool,
   148  	}
   149  
   150  	truth := []int{
   151  		0, 1, 0, 1, 0,
   152  		1, 1, 1, 1}
   153  
   154  	for i, tx := range txs {
   155  		judge(t, verifier.verifyA3(tx), truth[i] == 1, i)
   156  	}
   157  
   158  }