2019-11-13 13:55:20 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NotificationEvent is a tuple of scripthash that emitted the StackItem as a
|
|
|
|
// notification and that item itself.
|
|
|
|
type NotificationEvent struct {
|
|
|
|
ScriptHash util.Uint160
|
|
|
|
Item vm.StackItem
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 byte
|
|
|
|
VMState string
|
|
|
|
GasConsumed util.Fixed8
|
|
|
|
Stack string // JSON
|
|
|
|
Events []NotificationEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
// putAppExecResultIntoStore puts given application execution result into the
|
|
|
|
// given store.
|
|
|
|
func putAppExecResultIntoStore(s storage.Store, aer *AppExecResult) error {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
aer.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return buf.Err
|
|
|
|
}
|
2019-11-27 09:23:18 +00:00
|
|
|
key := storage.AppendPrefix(storage.STNotification, aer.TxHash.BytesBE())
|
2019-11-13 13:55:20 +00:00
|
|
|
return s.Put(key, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAppExecResultFromStore gets application execution result from the
|
|
|
|
// given store.
|
|
|
|
func getAppExecResultFromStore(s storage.Store, hash util.Uint256) (*AppExecResult, error) {
|
|
|
|
aer := &AppExecResult{}
|
2019-11-27 09:23:18 +00:00
|
|
|
key := storage.AppendPrefix(storage.STNotification, hash.BytesBE())
|
2019-11-13 13:55:20 +00:00
|
|
|
if b, err := s.Get(key); err == nil {
|
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
aer.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, errors.Wrap(r.Err, "decoding failure:")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return aer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the Serializable interface.
|
core: implement EncodeBinary on value
Fixes panic:
panic: core.NotificationEvent does not have EncodeBinary(*BinWriter)
goroutine 97 [running]:
github.com/CityOfZion/neo-go/pkg/io.(*BinWriter).WriteArray(0xc004f7dda0, 0xabf820, 0xc004f7ddc0)
/home/rik/dev/neo-go/pkg/io/binaryWriter.go:45 +0x4b7
github.com/CityOfZion/neo-go/pkg/core.(*AppExecResult).EncodeBinary(0xc000635498, 0xc004f7dda0)
/home/rik/dev/neo-go/pkg/core/notification_event.go:73 +0x9c
github.com/CityOfZion/neo-go/pkg/core.putAppExecResultIntoStore(0xcf0020, 0xc001aafec0, 0xc000635498, 0xc004f81940, 0x40)
/home/rik/dev/neo-go/pkg/core/notification_event.go:33 +0xab
github.com/CityOfZion/neo-go/pkg/core.(*Blockchain).storeBlock(0xc000118000, 0xc006f89ea0, 0xc006f89ea0, 0x0)
/home/rik/dev/neo-go/pkg/core/blockchain.go:534 +0x2539
github.com/CityOfZion/neo-go/pkg/core.(*Blockchain).AddBlock(0xc000118000, 0xc006f89ea0, 0xc004f368b0, 0x1)
/home/rik/dev/neo-go/pkg/core/blockchain.go:256 +0x91
github.com/CityOfZion/neo-go/pkg/network.(*blockQueue).run(0xc0048f4b80)
/home/rik/dev/neo-go/pkg/network/blockqueue.go:39 +0x14c
created by github.com/CityOfZion/neo-go/pkg/network.(*Server).Start
/home/rik/dev/neo-go/pkg/network/server.go:133 +0x272
2019-11-15 18:54:02 +00:00
|
|
|
func (ne NotificationEvent) EncodeBinary(w *io.BinWriter) {
|
2019-12-06 15:22:21 +00:00
|
|
|
w.WriteBytes(ne.ScriptHash[:])
|
2019-11-13 13:55:20 +00:00
|
|
|
vm.EncodeBinaryStackItem(ne.Item, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements the Serializable interface.
|
|
|
|
func (ne *NotificationEvent) DecodeBinary(r *io.BinReader) {
|
2019-12-06 15:37:46 +00:00
|
|
|
r.ReadBytes(ne.ScriptHash[:])
|
2019-11-13 13:55:20 +00:00
|
|
|
ne.Item = vm.DecodeBinaryStackItem(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the Serializable interface.
|
|
|
|
func (aer *AppExecResult) EncodeBinary(w *io.BinWriter) {
|
2019-12-06 15:22:21 +00:00
|
|
|
w.WriteBytes(aer.TxHash[:])
|
2019-11-13 13:55:20 +00:00
|
|
|
w.WriteArray(aer.Events)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements the Serializable interface.
|
|
|
|
func (aer *AppExecResult) DecodeBinary(r *io.BinReader) {
|
2019-12-06 15:37:46 +00:00
|
|
|
r.ReadBytes(aer.TxHash[:])
|
2019-11-13 13:55:20 +00:00
|
|
|
r.ReadArray(&aer.Events)
|
|
|
|
}
|