Skip to content

Commit 42f5960

Browse files
committed
initial commit
0 parents  commit 42f5960

File tree

7 files changed

+1215
-0
lines changed

7 files changed

+1215
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2024 The Sia Foundation
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# [![Sia Core](https://sia.tech/assets/banners/sia-banner-core.png)](http://sia.tech)
2+
3+
[![GoDoc](https://godoc.org/go.sia.tech/coreutils?status.svg)](https://godoc.org/go.sia.tech/coreutils)
4+
5+
This repo contains implementations of core Sia components, such as a P2P syncing/relay node, database-backed blockchain state manager, single-address wallet, and more. It is intended for use by Sia node implementations like `walletd`, `renterd`, and `hostd`.

db.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package coreutils
2+
3+
import (
4+
"go.etcd.io/bbolt"
5+
"go.sia.tech/core/chain"
6+
)
7+
8+
// BoltChainDB implements chain.DB with a BoltDB database.
9+
type BoltChainDB struct {
10+
tx *bbolt.Tx
11+
db *bbolt.DB
12+
}
13+
14+
func (db *BoltChainDB) newTx() (err error) {
15+
if db.tx == nil {
16+
db.tx, err = db.db.Begin(true)
17+
}
18+
return
19+
}
20+
21+
// Bucket implements chain.DB.
22+
func (db *BoltChainDB) Bucket(name []byte) chain.DBBucket {
23+
if err := db.newTx(); err != nil {
24+
panic(err)
25+
}
26+
// NOTE: can't simply return db.tx.Bucket here, since it returns a concrete
27+
// type and we need a nil interface if the bucket does not exist
28+
b := db.tx.Bucket(name)
29+
if b == nil {
30+
return nil
31+
}
32+
return b
33+
}
34+
35+
// CreateBucket implements chain.DB.
36+
func (db *BoltChainDB) CreateBucket(name []byte) (chain.DBBucket, error) {
37+
if err := db.newTx(); err != nil {
38+
return nil, err
39+
}
40+
// NOTE: unlike Bucket, ok to return directly here, because caller should
41+
// always check err first
42+
return db.tx.CreateBucket(name)
43+
}
44+
45+
// Flush implements chain.DB.
46+
func (db *BoltChainDB) Flush() error {
47+
if db.tx == nil {
48+
return nil
49+
}
50+
err := db.tx.Commit()
51+
db.tx = nil
52+
return err
53+
}
54+
55+
// Cancel implements chain.DB.
56+
func (db *BoltChainDB) Cancel() {
57+
if db.tx == nil {
58+
return
59+
}
60+
db.tx.Rollback()
61+
db.tx = nil
62+
}
63+
64+
// Close closes the BoltDB database.
65+
func (db *BoltChainDB) Close() error {
66+
db.Flush()
67+
return db.db.Close()
68+
}
69+
70+
// NewBoltChainDB creates a new BoltChainDB.
71+
func NewBoltChainDB(db *bbolt.DB) *BoltChainDB {
72+
return &BoltChainDB{db: db}
73+
}
74+
75+
// OpenBoltChainDB opens a BoltDB database.
76+
func OpenBoltChainDB(path string) (*BoltChainDB, error) {
77+
db, err := bbolt.Open(path, 0600, nil)
78+
return NewBoltChainDB(db), err
79+
}

go.mod

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module go.sia.tech/coreutils
2+
3+
go 1.21.5
4+
5+
require (
6+
go.etcd.io/bbolt v1.3.8
7+
go.sia.tech/core v0.1.12
8+
)
9+
10+
require (
11+
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
12+
golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 // indirect
13+
golang.org/x/sys v0.5.0 // indirect
14+
lukechampine.com/frand v1.4.2 // indirect
15+
)

go.sum

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY=
2+
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da/go.mod h1:eHEWzANqSiWQsof+nXEI9bUVUyV6F53Fp89EuCh2EAA=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
8+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
9+
go.etcd.io/bbolt v1.3.8 h1:xs88BrvEv273UsB79e0hcVrlUWmS0a8upikMFhSyAtA=
10+
go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
11+
go.sia.tech/core v0.1.11 h1:fRI8Mp1G280pOy0WYXSu6nI5dDDGPjHX1fbDwpwZLvc=
12+
go.sia.tech/core v0.1.11/go.mod h1:D17UWSn99SEfQnEaR9G9n6Kz9+BwqMoUgZ6Cl424LsQ=
13+
go.sia.tech/core v0.1.12 h1:nrq/BvYbTGVLtZu0MHBTExUAP5nfNbcGhaJbuK839gc=
14+
go.sia.tech/core v0.1.12/go.mod h1:3EoY+rR78w1/uGoXXVqcYdwSjSJKuEMI5bL7WROA27Q=
15+
golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 h1:NvGWuYG8dkDHFSKksI1P9faiVJ9rayE6l0+ouWVIDs8=
16+
golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
17+
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
18+
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
19+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
20+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
21+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
22+
lukechampine.com/frand v1.4.2 h1:RzFIpOvkMXuPMBb9maa4ND4wjBn71E1Jpf8BzJHMaVw=
23+
lukechampine.com/frand v1.4.2/go.mod h1:4S/TM2ZgrKejMcKMbeLjISpJMO+/eZ1zu3vYX9dtj3s=

0 commit comments

Comments
 (0)