Merge pull request #1413 from nspcc-dev/core/block_aer_marshalling

core: do not marshal block hash in application log
This commit is contained in:
Roman Khimov 2020-09-21 14:22:17 +03:00 committed by GitHub
commit dbfdfd8e9b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 10 deletions

View file

@ -883,7 +883,7 @@ func (bc *Blockchain) GetTransaction(hash util.Uint256) (*transaction.Transactio
} }
// GetAppExecResult returns application execution result by the given // GetAppExecResult returns application execution result by the given
// tx hash. // tx hash or block hash.
func (bc *Blockchain) GetAppExecResult(hash util.Uint256) (*state.AppExecResult, error) { func (bc *Blockchain) GetAppExecResult(hash util.Uint256) (*state.AppExecResult, error) {
return bc.dao.GetAppExecResult(hash) return bc.dao.GetAppExecResult(hash)
} }

View file

@ -123,7 +123,7 @@ func (ne *NotificationEvent) UnmarshalJSON(data []byte) error {
// appExecResultAux is an auxiliary struct for JSON marshalling // appExecResultAux is an auxiliary struct for JSON marshalling
type appExecResultAux struct { type appExecResultAux struct {
TxHash util.Uint256 `json:"txid"` TxHash *util.Uint256 `json:"txid"`
Trigger string `json:"trigger"` Trigger string `json:"trigger"`
VMState string `json:"vmstate"` VMState string `json:"vmstate"`
GasConsumed int64 `json:"gasconsumed,string"` GasConsumed int64 `json:"gasconsumed,string"`
@ -151,8 +151,14 @@ func (aer *AppExecResult) MarshalJSON() ([]byte, error) {
return nil, err return nil, err
} }
} }
// do not marshal block hash
var hash *util.Uint256
if aer.Trigger == trigger.Application {
hash = &aer.TxHash
}
return json.Marshal(&appExecResultAux{ return json.Marshal(&appExecResultAux{
TxHash: aer.TxHash, TxHash: hash,
Trigger: aer.Trigger.String(), Trigger: aer.Trigger.String(),
VMState: aer.VMState.String(), VMState: aer.VMState.String(),
GasConsumed: aer.GasConsumed, GasConsumed: aer.GasConsumed,
@ -186,7 +192,9 @@ func (aer *AppExecResult) UnmarshalJSON(data []byte) error {
return err return err
} }
aer.Trigger = trigger aer.Trigger = trigger
aer.TxHash = aux.TxHash if aux.TxHash != nil {
aer.TxHash = *aux.TxHash
}
state, err := vm.StateFromString(aux.VMState) state, err := vm.StateFromString(aux.VMState)
if err != nil { if err != nil {
return err return err

View file

@ -6,6 +6,8 @@ import (
"github.com/nspcc-dev/neo-go/pkg/internal/random" "github.com/nspcc-dev/neo-go/pkg/internal/random"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -72,10 +74,10 @@ func TestMarshalUnmarshalJSONNotificationEvent(t *testing.T) {
} }
func TestMarshalUnmarshalJSONAppExecResult(t *testing.T) { func TestMarshalUnmarshalJSONAppExecResult(t *testing.T) {
t.Run("positive", func(t *testing.T) { t.Run("positive, transaction", func(t *testing.T) {
appExecResult := &AppExecResult{ appExecResult := &AppExecResult{
TxHash: random.Uint256(), TxHash: random.Uint256(),
Trigger: 1, Trigger: trigger.Application,
VMState: vm.HaltState, VMState: vm.HaltState,
GasConsumed: 10, GasConsumed: 10,
Stack: []stackitem.Item{}, Stack: []stackitem.Item{},
@ -84,6 +86,31 @@ func TestMarshalUnmarshalJSONAppExecResult(t *testing.T) {
testserdes.MarshalUnmarshalJSON(t, appExecResult, new(AppExecResult)) testserdes.MarshalUnmarshalJSON(t, appExecResult, new(AppExecResult))
}) })
t.Run("positive, block", func(t *testing.T) {
appExecResult := &AppExecResult{
TxHash: random.Uint256(),
Trigger: trigger.System,
VMState: vm.HaltState,
GasConsumed: 10,
Stack: []stackitem.Item{},
Events: []NotificationEvent{},
}
data, err := json.Marshal(appExecResult)
require.NoError(t, err)
actual := new(AppExecResult)
require.NoError(t, json.Unmarshal(data, actual))
expected := &AppExecResult{
// we have no way to restore block hash as it was not marshalled
TxHash: util.Uint256{},
Trigger: appExecResult.Trigger,
VMState: appExecResult.VMState,
GasConsumed: appExecResult.GasConsumed,
Stack: appExecResult.Stack,
Events: appExecResult.Events,
}
require.Equal(t, expected, actual)
})
t.Run("MarshalJSON recursive reference", func(t *testing.T) { t.Run("MarshalJSON recursive reference", func(t *testing.T) {
i := make([]stackitem.Item, 1) i := make([]stackitem.Item, 1)
recursive := stackitem.NewArray(i) recursive := stackitem.NewArray(i)

View file

@ -500,16 +500,16 @@ func (s *Server) validateAddress(reqParams request.Params) (interface{}, *respon
return validateAddress(param.Value), nil return validateAddress(param.Value), nil
} }
// getApplicationLog returns the contract log based on the specified txid. // getApplicationLog returns the contract log based on the specified txid or blockid.
func (s *Server) getApplicationLog(reqParams request.Params) (interface{}, *response.Error) { func (s *Server) getApplicationLog(reqParams request.Params) (interface{}, *response.Error) {
txHash, err := reqParams.Value(0).GetUint256() hash, err := reqParams.Value(0).GetUint256()
if err != nil { if err != nil {
return nil, response.ErrInvalidParams return nil, response.ErrInvalidParams
} }
appExecResult, err := s.chain.GetAppExecResult(txHash) appExecResult, err := s.chain.GetAppExecResult(hash)
if err != nil { if err != nil {
return nil, response.NewRPCError("Unknown transaction", "", err) return nil, response.NewRPCError("Unknown transaction or block", "", err)
} }
return appExecResult, nil return appExecResult, nil