-
Notifications
You must be signed in to change notification settings - Fork 11
/
tx.go
135 lines (114 loc) · 3.84 KB
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package kvstore
import (
"bytes"
"errors"
"fmt"
"github.com/dashpay/tenderdash/abci/example/code"
abci "github.com/dashpay/tenderdash/abci/types"
"github.com/dashpay/tenderdash/types"
)
// PrepareTxsFunc prepares transactions, possibly adding and/or removing some of them
type PrepareTxsFunc func(req abci.RequestPrepareProposal) ([]*abci.TxRecord, error)
// VerifyTxFunc checks if transaction is correct
type VerifyTxFunc func(tx types.Tx, typ abci.CheckTxType) (abci.ResponseCheckTx, error)
// ExecTxFunc executes the transaction against some state
type ExecTxFunc func(tx types.Tx, roundState State) (abci.ExecTxResult, error)
func prepareTxs(req abci.RequestPrepareProposal) ([]*abci.TxRecord, error) {
return substPrepareTx(req.Txs, req.MaxTxBytes), nil
}
func txRecords2Txs(txRecords []*abci.TxRecord) types.Txs {
txRecordSet := types.NewTxRecordSet(txRecords)
return txRecordSet.IncludedTxs()
}
func verifyTx(tx types.Tx, _ abci.CheckTxType) (abci.ResponseCheckTx, error) {
_, _, err := parseTx(tx)
if err != nil {
return abci.ResponseCheckTx{
Code: code.CodeTypeEncodingError,
Data: []byte(err.Error()),
}, nil
}
return abci.ResponseCheckTx{Code: code.CodeTypeOK, GasWanted: 1}, nil
}
// tx is either "val:pubkey!power" or "key=value" or just arbitrary bytes
func execTx(tx types.Tx, roundState State) (abci.ExecTxResult, error) {
if isPrepareTx(tx) {
return execPrepareTx(tx)
}
key, value, err := parseTx(tx)
if err != nil {
err = fmt.Errorf("malformed transaction %X in ProcessProposal: %w", tx, err)
return abci.ExecTxResult{Code: code.CodeTypeUnknownError, Log: err.Error()}, err
}
err = roundState.Set([]byte(key), []byte(value))
if err != nil {
return abci.ExecTxResult{Code: code.CodeTypeUnknownError, Log: err.Error()}, err
}
events := []abci.Event{
{
Type: "app",
Attributes: []abci.EventAttribute{
{Key: "creator", Value: "Cosmoshi Netowoko", Index: true},
{Key: "key", Value: key, Index: true},
{Key: "index_key", Value: "index is working", Index: true},
{Key: "noindex_key", Value: "index is working", Index: false},
},
},
}
return abci.ExecTxResult{Code: code.CodeTypeOK, Events: events}, nil
}
// execPrepareTx is noop. tx data is considered as placeholder
// and is substitute at the PrepareProposal.
func execPrepareTx(tx []byte) (abci.ExecTxResult, error) {
// noop
return abci.ExecTxResult{}, nil
}
// substPrepareTx substitutes all the transactions prefixed with 'prepare' in the
// proposal for transactions with the prefix stripped.
// It marks all of the original transactions as 'REMOVED' so that
// Tendermint will remove them from its mempool.
func substPrepareTx(txs [][]byte, maxTxBytes int64) []*abci.TxRecord {
trs := make([]*abci.TxRecord, 0, len(txs))
var removed []*abci.TxRecord
var totalBytes int64
for _, tx := range txs {
action := abci.TxRecord_UNMODIFIED
if isPrepareTx(tx) {
// replace tx and add it as REMOVED
removed = append(removed, &abci.TxRecord{
Tx: tx,
Action: abci.TxRecord_REMOVED,
})
totalBytes -= int64(len(tx))
tx = bytes.TrimPrefix(tx, []byte(PreparePrefix))
action = abci.TxRecord_ADDED
}
totalBytes += int64(len(tx))
if totalBytes > maxTxBytes {
break
}
trs = append(trs, &abci.TxRecord{
Tx: tx,
Action: action,
})
}
return append(trs, removed...)
}
const PreparePrefix = "prepare"
func isPrepareTx(tx []byte) bool {
return bytes.HasPrefix(tx, []byte(PreparePrefix))
}
// parseTx parses a tx in 'key=value' format into a key and value.
func parseTx(tx types.Tx) (string, string, error) {
parts := bytes.SplitN(tx, []byte("="), 2)
switch len(parts) {
case 0:
return "", "", errors.New("key cannot be empty")
case 1:
return string(parts[0]), string(parts[0]), nil
case 2:
return string(parts[0]), string(parts[1]), nil
default:
return "", "", fmt.Errorf("invalid tx format: %q", string(tx))
}
}