github.com/annchain/OG@v0.0.9/arefactor_core/core/account_flow_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 core_test
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/annchain/OG/core/state"
    20  	"github.com/annchain/OG/types/tx_types"
    21  
    22  	"github.com/annchain/OG/common/crypto"
    23  	"github.com/annchain/OG/common/math"
    24  	"github.com/annchain/OG/core"
    25  	"github.com/annchain/OG/og"
    26  )
    27  
    28  func newTestAccountFlowTx(nonce uint64, value *math.BigInt) *tx_types.Tx {
    29  	txCreator := &og.TxCreator{}
    30  	pk, _ := crypto.PrivateKeyFromString(testPkSecp0)
    31  	addr := newTestAddress(pk)
    32  
    33  	tx := txCreator.NewSignedTx(addr, addr, value, nonce, pk, 0)
    34  	tx.SetHash(tx.CalcTxHash())
    35  
    36  	return tx.(*tx_types.Tx)
    37  }
    38  
    39  func TestTxList(t *testing.T) {
    40  	t.Parallel()
    41  
    42  	testNonces := []uint64{208, 505, 910, 157, 771, 718, 98, 897, 538, 38}
    43  
    44  	tl := core.NewTxList()
    45  	for _, nonce := range testNonces {
    46  		tx := newTestAccountFlowTx(nonce, math.NewBigInt(0))
    47  		tl.Put(tx)
    48  	}
    49  
    50  	if tl.Len() != len(testNonces) {
    51  		t.Fatalf("txlist's length not equal the number of inserted txs")
    52  	}
    53  	for _, nonce := range testNonces {
    54  		tx := tl.Get(nonce)
    55  		if tx == nil {
    56  			t.Fatalf("can't get tx from txlist, nonce: %d", nonce)
    57  		}
    58  		if tx.GetNonce() != nonce {
    59  			t.Fatalf("nonce not same, expect %d but get %d", nonce, tx.GetNonce())
    60  		}
    61  	}
    62  	for _, nonce := range testNonces {
    63  		if !tl.Remove(nonce) {
    64  			t.Fatalf("remove tx from txlist failed, nonce %d", nonce)
    65  		}
    66  		if tl.Get(nonce) != nil {
    67  			t.Fatalf("still get tx from txlist after removed, nonce %d", nonce)
    68  		}
    69  	}
    70  }
    71  
    72  func TestBalanceState(t *testing.T) {
    73  	t.Parallel()
    74  
    75  	var err error
    76  	var spent int64
    77  
    78  	originBalance := math.NewBigInt(100000)
    79  	bs := core.NewBalanceState(originBalance)
    80  
    81  	// test TryProcessTx
    82  	fstValue := int64(10000)
    83  	subValue := math.NewBigInt(fstValue)
    84  	err = bs.TryProcessTx(subValue)
    85  	if err != nil {
    86  		t.Fatalf("TryProcessTx err: %v", err)
    87  	}
    88  	spent = bs.Spent().GetInt64()
    89  	if spent != fstValue {
    90  		t.Fatalf("the value of spent is not correct, expect %d, get %d", fstValue, spent)
    91  	}
    92  
    93  	// test TryRemoveValue
    94  	tx0value := int64(1000)
    95  	tx1value := int64(2000)
    96  	tx2value := int64(3000)
    97  	tx0 := newTestAccountFlowTx(0, math.NewBigInt(tx0value))
    98  	tx1 := newTestAccountFlowTx(1, math.NewBigInt(tx1value))
    99  	tx2 := newTestAccountFlowTx(2, math.NewBigInt(tx2value))
   100  
   101  	err = bs.TryRemoveValue(tx0.GetValue())
   102  	if err != nil {
   103  		t.Fatalf("TryRemoveValue tx0 error: %v", err)
   104  	}
   105  	spent = bs.Spent().GetInt64()
   106  	if spent != (fstValue - tx0value) {
   107  		t.Fatalf("the value of spent is not correct, expect %d, get %d", fstValue-tx0value, spent)
   108  	}
   109  	err = bs.TryRemoveValue(tx1.GetValue())
   110  	if err != nil {
   111  		t.Fatalf("TryRemoveValue tx1 error: %v", err)
   112  	}
   113  	spent = bs.Spent().GetInt64()
   114  	if spent != (fstValue - tx0value - tx1value) {
   115  		t.Fatalf("the value of spent is not correct, expect %d, get %d", fstValue-tx0value-tx1value, spent)
   116  	}
   117  	err = bs.TryRemoveValue(tx2.GetValue())
   118  	if err != nil {
   119  		t.Fatalf("TryRemoveValue tx2 error: %v", err)
   120  	}
   121  	spent = bs.Spent().GetInt64()
   122  	if spent != (fstValue - tx0value - tx1value - tx2value) {
   123  		t.Fatalf("the value of spent is not correct, expect %d, get %d", fstValue-tx0value-tx1value-tx2value, spent)
   124  	}
   125  
   126  }
   127  
   128  func TestAccountFlow(t *testing.T) {
   129  	t.Parallel()
   130  
   131  	var err error
   132  
   133  	//balancevalue := int64(100000)
   134  	originBalance := state.NewBalanceSet()
   135  	af := core.NewAccountFlow(originBalance)
   136  
   137  	tx0value := int64(1000)
   138  	tx1value := int64(2000)
   139  	tx2value := int64(3000)
   140  	tx0 := newTestAccountFlowTx(0, math.NewBigInt(tx0value))
   141  	tx1 := newTestAccountFlowTx(1, math.NewBigInt(tx1value))
   142  	tx2 := newTestAccountFlowTx(2, math.NewBigInt(tx2value))
   143  
   144  	// test add, get
   145  	err = af.Add(tx0)
   146  	if err != nil {
   147  		t.Fatalf("can't add tx0 into account flow, err: %v", err)
   148  	}
   149  	err = af.Add(tx1)
   150  	if err != nil {
   151  		t.Fatalf("can't add tx1 into account flow, err: %v", err)
   152  	}
   153  	err = af.Add(tx2)
   154  	if err != nil {
   155  		t.Fatalf("can't add tx2 into account flow, err: %v", err)
   156  	}
   157  	tx0inAccountFlow := af.GetTx(tx0.GetNonce())
   158  	if tx0inAccountFlow == nil {
   159  		t.Fatalf("can't get tx0 from account flow after add")
   160  	}
   161  	tx1inAccountFlow := af.GetTx(tx1.GetNonce())
   162  	if tx1inAccountFlow == nil {
   163  		t.Fatalf("can't get tx1 from account flow after add")
   164  	}
   165  	tx2inAccountFlow := af.GetTx(tx2.GetNonce())
   166  	if tx2inAccountFlow == nil {
   167  		t.Fatalf("can't get tx2 from account flow after add")
   168  	}
   169  
   170  	// test latest nonce
   171  	latestnonce, lnerr := af.LatestNonce()
   172  	if lnerr != nil {
   173  		t.Fatalf("get latest nonce failed, err: %v", lnerr)
   174  	}
   175  	if latestnonce != uint64(2) {
   176  		t.Fatalf("latest nonce not correct, expect %d, get %d", 2, latestnonce)
   177  	}
   178  
   179  	// test remove
   180  	spent := af.BalanceState(0).Spent()
   181  
   182  	err = af.Remove(tx0.GetNonce())
   183  	if err != nil {
   184  		t.Fatalf("confirm tx0 err: %v", err)
   185  	}
   186  	spent = af.BalanceState(0).Spent()
   187  	if spent.Value.Cmp(math.NewBigInt(tx1value+tx2value).Value) != 0 {
   188  		t.Fatalf("spent not correct after confirm tx0, expect: %d, get %d", tx1value+tx2value, spent.GetInt64())
   189  	}
   190  
   191  	err = af.Remove(tx1.GetNonce())
   192  	if err != nil {
   193  		t.Fatalf("confirm tx1 err: %v", err)
   194  	}
   195  	spent = af.BalanceState(0).Spent()
   196  	if spent.Value.Cmp(math.NewBigInt(tx2value).Value) != 0 {
   197  		t.Fatalf("spent not correct after confirm tx1, expect: %d, get %d", tx2value, spent.GetInt64())
   198  	}
   199  
   200  	err = af.Remove(tx2.GetNonce())
   201  	if err != nil {
   202  		t.Fatalf("confirm tx2 err: %v", err)
   203  	}
   204  	spent = af.BalanceState(0).Spent()
   205  	if spent.Value.Cmp(math.NewBigInt(0).Value) != 0 {
   206  		t.Fatalf("spent not correct after confirm tx2, expect: %d, get %d", 0, spent.GetInt64())
   207  	}
   208  
   209  }