github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/radar/mainchain/league_consume_test.go (about) 1 package mainchain 2 3 import ( 4 "os" 5 "testing" 6 "time" 7 8 "fmt" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/sixexorg/magnetic-ring/bactor" 12 "github.com/sixexorg/magnetic-ring/common" 13 "github.com/sixexorg/magnetic-ring/core/mainchain/types" 14 orgtypes "github.com/sixexorg/magnetic-ring/core/orgchain/types" 15 "github.com/sixexorg/magnetic-ring/mock" 16 "github.com/sixexorg/magnetic-ring/store/mainchain/extstorages" 17 "github.com/sixexorg/magnetic-ring/store/storelaw" 18 ) 19 20 type MockTell struct { 21 } 22 23 func (MockTell) Tell(message interface{}) { 24 25 } 26 func mockTellInit() (bactor.Teller, error) { 27 return &MockTell{}, nil 28 } 29 func ValidateBlock(block *orgtypes.Block, ledgerStore storelaw.Ledger4Validation) (blkInfo *storelaw.OrgBlockInfo, err error) { 30 return &storelaw.OrgBlockInfo{ 31 Block: block, 32 }, nil 33 } 34 func TestEntireProcess(t *testing.T) { 35 dbdir := "test" 36 defer os.RemoveAll(dbdir) 37 stroer, err := extstorages.NewLedgerStore(dbdir) 38 if err != nil { 39 t.Fail() 40 t.Error(err) 41 return 42 } 43 adpter := &ConsumerAdapter{ 44 FuncGenesis: func(address common.Address) (*types.Transaction, uint64, error) { 45 return mock.CreateLeagueTx, 0, nil 46 }, 47 FuncBlk: func(height uint64) (*types.Header, error) { 48 return &types.Header{Timestamp: 1000}, nil 49 }, 50 FuncTx: mock.GetTransaction, 51 FuncValidate: ValidateBlock, 52 Ledger: stroer, 53 P2pTlrInit: mockTellInit, 54 P2pTlr: MockTell{}, 55 TxPoolTlrInit: mockTellInit, 56 TxPoolTlr: MockTell{}, 57 } 58 lgc := NewLeagueConsumers(adpter) 59 lgc.ReceiveBlock(mock.Blocks[0]) 60 lgc.ReceiveBlock(mock.Blocks[1]) 61 //lgc.Identify() 62 time.Sleep(2 * time.Second) 63 lgc.Settlement() 64 t.Log("------------part1--------------") 65 for _, v := range lgc.txs { 66 t.Logf("leagueId:%s\nstartheight:%d\nendheight:%d\nfee:%s\nblockroot:%s\n", 67 v.TxData.LeagueId.ToString(), 68 v.TxData.StartHeight, 69 v.TxData.EndHeight, 70 v.TxData.Energy.String(), 71 v.TxData.BlockRoot.String()) 72 } 73 leagueHMap := make(map[common.Address]uint64) 74 for k, _ := range lgc.consumers { 75 leagueHMap[k] = 2 76 break 77 } 78 mainTxs, err := lgc.generateMainTx(leagueHMap) 79 if err != nil { 80 t.Error(err) 81 t.Fail() 82 return 83 } 84 t.Log("------------part2--------------") 85 for _, v := range mainTxs { 86 t.Logf("leagueId:%s\nstartheight:%d\nendheight:%d\nfee:%s\nblockroot:%s\n", 87 v.TxData.LeagueId.ToString(), 88 v.TxData.StartHeight, 89 v.TxData.EndHeight, 90 v.TxData.Energy.String(), 91 v.TxData.BlockRoot.String()) 92 } 93 if lgc.txs.Len() > 0 { 94 lsp := NewLeagueStatePipe() 95 lgc.CheckLeaguesResult(lgc.txs, lsp) 96 leagueCount := len(lgc.consumers) 97 count := 0 98 for { 99 select { 100 case sign := <-lsp.StateSignal: 101 switch msg := sign.(type) { 102 case *LeagueNeed: 103 t.Log("LeagueNeed", msg) 104 case *LeagueExec: 105 t.Log("LeagueExec", msg) 106 case *LeagueErr: 107 t.Log("LeagueErr", msg) 108 } 109 case <-lsp.Successed: 110 count++ 111 if count == leagueCount { 112 t.Log("Check OK") 113 goto END 114 115 } 116 } 117 } 118 END: 119 lsp.Kill() 120 } 121 for k, _ := range leagueHMap { 122 t.Logf("leagueId:%s,lastHeight:%d\n", k.ToString(), lgc.consumers[k].getLastHeight()) 123 } 124 } 125 func TestIntercept(t *testing.T) { 126 h1 := [5]uint64{100, 50, 20, 6, 8} 127 h2 := [5]uint64{200, 300, 400, 500, 600} 128 h3 := [5]uint64{900, 20, 3000, 250, 76} 129 h4 := [5]uint64{88, 99, 33, 77, 66} 130 131 m1 := make(map[common.Address]uint64) 132 m1[mock.Address_1] = h1[0] 133 m1[mock.Address_2] = h2[0] 134 m1[mock.Address_3] = h3[0] 135 m1[mock.Address_4] = h4[0] 136 137 m2 := make(map[common.Address]uint64) 138 m2[mock.Address_1] = h1[1] 139 m2[mock.Address_2] = h2[1] 140 m2[mock.Address_3] = h3[1] 141 m2[mock.Address_4] = h4[1] 142 143 m3 := make(map[common.Address]uint64) 144 m3[mock.Address_1] = h1[2] 145 m3[mock.Address_2] = h2[2] 146 m3[mock.Address_3] = h3[2] 147 m3[mock.Address_4] = h4[2] 148 149 m4 := make(map[common.Address]uint64) 150 m4[mock.Address_1] = h1[3] 151 m4[mock.Address_2] = h2[3] 152 m4[mock.Address_3] = h3[3] 153 m4[mock.Address_4] = h4[3] 154 155 m5 := make(map[common.Address]uint64) 156 m5[mock.Address_1] = h1[4] 157 m5[mock.Address_2] = h2[4] 158 m5[mock.Address_3] = h3[4] 159 m5[mock.Address_4] = h4[4] 160 161 // map 会随机,当前排列可能出现2种中位数 162 //20, 200, 20, 33 163 //6,200,250,77 164 165 m := make(map[common.Address]uint64) 166 m[mock.Address_1] = 100 //20 = 20 //6 = 6 167 m[mock.Address_2] = 50 //200 = 50 //200 = 50 168 m[mock.Address_3] = 30 //20 =20 //250 = 30 169 m[mock.Address_4] = 200 //33 =33 //77 = 77 170 171 ls := &LeagueConsumers{} 172 result := ls.interceptHeight(m, m1, m2, m3, m4, m5) 173 real := make(map[common.Address]uint64) 174 if result[mock.Address_1] == 20 { 175 real[mock.Address_1] = min(m[mock.Address_1], 20) 176 real[mock.Address_2] = min(m[mock.Address_2], 200) 177 real[mock.Address_3] = min(m[mock.Address_3], 20) 178 real[mock.Address_4] = min(m[mock.Address_4], 33) 179 180 } else { 181 real[mock.Address_1] = min(m[mock.Address_1], 6) 182 real[mock.Address_2] = min(m[mock.Address_2], 200) 183 real[mock.Address_3] = min(m[mock.Address_3], 250) 184 real[mock.Address_4] = min(m[mock.Address_4], 77) 185 } 186 assert.Equal(t, result[mock.Address_1], real[mock.Address_1]) 187 assert.Equal(t, result[mock.Address_2], real[mock.Address_2]) 188 assert.Equal(t, result[mock.Address_3], real[mock.Address_3]) 189 assert.Equal(t, result[mock.Address_4], real[mock.Address_4]) 190 } 191 192 func min(a, b uint64) uint64 { 193 if a > b { 194 return b 195 } 196 return a 197 } 198 func TestInitRadar(t *testing.T) { 199 adpter := &ConsumerAdapter{ 200 P2pTlrInit: mockTellInit, 201 P2pTlr: MockTell{}, 202 TxPoolTlrInit: mockTellInit, 203 TxPoolTlr: MockTell{}, 204 } 205 adpter.initTeller() 206 fmt.Println(adpter.TlrReady) 207 adpter.initTeller() 208 fmt.Println(adpter.TlrReady) 209 }