github.com/annchain/OG@v0.0.9/arefactor_core/core/dag_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/common"
    20  	"github.com/annchain/OG/types/tx_types"
    21  
    22  	"encoding/hex"
    23  	"fmt"
    24  
    25  	"github.com/annchain/OG/common/crypto"
    26  	"github.com/annchain/OG/common/math"
    27  	"github.com/annchain/OG/core"
    28  	"github.com/annchain/OG/core/state"
    29  	"github.com/annchain/OG/og"
    30  )
    31  
    32  var (
    33  	testAddress01 = "0x0b5d53f433b7e4a4f853a01e987f977497dda261"
    34  	testAddress02 = "0x0b5d53f433b7e4a4f853a01e987f977497dda262"
    35  )
    36  
    37  func newTestDag(t *testing.T, dbDirPrefix string) (*core.Dag, *tx_types.Sequencer, func()) {
    38  	conf := core.DagConfig{}
    39  	db, remove := newTestLDB(dbDirPrefix)
    40  	stdbconf := state.DefaultStateDBConfig()
    41  	dag, errnew := core.NewDag(conf, stdbconf, db, nil)
    42  	if errnew != nil {
    43  		t.Fatalf("new dag failed with error: %v", errnew)
    44  	}
    45  
    46  	genesis, balance := core.DefaultGenesis("genesis.json")
    47  	err := dag.Init(genesis, balance)
    48  	if err != nil {
    49  		t.Fatalf("init dag failed with error: %v", err)
    50  	}
    51  	dag.Start()
    52  
    53  	return dag, genesis, func() {
    54  		dag.Stop()
    55  		remove()
    56  	}
    57  }
    58  
    59  func newTestDagTx(nonce uint64) *tx_types.Tx {
    60  	txCreator := &og.TxCreator{}
    61  	pk, _ := crypto.PrivateKeyFromString(testPkSecp0)
    62  	addr := newTestAddress(pk)
    63  
    64  	tx := txCreator.NewSignedTx(addr, addr, math.NewBigInt(0), nonce, pk, 0)
    65  	tx.SetHash(tx.CalcTxHash())
    66  
    67  	return tx.(*tx_types.Tx)
    68  }
    69  
    70  func TestDagInit(t *testing.T) {
    71  	t.Parallel()
    72  
    73  	dag, genesis, finish := newTestDag(t, "TestDagInit")
    74  	defer finish()
    75  
    76  	if dag.GetTx(genesis.GetTxHash()) == nil {
    77  		t.Fatalf("genesis is not stored in dag db")
    78  	}
    79  	ge := dag.Genesis()
    80  	if ge == nil {
    81  		t.Fatalf("genesis is not set in dag")
    82  	}
    83  	if !ge.Compare(genesis) {
    84  		t.Fatalf("genesis setted in dag is not the genesis we want")
    85  	}
    86  	ls := dag.LatestSequencer()
    87  	if ls == nil {
    88  		t.Fatalf("latest seq is not set in dag")
    89  	}
    90  	if !ls.Compare(genesis) {
    91  		t.Fatalf("latest seq in dag is not the genesis we want")
    92  	}
    93  }
    94  
    95  func TestDagLoadGenesis(t *testing.T) {
    96  	t.Parallel()
    97  
    98  	conf := core.DagConfig{}
    99  	db, remove := newTestLDB("TestDagLoadGenesis")
   100  	defer remove()
   101  	dag, errnew := core.NewDag(conf, state.DefaultStateDBConfig(), db, nil)
   102  	if errnew != nil {
   103  		t.Fatalf("can't new a dag: %v", errnew)
   104  	}
   105  
   106  	acc := core.NewAccessor(db)
   107  	genesis, _ := core.DefaultGenesis("genesis.json")
   108  	err := acc.WriteGenesis(genesis)
   109  	if err != nil {
   110  		t.Fatalf("can't write genesis into db: %v", err)
   111  	}
   112  	if ok, _ := dag.LoadLastState(); !ok {
   113  		t.Fatalf("can't load last state from db")
   114  	}
   115  
   116  	ge := dag.Genesis()
   117  	if ge == nil {
   118  		t.Fatalf("genesis is not set in dag")
   119  	}
   120  	if !ge.Compare(genesis) {
   121  		t.Fatalf("genesis setted in dag is not the genesis we want")
   122  	}
   123  	ls := dag.LatestSequencer()
   124  	if ls == nil {
   125  		t.Fatalf("latest seq is not set in dag")
   126  	}
   127  	if !ls.Compare(genesis) {
   128  		t.Fatalf("latest seq in dag is not the genesis we want")
   129  	}
   130  
   131  }
   132  
   133  func TestDagPush(t *testing.T) {
   134  	t.Parallel()
   135  
   136  	dag, genesis, finish := newTestDag(t, "TestDagPush")
   137  	defer finish()
   138  
   139  	var err error
   140  
   141  	tx1 := newTestDagTx(0)
   142  	tx1.ParentsHash = common.Hashes{genesis.GetTxHash()}
   143  	tx2 := newTestDagTx(1)
   144  	tx2.ParentsHash = common.Hashes{genesis.GetTxHash()}
   145  
   146  	bd := &core.batchDetail{txList: core.NewTxList(), cost: make(map[int32]*math.BigInt)}
   147  	bd.txList.Put(tx1)
   148  	bd.txList.Put(tx2)
   149  	//bd.Pos = math.NewBigInt(0)
   150  	bd.cost[0] = math.NewBigInt(0)
   151  
   152  	batch := map[common.Address]*core.batchDetail{}
   153  	batch[tx1.Sender()] = bd
   154  
   155  	seq := newTestSeq(1)
   156  	seq.ParentsHash = common.Hashes{
   157  		tx1.GetTxHash(),
   158  		tx2.GetTxHash(),
   159  	}
   160  
   161  	//hashes := &common.Hashes{tx1.GetTxHash(), tx2.GetTxHash()}
   162  
   163  	cb := &core.PushBatch{}
   164  	cb.Seq = seq
   165  	//cb.Batch = batch
   166  	//cb.TxHashes = hashes
   167  
   168  	err = dag.Push(cb)
   169  	if err != nil {
   170  		t.Fatalf("push confirm batch to dag failed: %v", err)
   171  	}
   172  	// check if txs stored into db
   173  	if dag.GetTx(tx1.GetTxHash()) == nil {
   174  		t.Fatalf("tx1 is not stored in dag")
   175  	}
   176  	if dag.GetTx(tx2.GetTxHash()) == nil {
   177  		t.Fatalf("tx2 is not stored in dag")
   178  	}
   179  	// check if seq stored into db
   180  	if dag.GetTx(seq.GetTxHash()) == nil {
   181  		t.Fatalf("seq is not stored in dag")
   182  	}
   183  	if dag.LatestSequencer().GetTxHash() != seq.GetTxHash() {
   184  		t.Fatalf("latest seq is not set")
   185  	}
   186  	// check txs' hashs
   187  	var hashsP *common.Hashes
   188  	hashsP, err = dag.Accessor().ReadIndexedTxHashs(seq.Height)
   189  	hashs := *hashsP
   190  	if err != nil {
   191  		t.Fatalf("read indexed tx hashs failed: %v", err)
   192  	}
   193  	if len(hashs) != 2 {
   194  		t.Fatalf("hashs length not match")
   195  	}
   196  	if !((hashs[0] == tx1.GetTxHash() && hashs[1] == tx2.GetTxHash()) ||
   197  		(hashs[1] == tx1.GetTxHash() && hashs[0] == tx2.GetTxHash())) {
   198  		t.Fatalf("indexed hashs are not the list of tx1 and tx2's hash")
   199  	}
   200  
   201  	txs := dag.GetTxisByNumber(seq.Height)
   202  	fmt.Println("txs", txs)
   203  
   204  	// TODO check addr balance
   205  }
   206  
   207  func TestDagProcess(t *testing.T) {
   208  	t.Parallel()
   209  
   210  	var ret []byte
   211  	var err error
   212  
   213  	dag, _, finish := newTestDag(t, "TestDagProcess")
   214  	stdb := dag.StateDatabase()
   215  	defer finish()
   216  
   217  	pk, _ := crypto.PrivateKeyFromString(testPkSecp0)
   218  	addr := newTestAddress(pk)
   219  
   220  	// evm contract bytecode, for source code detail please check:
   221  	// github.com/annchain/OG/vm/vm_test/contracts/setter.sol
   222  	contractCode := "6060604052341561000f57600080fd5b600a60008190555060006001819055506102078061002e6000396000f300606060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631c0f72e11461006b57806360fe47b114610094578063c605f76c146100b7578063e5aa3d5814610145575b34600181905550005b341561007657600080fd5b61007e61016e565b6040518082815260200191505060405180910390f35b341561009f57600080fd5b6100b56004808035906020019091905050610174565b005b34156100c257600080fd5b6100ca61017e565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561010a5780820151818401526020810190506100ef565b50505050905090810190601f1680156101375780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561015057600080fd5b6101586101c1565b6040518082815260200191505060405180910390f35b60015481565b8060008190555050565b6101866101c7565b6040805190810160405280600a81526020017f68656c6c6f576f726c6400000000000000000000000000000000000000000000815250905090565b60005481565b6020604051908101604052806000815250905600a165627a7a723058208e1bdbeee227900e60082cfcc0e44d400385e8811ae77ac6d7f3b72f630f04170029"
   223  
   224  	createTx := &tx_types.Tx{}
   225  	createTx.SetSender(addr)
   226  	createTx.Value = math.NewBigInt(0)
   227  	createTx.Data, err = hex.DecodeString(contractCode)
   228  	if err != nil {
   229  		t.Fatalf("decode hex string to bytes error: %v", err)
   230  	}
   231  	_, _, err = dag.ProcessTransaction(createTx, false)
   232  	if err != nil {
   233  		t.Fatalf("error during contract creation: %v", err)
   234  	}
   235  	contractAddr := crypto.CreateAddress(addr, uint64(0))
   236  
   237  	cObj := stdb.GetStateObject(contractAddr)
   238  	if cObj == nil {
   239  		t.Fatalf("contract object not initiated in statedb")
   240  	}
   241  	codeInDB := stdb.GetCode(contractAddr)
   242  	if codeInDB == nil {
   243  		t.Fatalf("code not saved in statedb")
   244  	}
   245  
   246  	// get i from setter contract
   247  	calldata := "e5aa3d58"
   248  	callTx := &tx_types.Tx{}
   249  	callTx.SetSender(addr)
   250  	callTx.Value = math.NewBigInt(0)
   251  	callTx.To = contractAddr
   252  	callTx.Data, _ = hex.DecodeString(calldata)
   253  	ret, _, err = dag.ProcessTransaction(callTx, false)
   254  	if err != nil {
   255  		t.Fatalf("error during contract calling: %v", err)
   256  	}
   257  	targetstr := "000000000000000000000000000000000000000000000000000000000000000a"
   258  	retstr := fmt.Sprintf("%x", ret)
   259  	if retstr != targetstr {
   260  		t.Fatalf("the [i] in contract is not 10, should be %s, get %s", targetstr, retstr)
   261  	}
   262  
   263  	// set i to be 100
   264  	setdata := "60fe47b10000000000000000000000000000000000000000000000000000000000000064"
   265  	setTx := &tx_types.Tx{}
   266  	setTx.SetSender(addr)
   267  	setTx.Value = math.NewBigInt(0)
   268  	setTx.To = contractAddr
   269  	setTx.Data, _ = hex.DecodeString(setdata)
   270  	ret, _, err = dag.ProcessTransaction(setTx, false)
   271  	if err != nil {
   272  		t.Fatalf("error during contract setting: %v", err)
   273  	}
   274  	// get i and check if it is changed
   275  	ret, _, err = dag.ProcessTransaction(callTx, false)
   276  	if err != nil {
   277  		t.Fatalf("error during contract calling: %v", err)
   278  	}
   279  	targetstr = "0000000000000000000000000000000000000000000000000000000000000064"
   280  	retstr = fmt.Sprintf("%x", ret)
   281  	if retstr != targetstr {
   282  		t.Fatalf("the [i] in contract is not 100, should be %s, get %s", targetstr, retstr)
   283  	}
   284  
   285  	// pay a 10 bill to contract
   286  	transferValue := int64(10)
   287  	payTx := &tx_types.Tx{}
   288  	setTx.SetSender(addr)
   289  	payTx.Value = math.NewBigInt(transferValue)
   290  	payTx.To = contractAddr
   291  	ret, _, err = dag.ProcessTransaction(payTx, false)
   292  	if err != nil {
   293  		t.Fatalf("error during contract setting: %v", err)
   294  	}
   295  	blc := stdb.GetBalance(contractAddr)
   296  	if blc.GetInt64() != transferValue {
   297  		t.Fatalf("the value is not tranferred to contract, should be: %d, get: %d", transferValue, blc.GetInt64())
   298  	}
   299  }
   300  
   301  // Check if the root of pre push and actual push is the same.
   302  func TestDag_PrePush(t *testing.T) {
   303  	//dag, _, finish := newTestMemDag(t)
   304  	//defer finish()
   305  	//
   306  	//addr1 := types.HexToAddress(testAddress01)
   307  	//addr2 := types.HexToAddress(testAddress02)
   308  	//
   309  	//dag.StateDatabase().AddBalance(addr1, math.NewBigInt(10000000))
   310  	//root, err := dag.StateDatabase().Commit()
   311  	//if err != nil {
   312  	//	t.Errorf("statedb commit error: %v", err)
   313  	//}
   314  	//fmt.Println("root 1: ", root)
   315  	//
   316  	//tx1 := types.Tx{}
   317  	//tx1.From = &addr1
   318  	//tx1.To = addr2
   319  	//tx1.AccountNonce = 1
   320  	//tx1.Value = math.NewBigInt(10)
   321  	//tx1.TokenId = token.OGTokenID
   322  	//tx1.Weight = 100
   323  	//tx1.Hash = types.HexToHash("0x010101")
   324  	//
   325  	//seq := types.Sequencer{}
   326  	//seq.AccountNonce = 2
   327  	//seq.ParentsHash = types.Hashes{tx1.Hash}
   328  	//
   329  	//batch := &core.PushBatch{}
   330  	//batch.Seq = &seq
   331  	//batch.Txs = types.Txis{&tx1}
   332  	//
   333  	//if err = dag.Push(batch); err != nil {
   334  	//	t.Errorf("dag push error: %v", err)
   335  	//}
   336  
   337  }