CodeSampleX

Exemple

go.etcd.io/bbolt v1.3.9: ErrDatabaseReadOnly

Échantillon vérifié pour golang go.etcd.io/bbolt v1.3.9: ErrDatabaseReadOnly. Le contrat s'est exécuté sur go 1.26 · linux debian/x64 · docker et a réussi.

sha256:c0e8cfad0025b90233577901a0cdc54cccd8c959383abb1f7c6a782c6925ef06

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 go

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-04

Cas

HOW
Objectif
verify go.etcd.io/bbolt.ErrDatabaseReadOnly in pkg:golang/go.etcd.io/bbolt@v1.3.9
Paquets
Symboles
  • go.etcd.io/bbolt.ErrDatabaseReadOnly
Créé
2026-09-04T14:03:38Z

Contrat

  1. bbolt.Open with Options.ReadOnly opens the database in read-only mode
  2. bbolt.DB.IsReadOnly returns true when opened in read-only mode
  3. bbolt.DB.View allows reading existing data from a read-only database
  4. bbolt.DB.Update returns bbolt.ErrDatabaseReadOnly when database is opened in read-only mode
  5. bbolt.DB.Begin with writable true returns bbolt.ErrDatabaseReadOnly on a read-only database

Fichiers

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.go
  • spec.json
  • test/contract.go

Télécharger l’artefact source (tar.gz)

Code source

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify go.etcd.io/bbolt.ErrDatabaseReadOnly in pkg:golang/go.etcd.io/bbolt@v1.3.9
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/go.etcd.io/bbolt@v1.3.9
Demonstrate these symbols/APIs:
  - go.etcd.io/bbolt.ErrDatabaseReadOnly

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:128b4de882f88f7ab7b1a51c2dbbcf2718af6f92bc5b293adf0616de2a74cf61","contract":["bbolt.Open with Options.ReadOnly opens the database in read-only mode","bbolt.DB.IsReadOnly returns true when opened in read-only mode","bbolt.DB.View allows reading existing data from a read-only database","bbolt.DB.Update returns bbolt.ErrDatabaseReadOnly when database is opened in read-only mode","bbolt.DB.Begin with writable true returns bbolt.ErrDatabaseReadOnly on a read-only database"],"goal":"verify go.etcd.io/bbolt.ErrDatabaseReadOnly in pkg:golang/go.etcd.io/bbolt@v1.3.9","kind":"HOW","packages":["pkg:golang/go.etcd.io/bbolt@v1.3.9"],"schemaVersion":1,"symbols":["go.etcd.io/bbolt.ErrDatabaseReadOnly"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/go.etcd.io/bbolt@v1.3.9"],"schemaVersion":1,"subject":"pkg:golang/go.etcd.io/bbolt@v1.3.9","symbols":["go.etcd.io/bbolt.ErrDatabaseReadOnly"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.25.0

require go.etcd.io/bbolt v1.3.9

require golang.org/x/sys v0.4.0 // indirect
go.sum
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.etcd.io/bbolt v1.3.9 h1:8x7aARPEXiXbHmtUwAIv7eV2fQFHrLLavdiJ3uzJXoI=
go.etcd.io/bbolt v1.3.9/go.mod h1:zaO32+Ti0PK1ivdPtgMESzuzL2VPoIG1PCQNvOdo/dE=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sample.go
package sample

import (
	"errors"
	"fmt"
	"time"

	bolt "go.etcd.io/bbolt"
)

// OpenDatabase opens a Bolt key-value database with the given readOnly setting.
func OpenDatabase(path string, readOnly bool, timeout time.Duration) (*bolt.DB, error) {
	opts := &bolt.Options{
		ReadOnly: readOnly,
		Timeout:  timeout,
	}
	return bolt.Open(path, 0600, opts)
}

// PutValue writes a key-value pair to a bucket inside a write transaction.
func PutValue(db *bolt.DB, bucketName, key, value string) error {
	return db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists([]byte(bucketName))
		if err != nil {
			return fmt.Errorf("create bucket: %w", err)
		}
		return b.Put([]byte(key), []byte(value))
	})
}

// GetValue reads a key from a bucket inside a read transaction.
func GetValue(db *bolt.DB, bucketName, key string) (string, error) {
	var val string
	err := db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(bucketName))
		if b == nil {
			return errors.New("bucket not found")
		}
		data := b.Get([]byte(key))
		if data == nil {
			return errors.New("key not found")
		}
		val = string(data)
		return nil
	})
	return val, err
}

// BeginWritableTransaction attempts to begin a writable transaction.
func BeginWritableTransaction(db *bolt.DB) (*bolt.Tx, error) {
	return db.Begin(true)
}

// CloseDatabase closes the opened Bolt database.
func CloseDatabase(db *bolt.DB) error {
	if db == nil {
		return nil
	}
	return db.Close()
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify go.etcd.io/bbolt.ErrDatabaseReadOnly in pkg:golang/go.etcd.io/bbolt@v1.3.9",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.etcd.io/bbolt@v1.3.9"
  ],
  "symbols": [
    "go.etcd.io/bbolt.ErrDatabaseReadOnly"
  ]
}
test/contract.go
package main

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"time"

	bolt "go.etcd.io/bbolt"
	"sample"
)

func main() {
	tmpDir, err := os.MkdirTemp("", "bbolt-readonly-test-*")
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to create temp dir: %v\n", err)
		os.Exit(1)
	}
	defer os.RemoveAll(tmpDir)

	dbPath := filepath.Join(tmpDir, "test.db")

	// Prepare data using a writable database first
	initDB, err := sample.OpenDatabase(dbPath, false, 2*time.Second)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to open init database: %v\n", err)
		os.Exit(1)
	}
	bucketName := "config"
	key := "env"
	val := "production"
	if err := sample.PutValue(initDB, bucketName, key, val); err != nil {
		fmt.Fprintf(os.Stderr, "failed to seed database: %v\n", err)
		os.Exit(1)
	}
	if err := sample.CloseDatabase(initDB); err != nil {
		fmt.Fprintf(os.Stderr, "failed to close init database: %v\n", err)
		os.Exit(1)
	}

	// Contract Assertion 1: bbolt.Open with Options.ReadOnly opens the database in read-only mode
	roDB, err := sample.OpenDatabase(dbPath, true, 2*time.Second)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to open database in read-only mode: %v\n", err)
		os.Exit(1)
	}
	defer sample.CloseDatabase(roDB)

	// Contract Assertion 2: bbolt.DB.IsReadOnly returns true when opened in read-only mode
	if !roDB.IsReadOnly() {
		fmt.Fprintf(os.Stderr, "expected db.IsReadOnly() to be true, got false\n")
		os.Exit(1)
	}

	// Contract Assertion 3: bbolt.DB.View allows reading existing data from a read-only database
	readVal, err := sample.GetValue(roDB, bucketName, key)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to read value from read-only database: %v\n", err)
		os.Exit(1)
	}
	if readVal != val {
		fmt.Fprintf(os.Stderr, "expected read value %q, got %q\n", val, readVal)
		os.Exit(1)
	}

	// Contract Assertion 4: bbolt.DB.Update returns bbolt.ErrDatabaseReadOnly when database is opened in read-only mode
	updateErr := sample.PutValue(roDB, bucketName, "newKey", "newValue")
	if updateErr == nil {
		fmt.Fprintf(os.Stderr, "expected Update to return an error on read-only database, got nil\n")
		os.Exit(1)
	}
	if !errors.Is(updateErr, bolt.ErrDatabaseReadOnly) {
		fmt.Fprintf(os.Stderr, "expected errors.Is(err, bolt.ErrDatabaseReadOnly), got: %v\n", updateErr)
		os.Exit(1)
	}

	// Contract Assertion 5: bbolt.DB.Begin with writable true returns bbolt.ErrDatabaseReadOnly on a read-only database
	tx, beginErr := sample.BeginWritableTransaction(roDB)
	if tx != nil {
		_ = tx.Rollback()
	}
	if beginErr == nil {
		fmt.Fprintf(os.Stderr, "expected Begin(true) to return an error on read-only database, got nil\n")
		os.Exit(1)
	}
	if !errors.Is(beginErr, bolt.ErrDatabaseReadOnly) {
		fmt.Fprintf(os.Stderr, "expected Begin(true) to return ErrDatabaseReadOnly, got: %v\n", beginErr)
		os.Exit(1)
	}

	fmt.Println("All bbolt.ErrDatabaseReadOnly contract assertions passed.")
}

Seeder d'origine

anonyme