tx

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorInvalidHeight = func(height string) string { return fmt.Sprintf("invalid height %s", height) }
	ErrorTxsNotFound   = func(height string) string { return fmt.Sprintf("txs at height %s not found... yet.", height) }
	ErrorInvalidHash   = func(hash string) string { return fmt.Sprintf("invalid hash %s", hash) }
	ErrorTxNotFound    = func(hash string) string { return fmt.Sprintf("tx (%s) not found... yet or forever.", hash) }
)
View Source
var IndexTx = indexer.CreateIndexer(func(batch tmdb.Batch, block *tm.Block, blockID *tm.BlockID, evc *mantlemint.EventCollector) error {

	txDecoder := cdc.TxConfig.TxDecoder()
	jsonEncoder := cdc.TxConfig.TxJSONEncoder()

	txHashes := make([]string, len(block.Txs))
	txRecords := make([]TxRecord, len(block.Txs))
	byHeightPayload := make([]TxByHeightRecord, len(block.Txs))

	for txIndex, txByte := range block.Txs {
		txRecord := TxRecord{}

		hash := txByte.Hash()
		tx, decodeErr := txDecoder(txByte)

		if decodeErr != nil {
			return decodeErr
		}

		txJSON, _ := jsonEncoder(tx)

		response := ToResponseDeliverTxJSON(evc.ResponseDeliverTxs[txIndex])
		responseJSON, responseMarshalErr := tmjson.Marshal(response)

		if responseMarshalErr != nil {
			return responseMarshalErr
		}

		txRecord.Tx = txJSON
		txRecord.TxResponse = responseJSON

		txHashes[txIndex] = fmt.Sprintf("%X", hash)
		txRecords[txIndex] = txRecord

		byHeightPayload[txIndex].Code = response.Code
		byHeightPayload[txIndex].Codespace = response.Codespace
		byHeightPayload[txIndex].GasUsed = response.GasUsed
		byHeightPayload[txIndex].GasWanted = response.GasWanted
		byHeightPayload[txIndex].Height = block.Height
		byHeightPayload[txIndex].RawLog = response.Log
		byHeightPayload[txIndex].Logs = func() json.RawMessage {
			if response.Code == 0 {
				return []byte(response.Log)
			} else {
				out, _ := json.Marshal([]string{})
				return out
			}
		}()
		byHeightPayload[txIndex].TxHash = fmt.Sprintf("%X", hash)
		byHeightPayload[txIndex].Timestamp = block.Time
		byHeightPayload[txIndex].Tx = txJSON
	}

	for txIndex, txRecord := range txRecords {
		txRecordJSON, marshalErr := tmjson.Marshal(txRecord)
		if marshalErr != nil {
			return marshalErr
		}

		batchSetErr := batch.Set(getKey(txHashes[txIndex]), txRecordJSON)
		if batchSetErr != nil {
			return batchSetErr
		}
	}

	byHeightJSON, byHeightErr := tmjson.Marshal(byHeightPayload)
	if byHeightErr != nil {
		return byHeightErr
	}

	batchSetErr := batch.Set(getByHeightKey(uint64(block.Height)), byHeightJSON)
	if batchSetErr != nil {
		return batchSetErr
	}

	return nil
})
View Source
var RegisterRESTRoute = indexer.CreateRESTRoute(func(router *mux.Router, indexerDB tmdb.DB) {
	router.HandleFunc("/index/tx/by_hash/{hash}", func(writer http.ResponseWriter, request *http.Request) {
		vars := mux.Vars(request)
		hash, ok := vars["hash"]
		if !ok {
			http.Error(writer, ErrorInvalidHash(hash), 400)
			return
		}

		if txn, err := txByHashHandler(indexerDB, hash); err != nil {
			http.Error(writer, indexer.ErrorInternal(err), 500)
			return
		} else if txn == nil {
			http.Error(writer, ErrorTxNotFound(hash), 400)
			return
		} else {
			writer.WriteHeader(200)
			writer.Write(txn)
			return
		}
	}).Methods("GET")

	router.HandleFunc("/index/tx/by_height/{height}", func(writer http.ResponseWriter, request *http.Request) {
		vars := mux.Vars(request)
		height, ok := vars["height"]
		if !ok {
			http.Error(writer, ErrorInvalidHeight(height), 400)
			return
		}

		if txns, err := txsByHeightHandler(indexerDB, height); err != nil {
			http.Error(writer, indexer.ErrorInternal(err), 400)
			return
		} else if txns == nil {
			http.Error(writer, ErrorTxsNotFound(height), 400)
			return
		} else {
			writer.WriteHeader(200)
			writer.Write(txns)
			return
		}
	}).Methods("GET")
})

Functions

This section is empty.

Types

type Event

type Event struct {
	Type       string           `json:"type,omitempty"`
	Attributes []EventAttribute `json:"attributes,omitempty"`
}

type EventAttribute

type EventAttribute struct {
	Key   string `json:"key,omitempty"`
	Value string `json:"value,omitempty"`
}

type ResponseDeliverTx

type ResponseDeliverTx struct {
	Code      uint32  `json:"code"`
	Data      []byte  `json:"data,omitempty"`
	Log       string  `json:"log,omitempty"`
	Info      string  `json:"info,omitempty"`
	GasWanted int64   `json:"gas_wanted,omitempty"`
	GasUsed   int64   `json:"gas_used,omitempty"`
	Events    []Event `json:"events,omitempty"`
	Codespace string  `json:"codespace,omitempty"`
}

func ToResponseDeliverTxJSON

func ToResponseDeliverTxJSON(responseDeliverTx *abci.ResponseDeliverTx) *ResponseDeliverTx

type TxByHeightRecord

type TxByHeightRecord struct {
	Code      uint32          `json:"code"`
	Codespace string          `json:"codespace"`
	GasUsed   int64           `json:"gas_used"`
	GasWanted int64           `json:"gas_wanted"`
	Height    int64           `json:"height"`
	RawLog    string          `json:"raw_log"`
	Logs      json.RawMessage `json:"logs"`
	TxHash    string          `json:"txhash"`
	Timestamp time.Time       `json:"timestamp"`
	Tx        json.RawMessage `json:"tx"`
}

type TxRecord

type TxRecord struct {
	Tx         json.RawMessage `json:"tx"`
	TxResponse json.RawMessage `json:"tx_response"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL