Skip to content

Commit

Permalink
include the first call on internal operations
Browse files Browse the repository at this point in the history
  • Loading branch information
kroggen committed Oct 24, 2024
1 parent ccfbc89 commit 3a1bb8f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
21 changes: 16 additions & 5 deletions contract/internal_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"sync"

"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/internal/enc/base58"
)

Expand All @@ -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 (
Expand Down Expand Up @@ -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 ""
Expand All @@ -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)
Expand Down
34 changes: 19 additions & 15 deletions contract/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
8 changes: 0 additions & 8 deletions contract/vm_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3a1bb8f

Please sign in to comment.