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 16 commits into
base: master
Choose a base branch
from
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.
ixje marked this conversation as resolved.
Show resolved Hide resolved
SaveInvocations bool `yaml:"SaveInvocations"`
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
}

// 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,
IsValid: true,
}},
},
}
err := dao.StoreAsTransaction(tx, 0, aer)
Expand Down
1 change: 1 addition & 0 deletions pkg/core/interop/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Context struct {
VM *vm.VM
Functions []Function
Invocations map[util.Uint160]int
InvocationCalls []state.ContractInvocation
ixje marked this conversation as resolved.
Show resolved Hide resolved
cancelFuncs []context.CancelFunc
getContract func(*dao.Simple, util.Uint160) (*state.Contract, error)
baseExecFee int64
Expand Down
20 changes: 20 additions & 0 deletions pkg/core/interop/contract/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ func Call(ic *interop.Context) error {
return fmt.Errorf("method not found: %s/%d", method, len(args))
}
hasReturn := md.ReturnType != smartcontract.VoidType

if ic.Chain.GetConfig().Ledger.SaveInvocations {
ixje marked this conversation as resolved.
Show resolved Hide resolved
arr := stackitem.NewArray(args)
ixje marked this conversation as resolved.
Show resolved Hide resolved
arrCount := len(args)
valid := true
argBytes := []byte{}
ixje marked this conversation as resolved.
Show resolved Hide resolved
if argBytes, err = ic.DAO.GetItemCtx().Serialize(arr, false); err != nil {
arr = stackitem.NewArray([]stackitem.Item{})
valid = false
}

ic.InvocationCalls = append(ic.InvocationCalls, state.ContractInvocation{
ixje marked this conversation as resolved.
Show resolved Hide resolved
Hash: u,
Method: method,
Arguments: arr,
ArgumentsBytes: argBytes,
ArgumentsCount: uint32(arrCount),
IsValid: valid,
})
}
return callInternal(ic, cs, method, fs, hasReturn, args, true)
}

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

type ContractInvocation struct {
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
Hash util.Uint160 `json:"contract_hash"`
Method string `json:"method"`
Arguments *stackitem.Array `json:"arguments"`
ArgumentsBytes []byte `json:"arguments_bytes"`
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
ArgumentsCount uint32 `json:"arguments_count"`
ixje marked this conversation as resolved.
Show resolved Hide resolved
IsValid bool `json:"is_valid"`
ixje marked this conversation as resolved.
Show resolved Hide resolved
}

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.IsValid = 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.IsValid)
}

// 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,
IsValid: ci.IsValid,
})
}

// 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.IsValid = aux.IsValid
return nil
}

func (ci *ContractInvocation) EncodeBinary(w *io.BinWriter) {
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
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 +165,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)))
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
for i := range aer.Invocations {
aer.Invocations[i].EncodeBinaryWithContext(w, sc)
}
}

// DecodeBinary implements the Serializable interface.
Expand All @@ -120,6 +194,7 @@ func (aer *AppExecResult) DecodeBinary(r *io.BinReader) {
aer.Stack = arr
r.ReadArray(&aer.Events)
aer.FaultException = r.ReadString()
r.ReadArray(&aer.Invocations)
ixje marked this conversation as resolved.
Show resolved Hide resolved
}

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

type ContractInvocationAux struct {
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
Hash util.Uint160 `json:"contract_hash"`
Method string `json:"method"`
Arguments json.RawMessage `json:"arguments"`
ArgumentsCount uint32 `json:"arguments_count"`
IsValid bool `json:"is_valid"`
}

// 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"`
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

Expand Down