Skip to content

Commit

Permalink
Merge pull request #347 from oasisprotocol/CU-861m9grpa_Expose-error-…
Browse files Browse the repository at this point in the history
…module-code-message-for-RuntimeTransaction_Xi-Zhang

api: Add runtime and Consensus transaction failure details
  • Loading branch information
aefhm committed Mar 16, 2023
2 parents 4271371 + 6eaebd1 commit e4ff22f
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 7 deletions.
12 changes: 10 additions & 2 deletions analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,14 @@ func (m *Main) queueTransactionInserts(batch *storage.QueryBatch, data *storage.
m.logger.Warn("failed to marshal transaction body", "err", err, "tx_hash", signedTx.Hash().Hex(), "height", data.Height)
bodyBytes = []byte{}
}
var module *string
if len(result.Error.Module) > 0 {
module = &result.Error.Module
}
var message *string
if len(result.Error.Message) > 0 {
message = &result.Error.Message
}
batch.Queue(queries.ConsensusTransactionInsert,
data.BlockHeader.Height,
signedTx.Hash().Hex(),
Expand All @@ -428,9 +436,9 @@ func (m *Main) queueTransactionInserts(batch *storage.QueryBatch, data *storage.
tx.Method,
sender,
bodyBytes,
result.Error.Module,
module,
result.Error.Code,
result.Error.Message,
message,
)
batch.Queue(queries.ConsensusAccountNonceUpdate,
sender,
Expand Down
21 changes: 21 additions & 0 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1240,9 +1240,27 @@ components:
success:
type: boolean
description: Whether this transaction successfully executed.
error:
$ref: '#/components/schemas/TxError'
description: Error details of a failed transaction.
description: |
A consensus transaction.
TxError:
type: object
required: [code]
properties:
code:
type: integer
format: uint32
description: The status code of a failed transaction.
module:
type: string
description: The module of a failed transaction.
message:
type: string
description: The message of a failed transaction.

ConsensusEventType:
type: string
enum:
Expand Down Expand Up @@ -2063,6 +2081,9 @@ components:
success:
type: boolean
description: Whether this transaction successfully executed.
error:
$ref: '#/components/schemas/TxError'
description: Error details of a failed transaction.
description: |
A runtime transaction.
Expand Down
11 changes: 11 additions & 0 deletions api/v1/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ func renderRuntimeTransaction(storageTransaction client.RuntimeTransaction) (api
Body: body,
Success: cr.IsSuccess(),
}
if !cr.IsSuccess() {
// `module` should be present but message may be null
// https://github.com/oasisprotocol/oasis-sdk/blob/fb741678585c04fdb413441f2bfba18aafbf98f3/client-sdk/go/types/transaction.go#L488-L492
apiTransaction.Error = &apiTypes.TxError{
Code: cr.Failed.Code,
Module: &cr.Failed.Module,
}
if len(cr.Failed.Message) > 0 {
apiTransaction.Error.Message = &cr.Failed.Message
}
}
if err = uncategorized.VisitCall(&tx.Call, &cr, &uncategorized.CallHandler{
AccountsTransfer: func(body *accounts.Transfer) error {
toAddr, err2 := uncategorized.StringifySdkAddress(&body.To)
Expand Down
6 changes: 5 additions & 1 deletion api/v1/strict_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@ func (srv *StrictServerImpl) GetRuntimeTransactionsTxHash(ctx context.Context, r
}

// Perform additional tx body parsing on the fly; DB stores only partially-parsed txs.
var apiTransactions apiTypes.RuntimeTransactionList
apiTransactions := apiTypes.RuntimeTransactionList{
Transactions: []apiTypes.RuntimeTransaction{},
TotalCount: storageTransactions.TotalCount,
IsTotalCountClipped: storageTransactions.IsTotalCountClipped,
}
for _, storageTransaction := range storageTransactions.Transactions {
apiTransaction, err2 := renderRuntimeTransaction(storageTransaction)
if err2 != nil {
Expand Down
24 changes: 22 additions & 2 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ func (c *StorageClient) Transactions(ctx context.Context, p apiTypes.GetConsensu
}
for res.rows.Next() {
var t Transaction
var code uint64
var code uint32
var module *string
var message *string
if err := res.rows.Scan(
&t.Block,
&t.Index,
Expand All @@ -290,12 +292,20 @@ func (c *StorageClient) Transactions(ctx context.Context, p apiTypes.GetConsensu
&t.Method,
&t.Body,
&code,
&module,
&message,
&t.Timestamp,
); err != nil {
return nil, wrapError(err)
}
if code == oasisErrors.CodeNoError {
t.Success = true
} else {
t.Error = &apiTypes.TxError{
Code: code,
Module: module,
Message: message,
}
}

ts.Transactions = append(ts.Transactions, t)
Expand All @@ -313,7 +323,9 @@ func (c *StorageClient) Transaction(ctx context.Context, txHash string) (*Transa
}

var t Transaction
var code uint64
var code uint32
var module *string
var message *string
if err := c.db.QueryRow(
ctx,
queries.Transaction,
Expand All @@ -328,12 +340,20 @@ func (c *StorageClient) Transaction(ctx context.Context, txHash string) (*Transa
&t.Method,
&t.Body,
&code,
&module,
&message,
&t.Timestamp,
); err != nil {
return nil, wrapError(err)
}
if code == oasisErrors.CodeNoError {
t.Success = true
} else {
t.Error = &apiTypes.TxError{
Code: code,
Module: module,
Message: message,
}
}

c.cacheTx(&t)
Expand Down
4 changes: 3 additions & 1 deletion storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const (
chain.transactions.method as method,
chain.transactions.body as body,
chain.transactions.code as code,
chain.transactions.module as module,
chain.transactions.message as message,
chain.blocks.time as time
FROM chain.transactions
JOIN chain.blocks ON chain.transactions.block = chain.blocks.height
Expand All @@ -65,7 +67,7 @@ const (
OFFSET $9::bigint`

Transaction = `
SELECT block, tx_index, tx_hash, sender, nonce, fee_amount, method, body, code, chain.blocks.time
SELECT block, tx_index, tx_hash, sender, nonce, fee_amount, method, body, code, module, message, chain.blocks.time
FROM chain.transactions
JOIN chain.blocks ON chain.transactions.block = chain.blocks.height
WHERE tx_hash = $1::text`
Expand Down
2 changes: 1 addition & 1 deletion storage/migrations/01_consensus.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CREATE TABLE chain.transactions
-- Error Fields
-- This includes an encoding of no error.
module TEXT,
code UINT31, -- From https://github.com/oasisprotocol/oasis-core/blob/f95186e3f15ec64bdd36493cde90be359bd17da8/go/consensus/api/transaction/results/results.go#L20-L20
code UINT31 NOT NULL, -- From https://github.com/oasisprotocol/oasis-core/blob/f95186e3f15ec64bdd36493cde90be359bd17da8/go/consensus/api/transaction/results/results.go#L20-L20
message TEXT,

-- We require a composite primary key since duplicate transactions (with identical hashes) can
Expand Down

0 comments on commit e4ff22f

Please sign in to comment.