github.com/aychain/blockbook@v0.1.1-0.20181121092459-6d1fc7e07c5b/bchain/types.go (about) 1 package bchain 2 3 import ( 4 "context" 5 "encoding/hex" 6 "encoding/json" 7 "errors" 8 "fmt" 9 "math/big" 10 ) 11 12 // errors with specific meaning returned by blockchain rpc 13 var ( 14 // ErrBlockNotFound is returned when block is not found 15 // either unknown hash or too high height 16 // can be returned from GetBlockHash, GetBlockHeader, GetBlock 17 ErrBlockNotFound = errors.New("Block not found") 18 // ErrAddressMissing is returned if address is not specified 19 // for example To address in ethereum can be missing in case of contract transaction 20 ErrAddressMissing = errors.New("Address missing") 21 // ErrTxidMissing is returned if txid is not specified 22 // for example coinbase transactions in Bitcoin 23 ErrTxidMissing = errors.New("Txid missing") 24 ) 25 26 type ScriptSig struct { 27 // Asm string `json:"asm"` 28 Hex string `json:"hex"` 29 } 30 31 type Vin struct { 32 Coinbase string `json:"coinbase"` 33 Txid string `json:"txid"` 34 Vout uint32 `json:"vout"` 35 ScriptSig ScriptSig `json:"scriptSig"` 36 Sequence uint32 `json:"sequence"` 37 Addresses []string `json:"addresses"` 38 } 39 40 type ScriptPubKey struct { 41 // Asm string `json:"asm"` 42 Hex string `json:"hex,omitempty"` 43 // Type string `json:"type"` 44 Addresses []string `json:"addresses"` 45 } 46 47 type Vout struct { 48 ValueSat big.Int 49 JsonValue json.Number `json:"value"` 50 N uint32 `json:"n"` 51 ScriptPubKey ScriptPubKey `json:"scriptPubKey"` 52 } 53 54 // Tx is blockchain transaction 55 // unnecessary fields are commented out to avoid overhead 56 type Tx struct { 57 Hex string `json:"hex"` 58 Txid string `json:"txid"` 59 Version int32 `json:"version"` 60 LockTime uint32 `json:"locktime"` 61 Vin []Vin `json:"vin"` 62 Vout []Vout `json:"vout"` 63 // BlockHash string `json:"blockhash,omitempty"` 64 Confirmations uint32 `json:"confirmations,omitempty"` 65 Time int64 `json:"time,omitempty"` 66 Blocktime int64 `json:"blocktime,omitempty"` 67 } 68 69 type Block struct { 70 BlockHeader 71 Txs []Tx `json:"tx"` 72 } 73 74 // BlockHeader contains limited data (as needed for indexing) from backend block header 75 type BlockHeader struct { 76 Hash string `json:"hash"` 77 Prev string `json:"previousblockhash"` 78 Next string `json:"nextblockhash"` 79 Height uint32 `json:"height"` 80 Confirmations int `json:"confirmations"` 81 Size int `json:"size"` 82 Time int64 `json:"time,omitempty"` 83 } 84 85 // BlockInfo contains extended block header data and a list of block txids 86 type BlockInfo struct { 87 BlockHeader 88 Version json.Number `json:"version"` 89 MerkleRoot string `json:"merkleroot"` 90 Nonce json.Number `json:"nonce"` 91 Bits string `json:"bits"` 92 Difficulty json.Number `json:"difficulty"` 93 Txids []string `json:"tx,omitempty"` 94 } 95 96 type MempoolEntry struct { 97 Size uint32 `json:"size"` 98 FeeSat big.Int 99 Fee json.Number `json:"fee"` 100 ModifiedFeeSat big.Int 101 ModifiedFee json.Number `json:"modifiedfee"` 102 Time uint64 `json:"time"` 103 Height uint32 `json:"height"` 104 DescendantCount uint32 `json:"descendantcount"` 105 DescendantSize uint32 `json:"descendantsize"` 106 DescendantFees uint32 `json:"descendantfees"` 107 AncestorCount uint32 `json:"ancestorcount"` 108 AncestorSize uint32 `json:"ancestorsize"` 109 AncestorFees uint32 `json:"ancestorfees"` 110 Depends []string `json:"depends"` 111 } 112 113 type ChainInfo struct { 114 Chain string `json:"chain"` 115 Blocks int `json:"blocks"` 116 Headers int `json:"headers"` 117 Bestblockhash string `json:"bestblockhash"` 118 Difficulty string `json:"difficulty"` 119 SizeOnDisk int64 `json:"size_on_disk"` 120 Version string `json:"version"` 121 Subversion string `json:"subversion"` 122 ProtocolVersion string `json:"protocolversion"` 123 Timeoffset float64 `json:"timeoffset"` 124 Warnings string `json:"warnings"` 125 } 126 127 type RPCError struct { 128 Code int `json:"code"` 129 Message string `json:"message"` 130 } 131 132 func (e *RPCError) Error() string { 133 return fmt.Sprintf("%d: %s", e.Code, e.Message) 134 } 135 136 // AddressDescriptor is an opaque type obtained by parser.GetAddrDesc* methods 137 type AddressDescriptor []byte 138 139 func (ad AddressDescriptor) String() string { 140 return "ad:" + hex.EncodeToString(ad) 141 } 142 143 // OnNewBlockFunc is used to send notification about a new block 144 type OnNewBlockFunc func(hash string, height uint32) 145 146 // OnNewTxAddrFunc is used to send notification about a new transaction/address 147 type OnNewTxAddrFunc func(txid string, desc AddressDescriptor, isOutput bool) 148 149 // BlockChain defines common interface to block chain daemon 150 type BlockChain interface { 151 // life-cycle methods 152 Initialize() error 153 Shutdown(ctx context.Context) error 154 // chain info 155 IsTestnet() bool 156 GetNetworkName() string 157 GetSubversion() string 158 GetCoinName() string 159 GetChainInfo() (*ChainInfo, error) 160 // requests 161 GetBestBlockHash() (string, error) 162 GetBestBlockHeight() (uint32, error) 163 GetBlockHash(height uint32) (string, error) 164 GetBlockHeader(hash string) (*BlockHeader, error) 165 GetBlock(hash string, height uint32) (*Block, error) 166 GetBlockInfo(hash string) (*BlockInfo, error) 167 GetMempool() ([]string, error) 168 GetTransaction(txid string) (*Tx, error) 169 GetTransactionForMempool(txid string) (*Tx, error) 170 GetTransactionSpecific(txid string) (json.RawMessage, error) 171 EstimateSmartFee(blocks int, conservative bool) (big.Int, error) 172 EstimateFee(blocks int) (big.Int, error) 173 SendRawTransaction(tx string) (string, error) 174 // mempool 175 ResyncMempool(onNewTxAddr OnNewTxAddrFunc) (int, error) 176 GetMempoolTransactions(address string) ([]string, error) 177 GetMempoolTransactionsForAddrDesc(addrDesc AddressDescriptor) ([]string, error) 178 GetMempoolEntry(txid string) (*MempoolEntry, error) 179 // parser 180 GetChainParser() BlockChainParser 181 } 182 183 // BlockChainParser defines common interface to parsing and conversions of block chain data 184 type BlockChainParser interface { 185 // chain configuration description 186 // UTXO chains need "inputs" column in db, that map transactions to transactions that spend them 187 // non UTXO chains have mapping of address to input and output transactions directly in "outputs" column in db 188 IsUTXOChain() bool 189 // KeepBlockAddresses returns number of blocks which are to be kept in blockaddresses column 190 // and used in case of fork 191 // if 0 the blockaddresses column is not used at all (usually non UTXO chains) 192 KeepBlockAddresses() int 193 // AmountToDecimalString converts amount in big.Int to string with decimal point in the correct place 194 AmountToDecimalString(a *big.Int) string 195 // AmountToBigInt converts amount in json.Number (string) to big.Int 196 // it uses string operations to avoid problems with rounding 197 AmountToBigInt(n json.Number) (big.Int, error) 198 // address descriptor conversions 199 GetAddrDescFromVout(output *Vout) (AddressDescriptor, error) 200 GetAddrDescFromAddress(address string) (AddressDescriptor, error) 201 GetAddressesFromAddrDesc(addrDesc AddressDescriptor) ([]string, bool, error) 202 GetScriptFromAddrDesc(addrDesc AddressDescriptor) ([]byte, error) 203 // transactions 204 PackedTxidLen() int 205 PackTxid(txid string) ([]byte, error) 206 UnpackTxid(buf []byte) (string, error) 207 ParseTx(b []byte) (*Tx, error) 208 ParseTxFromJson(json.RawMessage) (*Tx, error) 209 PackTx(tx *Tx, height uint32, blockTime int64) ([]byte, error) 210 UnpackTx(buf []byte) (*Tx, uint32, error) 211 // blocks 212 PackBlockHash(hash string) ([]byte, error) 213 UnpackBlockHash(buf []byte) (string, error) 214 ParseBlock(b []byte) (*Block, error) 215 }