Skip to content

Commit

Permalink
Fix error resolution in publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
lmoe committed Feb 26, 2024
1 parent 05966fd commit 08cd7d8
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ go.work
go.work.sum
TMP*
tmp.*
sdk/
sdk/
libiota_sdk.so
libiota_sdk.dylib
iota_sdk.dll
4 changes: 3 additions & 1 deletion packages/chain/chain_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/iotaledger/wasp/packages/chain/mempool"
"github.com/iotaledger/wasp/packages/cryptolib"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/kv"
"github.com/iotaledger/wasp/packages/state"
)

Expand All @@ -30,6 +31,7 @@ func NewEmptyChainListener() ChainListener {
return &emptyChainListener{}
}

func (ecl *emptyChainListener) BlockApplied(chainID isc.ChainID, block state.Block) {}
func (ecl *emptyChainListener) BlockApplied(chainID isc.ChainID, block state.Block, latestState kv.KVStoreReader) {
}
func (ecl *emptyChainListener) AccessNodesUpdated(isc.ChainID, []*cryptolib.PublicKey) {}
func (ecl *emptyChainListener) ServerNodesUpdated(isc.ChainID, []*cryptolib.PublicKey) {}
5 changes: 3 additions & 2 deletions packages/chain/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
"github.com/iotaledger/wasp/packages/cryptolib"
"github.com/iotaledger/wasp/packages/gpa"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/kv"
"github.com/iotaledger/wasp/packages/metrics"
"github.com/iotaledger/wasp/packages/peering"
"github.com/iotaledger/wasp/packages/state"
Expand Down Expand Up @@ -83,7 +84,7 @@ type ChainListener interface {
// This function is called by the chain when new block is applied to the
// state. This block might be not confirmed yet, but the chain is going
// to build the next block on top of this one.
BlockApplied(chainID isc.ChainID, block state.Block)
BlockApplied(chainID isc.ChainID, block state.Block, latestState kv.KVStoreReader)
}

type Mempool interface {
Expand Down Expand Up @@ -822,7 +823,7 @@ func (mpi *mempoolImpl) handleTrackNewChainHead(req *reqTrackNewChainHead) {
panic(fmt.Errorf("cannot extract receipts from block: %w", err))
}
mpi.metrics.IncBlocksPerChain()
mpi.listener.BlockApplied(mpi.chainID, block)
mpi.listener.BlockApplied(mpi.chainID, block, mpi.chainHeadState)
for _, receipt := range blockReceipts {
mpi.metrics.IncRequestsProcessed()
mpi.tryRemoveRequest(receipt.Request)
Expand Down
5 changes: 3 additions & 2 deletions packages/chains/chains_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/iotaledger/wasp/packages/chain"
"github.com/iotaledger/wasp/packages/cryptolib"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/kv"
"github.com/iotaledger/wasp/packages/state"
)

Expand All @@ -19,8 +20,8 @@ func NewChainsListener(parent chain.ChainListener, accessNodesCB func(chainID is
return &chainsListener{parent: parent, accessNodesCB: accessNodesCB}
}

func (cl *chainsListener) BlockApplied(chainID isc.ChainID, block state.Block) {
cl.parent.BlockApplied(chainID, block)
func (cl *chainsListener) BlockApplied(chainID isc.ChainID, block state.Block, latestState kv.KVStoreReader) {
cl.parent.BlockApplied(chainID, block, latestState)
}

func (cl *chainsListener) AccessNodesUpdated(chainID isc.ChainID, accessNodes []*cryptolib.PublicKey) {
Expand Down
4 changes: 3 additions & 1 deletion packages/publisher/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ func PublishBlockEvents(blockApplied *blockApplied, events *Events, log *logger.
if err != nil {
log.Errorf("unable to get receipts from a block: %v", err)
} else {
errorStatePartition := subrealm.NewReadOnly(blockApplied.latestState, kv.Key(errors.Contract.Hname().Bytes()))

for index, receipt := range receipts {
vmError, resolveError := errors.ResolveFromState(blocklogStatePartition, receipt.Error)
vmError, resolveError := errors.ResolveFromState(errorStatePartition, receipt.Error)
if resolveError != nil {
log.Errorf("Could not parse vmerror of receipt [%v]: %v", receipt.Request.ID(), resolveError)
}
Expand Down
10 changes: 6 additions & 4 deletions packages/publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/iotaledger/wasp/packages/chain"
"github.com/iotaledger/wasp/packages/cryptolib"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/kv"
"github.com/iotaledger/wasp/packages/state"
"github.com/iotaledger/wasp/packages/util/pipe"
)
Expand All @@ -34,8 +35,9 @@ type Publisher struct {
var _ chain.ChainListener = &Publisher{}

type blockApplied struct {
chainID isc.ChainID
block state.Block
chainID isc.ChainID
block state.Block
latestState kv.KVStoreReader
}

func New(log *logger.Logger) *Publisher {
Expand All @@ -56,8 +58,8 @@ func New(log *logger.Logger) *Publisher {

// Implements the chain.ChainListener interface.
// NOTE: Do not block the caller!
func (p *Publisher) BlockApplied(chainID isc.ChainID, block state.Block) {
p.blockAppliedPipe.In() <- &blockApplied{chainID: chainID, block: block}
func (p *Publisher) BlockApplied(chainID isc.ChainID, block state.Block, latestState kv.KVStoreReader) {
p.blockAppliedPipe.In() <- &blockApplied{chainID: chainID, block: block, latestState: latestState}
}

// Implements the chain.ChainListener interface.
Expand Down
6 changes: 5 additions & 1 deletion packages/solo/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.uber.org/zap"

iotago "github.com/iotaledger/iota.go/v3"
"github.com/iotaledger/wasp/packages/chain"
"github.com/iotaledger/wasp/packages/hashing"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/isc/rotate"
Expand Down Expand Up @@ -131,7 +132,10 @@ func (ch *Chain) settleStateTransition(stateTx *iotago.Transaction, stateDraft s
if err != nil {
panic(err)
}
ch.Env.Publisher().BlockApplied(ch.ChainID, block)

latestState, _ := ch.LatestState(chain.ActiveOrCommittedState)

ch.Env.Publisher().BlockApplied(ch.ChainID, block, latestState)

blockReceipts, err := blocklog.RequestReceiptsFromBlock(block)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions packages/vm/core/testcore/sbtests/misc_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/solo"
"github.com/iotaledger/wasp/packages/vm/core/testcore/sbtests/sbtestsc"
)
Expand Down Expand Up @@ -45,3 +46,17 @@ func testSandboxCall(t *testing.T, w bool) {
require.NoError(t, err)
require.NotNil(t, ret)
}

func TestCustomError(t *testing.T) { run2(t, testCustomError) }
func testCustomError(t *testing.T, w bool) {
_, chain := setupChain(t, nil)
setupTestSandboxSC(t, chain, nil, w)

req := solo.NewCallParams(ScName, sbtestsc.FuncTestCustomError.Name).
WithGasBudget(100_000)
ret, err := chain.PostRequestSync(req, nil)

require.Error(t, err)
require.IsType(t, &isc.VMError{}, err)
require.Nil(t, ret)
}
8 changes: 8 additions & 0 deletions packages/vm/core/testcore/sbtests/sbtestsc/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import (
"github.com/iotaledger/wasp/packages/vm/core/governance"
)

var testError *isc.VMErrorTemplate

func initialize(ctx isc.Sandbox) dict.Dict {
p := ctx.Params().Get(ParamFail)
ctx.Requiref(p == nil, "failing on purpose")
testError = ctx.RegisterError("ERROR_TEST")
return nil
}

Expand Down Expand Up @@ -52,6 +55,11 @@ func testPanicFullEP(ctx isc.Sandbox) dict.Dict {
return nil
}

func testCustomError(_ isc.Sandbox) dict.Dict {
panic(testError.Create("CUSTOM_ERROR"))
return nil

Check failure on line 60 in packages/vm/core/testcore/sbtests/sbtestsc/impl.go

View workflow job for this annotation

GitHub Actions / Lint

unreachable: unreachable code (govet)
}

func testPanicViewEP(ctx isc.SandboxView) dict.Dict {
ctx.Log().Panicf(MsgViewPanic)
return nil
Expand Down
2 changes: 2 additions & 0 deletions packages/vm/core/testcore/sbtests/sbtestsc/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var Processor = Contract.Processor(initialize,
FuncEventLogDeploy.WithHandler(testEventLogDeploy),
FuncSandboxCall.WithHandler(testSandboxCall),

FuncTestCustomError.WithHandler(testCustomError),
FuncPanicFullEP.WithHandler(testPanicFullEP),
FuncPanicViewEP.WithHandler(testPanicViewEP),
FuncCallPanicFullEP.WithHandler(testCallPanicFullEP),
Expand Down Expand Up @@ -71,6 +72,7 @@ var (
FuncCheckContextFromFullEP = coreutil.Func("checkContextFromFullEP")
FuncCheckContextFromViewEP = coreutil.ViewFunc("checkContextFromViewEP")

FuncTestCustomError = coreutil.Func("testCustomError")
FuncPanicFullEP = coreutil.Func("testPanicFullEP")
FuncPanicViewEP = coreutil.ViewFunc("testPanicViewEP")
FuncCallPanicFullEP = coreutil.Func("testCallPanicFullEP")
Expand Down

0 comments on commit 08cd7d8

Please sign in to comment.