{"record":{"id":"b509d490a121362f","repo":"ethereum/go-ethereum","slug":"trying-to-insert-into-existing-key","errorCode":null,"errorMessage":"Trying to insert into existing key","messagePattern":"Trying to insert into existing key","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"trie/stacktrie.go","lineNumber":288,"sourceCode":"\t\t// Insert both child leaves where they belong:\n\t\torigIdx := st.key[diffidx]\n\t\tnewIdx := key[diffidx]\n\t\tp.children[origIdx] = n\n\t\tp.children[newIdx] = o\n\t\tst.key = st.key[:diffidx]\n\n\tcase leafNode: /* Leaf */\n\t\t// Compare both key chunks and see where they differ\n\t\tdiffidx := st.getDiffIndex(key)\n\n\t\t// Overwriting a key isn't supported, which means that\n\t\t// the current leaf is expected to be split into 1) an\n\t\t// optional extension for the common prefix of these 2\n\t\t// keys, 2) a fullnode selecting the path on which the\n\t\t// keys differ, and 3) one leaf for the differentiated\n\t\t// component of each key.\n\t\tif diffidx >= len(st.key) {\n\t\t\tpanic(\"Trying to insert into existing key\")\n\t\t}\n\n\t\t// Check if the split occurs at the first nibble of the\n\t\t// chunk. In that case, no prefix extnode is necessary.\n\t\t// Otherwise, create that\n\t\tvar p *stNode\n\t\tif diffidx == 0 {\n\t\t\t// Convert current leaf into a branch\n\t\t\tst.typ = branchNode\n\t\t\tst.children[0] = nil\n\t\t\tp = st\n\t\t} else {\n\t\t\t// Convert current node into an ext,\n\t\t\t// and insert a child branch node.\n\t\t\tst.typ = extNode\n\t\t\tst.children[0] = stPool.Get().(*stNode)\n\t\t\tst.children[0].typ = branchNode\n\t\t\tp = st.children[0]","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/ethereum/go-ethereum/blob/6bb0588ad8e7f922e4ad5580f51265a4097af08f/trie/stacktrie.go#L270-L306","documentation":"Error \"Trying to insert into existing key\" thrown in ethereum/go-ethereum.","triggerScenarios":"Calling StackTrie.Update/insert with a key that already exists in the trie, or with unsorted input keys.","commonSituations":"Building a stacktrie from duplicate or unsorted key/value lists (e.g., receipts/tx lists containing duplicates).","solutions":["StackTrie does not support overwriting an existing key. Ensure keys are inserted exactly once and in strictly increasing (sorted) order.","If updates are needed, collect all key/value pairs first and build the StackTrie once from the final set."],"exampleFix":"sort.Slice(kvs, func(i, j int) bool { return bytes.Compare(kvs[i].K, kvs[j].K) < 0 })\nfor _, kv := range kvs {\n    st.Update(kv.K, kv.V) // each key exactly once, ascending order\n}","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"6bb0588ad8e7f922e4ad5580f51265a4097af08f","analyzedAt":"2026-08-15T10:06:53.996Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}