Skip to content

Commit

Permalink
log contract events and governance
Browse files Browse the repository at this point in the history
  • Loading branch information
kroggen committed Oct 3, 2024
1 parent 52be58a commit 4e234e3
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions contract/vm_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -1390,30 +1390,33 @@ func luaRandomInt(min, max, service C.int) C.int {
}

//export luaEvent
func luaEvent(L *LState, service C.int, eventName *C.char, args *C.char) *C.char {
func luaEvent(L *LState, service C.int, name *C.char, args *C.char) *C.char {
eventName := C.GoString(name)
eventArgs := C.GoString(args)
ctx := contexts[service]
if ctx.isQuery == true || ctx.nestedView > 0 {
return C.CString("[Contract.Event] event not permitted in query")
}
if ctx.eventCount >= maxEventCnt(ctx) {
return C.CString(fmt.Sprintf("[Contract.Event] exceeded the maximum number of events(%d)", maxEventCnt(ctx)))
}
if len(C.GoString(eventName)) > maxEventNameSize {
if len(eventName) > maxEventNameSize {
return C.CString(fmt.Sprintf("[Contract.Event] exceeded the maximum length of event name(%d)", maxEventNameSize))
}
if len(C.GoString(args)) > maxEventArgSize {
if len(eventArgs) > maxEventArgSize {
return C.CString(fmt.Sprintf("[Contract.Event] exceeded the maximum length of event args(%d)", maxEventArgSize))
}
ctx.events = append(
ctx.events,
&types.Event{
ContractAddress: ctx.curContract.contractId,
EventIdx: ctx.eventCount,
EventName: C.GoString(eventName),
JsonArgs: C.GoString(args),
EventName: eventName,
JsonArgs: eventArgs,
},
)
ctx.eventCount++
logOperation(ctx, "", "event", eventName, eventArgs)
return nil
}

Expand Down Expand Up @@ -1515,7 +1518,7 @@ func luaNameResolve(L *LState, service C.int, name_or_address *C.char) *C.char {
}

//export luaGovernance
func luaGovernance(L *LState, service C.int, gType C.char, arg *C.char) *C.char {
func luaGovernance(L *LState, service C.int, gType C.char, arg *C.char) (errormsg *C.char) {

ctx := contexts[service]
if ctx == nil {
Expand All @@ -1528,6 +1531,7 @@ func luaGovernance(L *LState, service C.int, gType C.char, arg *C.char) *C.char

var amountBig *big.Int
var payload []byte
var opId int64

switch gType {
case 'S', 'U':
Expand All @@ -1538,17 +1542,27 @@ func luaGovernance(L *LState, service C.int, gType C.char, arg *C.char) *C.char
}
if gType == 'S' {
payload = []byte(fmt.Sprintf(`{"Name":"%s"}`, types.Opstake.Cmd()))
opId = logOperation(ctx, "", "stake", amountBig.String())
} else {
payload = []byte(fmt.Sprintf(`{"Name":"%s"}`, types.Opunstake.Cmd()))
opId = logOperation(ctx, "", "unstake", amountBig.String())
}
case 'V':
amountBig = zeroBig
payload = []byte(fmt.Sprintf(`{"Name":"%s","Args":%s}`, types.OpvoteBP.Cmd(), C.GoString(arg)))
opId = logOperation(ctx, "", "vote", C.GoString(arg))
case 'D':
amountBig = zeroBig
payload = []byte(fmt.Sprintf(`{"Name":"%s","Args":%s}`, types.OpvoteDAO.Cmd(), C.GoString(arg)))
opId = logOperation(ctx, "", "voteDAO", C.GoString(arg))
}

defer func() {
if errormsg != nil {
logOperationResult(ctx, opId, C.GoString(errormsg))
}
}()

cid := []byte(types.AergoSystem)
aid := types.ToAccountID(cid)
scsState, err := getContractState(ctx, cid)
Expand Down

0 comments on commit 4e234e3

Please sign in to comment.