Skip to content

Commit

Permalink
feat!: extract BatchReturn to batch package, add methods & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jul 3, 2024
1 parent 6509812 commit 5025381
Show file tree
Hide file tree
Showing 32 changed files with 566 additions and 2,426 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tidy:

gen:
$(GO_BIN) run ./gen/gen.go
$(GO_BIN) run ./batch/gen/gen.go
$(GO_BIN) run ./builtin/v8/gen/gen.go
$(GO_BIN) run ./builtin/v9/gen/gen.go
$(GO_BIN) run ./builtin/v10/gen/gen.go
Expand Down
48 changes: 48 additions & 0 deletions batch/batch_return.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package batch

import "github.com/filecoin-project/go-state-types/exitcode"

type BatchReturn struct {
SuccessCount uint64
FailCodes []FailCode
}

type FailCode struct {
Idx uint64
Code exitcode.ExitCode
}

func (b BatchReturn) Size() int {
return int(b.SuccessCount) + len(b.FailCodes)
}

func (b BatchReturn) AllOk() bool {
return len(b.FailCodes) == 0
}

func (b BatchReturn) Codes() []exitcode.ExitCode {
codes := make([]exitcode.ExitCode, b.Size())
i := 0
for _, fc := range b.FailCodes {
if fc.Idx > uint64(i) {
for ; i < int(fc.Idx); i++ {
codes[i] = exitcode.Ok
}
}
codes[i] = fc.Code
i++
}
for ; i < len(codes); i++ {
codes[i] = exitcode.Ok
}
return codes
}

func (b BatchReturn) CodeAt(n uint64) exitcode.ExitCode {
for _, fc := range b.FailCodes {
if fc.Idx == n {
return fc.Code
}
}
return exitcode.Ok
}
105 changes: 105 additions & 0 deletions batch/batch_return_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package batch

import (
"bytes"
"encoding/hex"
"testing"

"github.com/filecoin-project/go-state-types/exitcode"
"github.com/stretchr/testify/require"
)

// Tests to match with Rust fil_actors_runtime::serialization
func TestSerializationBatchReturn(t *testing.T) {
testCases := []struct {
params BatchReturn
hex string
}{
{
params: BatchReturn{},
// [0,[]]
hex: "820080",
},
{
params: BatchReturn{SuccessCount: 1},
// [1,[]]
hex: "820180",
},
{
params: BatchReturn{FailCodes: []FailCode{{Idx: 0, Code: exitcode.ErrIllegalArgument}}},
// [0,[[0,16]]]
hex: "820081820010",
},
{
params: BatchReturn{SuccessCount: 2, FailCodes: []FailCode{
{Idx: 1, Code: exitcode.SysErrOutOfGas},
{Idx: 2, Code: exitcode.ErrIllegalState},
{Idx: 4, Code: exitcode.ErrIllegalArgument},
}},
// [2,[[1,7],[2,20],[4,16]]]
hex: "820283820107820214820410",
},
}

for _, tc := range testCases {
t.Run("", func(t *testing.T) {
req := require.New(t)

var buf bytes.Buffer
req.NoError(tc.params.MarshalCBOR(&buf))
req.Equal(tc.hex, hex.EncodeToString(buf.Bytes()))
var rt BatchReturn
req.NoError(rt.UnmarshalCBOR(&buf))
req.Equal(tc.params, rt)
})
}
}

func TestBatchReturn(t *testing.T) {
req := require.New(t)

br := BatchReturn{}
req.Equal(0, br.Size())
req.True(br.AllOk())
req.Equal([]exitcode.ExitCode{}, br.Codes())

br = BatchReturn{SuccessCount: 1}
req.Equal(1, br.Size())
req.True(br.AllOk())
req.Equal([]exitcode.ExitCode{exitcode.Ok}, br.Codes())
req.Equal(exitcode.Ok, br.CodeAt(0))

br = BatchReturn{FailCodes: []FailCode{{Idx: 0, Code: exitcode.ErrIllegalArgument}}}
req.Equal(1, br.Size())
req.False(br.AllOk())
req.Equal([]exitcode.ExitCode{exitcode.ErrIllegalArgument}, br.Codes())
req.Equal(exitcode.ErrIllegalArgument, br.CodeAt(0))

br = BatchReturn{SuccessCount: 1, FailCodes: []FailCode{{Idx: 1, Code: exitcode.ErrIllegalArgument}}}
req.Equal(2, br.Size())
req.False(br.AllOk())
req.Equal([]exitcode.ExitCode{exitcode.Ok, exitcode.ErrIllegalArgument}, br.Codes())
req.Equal(exitcode.Ok, br.CodeAt(0))
req.Equal(exitcode.ErrIllegalArgument, br.CodeAt(1))

br = BatchReturn{SuccessCount: 1, FailCodes: []FailCode{{Idx: 0, Code: exitcode.ErrForbidden}}}
req.Equal(2, br.Size())
req.False(br.AllOk())
req.Equal([]exitcode.ExitCode{exitcode.ErrForbidden, exitcode.Ok}, br.Codes())
req.Equal(exitcode.ErrForbidden, br.CodeAt(0))
req.Equal(exitcode.Ok, br.CodeAt(1))

br = BatchReturn{SuccessCount: 2, FailCodes: []FailCode{
{Idx: 1, Code: exitcode.SysErrOutOfGas},
{Idx: 2, Code: exitcode.ErrIllegalState},
{Idx: 4, Code: exitcode.ErrIllegalArgument},
}}
req.Equal(5, br.Size())
req.False(br.AllOk())
req.Equal([]exitcode.ExitCode{exitcode.Ok, exitcode.SysErrOutOfGas, exitcode.ErrIllegalState, exitcode.Ok, exitcode.ErrIllegalArgument}, br.Codes())
req.Equal(exitcode.Ok, br.CodeAt(0))
req.Equal(exitcode.SysErrOutOfGas, br.CodeAt(1))
req.Equal(exitcode.ErrIllegalState, br.CodeAt(2))
req.Equal(exitcode.Ok, br.CodeAt(3))
req.Equal(exitcode.ErrIllegalArgument, br.CodeAt(4))
}
234 changes: 234 additions & 0 deletions batch/cbor_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5025381

Please sign in to comment.