aergoio/aergo

View on GitHub
contract/vm_callback.go

Summary

Maintainability
F
1 wk
Test Coverage
F
4%

File vm_callback.go has 1172 lines of code (exceeds 500 allowed). Consider refactoring.
Open

package contract

/*
#cgo CFLAGS: -I${SRCDIR}/../libtool/include/luajit-2.1
#cgo LDFLAGS: ${SRCDIR}/../libtool/lib/libluajit-5.1.a -lm
Severity: Major
Found in contract/vm_callback.go - About 2 days to fix

    Function luaDeployContract has a Cognitive Complexity of 48 (exceeds 20 allowed). Consider refactoring.
    Open

    func luaDeployContract(
        L *LState,
        service C.int,
        contract *C.char,
        args *C.char,
    Severity: Minor
    Found in contract/vm_callback.go - About 4 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function luaDeployContract has 136 lines of code (exceeds 50 allowed). Consider refactoring.
    Open

    func luaDeployContract(
        L *LState,
        service C.int,
        contract *C.char,
        args *C.char,
    Severity: Major
    Found in contract/vm_callback.go - About 4 hrs to fix

      Function luaSendAmount has a Cognitive Complexity of 43 (exceeds 20 allowed). Consider refactoring.
      Open

      func luaSendAmount(L *LState, service C.int, contractId *C.char, amount *C.char) *C.char {
      
          ctx := contexts[service]
          if ctx == nil {
              return C.CString("[Contract.LuaSendAmount] contract state not found")
      Severity: Minor
      Found in contract/vm_callback.go - About 4 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function luaSendAmount has 102 lines of code (exceeds 50 allowed). Consider refactoring.
      Open

      func luaSendAmount(L *LState, service C.int, contractId *C.char, amount *C.char) *C.char {
      
          ctx := contexts[service]
          if ctx == nil {
              return C.CString("[Contract.LuaSendAmount] contract state not found")
      Severity: Major
      Found in contract/vm_callback.go - About 3 hrs to fix

        Function luaCallContract has 91 lines of code (exceeds 50 allowed). Consider refactoring.
        Open

        func luaCallContract(L *LState, service C.int, contractId *C.char, fname *C.char, args *C.char,
            amount *C.char, gas uint64) (C.int, *C.char) {
            fnameStr := C.GoString(fname)
            argsStr := C.GoString(args)
        
        
        Severity: Major
        Found in contract/vm_callback.go - About 2 hrs to fix

          Function luaGovernance has 90 lines of code (exceeds 50 allowed). Consider refactoring.
          Open

          func luaGovernance(L *LState, service C.int, gType C.char, arg *C.char) *C.char {
          
              ctx := contexts[service]
              if ctx == nil {
                  return C.CString("[Contract.LuaGovernance] contract state not found")
          Severity: Major
          Found in contract/vm_callback.go - About 2 hrs to fix

            Function luaGetDB has a Cognitive Complexity of 34 (exceeds 20 allowed). Consider refactoring.
            Open

            func luaGetDB(L *LState, service C.int, key unsafe.Pointer, keyLen C.int, blkno *C.char) (*C.char, *C.char) {
                ctx := contexts[service]
                if ctx == nil {
                    return nil, C.CString("[System.LuaGetDB] contract state not found")
                }
            Severity: Minor
            Found in contract/vm_callback.go - About 2 hrs to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Function luaDeployContract has 23 return statements (exceeds 4 allowed).
            Open

            func luaDeployContract(
                L *LState,
                service C.int,
                contract *C.char,
                args *C.char,
            Severity: Major
            Found in contract/vm_callback.go - About 2 hrs to fix

              Function luaGovernance has a Cognitive Complexity of 29 (exceeds 20 allowed). Consider refactoring.
              Open

              func luaGovernance(L *LState, service C.int, gType C.char, arg *C.char) *C.char {
              
                  ctx := contexts[service]
                  if ctx == nil {
                      return C.CString("[Contract.LuaGovernance] contract state not found")
              Severity: Minor
              Found in contract/vm_callback.go - About 1 hr to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Function luaDelegateCallContract has 67 lines of code (exceeds 50 allowed). Consider refactoring.
              Open

              func luaDelegateCallContract(L *LState, service C.int, contractId *C.char,
                  fname *C.char, args *C.char, gas uint64) (C.int, *C.char) {
                  contractIdStr := C.GoString(contractId)
                  fnameStr := C.GoString(fname)
                  argsStr := C.GoString(args)
              Severity: Minor
              Found in contract/vm_callback.go - About 1 hr to fix

                Function luaECVerify has 65 lines of code (exceeds 50 allowed). Consider refactoring.
                Open

                func luaECVerify(L *LState, service C.int, msg *C.char, sig *C.char, addr *C.char) (C.int, *C.char) {
                    bMsg, err := decodeHex(C.GoString(msg))
                    if err != nil {
                        return -1, C.CString("[Contract.LuaEcVerify] invalid message format: " + err.Error())
                    }
                Severity: Minor
                Found in contract/vm_callback.go - About 1 hr to fix

                  Function luaSendAmount has 17 return statements (exceeds 4 allowed).
                  Open

                  func luaSendAmount(L *LState, service C.int, contractId *C.char, amount *C.char) *C.char {
                  
                      ctx := contexts[service]
                      if ctx == nil {
                          return C.CString("[Contract.LuaSendAmount] contract state not found")
                  Severity: Major
                  Found in contract/vm_callback.go - About 1 hr to fix

                    Function luaCallContract has 13 return statements (exceeds 4 allowed).
                    Open

                    func luaCallContract(L *LState, service C.int, contractId *C.char, fname *C.char, args *C.char,
                        amount *C.char, gas uint64) (C.int, *C.char) {
                        fnameStr := C.GoString(fname)
                        argsStr := C.GoString(args)
                    
                    
                    Severity: Major
                    Found in contract/vm_callback.go - About 1 hr to fix

                      Function luaGetDB has 12 return statements (exceeds 4 allowed).
                      Open

                      func luaGetDB(L *LState, service C.int, key unsafe.Pointer, keyLen C.int, blkno *C.char) (*C.char, *C.char) {
                          ctx := contexts[service]
                          if ctx == nil {
                              return nil, C.CString("[System.LuaGetDB] contract state not found")
                          }
                      Severity: Major
                      Found in contract/vm_callback.go - About 1 hr to fix

                        Function luaECVerify has 11 return statements (exceeds 4 allowed).
                        Open

                        func luaECVerify(L *LState, service C.int, msg *C.char, sig *C.char, addr *C.char) (C.int, *C.char) {
                            bMsg, err := decodeHex(C.GoString(msg))
                            if err != nil {
                                return -1, C.CString("[Contract.LuaEcVerify] invalid message format: " + err.Error())
                            }
                        Severity: Major
                        Found in contract/vm_callback.go - About 1 hr to fix

                          Function luaCallContract has a Cognitive Complexity of 25 (exceeds 20 allowed). Consider refactoring.
                          Open

                          func luaCallContract(L *LState, service C.int, contractId *C.char, fname *C.char, args *C.char,
                              amount *C.char, gas uint64) (C.int, *C.char) {
                              fnameStr := C.GoString(fname)
                              argsStr := C.GoString(args)
                          
                          
                          Severity: Minor
                          Found in contract/vm_callback.go - About 1 hr to fix

                          Cognitive Complexity

                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                          A method's cognitive complexity is based on a few simple rules:

                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                          • Code is considered more complex for each "break in the linear flow of the code"
                          • Code is considered more complex when "flow breaking structures are nested"

                          Further reading

                          Function luaECVerify has a Cognitive Complexity of 25 (exceeds 20 allowed). Consider refactoring.
                          Open

                          func luaECVerify(L *LState, service C.int, msg *C.char, sig *C.char, addr *C.char) (C.int, *C.char) {
                              bMsg, err := decodeHex(C.GoString(msg))
                              if err != nil {
                                  return -1, C.CString("[Contract.LuaEcVerify] invalid message format: " + err.Error())
                              }
                          Severity: Minor
                          Found in contract/vm_callback.go - About 1 hr to fix

                          Cognitive Complexity

                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                          A method's cognitive complexity is based on a few simple rules:

                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                          • Code is considered more complex for each "break in the linear flow of the code"
                          • Code is considered more complex when "flow breaking structures are nested"

                          Further reading

                          Function luaGetDB has 51 lines of code (exceeds 50 allowed). Consider refactoring.
                          Open

                          func luaGetDB(L *LState, service C.int, key unsafe.Pointer, keyLen C.int, blkno *C.char) (*C.char, *C.char) {
                              ctx := contexts[service]
                              if ctx == nil {
                                  return nil, C.CString("[System.LuaGetDB] contract state not found")
                              }
                          Severity: Minor
                          Found in contract/vm_callback.go - About 1 hr to fix

                            Function luaGovernance has 10 return statements (exceeds 4 allowed).
                            Open

                            func luaGovernance(L *LState, service C.int, gType C.char, arg *C.char) *C.char {
                            
                                ctx := contexts[service]
                                if ctx == nil {
                                    return C.CString("[Contract.LuaGovernance] contract state not found")
                            Severity: Major
                            Found in contract/vm_callback.go - About 1 hr to fix

                              Function luaDelegateCallContract has 10 return statements (exceeds 4 allowed).
                              Open

                              func luaDelegateCallContract(L *LState, service C.int, contractId *C.char,
                                  fname *C.char, args *C.char, gas uint64) (C.int, *C.char) {
                                  contractIdStr := C.GoString(contractId)
                                  fnameStr := C.GoString(fname)
                                  argsStr := C.GoString(args)
                              Severity: Major
                              Found in contract/vm_callback.go - About 1 hr to fix

                                Avoid deeply nested control flow statements.
                                Open

                                                if err != nil {
                                                    return nil, C.CString("[System.LuaGetDB] failed to get snapshot state variable in contract")
                                                }
                                Severity: Major
                                Found in contract/vm_callback.go - About 45 mins to fix

                                  Avoid deeply nested control flow statements.
                                  Open

                                                  if varProof.Inclusion {
                                                      if len(varProof.GetValue()) == 0 {
                                                          return nil, nil
                                                      }
                                                      return C.CString(string(varProof.GetValue())), nil
                                  Severity: Major
                                  Found in contract/vm_callback.go - About 45 mins to fix

                                    Function luaSetDB has 5 return statements (exceeds 4 allowed).
                                    Open

                                    func luaSetDB(L *LState, service C.int, key unsafe.Pointer, keyLen C.int, value *C.char) *C.char {
                                        ctx := contexts[service]
                                        if ctx == nil {
                                            return C.CString("[System.LuaSetDB] contract state not found")
                                        }
                                    Severity: Major
                                    Found in contract/vm_callback.go - About 35 mins to fix

                                      Function luaGetBalance has 5 return statements (exceeds 4 allowed).
                                      Open

                                      func luaGetBalance(L *LState, service C.int, contractId *C.char) (*C.char, *C.char) {
                                          ctx := contexts[service]
                                          if contractId == nil {
                                              return C.CString(ctx.curContract.callState.ctrState.GetBalanceBigInt().String()), nil
                                          }
                                      Severity: Major
                                      Found in contract/vm_callback.go - About 35 mins to fix

                                        Function luaDelDB has 5 return statements (exceeds 4 allowed).
                                        Open

                                        func luaDelDB(L *LState, service C.int, key unsafe.Pointer, keyLen C.int) *C.char {
                                            ctx := contexts[service]
                                            if ctx == nil {
                                                return C.CString("[System.LuaDelDB] contract state not found")
                                            }
                                        Severity: Major
                                        Found in contract/vm_callback.go - About 35 mins to fix

                                          Function luaEvent has 5 return statements (exceeds 4 allowed).
                                          Open

                                          func luaEvent(L *LState, service C.int, eventName *C.char, args *C.char) *C.char {
                                              ctx := contexts[service]
                                              if ctx.isQuery == true || ctx.nestedView > 0 {
                                                  return C.CString("[Contract.Event] event not permitted in query")
                                              }
                                          Severity: Major
                                          Found in contract/vm_callback.go - About 35 mins to fix

                                            Function getAddressNameResolved has 5 return statements (exceeds 4 allowed).
                                            Open

                                            func getAddressNameResolved(account string, bs *state.BlockState) ([]byte, error) {
                                                accountLen := len(account)
                                                if accountLen == types.EncodedAddressLength {
                                                    return types.DecodeAddress(account)
                                                } else if accountLen == types.NameLength {
                                            Severity: Major
                                            Found in contract/vm_callback.go - About 35 mins to fix

                                              Function LuaGetDbHandleSnap has 5 return statements (exceeds 4 allowed).
                                              Open

                                              func LuaGetDbHandleSnap(service C.int, snap *C.char) *C.char {
                                              
                                                  stateSet := contexts[service]
                                                  curContract := stateSet.curContract
                                                  callState := curContract.callState
                                              Severity: Major
                                              Found in contract/vm_callback.go - About 35 mins to fix

                                                Similar blocks of code found in 2 locations. Consider refactoring.
                                                Open

                                                            if ctx.traceFile != nil {
                                                                _, _ = ctx.traceFile.WriteString(fmt.Sprintf("[GOVERNANCE]aid(%s)\n", aid.String()))
                                                                _, _ = ctx.traceFile.WriteString(fmt.Sprintf("snapshot set %d\n", seq))
                                                                _, _ = ctx.traceFile.WriteString(fmt.Sprintf("unstaking : %s\n", amountBig.String()))
                                                                _, _ = ctx.traceFile.WriteString(fmt.Sprintf("After sender: %s receiver: %s\n",
                                                Severity: Major
                                                Found in contract/vm_callback.go and 1 other location - About 1 hr to fix
                                                contract/vm_callback.go on lines 1372..1378

                                                Duplicated Code

                                                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                                Tuning

                                                This issue has a mass of 159.

                                                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                                Refactorings

                                                Further Reading

                                                Similar blocks of code found in 2 locations. Consider refactoring.
                                                Open

                                                            if ctx.traceFile != nil {
                                                                _, _ = ctx.traceFile.WriteString(fmt.Sprintf("[GOVERNANCE]aid(%s)\n", aid.String()))
                                                                _, _ = ctx.traceFile.WriteString(fmt.Sprintf("snapshot set %d\n", seq))
                                                                _, _ = ctx.traceFile.WriteString(fmt.Sprintf("staking : %s\n", amountBig.String()))
                                                                _, _ = ctx.traceFile.WriteString(fmt.Sprintf("After sender: %s receiver: %s\n",
                                                Severity: Major
                                                Found in contract/vm_callback.go and 1 other location - About 1 hr to fix
                                                contract/vm_callback.go on lines 1381..1387

                                                Duplicated Code

                                                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                                Tuning

                                                This issue has a mass of 159.

                                                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                                Refactorings

                                                Further Reading

                                                There are no issues that match your filters.

                                                Category
                                                Status