Skip to content

Commit 099f44e

Browse files
authored
fix: properly bubble up errors in BlockHash rpcs and fix flaky tests (cosmos#330)
* add err returns * fix test exp return * fix flaky non padded copy * add nil check for voting times * use FillBytes instead
1 parent 6085578 commit 099f44e

File tree

9 files changed

+49
-36
lines changed

9 files changed

+49
-36
lines changed

precompiles/gov/types.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ func (po *ProposalsOutput) FromResponse(res *govv1.QueryProposalsResponse) *Prop
724724
return nil
725725
}
726726

727-
po.Proposals[i] = ProposalData{
727+
proposalData := ProposalData{
728728
Id: p.Id,
729729
Messages: msgs,
730730
Status: uint32(p.Status), //nolint:gosec // G115
@@ -734,16 +734,24 @@ func (po *ProposalsOutput) FromResponse(res *govv1.QueryProposalsResponse) *Prop
734734
No: p.FinalTallyResult.NoCount,
735735
NoWithVeto: p.FinalTallyResult.NoWithVetoCount,
736736
},
737-
SubmitTime: uint64(p.SubmitTime.Unix()), //nolint:gosec // G115
738-
DepositEndTime: uint64(p.DepositEndTime.Unix()), //nolint:gosec // G115
739-
TotalDeposit: coins,
740-
VotingStartTime: uint64(p.VotingStartTime.Unix()), //nolint:gosec // G115
741-
VotingEndTime: uint64(p.VotingEndTime.Unix()), //nolint:gosec // G115
742-
Metadata: p.Metadata,
743-
Title: p.Title,
744-
Summary: p.Summary,
745-
Proposer: proposer,
737+
SubmitTime: uint64(p.SubmitTime.Unix()), //nolint:gosec // G115
738+
DepositEndTime: uint64(p.DepositEndTime.Unix()), //nolint:gosec // G115
739+
TotalDeposit: coins,
740+
Metadata: p.Metadata,
741+
Title: p.Title,
742+
Summary: p.Summary,
743+
Proposer: proposer,
746744
}
745+
746+
// The following fields are nil when proposal is in deposit period
747+
if p.VotingStartTime != nil {
748+
proposalData.VotingStartTime = uint64(p.VotingStartTime.Unix()) //nolint:gosec // G115
749+
}
750+
if p.VotingEndTime != nil {
751+
proposalData.VotingEndTime = uint64(p.VotingEndTime.Unix()) //nolint:gosec // G115
752+
}
753+
754+
po.Proposals[i] = proposalData
747755
}
748756

749757
if res.Pagination != nil {

precompiles/p256/p256.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ func (p *Precompile) Run(_ *vm.EVM, contract *vm.Contract, _ bool) (bz []byte, e
7777
// Verify the secp256r1 signature
7878
if secp256r1.Verify(hash, r, s, x, y) {
7979
// Signature is valid
80-
return common.LeftPadBytes(common.Big1.Bytes(), 32), nil
80+
result := make([]byte, 32)
81+
common.Big1.FillBytes(result)
82+
return result, nil
8183
}
8284

8385
// Signature is invalid

rpc/backend/blocks.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@ func (b *Backend) TendermintBlockByNumber(blockNum rpctypes.BlockNumber) (*tmrpc
186186
// TendermintBlockResultByNumber returns a Tendermint-formatted block result
187187
// by block number
188188
func (b *Backend) TendermintBlockResultByNumber(height *int64) (*tmrpctypes.ResultBlockResults, error) {
189-
return b.RPCClient.BlockResults(b.Ctx, height)
189+
res, err := b.RPCClient.BlockResults(b.Ctx, height)
190+
if err != nil {
191+
return nil, fmt.Errorf("failed to fetch block result from Tendermint %d: %w", *height, err)
192+
}
193+
194+
return res, nil
190195
}
191196

192197
// TendermintBlockByHash returns a Tendermint-formatted block by block number
@@ -199,7 +204,7 @@ func (b *Backend) TendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.Resu
199204

200205
if resBlock == nil || resBlock.Block == nil {
201206
b.Logger.Debug("TendermintBlockByHash block not found", "blockHash", blockHash.Hex())
202-
return nil, nil
207+
return nil, fmt.Errorf("block not found for hash %s", blockHash.Hex())
203208
}
204209

205210
return resBlock, nil

rpc/backend/node_info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (b *Backend) SetEtherbase(etherbase common.Address) bool {
101101
// Assemble transaction from fields
102102
builder, ok := b.ClientCtx.TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder)
103103
if !ok {
104-
b.Logger.Debug("clientCtx.TxConfig.NewTxBuilder returns unsupported builder", "error", err.Error())
104+
b.Logger.Debug("clientCtx.TxConfig.NewTxBuilder returns unsupported builder")
105105
return false
106106
}
107107

rpc/namespaces/ethereum/eth/filters/filters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (f *Filter) Logs(_ context.Context, logLimit int, blockLimit int64) ([]*eth
108108
blockRes, err := f.backend.TendermintBlockResultByNumber(&resBlock.Block.Height)
109109
if err != nil {
110110
f.logger.Debug("failed to fetch block result from Tendermint", "height", resBlock.Block.Height, "error", err.Error())
111-
return nil, nil
111+
return nil, err
112112
}
113113

114114
bloom, err := f.backend.BlockBloom(blockRes)

tests/integration/precompiles/p256/test_integration.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ func TestPrecompileIntegrationTestSuite(t *testing.T, create network.CreateEvmAp
110110
copy(input[0:32], hash)
111111

112112
// ALWAYS left-pad to 32 bytes:
113-
copy(input[32:64], common.LeftPadBytes(rInt.Bytes(), 32))
114-
copy(input[64:96], common.LeftPadBytes(sInt.Bytes(), 32))
115-
copy(input[96:128], common.LeftPadBytes(pub.X.Bytes(), 32))
116-
copy(input[128:160], common.LeftPadBytes(pub.Y.Bytes(), 32))
113+
rInt.FillBytes(input[32:64])
114+
sInt.FillBytes(input[64:96])
115+
pub.X.FillBytes(input[96:128])
116+
pub.Y.FillBytes(input[128:160])
117117
return input, nil, ""
118118
},
119119
),
@@ -170,11 +170,10 @@ func TestPrecompileIntegrationTestSuite(t *testing.T, create network.CreateEvmAp
170170

171171
input = make([]byte, p256.VerifyInputLength)
172172
copy(input[0:32], hash)
173-
// ALWAYS left-pad to 32 bytes:
174-
copy(input[32:64], common.LeftPadBytes(rInt.Bytes(), 32))
175-
copy(input[64:96], common.LeftPadBytes(sInt.Bytes(), 32))
176-
copy(input[96:128], common.LeftPadBytes(privB.PublicKey.X.Bytes(), 32))
177-
copy(input[128:160], common.LeftPadBytes(privB.PublicKey.Y.Bytes(), 32))
173+
rInt.FillBytes(input[32:64])
174+
sInt.FillBytes(input[64:96])
175+
privB.PublicKey.X.FillBytes(input[96:128])
176+
privB.PublicKey.Y.FillBytes(input[128:160])
178177
return input
179178
},
180179
),

tests/integration/precompiles/p256/test_p256.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/ecdsa"
55
"crypto/elliptic"
66
"crypto/rand"
7+
"math/big"
78

89
"github.com/ethereum/go-ethereum/common"
910
"github.com/ethereum/go-ethereum/core/vm"
@@ -41,7 +42,6 @@ func (s *PrecompileTestSuite) TestRun() {
4142

4243
input := make([]byte, p256.VerifyInputLength)
4344
copy(input[0:32], hash)
44-
4545
rInt.FillBytes(input[32:64])
4646
sInt.FillBytes(input[64:96])
4747
s.p256Priv.X.FillBytes(input[96:128])
@@ -61,13 +61,13 @@ func (s *PrecompileTestSuite) TestRun() {
6161
s.Require().NoError(err)
6262

6363
rBz, sBz, err := parseSignature(sig)
64+
rInt, sInt := new(big.Int).SetBytes(rBz), new(big.Int).SetBytes(sBz)
6465
s.Require().NoError(err)
6566

6667
input := make([]byte, p256.VerifyInputLength)
6768
copy(input[0:32], hash)
68-
// ALWAYS left-pad to 32 bytes:
69-
copy(input[32:64], common.LeftPadBytes(rBz, 32))
70-
copy(input[64:96], common.LeftPadBytes(sBz, 32))
69+
rInt.FillBytes(input[32:64])
70+
sInt.FillBytes(input[64:96])
7171
s.p256Priv.X.FillBytes(input[96:128])
7272
s.p256Priv.Y.FillBytes(input[128:160])
7373

@@ -92,7 +92,6 @@ func (s *PrecompileTestSuite) TestRun() {
9292

9393
input := make([]byte, p256.VerifyInputLength)
9494
copy(input[0:32], hash)
95-
9695
rInt.FillBytes(input[32:64])
9796
sInt.FillBytes(input[64:96])
9897
privB.X.FillBytes(input[96:128])

tests/integration/precompiles/p256/test_setup.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ func signMsg(msg []byte, priv *ecdsa.PrivateKey) ([]byte, error) {
4848

4949
input := make([]byte, p256.VerifyInputLength)
5050
copy(input[0:32], hash)
51-
copy(input[32:64], rInt.Bytes())
52-
copy(input[64:96], sInt.Bytes())
53-
copy(input[96:128], priv.X.Bytes())
54-
copy(input[128:160], priv.Y.Bytes())
51+
rInt.FillBytes(input[32:64])
52+
sInt.FillBytes(input[64:96])
53+
priv.X.FillBytes(input[96:128])
54+
priv.Y.FillBytes(input[128:160])
5555

5656
return input, nil
5757
}

tests/integration/rpc/backend/test_blocks.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (s *TestSuite) TestGetBlockByHash() {
262262
false,
263263
},
264264
{
265-
"noop - tendermint blockres not found",
265+
"fail - tendermint blockres not found",
266266
common.BytesToHash(block.Hash()),
267267
true,
268268
math.NewInt(1).BigInt(),
@@ -273,8 +273,8 @@ func (s *TestSuite) TestGetBlockByHash() {
273273
client := s.backend.ClientCtx.Client.(*mocks.Client)
274274
RegisterBlockByHashNotFound(client, hash, txBz)
275275
},
276-
true,
277-
true,
276+
false,
277+
false,
278278
},
279279
{
280280
"noop - tendermint failed to fetch block result",

0 commit comments

Comments
 (0)