Adds a focused NEAR Hackathon 2026 entry built on Ration's existing,
real audit-log service rather than a Ration-on-NEAR rewrite: a minimal
NEAR smart contract (contracts-near/audit_anchor) storing/verifying
{tenant_id, chain_tip_hash, sequence, occurred_at_unix}, plus a
from-scratch Go NEAR transaction signer and CLI
(services/audit-log/internal/nearanchor, cmd/near-anchor) that reads
audit-log's real local chain tip and anchors it on-chain.
Verified live on NEAR Testnet (account ration-anchor-286ca315f1.testnet):
contract deployed and code_hash-matched, two successful on-chain
anchors, one deliberate replay correctly rejected by nearcore's own
sequence guard, and a real (non-mocked) Go integration test passing
against the live contract. Transaction encoding cross-checked
byte-for-byte against near-api-js. Zero regressions in existing Go/Rust
suites. Full detail and every tx hash in docs/near-hackathon/.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package store
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// ReadTip opens the append-only log file at path read-only and returns
|
|
// the single most recent record (the chain's current "tip" — highest
|
|
// Sequence, whatever Hash currently closes the chain) without loading
|
|
// the whole file into memory the way Open/replay does. It is the
|
|
// read-only counterpart to VerifyFile: both are safe to run against a
|
|
// live audit-log's data file without any gRPC dependency and without
|
|
// ever requiring write access, so an out-of-band tool (see
|
|
// cmd/near-anchor) can read the real chain tip even while audit-log
|
|
// itself is running.
|
|
//
|
|
// Returns ok=false (zero Record, nil error) if the file exists but is
|
|
// empty — "no records yet" is not an error condition on its own.
|
|
func ReadTip(path string) (rec Record, ok bool, err error) {
|
|
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
|
if err != nil {
|
|
return Record{}, false, fmt.Errorf("store: open %s read-only: %w", path, err)
|
|
}
|
|
defer f.Close()
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
scanner.Buffer(make([]byte, 0, 64*1024), 16*1024*1024)
|
|
|
|
var last Record
|
|
found := false
|
|
lineNo := 0
|
|
for scanner.Scan() {
|
|
lineNo++
|
|
line := scanner.Bytes()
|
|
if len(line) == 0 {
|
|
continue
|
|
}
|
|
var r Record
|
|
if err := json.Unmarshal(line, &r); err != nil {
|
|
return Record{}, false, fmt.Errorf("store: read tip: corrupt record at line %d: %w", lineNo, err)
|
|
}
|
|
last = r
|
|
found = true
|
|
}
|
|
if err := scanner.Err(); err != nil {
|
|
return Record{}, false, fmt.Errorf("store: read tip: reading %s: %w", path, err)
|
|
}
|
|
return last, found, nil
|
|
}
|