Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
virajbhartiya committed Oct 30, 2024
1 parent ae5a635 commit 2387b18
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 5 deletions.
34 changes: 29 additions & 5 deletions api/api_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var (
_ error = (*ErrExecutionReverted)(nil)
_ jsonrpc.RPCErrorCodec = (*ErrExecutionReverted)(nil)
_ error = (*ErrNullRound)(nil)
_ jsonrpc.RPCErrorCodec = (*ErrNullRound)(nil)
)

func init() {
Expand Down Expand Up @@ -166,17 +167,40 @@ func NewErrExecutionReverted(reason string) *ErrExecutionReverted {
}

type ErrNullRound struct {
Epoch int64
Epoch int64
Message string
}

func NewErrNullRound(epoch int64) *ErrNullRound {
return &ErrNullRound{Epoch: epoch}
return &ErrNullRound{
Epoch: epoch,
Message: fmt.Sprintf("requested epoch was a null round (%d)", epoch),
}
}

func (e *ErrNullRound) Error() string {
return fmt.Sprintf("requested epoch was a null round (%d)", e.Epoch)
return e.Message
}

func init() {
RPCErrors.Register(ENullRound, new(*ErrNullRound))
func (e *ErrNullRound) FromJSONRPCError(jerr jsonrpc.JSONRPCError) error {
if jerr.Code != ENullRound {
return fmt.Errorf("unexpected error code: %d", jerr.Code)
}

epoch, ok := jerr.Data.(float64)
if !ok {
return fmt.Errorf("expected number data in null round error, got %T", jerr.Data)
}

e.Epoch = int64(epoch)
e.Message = jerr.Message
return nil
}

func (e *ErrNullRound) ToJSONRPCError() (jsonrpc.JSONRPCError, error) {
return jsonrpc.JSONRPCError{
Code: ENullRound,
Message: e.Message,
Data: e.Epoch,
}, nil
}
98 changes: 98 additions & 0 deletions itests/fevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -1659,3 +1660,100 @@ func TestEthEstimateGas(t *testing.T) {
})
}
}

func TestEthNullRoundHandling(t *testing.T) {
blockTime := 100 * time.Millisecond
client, _, ens := kit.EnsembleMinimal(t, kit.MockProofs(), kit.ThroughRPC())

bms := ens.InterconnectAll().BeginMining(blockTime)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

bms[0].InjectNulls(10)

tctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
ch, err := client.ChainNotify(tctx)
require.NoError(t, err)
<-ch
hc := <-ch
require.Equal(t, store.HCApply, hc[0].Type)

afterNullHeight := hc[0].Val.Height()

nullHeight := afterNullHeight - 1
for nullHeight > 0 {
ts, err := client.ChainGetTipSetByHeight(ctx, nullHeight, types.EmptyTSK)
require.NoError(t, err)
if ts.Height() == nullHeight {
nullHeight--
} else {
break
}
}

nullBlockHex := fmt.Sprintf("0x%x", nullHeight)

testCases := []struct {
name string
testFunc func() error
}{
{
name: "EthGetBlockByNumber",
testFunc: func() error {
_, err := client.EthGetBlockByNumber(ctx, nullBlockHex, true)
if err == nil {
return errors.New("expected error for null round")
}
return err
},
},
{
name: "EthFeeHistory",
testFunc: func() error {
params, err := json.Marshal([]interface{}{1, nullBlockHex})
if err != nil {
return err
}
_, err = client.EthFeeHistory(ctx, params)
return err
},
},
{
name: "EthTraceBlock",
testFunc: func() error {
_, err := client.EthTraceBlock(ctx, nullBlockHex)
return err
},
},
{
name: "EthTraceReplayBlockTransactions",
testFunc: func() error {
_, err := client.EthTraceReplayBlockTransactions(ctx, nullBlockHex, []string{"trace"})
return err
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.testFunc()
t.Logf("error: %v", err)
require.Error(t, err)

// Test errors.Is
require.True(t, errors.Is(err, new(api.ErrNullRound)),
"error should be or wrap ErrNullRound")

// Test errors.As and verify message
var nullRoundErr *api.ErrNullRound
require.True(t, errors.As(err, &nullRoundErr),
"error should be convertible to ErrNullRound")

expectedMsg := fmt.Sprintf("block number %s was a null round", nullBlockHex)
require.Equal(t, expectedMsg, nullRoundErr.Error())
require.Equal(t, nullHeight, nullRoundErr.Epoch)
})
}
}

0 comments on commit 2387b18

Please sign in to comment.