-
Notifications
You must be signed in to change notification settings - Fork 293
/
prepare_proposal_test.go
184 lines (152 loc) · 4.98 KB
/
prepare_proposal_test.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package app_test
import (
"bytes"
"testing"
"github.com/celestiaorg/nmt/namespace"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
core "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/celestia-app/testutil"
"github.com/celestiaorg/celestia-app/x/blob/types"
)
func TestPrepareProposal(t *testing.T) {
signer := testutil.GenerateKeyringSigner(t, testAccName)
encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...)
testApp := testutil.SetupTestAppWithGenesisValSet(t)
type test struct {
input abci.RequestPrepareProposal
expectedBlobs []core.Blob
expectedTxs int
}
firstNamespace := []byte{2, 2, 2, 2, 2, 2, 2, 2}
firstMessage := bytes.Repeat([]byte{4}, 512)
firstRawTx := generateRawTx(t, encCfg.TxConfig, firstNamespace, firstMessage, signer)
secondNamespace := []byte{1, 1, 1, 1, 1, 1, 1, 1}
secondMessage := []byte{2}
secondRawTx := generateRawTx(t, encCfg.TxConfig, secondNamespace, secondMessage, signer)
thirdNamespace := []byte{3, 3, 3, 3, 3, 3, 3, 3}
thirdMessage := []byte{1}
thirdRawTx := generateRawTx(t, encCfg.TxConfig, thirdNamespace, thirdMessage, signer)
tests := []test{
{
input: abci.RequestPrepareProposal{
BlockData: &core.Data{
Txs: [][]byte{firstRawTx, secondRawTx, thirdRawTx},
},
},
expectedBlobs: []core.Blob{
{
NamespaceId: secondNamespace, // the second blob should be first
Data: []byte{2},
},
{
NamespaceId: firstNamespace,
Data: firstMessage,
},
{
NamespaceId: thirdNamespace,
Data: []byte{1},
},
},
expectedTxs: 3,
},
}
for _, tt := range tests {
res := testApp.PrepareProposal(tt.input)
assert.Equal(t, tt.expectedBlobs, res.BlockData.Blobs)
assert.Equal(t, tt.expectedTxs, len(res.BlockData.Txs))
// verify the signatures of the prepared txs
sdata, err := signer.GetSignerData()
if err != nil {
require.NoError(t, err)
}
dec := encoding.MalleatedTxDecoder(encCfg.TxConfig.TxDecoder())
for _, tx := range res.BlockData.Txs {
sTx, err := dec(tx)
require.NoError(t, err)
sigTx, ok := sTx.(authsigning.SigVerifiableTx)
require.True(t, ok)
sigs, err := sigTx.GetSignaturesV2()
require.NoError(t, err)
require.Equal(t, 1, len(sigs))
sig := sigs[0]
err = authsigning.VerifySignature(
sdata.PubKey,
sdata,
sig.Data,
encCfg.TxConfig.SignModeHandler(),
sTx,
)
assert.NoError(t, err)
}
}
}
func TestPrepareMessagesWithReservedNamespaces(t *testing.T) {
testApp := testutil.SetupTestAppWithGenesisValSet(t)
encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...)
signer := testutil.GenerateKeyringSigner(t, testAccName)
type test struct {
name string
namespace namespace.ID
expectedMessages int
}
tests := []test{
{"transaction namespace id for message", appconsts.TxNamespaceID, 0},
{"evidence namespace id for message", appconsts.EvidenceNamespaceID, 0},
{"tail padding namespace id for message", appconsts.TailPaddingNamespaceID, 0},
{"parity shares namespace id for message", appconsts.ParitySharesNamespaceID, 0},
{"reserved namespace id for message", namespace.ID{0, 0, 0, 0, 0, 0, 0, 200}, 0},
{"valid namespace id for message", namespace.ID{3, 3, 2, 2, 2, 1, 1, 1}, 1},
}
for _, tt := range tests {
message := []byte{1}
tx := generateRawTx(t, encCfg.TxConfig, tt.namespace, message, signer)
input := abci.RequestPrepareProposal{
BlockData: &core.Data{
Txs: [][]byte{tx},
},
}
res := testApp.PrepareProposal(input)
assert.Equal(t, tt.expectedMessages, len(res.BlockData.Blobs))
}
}
func generateRawTx(t *testing.T, txConfig client.TxConfig, ns, message []byte, signer *types.KeyringSigner) (rawTx []byte) {
coin := sdk.Coin{
Denom: app.BondDenom,
Amount: sdk.NewInt(10),
}
opts := []types.TxBuilderOption{
types.SetFeeAmount(sdk.NewCoins(coin)),
types.SetGasLimit(10000000),
}
// create a msg
msg := generateSignedWirePayForBlob(t, ns, message, appconsts.ShareVersionZero, signer, opts)
builder := signer.NewTxBuilder(opts...)
tx, err := signer.BuildSignedTx(builder, msg)
require.NoError(t, err)
// encode the tx
rawTx, err = txConfig.TxEncoder()(tx)
require.NoError(t, err)
return rawTx
}
func generateSignedWirePayForBlob(t *testing.T, ns []byte, message []byte, shareVersion uint8, signer *types.KeyringSigner, options []types.TxBuilderOption) *types.MsgWirePayForBlob {
msg, err := types.NewWirePayForBlob(ns, message, shareVersion)
if err != nil {
t.Error(err)
}
err = msg.SignShareCommitment(signer, options...)
if err != nil {
t.Error(err)
}
return msg
}
const (
testAccName = "test-account"
)