github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/consensus/dpos/dpos_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:33</date>
    10  //</624342611271880704>
    11  
    12  package dpos
    13  
    14  import (
    15  	"testing"
    16  
    17  	"encoding/binary"
    18  
    19  	"github.com/ethereum/go-ethereum/common"
    20  	"github.com/ethereum/go-ethereum/core/types"
    21  	"github.com/ethereum/go-ethereum/ethdb"
    22  	"github.com/ethereum/go-ethereum/trie"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  var (
    27  	MockEpoch = []string{
    28  		"0x44d1ce0b7cb3588bca96151fe1bc05af38f91b6e",
    29  		"0xa60a3886b552ff9992cfcd208ec1152079e046c2",
    30  		"0x4e080e49f62694554871e669aeb4ebe17c4a9670",
    31  		"0xb040353ec0f2c113d5639444f7253681aecda1f8",
    32  		"0x14432e15f21237013017fa6ee90fc99433dec82c",
    33  		"0x9f30d0e5c9c88cade54cd1adecf6bc2c7e0e5af6",
    34  		"0xd83b44a3719720ec54cdb9f54c0202de68f1ebcb",
    35  		"0x56cc452e450551b7b9cffe25084a069e8c1e9441",
    36  		"0xbcfcb3fa8250be4f2bf2b1e70e1da500c668377b",
    37  		"0x9d9667c71bb09d6ca7c3ed12bfe5e7be24e2ffe1",
    38  		"0xabde197e97398864ba74511f02832726edad5967",
    39  		"0x6f99d97a394fa7a623fdf84fdc7446b99c3cb335",
    40  		"0xf78b011e639ce6d8b76f97712118f3fe4a12dd95",
    41  		"0x8db3b6c801dddd624d6ddc2088aa64b5a2493661",
    42  		"0x751b484bd5296f8d267a8537d33f25a848f7f7af",
    43  		"0x646ba1fa42eb940aac67103a71e9a908ef484ec3",
    44  		"0x34d4a8d9f6b53a8f5e674516cb8ad66c843b2801",
    45  		"0x5b76fff970bf8a351c1c9ebfb5e5a9493e956ddd",
    46  		"0x8da3c5aedaf106c61cfee6d8483e1f255fdd60c0",
    47  		"0x2cdbe87a1bd7ee60dd6fe97f7b2d1efbacd5d95d",
    48  		"0x743415d0e979dc6e426bc8189e40beb65bf5ac1d",
    49  	}
    50  )
    51  
    52  func mockNewDposContext(db ethdb.Database) *types.DposContext {
    53  	trieDB := trie.NewDatabase(db)
    54  	dposContext, err := types.NewDposContextFromProto(trieDB, &types.DposContextProto{})
    55  	if err != nil {
    56  		return nil
    57  	}
    58  	delegator := []byte{}
    59  	candidate := []byte{}
    60  	addresses := []common.Address{}
    61  	for i := 0; i < maxValidatorSize; i++ {
    62  		addresses = append(addresses, common.HexToAddress(MockEpoch[i]))
    63  	}
    64  	dposContext.SetValidators(addresses)
    65  	for j := 0; j < len(MockEpoch); j++ {
    66  		delegator = common.HexToAddress(MockEpoch[j]).Bytes()
    67  		candidate = common.HexToAddress(MockEpoch[j]).Bytes()
    68  		dposContext.DelegateTrie().TryUpdate(append(candidate, delegator...), candidate)
    69  		dposContext.CandidateTrie().TryUpdate(candidate, candidate)
    70  		dposContext.VoteTrie().TryUpdate(candidate, candidate)
    71  	}
    72  	return dposContext
    73  }
    74  
    75  func setMintCntTrie(epochID int64, candidate common.Address, mintCntTrie *trie.Trie, count int64) {
    76  	key := make([]byte, 8)
    77  	binary.BigEndian.PutUint64(key, uint64(epochID))
    78  	cntBytes := make([]byte, 8)
    79  	binary.BigEndian.PutUint64(cntBytes, uint64(count))
    80  	mintCntTrie.TryUpdate(append(key, candidate.Bytes()...), cntBytes)
    81  }
    82  
    83  func getMintCnt(epochID int64, candidate common.Address, mintCntTrie *trie.Trie) int64 {
    84  	key := make([]byte, 8)
    85  	binary.BigEndian.PutUint64(key, uint64(epochID))
    86  	cntBytes := mintCntTrie.Get(append(key, candidate.Bytes()...))
    87  	if cntBytes == nil {
    88  		return 0
    89  	} else {
    90  		return int64(binary.BigEndian.Uint64(cntBytes))
    91  	}
    92  }
    93  
    94  func TestUpdateMintCnt(t *testing.T) {
    95  	db := ethdb.NewMemDatabase()
    96  	dposContext := mockNewDposContext(db)
    97  
    98  //新块仍与当前块处于同一时代,但新矿工是该时代首次造币。
    99  	lastTime := int64(epochInterval)
   100  
   101  	miner := common.HexToAddress("0xa60a3886b552ff9992cfcd208ec1152079e046c2")
   102  	blockTime := int64(epochInterval + blockInterval)
   103  
   104  	beforeUpdateCnt := getMintCnt(blockTime/epochInterval, miner, dposContext.MintCntTrie())
   105  	updateMintCnt(lastTime, blockTime, miner, dposContext)
   106  	afterUpdateCnt := getMintCnt(blockTime/epochInterval, miner, dposContext.MintCntTrie())
   107  	assert.Equal(t, int64(0), beforeUpdateCnt)
   108  	assert.Equal(t, int64(1), afterUpdateCnt)
   109  
   110  //新块体仍与当前块体处于同一时代,新矿工在该时代之前也有铸币块体。
   111  	setMintCntTrie(blockTime/epochInterval, miner, dposContext.MintCntTrie(), int64(1))
   112  
   113  	blockTime = epochInterval + blockInterval*4
   114  
   115  //
   116  	beforeUpdateCnt = getMintCnt(blockTime/epochInterval, miner, dposContext.MintCntTrie())
   117  	updateMintCnt(lastTime, blockTime, miner, dposContext)
   118  	afterUpdateCnt = getMintCnt(blockTime/epochInterval, miner, dposContext.MintCntTrie())
   119  	assert.Equal(t, int64(1), beforeUpdateCnt)
   120  	assert.Equal(t, int64(2), afterUpdateCnt)
   121  
   122  //新街区进入新时代
   123  	blockTime = epochInterval * 2
   124  
   125  	beforeUpdateCnt = getMintCnt(blockTime/epochInterval, miner, dposContext.MintCntTrie())
   126  	updateMintCnt(lastTime, blockTime, miner, dposContext)
   127  	afterUpdateCnt = getMintCnt(blockTime/epochInterval, miner, dposContext.MintCntTrie())
   128  	assert.Equal(t, int64(0), beforeUpdateCnt)
   129  	assert.Equal(t, int64(1), afterUpdateCnt)
   130  }
   131