Skip to content

Commit

Permalink
Merge branch 'master' into refactor/generating-docs-config
Browse files Browse the repository at this point in the history
  • Loading branch information
Freydal authored Nov 5, 2019
2 parents e63ba98 + 030e18b commit e6494e4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 42 deletions.
94 changes: 52 additions & 42 deletions packages/go-kosu/abci/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,58 +260,67 @@ func (app *App) EndBlock(req abci.RequestEndBlock) abci.ResponseEndBlock {
}
}

// CheckTx .
func (app *App) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
stx, err := types.NewSignedTransactionFromBytes(req.Tx)
// NewCheckTxResponse returns a abci.ResponseCheckTx.
// If provided error is not nil, the response will IsErr() and Log the error message.
func NewCheckTxResponse(err error) abci.ResponseCheckTx {
if err != nil {
return abci.ResponseCheckTx{Code: 1, Log: err.Error()}
}
return abci.ResponseCheckTx{}
}

nodeID := tmhash.SumTruncated(stx.Proof.PublicKey)
tx := stx.Tx
// TODO(gchaincl) refactor the validator verification
switch tx.GetData().(type) {
case *types.Transaction_Rebalance:
if !app.store.ValidatorExists(nodeID) {
msg := fmt.Sprintf("NodeID %s does not belong to a validator", hex.EncodeToString(nodeID))
return abci.ResponseCheckTx{Code: 1, Log: msg}
}
// NodeIsValidator returns an error if the provided node id is not in the validator set
func NodeIsValidator(s store.Store, id types.NodeID) error {
if !s.ValidatorExists(id) {
return fmt.Errorf("NodeID %s does not belong to a validator", hex.EncodeToString(id))
}
return nil
}

if err := app.checkRebalanceTx(tx.GetRebalance()); err != nil {
return abci.ResponseCheckTx{Code: 1, Log: err.Error()}
// CheckTx implements ABCI CheckTx
func (app *App) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
stx, err := types.NewSignedTransactionFromBytes(req.Tx)
if err != nil {
return NewCheckTxResponse(err)
}

nodeID := stx.NodeID()
switch stx.Route() {
case "rebalance":
if err := NodeIsValidator(app.store, nodeID); err != nil {
return NewCheckTxResponse(err)
}
return abci.ResponseCheckTx{}
case *types.Transaction_Witness:
if !app.store.ValidatorExists(nodeID) {
msg := fmt.Sprintf("NodeID %s does not belong to a validator", hex.EncodeToString(nodeID))
return abci.ResponseCheckTx{Code: 1, Log: msg}
return NewCheckTxResponse(app.checkRebalanceTx(stx.Tx.GetRebalance()))
case "witness":
if err := NodeIsValidator(app.store, nodeID); err != nil {
return NewCheckTxResponse(err)
}

w := tx.GetWitness()
tx := stx.Tx.GetWitness()

// .Confirmations should not be defined in the request
w.Confirmations = 0
if err := app.checkWitnessTx(w); err != nil {
return abci.ResponseCheckTx{Code: 1, Log: err.Error()}
if tx.Confirmations != 0 {
return NewCheckTxResponse(fmt.Errorf("WitnessTx can't define Confirmations"))
}
return abci.ResponseCheckTx{}
case *types.Transaction_Order:

return NewCheckTxResponse(app.checkWitnessTx(tx))
case "order":
if max := app.store.ConsensusParams().MaxOrderBytes; max > 0 {
if uint32(len(req.Tx)) > max {
return abci.ResponseCheckTx{Code: 1, Log: fmt.Sprintf("Tx size exceeds %d", max)}
return NewCheckTxResponse(fmt.Errorf("Tx size exceeds %d", max))
}
}
if err := app.checkOrderTx(tx.GetOrder()); err != nil {
return abci.ResponseCheckTx{Code: 1, Log: err.Error()}
}
return abci.ResponseCheckTx{}
default:
fmt.Printf("Unknown Tx: %t", tx.GetData())

tx := stx.Tx.GetOrder()
return NewCheckTxResponse(app.checkOrderTx(tx))
}

return abci.ResponseCheckTx{Code: 1, Info: "Unknown Transaction type"}
err = fmt.Errorf("check_tx: unknown Transaction type: %T (route=%s)", stx.Tx.GetData(), stx.Route())
app.log.Error("Can't process Tx", "err", err)
return NewCheckTxResponse(err)
}

// DeliverTx .
// DeliverTx implements ABCI DeliverTx
func (app *App) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
stx, err := types.NewSignedTransactionFromBytes(req.Tx)
if err != nil {
Expand All @@ -320,17 +329,18 @@ func (app *App) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
}

tx := stx.Tx
switch tx.GetData().(type) {
case *types.Transaction_Rebalance:
switch stx.Route() {
case "rebalance":
return app.deliverRebalance(tx.GetRebalance())
case *types.Transaction_Witness:
nodeID := tmhash.SumTruncated(stx.Proof.PublicKey)
return app.deliverWitnessTx(tx.GetWitness(), nodeID)
case *types.Transaction_Order:
case "witness":
return app.deliverWitnessTx(tx.GetWitness(), stx.NodeID())
case "order":
return app.deliverOrderTx(tx.GetOrder())
default:
fmt.Printf("Unknown Tx: %t", tx.GetData())
app.log.Error("Unknown Tx", "%t", tx.GetData())
}

return abci.ResponseDeliverTx{Code: 1, Log: "Unknown Transaction type"}
err = fmt.Errorf("deliver_tx: unknown Transaction type: %T (route=%s)", stx.Tx.GetData(), stx.Route())
app.log.Error("Can't process Tx", "err", err)
return abci.ResponseDeliverTx{Code: 1, Log: err.Error()}
}
7 changes: 7 additions & 0 deletions packages/go-kosu/abci/types/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"time"

"golang.org/x/crypto/ed25519"

"github.com/tendermint/tendermint/crypto/tmhash"
)

var randReader = rand.New(
Expand Down Expand Up @@ -69,3 +71,8 @@ func (tx *SignedTransaction) Verify() (bool, error) {

return ed25519.Verify(tx.Proof.PublicKey, buf, tx.Proof.Signature), nil
}

// NodeID of the Transaction signer
func (tx *SignedTransaction) NodeID() NodeID {
return tmhash.SumTruncated(tx.Proof.PublicKey)
}
21 changes: 21 additions & 0 deletions packages/go-kosu/abci/types/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package types

// Route name definitions

func (tx *Transaction_Witness) Route() string { return "witness" } //nolint
func (tx *Transaction_Rebalance) Route() string { return "rebalance" } //nolint
func (tx *Transaction_Order) Route() string { return "order" } //nolint

// Route return the route representation of the underlying SignedTransaction Tx.
// if the route can't be resolved, an empty string is returned.
func (tx *SignedTransaction) Route() string {
data, ok := tx.GetTx().GetData().(interface {
Route() string
})

if ok {
return data.Route()
}

return ""
}
7 changes: 7 additions & 0 deletions packages/go-kosu/abci/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -136,3 +137,9 @@ func (tx *Transaction) GetOneOf() interface{} {

return nil
}

// NodeID is the id of a node submitting transactions
type NodeID []byte

// String is the string representation
func (id NodeID) String() string { return hex.EncodeToString(id) }

0 comments on commit e6494e4

Please sign in to comment.