diff --git a/contract/internal_operations.go b/contract/internal_operations.go index c8cd2ba20..e88b0f96b 100644 --- a/contract/internal_operations.go +++ b/contract/internal_operations.go @@ -5,7 +5,6 @@ import ( "log" "sync" - "github.com/aergoio/aergo/v2/types" "github.com/aergoio/aergo/v2/internal/enc/base58" ) @@ -28,8 +27,7 @@ type InternalCall struct { type InternalOperations struct { TxHash string `json:"txhash"` - Contract string `json:"contract"` - Operations []InternalOperation `json:"operations"` + Call InternalCall `json:"call"` } var ( @@ -142,6 +140,20 @@ func logInternalCall(ctx *vmContext, contract string, function string, args stri return nil } +func logFirstCall(ctx *vmContext, contract string, function string, args string) { + ctx.internalOpsCall.Contract = contract + ctx.internalOpsCall.Function = function + ctx.internalOpsCall.Args = args +} + +func logCall(ctx *vmContext, contract string, function string, args string) { + if ctx.internalOpsCall.Contract == "" { + logFirstCall(ctx, contract, function, args) + } else { + logInternalCall(ctx, contract, function, args) + } +} + func getInternalOperations(ctx *vmContext) string { if doNotLog(ctx) { return "" @@ -155,8 +167,7 @@ func getInternalOperations(ctx *vmContext) string { internalOps := InternalOperations{ TxHash: base58.Encode(ctx.txHash), - Contract: types.EncodeAddress(ctx.curContract.contractId), - Operations: ctx.internalOpsCall.Operations, + Call: ctx.internalOpsCall, } data, err := json.Marshal(internalOps) diff --git a/contract/vm.go b/contract/vm.go index 7b45c96fc..8830f17eb 100644 --- a/contract/vm.go +++ b/contract/vm.go @@ -418,6 +418,17 @@ func (ce *executor) processArgs() { } } +func convertArgs(argsList []interface{}) (string) { + if argsList == nil { + return "" + } + args, err := json.Marshal(argsList) + if err != nil { + return "" + } + return string(args) +} + func (ce *executor) getEvents() []*types.Event { if ce == nil || ce.ctx == nil { return nil @@ -559,11 +570,11 @@ func (ce *executor) call(instLimit C.int, target *LState) (ret C.int) { ce.err = ce.preErr return 0 } + contract := types.EncodeAddress(ce.ctx.curContract.contractId) if ce.isAutoload { if loaded := vmAutoload(ce.L, ce.fname); !loaded { if ce.fname != constructor { - ce.err = errors.New(fmt.Sprintf("contract autoload failed %s : %s", - types.EncodeAddress(ce.ctx.curContract.contractId), ce.fname)) + ce.err = errors.New(fmt.Sprintf("contract autoload failed %s : %s", contract, ce.fname)) } return 0 } @@ -575,10 +586,10 @@ func (ce *executor) call(instLimit C.int, target *LState) (ret C.int) { } ce.processArgs() if ce.err != nil { - ctrLgr.Debug().Err(ce.err).Stringer("contract", - types.LogAddr(ce.ctx.curContract.contractId)).Msg("invalid argument") + ctrLgr.Debug().Err(ce.err).Str("contract", contract).Msg("invalid argument") return 0 } + logCall(ce.ctx, contract, ce.fname, convertArgs(ce.ci.Args)) ce.setCountHook(instLimit) nRet := C.int(0) cErrMsg := C.vm_pcall(ce.L, ce.numArgs, &nRet) @@ -594,10 +605,7 @@ func (ce *executor) call(instLimit C.int, target *LState) (ret C.int) { ce.err = errors.New(errMsg) } } - ctrLgr.Debug().Err(ce.err).Stringer( - "contract", - types.LogAddr(ce.ctx.curContract.contractId), - ).Msg("contract is failed") + ctrLgr.Debug().Err(ce.err).Str("contract", contract).Msg("contract is failed") return 0 } if target == nil { @@ -612,15 +620,11 @@ func (ce *executor) call(instLimit C.int, target *LState) (ret C.int) { if c2ErrMsg := C.vm_copy_result(ce.L, target, nRet); c2ErrMsg != nil { errMsg := C.GoString(c2ErrMsg) ce.err = errors.New(errMsg) - ctrLgr.Debug().Err(ce.err).Stringer( - "contract", - types.LogAddr(ce.ctx.curContract.contractId), - ).Msg("failed to move results") + ctrLgr.Debug().Err(ce.err).Str("contract", contract).Msg("failed to move results") } } if ce.ctx.traceFile != nil { - address := types.EncodeAddress(ce.ctx.curContract.contractId) - codeFile := fmt.Sprintf("%s%s%s.code", os.TempDir(), string(os.PathSeparator), address) + codeFile := fmt.Sprintf("%s%s%s.code", os.TempDir(), string(os.PathSeparator), contract) if _, err := os.Stat(codeFile); os.IsNotExist(err) { f, err := os.OpenFile(codeFile, os.O_WRONLY|os.O_CREATE, 0644) if err == nil { @@ -629,7 +633,7 @@ func (ce *executor) call(instLimit C.int, target *LState) (ret C.int) { } } _, _ = ce.ctx.traceFile.WriteString(fmt.Sprintf("contract %s used fee: %s\n", - address, ce.ctx.usedFee().String())) + contract, ce.ctx.usedFee().String())) } return nRet } diff --git a/contract/vm_callback.go b/contract/vm_callback.go index 5cd0c1039..634f8b5b2 100644 --- a/contract/vm_callback.go +++ b/contract/vm_callback.go @@ -262,8 +262,6 @@ func luaCallContract(L *LState, service C.int, contractId *C.char, fname *C.char return -1, C.CString("[Contract.LuaCallContract] invalid arguments: " + err.Error()) } - logInternalCall(ctx, contractAddress, fnameStr, argsStr) - // get the remaining gas from the parent LState ctx.refreshRemainingGas(L) // create a new executor with the remaining gas on the child LState @@ -419,8 +417,6 @@ func luaDelegateCallContract(L *LState, service C.int, contractId *C.char, return -1, C.CString("[Contract.LuaDelegateCallContract] invalid arguments: " + err.Error()) } - logInternalCall(ctx, contractIdStr, fnameStr, argsStr) - // get the remaining gas from the parent LState ctx.refreshRemainingGas(L) // create a new executor with the remaining gas on the child LState @@ -564,8 +560,6 @@ func luaSendAmount(L *LState, service C.int, contractId *C.char, amount *C.char) return C.CString("[Contract.LuaSendAmount] cannot find contract:" + contractAddress) } - logInternalCall(ctx, contractAddress, "default", "[]") - // get the remaining gas from the parent LState ctx.refreshRemainingGas(L) // create a new executor with the remaining gas on the child LState @@ -1306,8 +1300,6 @@ func luaDeployContract( return -1, C.CString("[Contract.LuaDeployContract]:" + err.Error()) } - logInternalCall(ctx, types.EncodeAddress(newContract.ID()), "constructor", argsStr) - // get the remaining gas from the parent LState ctx.refreshRemainingGas(L) // create a new executor with the remaining gas on the child LState