Developers Forum for XinFin XDC Network

Discussion on: Issue Report – Unexpected Gas Behavior on XDC Network

Collapse
gzliudan profile image
Daniel Liu • Edited on

Why gasUsed grows when calling postCertificate:

  • On-chain state growth (dynamic arrays): studentInfo is a mapping whose value (Student) contains dynamic arrays (uri, instituteName). As these arrays grow, any operation that reads/writes many elements costs more gas.
  • Whole-struct assignment triggers array copy: Code that does something like studentInfo[addr] = Student(..., studentInfo[addr].uri, ...) forces the compiler to copy storage arrays into memory and back. That copy cost scales linearly with array length, so per-tx gasUsed increases as arrays get longer.
  • Duplicated string storage (embedded struct): Cert embeds a full Institute (which contains strings). Each certificates[hash] = Cert(institutes[id], ...) duplicates those strings into storage for every certificate, multiplying SSTOREs and increasing gas usage.
  • More SSTOREs when state expands: Adding certificates/URIs creates new storage slots (0→non-zero) which are expensive SSTORE operations. As more slots are consumed over time, transactions that create or update them consume more gas.
  • Larger event/log payloads: Events like CertificatePosted(string hash, ...) include strings. If logged strings or number of logs grow, log-writing gas rises.