Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: add invocations to applicationlog #3569

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/config/ledger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Ledger struct {
// SkipBlockVerification allows to disable verification of received
// blocks (including cryptographic checks).
SkipBlockVerification bool `yaml:"SkipBlockVerification"`
// SaveInvocations enables contract smart contract invocation data saving.
SaveInvocations bool `yaml:"SaveInvocations"`
}

// Blockchain is a set of settings for core.Blockchain to use, it includes protocol
Expand Down
1 change: 1 addition & 0 deletions pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,7 @@ func (bc *Blockchain) storeBlock(block *block.Block, txpool *mempool.Pool) error
Stack: v.Estack().ToArray(),
Events: systemInterop.Notifications,
FaultException: faultException,
Invocations: systemInterop.InvocationCalls,
},
}
appExecResults = append(appExecResults, aer)
Expand Down
21 changes: 19 additions & 2 deletions pkg/core/dao/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,25 @@ func TestStoreAsTransaction(t *testing.T) {
Container: hash,
Execution: state.Execution{
Trigger: trigger.Application,
Events: []state.NotificationEvent{},
Stack: []stackitem.Item{},
Events: []state.NotificationEvent{
{
ScriptHash: util.Uint160{},
Name: "fakeTransferEvent",
Item: stackitem.NewArray([]stackitem.Item{
stackitem.NewBool(false),
}),
},
},
Stack: []stackitem.Item{},
Invocations: []state.ContractInvocation{{
Hash: util.Uint160{},
Method: "fakeMethodCall",
Arguments: stackitem.NewArray([]stackitem.Item{
stackitem.NewBool(false),
}),
ArgumentsCount: 1,
Truncated: false,
}},
},
}
err := dao.StoreAsTransaction(tx, 0, aer)
Expand Down
31 changes: 17 additions & 14 deletions pkg/core/interop/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ type Context struct {
VM *vm.VM
Functions []Function
Invocations map[util.Uint160]int
InvocationCalls []state.ContractInvocation
cancelFuncs []context.CancelFunc
getContract func(*dao.Simple, util.Uint160) (*state.Contract, error)
baseExecFee int64
baseStorageFee int64
loadToken func(ic *Context, id int32) error
GetRandomCounter uint32
signers []transaction.Signer
SaveInvocations bool
}

// NewContext returns new interop context.
Expand All @@ -80,20 +82,21 @@ func NewContext(trigger trigger.Type, bc Ledger, d *dao.Simple, baseExecFee, bas
dao := d.GetPrivate()
cfg := bc.GetConfig().ProtocolConfiguration
return &Context{
Chain: bc,
Network: uint32(cfg.Magic),
Hardforks: cfg.Hardforks,
Natives: natives,
Trigger: trigger,
Block: block,
Tx: tx,
DAO: dao,
Log: log,
Invocations: make(map[util.Uint160]int),
getContract: getContract,
baseExecFee: baseExecFee,
baseStorageFee: baseStorageFee,
loadToken: loadTokenFunc,
Chain: bc,
Network: uint32(cfg.Magic),
Hardforks: cfg.Hardforks,
Natives: natives,
Trigger: trigger,
Block: block,
Tx: tx,
DAO: dao,
Log: log,
Invocations: make(map[util.Uint160]int),
getContract: getContract,
baseExecFee: baseExecFee,
baseStorageFee: baseStorageFee,
loadToken: loadTokenFunc,
SaveInvocations: bc.GetConfig().SaveInvocations,
}
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/core/interop/contract/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ func Call(ic *interop.Context) error {
return fmt.Errorf("method not found: %s/%d", method, len(args))
}
hasReturn := md.ReturnType != smartcontract.VoidType

if ic.SaveInvocations {
var (
arrCount = len(args)
truncated = false
argBytes []byte
)
if argBytes, err = ic.DAO.GetItemCtx().Serialize(stackitem.NewArray(args), false); err != nil {
truncated = true
}

ic.InvocationCalls = append(ic.InvocationCalls, state.ContractInvocation{
Hash: u,
Method: method,
ArgumentsBytes: argBytes,
ArgumentsCount: uint32(arrCount),
Truncated: truncated,
})
}
return callInternal(ic, cs, method, fs, hasReturn, args, true)
}

Expand Down
104 changes: 98 additions & 6 deletions pkg/core/state/notification_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,79 @@ type NotificationEvent struct {
Item *stackitem.Array `json:"state"`
}

// ContractInvocation contains method call information.
// The Arguments field will be nil if serialization of the arguments exceeds a predefined limit
// (for security reasons). In that case Truncated will be set to true.
type ContractInvocation struct {
Hash util.Uint160 `json:"contracthash"`
Method string `json:"method"`
Arguments *stackitem.Array `json:"arguments"`
ArgumentsBytes []byte
ArgumentsCount uint32 `json:"argumentscount"`
Truncated bool `json:"truncated"`
}

func (ci *ContractInvocation) DecodeBinary(r *io.BinReader) {
ci.Hash.DecodeBinary(r)
ci.Method = r.ReadString()
ci.ArgumentsBytes = r.ReadVarBytes()
si, err := stackitem.Deserialize(ci.ArgumentsBytes)
if err != nil {
return
}
ci.Arguments = si.(*stackitem.Array)
ci.ArgumentsCount = r.ReadU32LE()
ci.Truncated = r.ReadBool()
}

func (ci *ContractInvocation) EncodeBinaryWithContext(w *io.BinWriter, sc *stackitem.SerializationContext) {
ci.Hash.EncodeBinary(w)
w.WriteString(ci.Method)
w.WriteVarBytes(ci.ArgumentsBytes)
w.WriteU32LE(ci.ArgumentsCount)
w.WriteBool(ci.Truncated)
}

// MarshalJSON implements the json.Marshaler interface.
func (ci ContractInvocation) MarshalJSON() ([]byte, error) {
item, err := stackitem.ToJSONWithTypes(ci.Arguments)
if err != nil {
item = []byte(fmt.Sprintf(`"error: %v"`, err))
}
return json.Marshal(ContractInvocationAux{
Hash: ci.Hash,
Method: ci.Method,
Arguments: item,
ArgumentsCount: ci.ArgumentsCount,
Truncated: ci.Truncated,
})
}

// UnmarshalJSON implements the json.Unmarshaler interface.
func (ci *ContractInvocation) UnmarshalJSON(data []byte) error {
aux := new(ContractInvocationAux)
if err := json.Unmarshal(data, aux); err != nil {
return err
}
params, err := stackitem.FromJSONWithTypes(aux.Arguments)
if err != nil {
return err
}
if t := params.Type(); t != stackitem.ArrayT {
return fmt.Errorf("failed to convert invocation state of type %s to array", t.String())
}
ci.Arguments = params.(*stackitem.Array)
ci.Method = aux.Method
ci.Hash = aux.Hash
ci.ArgumentsCount = aux.ArgumentsCount
ci.Truncated = aux.Truncated
return nil
}

func (ci *ContractInvocation) EncodeBinary(w *io.BinWriter) {
ci.EncodeBinaryWithContext(w, stackitem.NewSerializationContext())
}

// AppExecResult represents the result of the script execution, gathering together
// all resulting notifications, state, stack and other metadata.
type AppExecResult struct {
Expand Down Expand Up @@ -95,6 +168,10 @@ func (aer *AppExecResult) EncodeBinaryWithContext(w *io.BinWriter, sc *stackitem
aer.Events[i].EncodeBinaryWithContext(w, sc)
}
w.WriteVarBytes([]byte(aer.FaultException))
w.WriteVarUint(uint64(len(aer.Invocations)))
for i := range aer.Invocations {
aer.Invocations[i].EncodeBinaryWithContext(w, sc)
}
}

// DecodeBinary implements the Serializable interface.
Expand All @@ -120,6 +197,9 @@ func (aer *AppExecResult) DecodeBinary(r *io.BinReader) {
aer.Stack = arr
r.ReadArray(&aer.Events)
aer.FaultException = r.ReadString()
if r.Len() > 0 {
r.ReadArray(&aer.Invocations)
}
}

// notificationEventAux is an auxiliary struct for NotificationEvent JSON marshalling.
Expand Down Expand Up @@ -209,16 +289,26 @@ type Execution struct {
Stack []stackitem.Item
Events []NotificationEvent
FaultException string
Invocations []ContractInvocation
}

type ContractInvocationAux struct {
Hash util.Uint160 `json:"contracthash"`
Method string `json:"method"`
Arguments json.RawMessage `json:"arguments"`
ArgumentsCount uint32 `json:"argumentscount"`
Truncated bool `json:"truncated"`
}

// executionAux represents an auxiliary struct for Execution JSON marshalling.
type executionAux struct {
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"`
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"`
Invocations []ContractInvocation `json:"invocations"`
}

// MarshalJSON implements the json.Marshaler interface.
Expand Down Expand Up @@ -246,6 +336,7 @@ func (e Execution) MarshalJSON() ([]byte, error) {
Stack: st,
Events: e.Events,
FaultException: exception,
Invocations: e.Invocations,
})
}

Expand Down Expand Up @@ -287,6 +378,7 @@ func (e *Execution) UnmarshalJSON(data []byte) error {
if aux.FaultException != nil {
e.FaultException = *aux.FaultException
}
e.Invocations = aux.Invocations
return nil
}

Expand Down