Transaction Processor: Creating a Transaction Handler

A transaction processor has two top-level components:

  • Processor class. The SDK provides a general-purpose processor class.

  • Handler class. The handler class is application-dependent. It contains the business logic for a particular family of transactions. Multiple handlers can be connected to an instance of the processor class.

Entry Point

Since a transaction processor is a long running process, it must have an entry point.

In the entry point, the TransactionProcessor class is given the address to connect with the validator and the handler class.

a simplified sawtooth_xo/main.go
import (
    "sawtooth_sdk/processor"
    xo "sawtooth_xo/handler"
    "syscall"
)

func main() {

    endpoint := "tcp://127.0.0.1:4004"
    // In docker, endpoint would be the validator's container name
    // with port 4004
    handler := &xo.XoHandler{}
    processor := processor.NewTransactionProcessor(endpoint)
    processor.AddHandler(handler)
    processor.ShutdownOnSignal(syscall.SIGINT, syscall.SIGTERM)

    processor.Start()
}

Handlers get called in two ways: with an apply method and with various “metadata” methods. The metadata is used to connect the handler to the processor. The bulk of the handler, however, is made up of apply and its helper functions.

sawtooth_xo/handler/handler.go XoHandler struct
type XoHandler struct {
}

func (self *XoHandler) FamilyName() string {
    return "xo"
}

func (self *XoHandler) FamilyVersions() []string {
    return []string{"1.0"}
}

func (self *XoHandler) Namespaces() []string {
    return []string{xo_state.Namespace}
}

func (self *XoHandler) Apply(request *processor_pb2.TpProcessRequest, context *processor.Context) error {

The apply Method

apply gets called with two arguments, request and context. request holds the command that is to be executed (e.g. taking a space or creating a game), while context stores information about the current state of the game (e.g. the board layout and whose turn it is).

The transaction contains payload bytes that are opaque to the validator core, and transaction family specific. When implementing a transaction handler the binary serialization protocol is up to the implementer.

To separate details of state encoding and payload handling from validation logic, the XO example has XoState and XoPayload classes. The XoPayload has name, action, and space fields, while the XoState contains information about the game name, board, state, and which players are playing in the game.

Valid actions are: create a new game, take an unoccupied space, and delete a game.

sawtooth_xo/handler/handler.go apply overview
func (self *XoHandler) Apply(request *processor_pb2.TpProcessRequest, context *processor.Context) error {
    // The xo player is defined as the signer of the transaction, so we unpack
    // the transaction header to obtain the signer's public key, which will be
    // used as the player's identity.
    header := request.GetHeader()
    player := header.GetSignerPublicKey()

    // The payload is sent to the transaction processor as bytes (just as it
    // appears in the transaction constructed by the transactor).  We unpack
    // the payload into an XoPayload struct so we can access its fields.
    payload, err := xo_payload.FromBytes(request.GetPayload())
    if err != nil {
        return err
    }

    xoState := xo_state.NewXoState(context)

    switch payload.Action {
    case "create":
        ...
    case "delete":
        ...
    case "take":
        ...
    default:
        return &processor.InvalidTransaction{
            Msg: fmt.Sprintf("Invalid Action : '%v'", payload.Action)}
    }

For every new payload, the transaction processor validates rules surrounding the action. If all of the rules validate, then state is updated based on whether we are creating a game, deleting a game, or updating the game by taking a space.

Game Logic

The XO game logic is described in the XO transaction family specification; see Execution.

The validation rules and state updates that are associated with the create, delete, and take actions are shown below.

Create

The create action has the following implementation:

sawtooth_xo/handler/handler.go apply ‘create’
case "create":
            err := validateCreate(xoState, payload.Name)
            if err != nil {
                    return err
            }
            game := &xo_state.Game{
                    Board:   "---------",
                    State:   "P1-NEXT",
                    Player1: "",
                    Player2: "",
                    Name:    payload.Name,
            }
            displayCreate(payload, player)
    return xoState.SetGame(payload.Name, game)

validateCreate is defined as follows:

func validateCreate(xoState *xo_state.XoState, name string) error {
    game, err := xoState.GetGame(name)
    if err != nil {
        return err
    }
    if game != nil {
        return &processor.InvalidTransactionError{Msg: "Game already exists"}
    }

    return nil
}

Delete

The delete action has the following implementation:

sawtooth_xo/handler/handler.go apply ‘delete’
case "delete":
            err := validateDelete(xoState, payload.Name)
            if err != nil {
                    return err
            }
    return xoState.DeleteGame(payload.Name)

validateDelete is defined as follows:

func validateDelete(xoState *xo_state.XoState, name string) error {
    game, err := xoState.GetGame(name)
    if err != nil {
        return err
    }
    if game == nil {
        return &processor.InvalidTransactionError{Msg: "Delete requires an existing game"}
    }
    return nil
}

Take

The take action has the following implementation:

sawtooth_xo/handler/handler.go apply ‘take’
case "take":
            err := validateTake(xoState, payload, player)
            if err != nil {
                    return err
            }
            game, err := xoState.GetGame(payload.Name)
            if err != nil {
                    return err
            }
            // Assign players if new game
            if game.Player1 == "" {
                    game.Player1 = player
            } else if game.Player2 == "" {
                    game.Player2 = player
            }

            if game.State == "P1-NEXT" && player == game.Player1 {
                    boardRunes := []rune(game.Board)
                    boardRunes[payload.Space-1] = 'X'
                    game.Board = string(boardRunes)
                    game.State = "P2-NEXT"
            } else if game.State == "P2-NEXT" && player == game.Player2 {
                    boardRunes := []rune(game.Board)
                    boardRunes[payload.Space-1] = 'O'
                    game.Board = string(boardRunes)
                    game.State = "P1-NEXT"
            } else {
                    return &processor.InvalidTransactionError{
                            Msg: fmt.Sprintf("Not this player's turn: '%v'", player)}
            }

            if isWin(game.Board, 'X') {
                    game.State = "P1-WIN"
            } else if isWin(game.Board, 'O') {
                    game.State = "P2-WIN"
            } else if !strings.Contains(game.Board, "-") {
                    game.State = "TIE"
            }
            displayTake(payload, player, game)
    return xoState.SetGame(payload.Name, game)

validateTake is defined as follows:

func validateTake(xoState *xo_state.XoState, payload *xo_payload.XoPayload, signer string) error {
    game, err := xoState.GetGame(payload.Name)
    if err != nil {
        return err
    }
    if game == nil {
        return &processor.InvalidTransactionError{Msg: "Take requires an existing game"}
    }
    if game.State == "P1-WIN" || game.State == "P2-WIN" || game.State == "TIE" {
        return &processor.InvalidTransactionError{Msg: "Game has ended"}
    }

    if game.State == "P1-WIN" || game.State == "P2-WIN" || game.State == "TIE" {
        return &processor.InvalidTransactionError{
            Msg: "Invalid Action: Game has ended"}
    }

    if game.Board[payload.Space-1] != '-' {
        return &processor.InvalidTransactionError{Msg: "Space already taken"}
    }
    return nil
}

Payload

Note

Transactions and Batches contains a detailed description of how transactions are structured and used. Please read this document before proceeding, if you have not reviewed it.

So how do we get data out of the transaction? The transaction consists of a header and a payload. The header contains the “signer”, which is used to identify the current player. The payload will contain an encoding of the game name, the action (create a game, delete a game, take a space), and the space (which will be an empty string if the action isn’t take).

An XO transaction request payload consists of the UTF-8 encoding of a string with exactly two commas, which is formatted as follows:

<name>,<action>,<space>

  • <name> is the game name as a non-empty string not containing the character |. If the action is create, the new name must be unique.

  • <action> is the game action: create, take, or delete

  • <space> is the location on the board, as an integer between 1-9 (inclusive), if the action is take

sawtooth_xo/xo_payload/xo_payload.go
type XoPayload struct {
    Name   string
    Action string
    Space  int
}

func FromBytes(payloadData []byte) (*XoPayload, error) {
    if payloadData == nil {
        return nil, &processor.InvalidTransactionError{Msg: "Must contain payload"}
    }

    parts := strings.Split(string(payloadData), ",")
    if len(parts) != 3 {
        return nil, &processor.InvalidTransactionError{Msg: "Payload is malformed"}
    }

    payload := XoPayload{}
    payload.Name = parts[0]
    payload.Action = parts[1]

    if len(payload.Name) < 1 {
        return nil, &processor.InvalidTransactionError{Msg: "Name is required"}
    }

    if len(payload.Action) < 1 {
        return nil, &processor.InvalidTransactionError{Msg: "Action is required"}
    }

    if payload.Action == "take" {
        space, err := strconv.Atoi(parts[2])
        if err != nil {
            return nil, &processor.InvalidTransactionError{
                Msg: fmt.Sprintf("Invalid Space: '%v'", parts[2])}
        }
        payload.Space = space
    }

    if strings.Contains(payload.Name, "|") {
        return nil, &processor.InvalidTransactionError{
            Msg: fmt.Sprintf("Invalid Name (char '|' not allowed): '%v'", parts[2])}
    }

    return &payload, nil
}

State

The XoState class turns game information into bytes and stores it in the validator’s Radix-Merkle tree, turns bytes stored in the validator’s Radix-Merkle tree into game information, and does these operations with a state storage scheme that handles hash collisions.

An XO state entry consists of the UTF-8 encoding of a string with exactly four commas formatted as follows:

<name>,<board>,<game-state>,<player-key-1>,<player-key-2>

where

  • <name> is a nonempty string not containing |,

  • <board> is a string of length 9 containing only O, X, or -,

  • <game-state> is one of the following: P1-NEXT, P2-NEXT, P1-WIN,

  • P2-WIN, or TIE, and

  • <player-key-1> and <player-key-2> are the (possibly empty) public keys

  • associated with the game’s players.

In the event of a hash collision (i.e. two or more state entries sharing the same address), the colliding state entries will stored as the UTF-8 encoding of the string <a-entry>|<b-entry>|..., where <a-entry>, <b-entry>,… are sorted alphabetically.

sawtooth_xo/xo_state/xo_state.go
var Namespace = hexdigest("xo")[:6]

type Game struct {
    Board   string
    State   string
    Player1 string
    Player2 string
    Name    string
}

// XoState handles addressing, serialization, deserialization,
// and holding an addressCache of data at the address.
type XoState struct {
    context      *processor.Context
    addressCache map[string][]byte
}

// NewXoState constructs a new XoState struct.
func NewXoState(context *processor.Context) *XoState {
    return &XoState{
        context:      context,
        addressCache: make(map[string][]byte),
    }
}

// GetGame returns a game by its name.
func (self *XoState) GetGame(name string) (*Game, error) {
    games, err := self.loadGames(name)
    if err != nil {
        return nil, err
    }
    game, ok := games[name]
    if ok {
        return game, nil
    }
    return nil, nil
}

// SetGame sets a game to its name
func (self *XoState) SetGame(name string, game *Game) error {
    games, err := self.loadGames(name)
    if err != nil {
        return err
    }

    games[name] = game

    return self.storeGames(name, games)
}

// DeleteGame deletes the game from state, handling
// hash collisions.
func (self *XoState) DeleteGame(name string) error {
    games, err := self.loadGames(name)
    if err != nil {
        return err
    }
    delete(games, name)
    if len(games) > 0 {
        return self.storeGames(name, games)
    } else {
        return self.deleteGames(name)
    }
}

func (self *XoState) loadGames(name string) (map[string]*Game, error) {
    address := makeAddress(name)

    data, ok := self.addressCache[address]
    if ok {
        if self.addressCache[address] != nil {
            return deserialize(data)
        }
        return make(map[string]*Game), nil

    }
    results, err := self.context.GetState([]string{address})
    if err != nil {
        return nil, err
    }
    if len(string(results[address])) > 0 {
        self.addressCache[address] = results[address]
        return deserialize(results[address])
    }
    self.addressCache[address] = nil
    games := make(map[string]*Game)
    return games, nil
}

func (self *XoState) storeGames(name string, games map[string]*Game) error {
    address := makeAddress(name)

    var names []string
    for name := range games {
        names = append(names, name)
    }
    sort.Strings(names)

    var g []*Game
    for _, name := range names {
        g = append(g, games[name])
    }

    data := serialize(g)

    self.addressCache[address] = data

    _, err := self.context.SetState(map[string][]byte{
        address: data,
    })
    return err
}

func (self *XoState) deleteGames(name string) error {
    address := makeAddress(name)

    _, err := self.context.DeleteState([]string{address})
    return err
}

func deserialize(data []byte) (map[string]*Game, error) {
    games := make(map[string]*Game)
    for _, str := range strings.Split(string(data), "|") {

        parts := strings.Split(string(str), ",")
        if len(parts) != 5 {
            return nil, &processor.InternalError{
                Msg: fmt.Sprintf("Malformed game data: '%v'", string(data))}
        }

        game := &Game{
            Name:    parts[0],
            Board:   parts[1],
            State:   parts[2],
            Player1: parts[3],
            Player2: parts[4],
        }
        games[parts[0]] = game
    }

    return games, nil
}

func serialize(games []*Game) []byte {
    var buffer bytes.Buffer
    for i, game := range games {

        buffer.WriteString(game.Name)
        buffer.WriteString(",")
        buffer.WriteString(game.Board)
        buffer.WriteString(",")
        buffer.WriteString(game.State)
        buffer.WriteString(",")
        buffer.WriteString(game.Player1)
        buffer.WriteString(",")
        buffer.WriteString(game.Player2)
        if i+1 != len(games) {
            buffer.WriteString("|")
        }
    }
    return buffer.Bytes()
}

func hexdigest(str string) string {
    hash := sha512.New()
    hash.Write([]byte(str))
    hashBytes := hash.Sum(nil)
    return strings.ToLower(hex.EncodeToString(hashBytes))
}

Addressing

By convention, we’ll store game data at an address obtained from hashing the game name prepended with some constant.

XO data is stored in state using addresses generated from the XO family name and the name of the game being stored. In particular, an XO address consists of the first 6 characters of the SHA-512 hash of the UTF-8 encoding of the string “xo” (which is “5b7349”) plus the first 64 characters of the SHA-512 hash of the UTF-8 encoding of the game name.

For example, the XO address for a game called “my-game” could be generated as follows (in Python):

>>> XO_NAMESPACE = hashlib.sha512('xo'.encode('utf-8')).hexdigest()[:6]
>>> XO_NAMESPACE
'5b7349'
>>> y = hashlib.sha512('my-game'.encode('utf-8')).hexdigest()[:64]
>>> y
'4d4cffe9cf3fb4e41def5114a323e292af9b0e07925cca6299d671ce7fc7ec37'
>>> XO_NAMESPACE+y
'5b73494d4cffe9cf3fb4e41def5114a323e292af9b0e07925cca6299d671ce7fc7ec37'

Addressing is implemented as follows:

func makeAddress(name string) string {
        return Namespace + hexdigest(name)[:64]
}