It is difficult to track the history of complex actions and ensure that no part of that history has been tampered with. Standard logs often fail to show how a series of events led to a specific current state.
It links every transaction together in a chain where each piece of data refers to the one before it. It also records the specific outcome of every action taken.
It ensures that every step in a sequence is linked and verifiable while maintaining a clear record of the results of those actions.
It was run inside an isolated container with no network access. This is the exact command and the real output it produced — captured process output, not written by a model.
$ python3 hash_chained_state_audit.py
Chain valid? True, Message: Chain is valid
Transaction 0:
{
"previous_hash": "0",
"data": {
"type": "genesis"
},
"outcome": "initialized",
"timestamp": 1786086318.9731061,
"current_hash": "69235b6262e3ac39a99224cb2c9e55cd91e11589d2082eb458622b978fe292c9"
}
Transaction 1:
{
"previous_hash": "69235b6262e3ac39a99224cb2c9e55cd91e11589d2082eb458622b978fe292c9",
"data": {
"action": "buy"
},
"outcome": "success",
"timestamp": 1786086318.9732006,
"current_hash": "701ca79722786dc99998a39f0f46c672b1e89ad51ebc1b5b8c2d3093ca182612"
}
Transaction 2:
{
"previous_hash": "701ca79722786dc99998a39f0f46c672b1e89ad51ebc1b5b8c2d3093ca182612",
"data": {
"action": "sell"
},
"outcome": "failure",
"timestamp": 1786086318.9732094,
"current_hash": "79561bf1d469443db72a26eb3ac6c27a218b8dfe873f22ba17101f3baf5e8062"
}A screenshot of that run.
A clean run proves this does what is shown above, in a CPU-only sandbox. It is a small research demo — not a production tool, and nothing here was published anywhere.
All of it — 70 lines, one file, standard library only.
# hash_chained_state_audit.py
import hashlib
import json
import time
class Transaction:
def __init__(self, previous_hash, data, outcome):
self.previous_hash = previous_hash
self.data = data
self.outcome = outcome
self.timestamp = time.time()
self.current_hash = self.calculate_hash()
def calculate_hash(self):
data_str = json.dumps(self.data, sort_keys=True)
outcome_str = str(self.outcome)
combined = f"{self.previous_hash}{data_str}{outcome_str}{self.timestamp}".encode('utf-8')
return hashlib.sha256(combined).hexdigest()
def to_dict(self):
return {
'previous_hash': self.previous_hash,
'data': self.data,
'outcome': self.outcome,
'timestamp': self.timestamp,
'current_hash': self.current_hash
}
class AuditChain:
def __init__(self):
self.chain = []
self.create_genesis_transaction()
def create_genesis_transaction(self):
genesis_data = {"type": "genesis"}
genesis_outcome = "initialized"
self.add_transaction(genesis_data, genesis_outcome)
def add_transaction(self, data, outcome):
previous_hash = self.chain[-1].current_hash if self.chain else '0'
new_transaction = Transaction(previous_hash, data, outcome)
self.chain.append(new_transaction)
return new_transaction
def verify_chain(self):
for i in range(1, len(self.chain)):
current = self.chain[i]
previous = self.chain[i-1]
if current.previous_hash != previous.current_hash:
return False, f"Transaction {i} has invalid previous hash"
recomputed_hash = current.calculate_hash()
if current.current_hash != recomputed_hash:
return False, f"Transaction {i} has invalid hash"
return True, "Chain is valid"
if __name__ == "__main__":
# Example usage
chain = AuditChain()
chain.add_transaction({"action": "buy"}, "success")
chain.add_transaction({"action": "sell"}, "failure")
valid, message = chain.verify_chain()
print(f"Chain valid? {valid}, Message: {message}")
# Print chain details
for i, tx in enumerate(chain.chain):
print(f"Transaction {i}:")
print(json.dumps(tx.to_dict(), indent=2))