github.com/igggame/nebulas-go@v2.1.0+incompatible/nip/dip/dip_test.go (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // the go-nebulas library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 19 package dip 20 21 import ( 22 "encoding/json" 23 "testing" 24 "time" 25 26 "github.com/nebulasio/go-nebulas/storage" 27 28 "github.com/nebulasio/go-nebulas/account" 29 "github.com/nebulasio/go-nebulas/core" 30 "github.com/nebulasio/go-nebulas/crypto/keystore" 31 "github.com/nebulasio/go-nebulas/crypto/keystore/secp256k1" 32 "github.com/nebulasio/go-nebulas/neblet/pb" 33 "github.com/nebulasio/go-nebulas/util" 34 "github.com/stretchr/testify/assert" 35 ) 36 37 var ( 38 dipAddr, _ = core.AddressParse("n1HWE22w4DaE1gz6E9D9TGokrxfHBegoyB8") 39 ) 40 41 func mockAddress() *core.Address { 42 ks := keystore.DefaultKS 43 priv1 := secp256k1.GeneratePrivateKey() 44 pubdata1, _ := priv1.PublicKey().Encoded() 45 addr, _ := core.NewAddressFromPublicKey(pubdata1) 46 ks.SetKey(addr.String(), priv1, []byte("passphrase")) 47 ks.Unlock(addr.String(), []byte("passphrase"), time.Second*60*60*24*365) 48 return addr 49 } 50 51 type mockNeb struct { 52 config *nebletpb.Config 53 chain *core.BlockChain 54 am core.AccountManager 55 } 56 57 func (n *mockNeb) Config() *nebletpb.Config { 58 return n.config 59 } 60 61 func (n *mockNeb) AccountManager() core.AccountManager { 62 return n.am 63 } 64 65 func (n *mockNeb) BlockChain() *core.BlockChain { 66 return n.chain 67 } 68 69 func testNeb(t *testing.T) *mockNeb { 70 71 neb := &mockNeb{ 72 config: &nebletpb.Config{Chain: &nebletpb.ChainConfig{ChainId: 1}, 73 Nbre: &nebletpb.NbreConfig{}, 74 }, 75 } 76 77 account, _ := account.NewManager(neb) 78 //assert.Nil(t, err) 79 neb.am = account 80 return neb 81 } 82 83 func TestDip_CheckReward(t *testing.T) { 84 neb := testNeb(t) 85 dip, err := NewDIP(neb) 86 assert.Nil(t, err) 87 88 tests := []struct { 89 key string 90 txType string 91 from *core.Address 92 to *core.Address 93 value string 94 err error 95 }{ 96 { 97 key: "binary tx", 98 txType: core.TxPayloadBinaryType, 99 from: mockAddress(), 100 to: nil, 101 value: "0", 102 err: nil, 103 }, 104 { 105 key: "binary tx with dip from", 106 txType: core.TxPayloadBinaryType, 107 from: dip.RewardAddress(), 108 to: nil, 109 value: "0", 110 err: ErrUnsupportedTransactionFromDipAddress, 111 }, 112 { 113 key: "deploy tx", 114 txType: core.TxPayloadDeployType, 115 from: mockAddress(), 116 to: nil, 117 value: "0", 118 err: nil, 119 }, 120 { 121 key: "deploy tx with dip from", 122 txType: core.TxPayloadDeployType, 123 from: dip.RewardAddress(), 124 to: nil, 125 value: "0", 126 err: ErrUnsupportedTransactionFromDipAddress, 127 }, 128 { 129 key: "call tx", 130 txType: core.TxPayloadCallType, 131 from: mockAddress(), 132 to: nil, 133 value: "0", 134 err: nil, 135 }, 136 { 137 key: "call tx with dip from", 138 txType: core.TxPayloadCallType, 139 from: dip.RewardAddress(), 140 to: nil, 141 value: "0", 142 err: ErrUnsupportedTransactionFromDipAddress, 143 }, 144 { 145 key: "protocol tx", 146 txType: core.TxPayloadProtocolType, 147 from: mockAddress(), 148 to: nil, 149 value: "0", 150 err: nil, 151 }, 152 { 153 key: "protocol tx with dip from", 154 txType: core.TxPayloadProtocolType, 155 from: dip.RewardAddress(), 156 to: nil, 157 value: "0", 158 err: ErrUnsupportedTransactionFromDipAddress, 159 }, 160 { 161 key: "dip tx not form dip addr", 162 txType: core.TxPayloadDipType, 163 from: mockAddress(), 164 to: nil, 165 value: "0", 166 err: ErrInvalidDipAddress, 167 }, 168 { 169 key: "dip tx addr not found in list", 170 txType: core.TxPayloadDipType, 171 from: dip.RewardAddress(), 172 to: nil, 173 value: "1", 174 err: ErrDipNotFound, 175 }, 176 { 177 key: "dip tx value not found", 178 txType: core.TxPayloadDipType, 179 from: dip.RewardAddress(), 180 to: dipAddr, 181 value: "0", 182 err: ErrDipNotFound, 183 }, 184 { 185 key: "dip tx normal", 186 txType: core.TxPayloadDipType, 187 from: dip.RewardAddress(), 188 to: dipAddr, 189 value: "1", 190 err: nil, 191 }, 192 } 193 194 //data, err := dip.GetDipList(1) 195 //assert.Nil(t, err) 196 //dipData := data.(*DIPData) 197 //println("dip addr:", dipData.Dips[0].Address) 198 //println("dip value:", dipData.Dips[0].Reward) 199 200 for _, tt := range tests { 201 t.Run(tt.key, func(t *testing.T) { 202 to := tt.to 203 if to == nil { 204 to = mockAddress() 205 } 206 value, err := util.NewUint128FromString(tt.value) 207 assert.Nil(t, err) 208 var ( 209 payloadBytes []byte 210 ) 211 if tt.txType == core.TxPayloadDipType { 212 payload, err := core.NewDipPayload(1, 1, 1, "") 213 assert.Nil(t, err) 214 payloadBytes, err = payload.ToBytes() 215 assert.Nil(t, err) 216 } 217 tx, err := core.NewTransaction(11, tt.from, to, value, 1, tt.txType, payloadBytes, core.TransactionGasPrice, core.TransactionMaxGas) 218 assert.Nil(t, err) 219 err = dip.CheckReward(tx) 220 assert.Equal(t, tt.err, err) 221 }) 222 } 223 } 224 225 func readNbreDB(key string) ([]byte, error) { 226 rs, err := storage.NewRocksStorage("../../mainnet/nbre/nbre.db") 227 if err != nil { 228 return nil, err 229 } 230 data, err := rs.Get([]byte(key)) 231 if err != nil { 232 return nil, err 233 } 234 return data, nil 235 } 236 237 func TestDip_ReadDIP(t *testing.T) { 238 dbdata, err := readNbreDB("dip_rewards") 239 assert.Nil(t, err) 240 data := DipReward{} 241 err = json.Unmarshal(dbdata, &data) 242 assert.Nil(t, err) 243 items := make([]*DIPData, len(data.DipRewards)) 244 for i, str := range data.DipRewards { 245 item := DIPData{} 246 err := json.Unmarshal([]byte(str), &item) 247 assert.Nil(t, err) 248 items[i] = &item 249 } 250 assert.Nil(t, err) 251 //recordData, err := json.Marshal(items) 252 //assert.Nil(t, err) 253 //util.FileWrite("./dipdata/dip_data.json", recordData, true) 254 } 255 256 func TestNR_LoadCache(t *testing.T) { 257 neb := testNeb(t) 258 dip, err := NewDIP(neb) 259 assert.Nil(t, err) 260 dip.loadCache() 261 }