core: add FaultException to AppExecResult

This commit is contained in:
Anna Shaleva 2020-10-05 17:04:17 +03:00
parent 9a493dd2a0
commit 0f71088246
3 changed files with 69 additions and 33 deletions

View file

@ -584,6 +584,7 @@ func (bc *Blockchain) storeBlock(block *block.Block, txpool *mempool.Pool) error
v.GasLimit = tx.SystemFee
err := v.Run()
var faultException string
if !v.HasFailed() {
_, err := systemInterop.DAO.Persist()
if err != nil {
@ -597,14 +598,16 @@ func (bc *Blockchain) storeBlock(block *block.Block, txpool *mempool.Pool) error
zap.String("tx", tx.Hash().StringLE()),
zap.Uint32("block", block.Index),
zap.Error(err))
faultException = err.Error()
}
aer := &state.AppExecResult{
TxHash: tx.Hash(),
Trigger: trigger.Application,
VMState: v.State(),
GasConsumed: v.GasConsumed(),
Stack: v.Estack().ToArray(),
Events: systemInterop.Notifications,
TxHash: tx.Hash(),
Trigger: trigger.Application,
VMState: v.State(),
GasConsumed: v.GasConsumed(),
Stack: v.Estack().ToArray(),
Events: systemInterop.Notifications,
FaultException: faultException,
}
appExecResults = append(appExecResults, aer)
err = cache.PutAppExecResult(aer, writeBuf)

View file

@ -23,12 +23,13 @@ type NotificationEvent struct {
// AppExecResult represent the result of the script execution, gathering together
// all resulting notifications, state, stack and other metadata.
type AppExecResult struct {
TxHash util.Uint256
Trigger trigger.Type
VMState vm.State
GasConsumed int64
Stack []stackitem.Item
Events []NotificationEvent
TxHash util.Uint256
Trigger trigger.Type
VMState vm.State
GasConsumed int64
Stack []stackitem.Item
Events []NotificationEvent
FaultException string
}
// EncodeBinary implements the Serializable interface.
@ -62,6 +63,7 @@ func (aer *AppExecResult) EncodeBinary(w *io.BinWriter) {
w.WriteU64LE(uint64(aer.GasConsumed))
stackitem.EncodeBinaryStackItem(stackitem.NewArray(aer.Stack), w)
w.WriteArray(aer.Events)
w.WriteVarBytes([]byte(aer.FaultException))
}
// DecodeBinary implements the Serializable interface.
@ -80,6 +82,7 @@ func (aer *AppExecResult) DecodeBinary(r *io.BinReader) {
aer.Stack = arr
}
r.ReadArray(&aer.Events)
aer.FaultException = r.ReadString()
}
// notificationEventAux is an auxiliary struct for NotificationEvent JSON marshalling.
@ -123,12 +126,13 @@ func (ne *NotificationEvent) UnmarshalJSON(data []byte) error {
// appExecResultAux is an auxiliary struct for JSON marshalling
type appExecResultAux struct {
TxHash *util.Uint256 `json:"txid"`
Trigger string `json:"trigger"`
VMState string `json:"vmstate"`
GasConsumed int64 `json:"gasconsumed,string"`
Stack json.RawMessage `json:"stack"`
Events []NotificationEvent `json:"notifications"`
TxHash *util.Uint256 `json:"txid"`
Trigger string `json:"trigger"`
VMState string `json:"vmstate"`
GasConsumed int64 `json:"gasconsumed,string"`
Stack json.RawMessage `json:"stack"`
Events []NotificationEvent `json:"notifications"`
FaultException string `json:"exception,omitempty"`
}
// MarshalJSON implements implements json.Marshaler interface.
@ -158,12 +162,13 @@ func (aer *AppExecResult) MarshalJSON() ([]byte, error) {
hash = &aer.TxHash
}
return json.Marshal(&appExecResultAux{
TxHash: hash,
Trigger: aer.Trigger.String(),
VMState: aer.VMState.String(),
GasConsumed: aer.GasConsumed,
Stack: st,
Events: aer.Events,
TxHash: hash,
Trigger: aer.Trigger.String(),
VMState: aer.VMState.String(),
GasConsumed: aer.GasConsumed,
Stack: st,
Events: aer.Events,
FaultException: aer.FaultException,
})
}
@ -202,6 +207,7 @@ func (aer *AppExecResult) UnmarshalJSON(data []byte) error {
aer.VMState = state
aer.Events = aux.Events
aer.GasConsumed = aux.GasConsumed
aer.FaultException = aux.FaultException
return nil
}

View file

@ -24,16 +24,31 @@ func TestEncodeDecodeNotificationEvent(t *testing.T) {
}
func TestEncodeDecodeAppExecResult(t *testing.T) {
appExecResult := &AppExecResult{
TxHash: random.Uint256(),
Trigger: 1,
VMState: vm.HaltState,
GasConsumed: 10,
Stack: []stackitem.Item{},
Events: []NotificationEvent{},
}
t.Run("halt", func(t *testing.T) {
appExecResult := &AppExecResult{
TxHash: random.Uint256(),
Trigger: 1,
VMState: vm.HaltState,
GasConsumed: 10,
Stack: []stackitem.Item{},
Events: []NotificationEvent{},
}
testserdes.EncodeDecodeBinary(t, appExecResult, new(AppExecResult))
testserdes.EncodeDecodeBinary(t, appExecResult, new(AppExecResult))
})
t.Run("fault", func(t *testing.T) {
appExecResult := &AppExecResult{
TxHash: random.Uint256(),
Trigger: 1,
VMState: vm.FaultState,
GasConsumed: 10,
Stack: []stackitem.Item{},
Events: []NotificationEvent{},
FaultException: "unhandled error",
}
testserdes.EncodeDecodeBinary(t, appExecResult, new(AppExecResult))
})
}
func TestMarshalUnmarshalJSONNotificationEvent(t *testing.T) {
@ -86,6 +101,18 @@ func TestMarshalUnmarshalJSONAppExecResult(t *testing.T) {
testserdes.MarshalUnmarshalJSON(t, appExecResult, new(AppExecResult))
})
t.Run("positive, fault state", func(t *testing.T) {
appExecResult := &AppExecResult{
TxHash: random.Uint256(),
Trigger: trigger.Application,
VMState: vm.FaultState,
GasConsumed: 10,
Stack: []stackitem.Item{},
Events: []NotificationEvent{},
FaultException: "unhandled exception",
}
testserdes.MarshalUnmarshalJSON(t, appExecResult, new(AppExecResult))
})
t.Run("positive, block", func(t *testing.T) {
appExecResult := &AppExecResult{
TxHash: random.Uint256(),