github.com/grupokindynos/coins-explorer@v0.0.0-20210507172551-fa8983d19250/tests/dbtestdata/fakechain.go (about) 1 package dbtestdata 2 3 import ( 4 "context" 5 "encoding/json" 6 "errors" 7 "github.com/grupokindynos/coins-explorer/bchain" 8 "math/big" 9 ) 10 11 type fakeBlockChain struct { 12 *bchain.BaseChain 13 } 14 15 // NewFakeBlockChain returns mocked blockchain RPC interface used for tests 16 func NewFakeBlockChain(parser bchain.BlockChainParser) (bchain.BlockChain, error) { 17 return &fakeBlockChain{&bchain.BaseChain{Parser: parser}}, nil 18 } 19 20 func (c *fakeBlockChain) CreateMempool(chain bchain.BlockChain) (bchain.Mempool, error) { 21 return bchain.NewMempoolBitcoinType(chain, 1, 1), nil 22 } 23 24 func (c *fakeBlockChain) Initialize() error { 25 return nil 26 } 27 28 func (c *fakeBlockChain) InitializeMempool(addrDescForOutpoint bchain.AddrDescForOutpointFunc, onNewTxAddr bchain.OnNewTxAddrFunc) error { 29 return nil 30 } 31 32 func (c *fakeBlockChain) Shutdown(ctx context.Context) error { 33 return nil 34 } 35 36 func (c *fakeBlockChain) IsTestnet() bool { 37 return true 38 } 39 40 func (c *fakeBlockChain) GetNetworkName() string { 41 return "fakecoin" 42 } 43 44 func (c *fakeBlockChain) GetCoinName() string { 45 return "Fakecoin" 46 } 47 48 func (c *fakeBlockChain) GetSubversion() string { 49 return "/Fakecoin:0.0.1/" 50 } 51 52 func (c *fakeBlockChain) GetChainInfo() (v *bchain.ChainInfo, err error) { 53 return &bchain.ChainInfo{ 54 Chain: c.GetNetworkName(), 55 Blocks: 2, 56 Headers: 2, 57 Bestblockhash: GetTestBitcoinTypeBlock2(c.Parser).BlockHeader.Hash, 58 Version: "001001", 59 Subversion: c.GetSubversion(), 60 }, nil 61 } 62 63 func (c *fakeBlockChain) GetBestBlockHash() (v string, err error) { 64 return GetTestBitcoinTypeBlock2(c.Parser).BlockHeader.Hash, nil 65 } 66 67 func (c *fakeBlockChain) GetBestBlockHeight() (v uint32, err error) { 68 return GetTestBitcoinTypeBlock2(c.Parser).BlockHeader.Height, nil 69 } 70 71 func (c *fakeBlockChain) GetBlockHash(height uint32) (v string, err error) { 72 b1 := GetTestBitcoinTypeBlock1(c.Parser) 73 if height == b1.BlockHeader.Height { 74 return b1.BlockHeader.Hash, nil 75 } 76 b2 := GetTestBitcoinTypeBlock2(c.Parser) 77 if height == b2.BlockHeader.Height { 78 return b2.BlockHeader.Hash, nil 79 } 80 return "", bchain.ErrBlockNotFound 81 } 82 83 func (c *fakeBlockChain) GetBlockHeader(hash string) (v *bchain.BlockHeader, err error) { 84 b1 := GetTestBitcoinTypeBlock1(c.Parser) 85 if hash == b1.BlockHeader.Hash { 86 return &b1.BlockHeader, nil 87 } 88 b2 := GetTestBitcoinTypeBlock2(c.Parser) 89 if hash == b2.BlockHeader.Hash { 90 return &b2.BlockHeader, nil 91 } 92 return nil, bchain.ErrBlockNotFound 93 } 94 95 func (c *fakeBlockChain) GetBlock(hash string, height uint32) (v *bchain.Block, err error) { 96 b1 := GetTestBitcoinTypeBlock1(c.Parser) 97 if hash == b1.BlockHeader.Hash || height == b1.BlockHeader.Height { 98 return b1, nil 99 } 100 b2 := GetTestBitcoinTypeBlock2(c.Parser) 101 if hash == b2.BlockHeader.Hash || height == b2.BlockHeader.Height { 102 return b2, nil 103 } 104 return nil, bchain.ErrBlockNotFound 105 } 106 107 func getBlockInfo(b *bchain.Block) *bchain.BlockInfo { 108 bi := &bchain.BlockInfo{ 109 BlockHeader: b.BlockHeader, 110 } 111 for _, tx := range b.Txs { 112 bi.Txids = append(bi.Txids, tx.Txid) 113 } 114 return bi 115 } 116 117 func (c *fakeBlockChain) GetBlockInfo(hash string) (v *bchain.BlockInfo, err error) { 118 b1 := GetTestBitcoinTypeBlock1(c.Parser) 119 if hash == b1.BlockHeader.Hash { 120 return getBlockInfo(b1), nil 121 } 122 b2 := GetTestBitcoinTypeBlock2(c.Parser) 123 if hash == b2.BlockHeader.Hash { 124 return getBlockInfo(b2), nil 125 } 126 return nil, bchain.ErrBlockNotFound 127 } 128 129 func getTxInBlock(b *bchain.Block, txid string) *bchain.Tx { 130 for _, tx := range b.Txs { 131 if tx.Txid == txid { 132 return &tx 133 } 134 } 135 return nil 136 } 137 138 func (c *fakeBlockChain) GetTransaction(txid string) (v *bchain.Tx, err error) { 139 v = getTxInBlock(GetTestBitcoinTypeBlock1(c.Parser), txid) 140 if v == nil { 141 v = getTxInBlock(GetTestBitcoinTypeBlock2(c.Parser), txid) 142 } 143 if v != nil { 144 return v, nil 145 } 146 return nil, bchain.ErrTxNotFound 147 } 148 149 func (c *fakeBlockChain) GetTransactionSpecific(tx *bchain.Tx) (v json.RawMessage, err error) { 150 // txSpecific extends Tx with an additional Size and Vsize info 151 type txSpecific struct { 152 *bchain.Tx 153 Vsize int `json:"vsize,omitempty"` 154 Size int `json:"size,omitempty"` 155 } 156 157 tx, err = c.GetTransaction(tx.Txid) 158 if err != nil { 159 return nil, err 160 } 161 txS := txSpecific{Tx: tx} 162 163 if tx.Txid == "7c3be24063f268aaa1ed81b64776798f56088757641a34fb156c4f51ed2e9d25" { 164 txS.Vsize = 206 165 txS.Size = 376 166 } else if tx.Txid == "fdd824a780cbb718eeb766eb05d83fdefc793a27082cd5e67f856d69798cf7db" { 167 txS.Size = 300 168 } else if tx.Txid == "05e2e48aeabdd9b75def7b48d756ba304713c2aba7b522bf9dbc893fc4231b07" { 169 txS.Hex = "010000000001012720b597ef06045c935960342b0bbc45aab5fd5642017282f5110216caaa2364010000002322002069dae530beb09a05d46d0b2aee98645b15bb5d1e808a386b5ef0c48aed5531cbffffffff021ec403000000000017a914203c9dbd3ffbd1a790fc1609fb430efa5cbe516d87061523000000000017a91465dfc5c16e80b86b589df3f85dacd43f5c5b4a8f8704004730440220783e9349fc48f22aa0064acf32bc255eafa761eb9fa8f90a504986713c52dc3702206fc6a1a42f74ea0b416b35671770c0d26fc453668e6107edc271f11e629cda1001483045022100b82ef510c7eec61f39bee3e73a19df451fb8cca842b66bc94696d6a095dd8e96022071767bf8e4859de06cd5caf75e833e284328570ea1caa88bc93478a8d0fa9ac90147522103958c08660082c9ce90399ded0da7c3b39ed20a7767160f12428191e005aa42572102b1e6d8187f54d83d1ffd70508e24c5bd3603bccb2346d8c6677434169de8bc2652ae00000000" 170 } else if tx.Txid == "3d90d15ed026dc45e19ffb52875ed18fa9e8012ad123d7f7212176e2b0ebdb71" { 171 txS.Vsize = 400 172 } 173 174 rm, err := json.Marshal(txS) 175 if err != nil { 176 return nil, err 177 } 178 return json.RawMessage(rm), nil 179 } 180 181 func (c *fakeBlockChain) GetTransactionForMempool(txid string) (v *bchain.Tx, err error) { 182 return nil, errors.New("Not implemented") 183 } 184 185 func (c *fakeBlockChain) EstimateSmartFee(blocks int, conservative bool) (v big.Int, err error) { 186 if conservative == false { 187 v.SetInt64(int64(blocks)*100 - 1) 188 } else { 189 v.SetInt64(int64(blocks) * 100) 190 } 191 return 192 } 193 194 func (c *fakeBlockChain) EstimateFee(blocks int) (v big.Int, err error) { 195 v.SetInt64(int64(blocks) * 200) 196 return 197 } 198 199 func (c *fakeBlockChain) SendRawTransaction(tx string) (v string, err error) { 200 if tx == "123456" { 201 return "9876", nil 202 } 203 return "", errors.New("Invalid data") 204 } 205 206 // GetChainParser returns parser for the blockchain 207 func (c *fakeBlockChain) GetChainParser() bchain.BlockChainParser { 208 return c.Parser 209 } 210 211 // GetMempoolTransactions returns transactions in mempool 212 func (c *fakeBlockChain) GetMempoolTransactions() ([]string, error) { 213 return nil, errors.New("Not implemented") 214 }