diff --git a/chain/chain.go b/chain/chain.go new file mode 100644 index 0000000..2cd49d6 --- /dev/null +++ b/chain/chain.go @@ -0,0 +1,47 @@ +// Copyright (c) 2017-2026 Lethean (https://lt.hn) +// +// Licensed under the European Union Public Licence (EUPL) version 1.2. +// SPDX-License-Identifier: EUPL-1.2 + +// Package chain stores and indexes the Lethean blockchain by syncing from +// a C++ daemon via RPC. +package chain + +import ( + "fmt" + + store "forge.lthn.ai/core/go-store" + "forge.lthn.ai/core/go-blockchain/types" +) + +// Chain manages blockchain storage and indexing. +type Chain struct { + store *store.Store +} + +// New creates a Chain backed by the given store. +func New(s *store.Store) *Chain { + return &Chain{store: s} +} + +// Height returns the number of stored blocks (0 if empty). +func (c *Chain) Height() (uint64, error) { + n, err := c.store.Count(groupBlocks) + if err != nil { + return 0, fmt.Errorf("chain: height: %w", err) + } + return uint64(n), nil +} + +// TopBlock returns the highest stored block and its metadata. +// Returns an error if the chain is empty. +func (c *Chain) TopBlock() (*types.Block, *BlockMeta, error) { + h, err := c.Height() + if err != nil { + return nil, nil, err + } + if h == 0 { + return nil, nil, fmt.Errorf("chain: no blocks stored") + } + return c.GetBlockByHeight(h - 1) +} diff --git a/chain/chain_test.go b/chain/chain_test.go new file mode 100644 index 0000000..e60141d --- /dev/null +++ b/chain/chain_test.go @@ -0,0 +1,131 @@ +// Copyright (c) 2017-2026 Lethean (https://lt.hn) +// +// Licensed under the European Union Public Licence (EUPL) version 1.2. +// SPDX-License-Identifier: EUPL-1.2 + +package chain + +import ( + "testing" + + store "forge.lthn.ai/core/go-store" + "forge.lthn.ai/core/go-blockchain/types" + "forge.lthn.ai/core/go-blockchain/wire" +) + +func newTestChain(t *testing.T) *Chain { + t.Helper() + s, err := store.New(":memory:") + if err != nil { + t.Fatalf("store.New: %v", err) + } + t.Cleanup(func() { s.Close() }) + return New(s) +} + +// testCoinbaseTx returns a minimal v1 coinbase transaction that round-trips +// cleanly through the wire encoder/decoder. +func testCoinbaseTx(height uint64) types.Transaction { + return types.Transaction{ + Version: 1, + Vin: []types.TxInput{types.TxInputGenesis{Height: height}}, + Vout: []types.TxOutput{types.TxOutputBare{ + Amount: 1000000, + Target: types.TxOutToKey{Key: types.PublicKey{0x01}}, + }}, + Extra: wire.EncodeVarint(0), + Attachment: wire.EncodeVarint(0), + } +} + +func TestChain_Height_Empty(t *testing.T) { + c := newTestChain(t) + h, err := c.Height() + if err != nil { + t.Fatalf("Height: %v", err) + } + if h != 0 { + t.Errorf("height: got %d, want 0", h) + } +} + +func TestChain_PutGetBlock_Good(t *testing.T) { + c := newTestChain(t) + + blk := &types.Block{ + BlockHeader: types.BlockHeader{ + MajorVersion: 1, + Timestamp: 1770897600, + }, + MinerTx: testCoinbaseTx(0), + } + meta := &BlockMeta{ + Hash: types.Hash{0xab, 0xcd}, + Height: 0, + Timestamp: 1770897600, + Difficulty: 1, + } + + if err := c.PutBlock(blk, meta); err != nil { + t.Fatalf("PutBlock: %v", err) + } + + h, err := c.Height() + if err != nil { + t.Fatalf("Height: %v", err) + } + if h != 1 { + t.Errorf("height: got %d, want 1", h) + } + + gotBlk, gotMeta, err := c.GetBlockByHeight(0) + if err != nil { + t.Fatalf("GetBlockByHeight: %v", err) + } + if gotBlk.MajorVersion != 1 { + t.Errorf("major_version: got %d, want 1", gotBlk.MajorVersion) + } + if gotMeta.Hash != meta.Hash { + t.Errorf("hash mismatch") + } + + gotBlk2, gotMeta2, err := c.GetBlockByHash(meta.Hash) + if err != nil { + t.Fatalf("GetBlockByHash: %v", err) + } + if gotBlk2.Timestamp != blk.Timestamp { + t.Errorf("timestamp mismatch") + } + if gotMeta2.Height != 0 { + t.Errorf("height: got %d, want 0", gotMeta2.Height) + } +} + +func TestChain_TopBlock_Good(t *testing.T) { + c := newTestChain(t) + + for i := uint64(0); i < 3; i++ { + blk := &types.Block{ + BlockHeader: types.BlockHeader{ + MajorVersion: 1, + Timestamp: 1770897600 + i*120, + }, + MinerTx: testCoinbaseTx(i), + } + meta := &BlockMeta{ + Hash: types.Hash{byte(i)}, + Height: i, + } + if err := c.PutBlock(blk, meta); err != nil { + t.Fatalf("PutBlock(%d): %v", i, err) + } + } + + _, topMeta, err := c.TopBlock() + if err != nil { + t.Fatalf("TopBlock: %v", err) + } + if topMeta.Height != 2 { + t.Errorf("top height: got %d, want 2", topMeta.Height) + } +} diff --git a/chain/store.go b/chain/store.go new file mode 100644 index 0000000..2a08ed8 --- /dev/null +++ b/chain/store.go @@ -0,0 +1,115 @@ +// Copyright (c) 2017-2026 Lethean (https://lt.hn) +// +// Licensed under the European Union Public Licence (EUPL) version 1.2. +// SPDX-License-Identifier: EUPL-1.2 + +package chain + +import ( + "bytes" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "strconv" + + store "forge.lthn.ai/core/go-store" + "forge.lthn.ai/core/go-blockchain/types" + "forge.lthn.ai/core/go-blockchain/wire" +) + +// Storage group constants matching the design schema. +const ( + groupBlocks = "blocks" + groupBlockIndex = "block_index" + groupTx = "transactions" + groupSpentKeys = "spent_keys" + groupOutputsPfx = "outputs:" // suffixed with amount +) + +// heightKey returns a zero-padded 10-digit decimal key for the given height. +func heightKey(h uint64) string { + return fmt.Sprintf("%010d", h) +} + +// blockRecord is the JSON value stored in the blocks group. +type blockRecord struct { + Meta BlockMeta `json:"meta"` + Blob string `json:"blob"` // hex-encoded wire format +} + +// PutBlock stores a block and updates the block_index. +func (c *Chain) PutBlock(b *types.Block, meta *BlockMeta) error { + var buf bytes.Buffer + enc := wire.NewEncoder(&buf) + wire.EncodeBlock(enc, b) + if err := enc.Err(); err != nil { + return fmt.Errorf("chain: encode block %d: %w", meta.Height, err) + } + + rec := blockRecord{ + Meta: *meta, + Blob: hex.EncodeToString(buf.Bytes()), + } + val, err := json.Marshal(rec) + if err != nil { + return fmt.Errorf("chain: marshal block %d: %w", meta.Height, err) + } + + if err := c.store.Set(groupBlocks, heightKey(meta.Height), string(val)); err != nil { + return fmt.Errorf("chain: store block %d: %w", meta.Height, err) + } + + // Update hash -> height index. + hashHex := meta.Hash.String() + if err := c.store.Set(groupBlockIndex, hashHex, strconv.FormatUint(meta.Height, 10)); err != nil { + return fmt.Errorf("chain: index block %d: %w", meta.Height, err) + } + + return nil +} + +// GetBlockByHeight retrieves a block by its height. +func (c *Chain) GetBlockByHeight(height uint64) (*types.Block, *BlockMeta, error) { + val, err := c.store.Get(groupBlocks, heightKey(height)) + if err != nil { + if errors.Is(err, store.ErrNotFound) { + return nil, nil, fmt.Errorf("chain: block %d not found", height) + } + return nil, nil, fmt.Errorf("chain: get block %d: %w", height, err) + } + return decodeBlockRecord(val) +} + +// GetBlockByHash retrieves a block by its hash. +func (c *Chain) GetBlockByHash(hash types.Hash) (*types.Block, *BlockMeta, error) { + heightStr, err := c.store.Get(groupBlockIndex, hash.String()) + if err != nil { + if errors.Is(err, store.ErrNotFound) { + return nil, nil, fmt.Errorf("chain: block %s not found", hash) + } + return nil, nil, fmt.Errorf("chain: get block index %s: %w", hash, err) + } + height, err := strconv.ParseUint(heightStr, 10, 64) + if err != nil { + return nil, nil, fmt.Errorf("chain: parse height %q: %w", heightStr, err) + } + return c.GetBlockByHeight(height) +} + +func decodeBlockRecord(val string) (*types.Block, *BlockMeta, error) { + var rec blockRecord + if err := json.Unmarshal([]byte(val), &rec); err != nil { + return nil, nil, fmt.Errorf("chain: unmarshal block: %w", err) + } + blob, err := hex.DecodeString(rec.Blob) + if err != nil { + return nil, nil, fmt.Errorf("chain: decode block hex: %w", err) + } + dec := wire.NewDecoder(bytes.NewReader(blob)) + blk := wire.DecodeBlock(dec) + if err := dec.Err(); err != nil { + return nil, nil, fmt.Errorf("chain: decode block wire: %w", err) + } + return &blk, &rec.Meta, nil +} diff --git a/go.mod b/go.mod index 550763e..5909416 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,19 @@ require ( golang.org/x/crypto v0.48.0 ) -require golang.org/x/sys v0.41.0 // indirect +require ( + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/ncruces/go-strftime v1.0.0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect + golang.org/x/sys v0.41.0 // indirect + modernc.org/libc v1.67.6 // indirect + modernc.org/mathutil v1.7.1 // indirect + modernc.org/memory v1.11.0 // indirect + modernc.org/sqlite v1.46.1 // indirect +) replace forge.lthn.ai/core/go-p2p => /home/claude/Code/core/go-p2p diff --git a/go.sum b/go.sum index 59fa593..62528dd 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,63 @@ 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/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs= +github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w= +github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= 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/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts= golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos= +golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 h1:mgKeJMpvi0yx/sU5GsxQ7p6s2wtOnGAHZWCHUM4KGzY= +golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546/go.mod h1:j/pmGrbnkbPtQfxEe5D0VQhZC6qKbfKifgD0oM7sR70= +golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA= +golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w= +golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= +golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ= +golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis= +modernc.org/cc/v4 v4.27.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0= +modernc.org/ccgo/v4 v4.30.1 h1:4r4U1J6Fhj98NKfSjnPUN7Ze2c6MnAdL0hWw6+LrJpc= +modernc.org/ccgo/v4 v4.30.1/go.mod h1:bIOeI1JL54Utlxn+LwrFyjCx2n2RDiYEaJVSrgdrRfM= +modernc.org/fileutil v1.3.40 h1:ZGMswMNc9JOCrcrakF1HrvmergNLAmxOPjizirpfqBA= +modernc.org/fileutil v1.3.40/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc= +modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI= +modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito= +modernc.org/gc/v3 v3.1.1 h1:k8T3gkXWY9sEiytKhcgyiZ2L0DTyCQ/nvX+LoCljoRE= +modernc.org/gc/v3 v3.1.1/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY= +modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks= +modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI= +modernc.org/libc v1.67.6 h1:eVOQvpModVLKOdT+LvBPjdQqfrZq+pC39BygcT+E7OI= +modernc.org/libc v1.67.6/go.mod h1:JAhxUVlolfYDErnwiqaLvUqc8nfb2r6S6slAgZOnaiE= +modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= +modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= +modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI= +modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= +modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8= +modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns= +modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w= +modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE= +modernc.org/sqlite v1.46.1 h1:eFJ2ShBLIEnUWlLy12raN0Z1plqmFX9Qe3rjQTKt6sU= +modernc.org/sqlite v1.46.1/go.mod h1:CzbrU2lSB1DKUusvwGz7rqEKIq+NUd8GWuBBZDs9/nA= +modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0= +modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=