Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] common,bp,txapp: call notices and row results in log, and use log from tx routes #1368

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math/big"
"strings"

"github.com/kwilteam/kwil-db/config"
"github.com/kwilteam/kwil-db/core/crypto"
Expand Down Expand Up @@ -157,6 +158,49 @@ type Row struct {
Values []any
}

func (r Row) TypeStrings() []string {
var typeStrings []string
for _, ty := range r.ColumnTypes {
typeStrings = append(typeStrings, ty.String())
}
return typeStrings
}

func (r Row) ValueStrings() []string {
valueStrings := make([]string, 0, len(r.Values))
for _, value := range r.Values {
valueStrings = append(valueStrings, fmt.Sprintf("%v", value))
}
return valueStrings
}

func (r Row) String() string {
var sb strings.Builder
for i, name := range r.ColumnNames {
fmt.Fprintf(&sb, "%s[%s]: %v", name, r.ColumnTypes[i].String(), r.Values[i])
if i != len(r.ColumnNames)-1 {
sb.WriteString(", ")
}
}
return sb.String()
}

const unknownColName = "?column?" // TODO: get from interpreter pkg

func (r Row) ColHeaders() []string { // "name (type)" in each string
if len(r.ColumnNames) == 1 && r.ColumnNames[0] == unknownColName { // just a single returned value, not table
return []string{r.ColumnTypes[0].String()}
}
var colHeaders []string
for i, name := range r.ColumnNames {
if name == unknownColName {
name = fmt.Sprintf("col%d", i)
}
colHeaders = append(colHeaders, fmt.Sprintf("%s (%s)", name, r.ColumnTypes[i].String()))
}
return colHeaders
}

// Accounts is an interface for managing accounts on the Kwil network. It
// should be used to credit, debit, and transfer funds between Kwil accounts.
type Accounts interface {
Expand Down
37 changes: 37 additions & 0 deletions common/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package common_test

import (
"fmt"

"github.com/kwilteam/kwil-db/common"
"github.com/kwilteam/kwil-db/core/types"
)

func ExampleRow_String() {
row := common.Row{
ColumnNames: []string{"id", "name", "scores"},
ColumnTypes: []*types.DataType{
{Name: "INT"},
{Name: "TEXT"},
{Name: "INT", IsArray: true},
},
Values: []any{
42,
"Alice",
[]int{95, 87, 91},
},
}
for _, ty := range row.ColumnTypes {
if err := ty.Clean(); err != nil {
panic(err)
}
}

fmt.Println(row.String())
fmt.Println(row.TypeStrings())
fmt.Println(row.ValueStrings())
// Output:
// id[int8]: 42, name[text]: Alice, scores[int8[]]: [95 87 91]
// [int8 text int8[]]
// [42 Alice [95 87 91]]
}
2 changes: 1 addition & 1 deletion core/types/data_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c *DataType) UnmarshalBinary(data []byte) error {
// String returns the string representation of the type.
func (c *DataType) String() string {
str := strings.Builder{}
aliased, ok := typeAlias[c.Name]
aliased, ok := typeAlias[strings.ToLower(c.Name)]
if !ok {
aliased = "[!invalid!]" + c.Name
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/consensus/payloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ type Route interface {
// InTx executes the transaction, which may include state changes via the DB
// or Engine. The TxCode is returned by the Router, and it should be CodeOk
// for a nil error.
InTx(ctx *common.TxContext, app *common.App, tx *types.Transaction) (types.TxCode, error)
InTx(ctx *common.TxContext, app *common.App, tx *types.Transaction) (types.TxCode, string, error)
}
7 changes: 4 additions & 3 deletions node/block_processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ func (bp *BlockProcessor) ExecuteBlock(ctx context.Context, req *ktypes.BlockExe
txResult := ktypes.TxResult{
Code: uint32(res.ResponseCode),
Gas: res.Spend,
Log: res.Log,
}

// bookkeeping for the block execution status
Expand All @@ -405,10 +406,10 @@ func (bp *BlockProcessor) ExecuteBlock(ctx context.Context, req *ktypes.BlockExe
return nil, fmt.Errorf("fatal db error during block execution: %w", res.Error)
}

txResult.Log = res.Error.Error()
if txResult.Log == "" {
txResult.Log = res.Error.Error() // maybe don't?
}
bp.log.Info("Failed to execute transaction", "tx", txHash, "err", res.Error)
} else {
txResult.Log = "success"
}

txResults[i] = txResult
Expand Down
2 changes: 1 addition & 1 deletion node/engine/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func funcDefToExecutable(funcName string, funcDef *engine.ScalarFunctionDefiniti
}

// if the function name is notice, then we need to get write the notice to our logs locally,
// instead of executing a query. This is the functional eqauivalent of Kwil's console.log().
// instead of executing a query. This is the functional equivalent of Kwil's console.log().
if funcName == "notice" {
var log string
if !args[0].Null() {
Expand Down
Loading