Allow metadata storage on blocks

This commit is contained in:
DataHoarder 2024-04-13 06:44:29 +02:00
parent c7b42267c7
commit 08f79500c0
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
3 changed files with 48 additions and 24 deletions

View file

@ -18,6 +18,12 @@ import (
// EpochSize Maximum amount of blocks without a full one
const EpochSize = 32
const (
FlagStorePruned = 1 << iota
FlagStoreCompact
FlagStoreMetadata
)
type Cache struct {
db *bolt.DB
consensus *sidechain.Consensus
@ -66,7 +72,7 @@ func multiRecordFromBytes(b []byte) multiRecord {
return nil
}
return slices.Clone(unsafe.Slice((*types.Hash)(unsafe.Pointer(&b[0])), len(b)/types.HashSize))
return slices.Clone(unsafe.Slice((*types.Hash)(unsafe.Pointer(unsafe.SliceData(b))), len(b)/types.HashSize))
}
func (r multiRecord) Contains(hash types.Hash) bool {
@ -77,7 +83,7 @@ func (r multiRecord) Bytes() []byte {
if len(r) == 0 {
return nil
}
return slices.Clone(unsafe.Slice((*byte)(unsafe.Pointer(&r[0])), len(r)*types.HashSize))
return slices.Clone(unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(r))), len(r)*types.HashSize))
}
func (c *Cache) Store(block *sidechain.PoolBlock) {
@ -121,6 +127,8 @@ func (c *Cache) Store(block *sidechain.PoolBlock) {
}
}
metadataBlob, _ := block.Metadata.MarshalBinary()
if blob, err := block.AppendBinaryFlags(make([]byte, 0, block.BufferLength()), storePruned, storeCompact); err == nil {
utils.Noticef("Archive Cache", "Store block id = %s, template id = %s, height = %d, sidechain height = %d, depth = %d, pruned = %t, compact = %t, blob size = %d bytes", mainId.String(), sideId.String(), block.Main.Coinbase.GenHeight, block.Side.Height, block.Depth.Load(), storePruned, storeCompact, len(blob))
@ -129,13 +137,17 @@ func (c *Cache) Store(block *sidechain.PoolBlock) {
var flags uint64
if storePruned {
flags |= 0b1
flags |= FlagStorePruned
}
if storeCompact {
flags |= 0b10
flags |= FlagStoreCompact
}
buf := make([]byte, 0, len(blob)+8)
flags |= FlagStoreMetadata
buf := make([]byte, 0, len(blob)+8+4+len(metadataBlob))
buf = binary.LittleEndian.AppendUint64(buf, flags)
buf = binary.LittleEndian.AppendUint32(buf, uint32(len(metadataBlob)))
buf = append(buf, metadataBlob...)
buf = append(buf, blob...)
if err = b1.Put(mainId[:], buf); err != nil {
return err
@ -210,10 +222,24 @@ func (c *Cache) decodeBlock(blob []byte) *sidechain.PoolBlock {
}
flags := binary.LittleEndian.Uint64(blob)
blob = blob[8:]
var meta sidechain.PoolBlockReceptionMetadata
if (flags & FlagStoreMetadata) > 0 {
blobSize := binary.LittleEndian.Uint32(blob)
blob = blob[4:]
err := meta.UnmarshalBinary(blob[:blobSize])
if err != nil {
utils.Errorf("Archive Cache", "error decoding block metadata: %s", err)
}
blob = blob[blobSize:]
}
b := &sidechain.PoolBlock{}
reader := bytes.NewReader(blob[8:])
if (flags & 0b10) > 0 {
reader := bytes.NewReader(blob)
if (flags & FlagStoreCompact) > 0 {
if err := b.FromCompactReader(c.consensus, c.derivationCache, reader); err != nil {
utils.Errorf("Archive Cache", "error decoding block: %s", err)
return nil
@ -225,6 +251,8 @@ func (c *Cache) decodeBlock(blob []byte) *sidechain.PoolBlock {
}
}
b.Metadata = meta
return b
}

10
go.mod
View file

@ -3,23 +3,23 @@ module git.gammaspectra.live/P2Pool/observer-cache-archive
go 1.22
require (
git.gammaspectra.live/P2Pool/consensus/v3 v3.4.0
git.gammaspectra.live/P2Pool/consensus/v3 v3.6.3
go.etcd.io/bbolt v1.3.9
)
require (
git.gammaspectra.live/P2Pool/edwards25519 v0.0.0-20240405085108-e2f706cb5c00 // indirect
git.gammaspectra.live/P2Pool/go-randomx v0.0.0-20221027085532-f46adfce03a7 // indirect
git.gammaspectra.live/P2Pool/go-randomx/v2 v2.1.0 // indirect
git.gammaspectra.live/P2Pool/monero-base58 v1.0.0 // indirect
git.gammaspectra.live/P2Pool/randomx-go-bindings v0.0.0-20230514082649-9c5f18cd5a71 // indirect
git.gammaspectra.live/P2Pool/randomx-go-bindings v1.0.0 // indirect
git.gammaspectra.live/P2Pool/sha3 v0.17.0 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/dolthub/maphash v0.1.0 // indirect
github.com/dolthub/swiss v0.2.2-0.20240312182618-f4b2babd2bc1 // indirect
github.com/floatdrop/lru v1.3.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
lukechampine.com/uint128 v1.3.0 // indirect
)

20
go.sum
View file

@ -1,31 +1,27 @@
git.gammaspectra.live/P2Pool/consensus/v3 v3.4.0 h1:8rWe3h1lDS1aWTGn+bL0QGKKLGMltlb0L176KQc+zZ4=
git.gammaspectra.live/P2Pool/consensus/v3 v3.4.0/go.mod h1:pI4X4sieSwKWicOkRZ90UZEQeZbvd2swzYjwKfGp46Y=
git.gammaspectra.live/P2Pool/consensus/v3 v3.6.3 h1:bDCJzl5Rm2VfSrRY7gLVzK43Y8JxznTTgZvpHcFmDnE=
git.gammaspectra.live/P2Pool/consensus/v3 v3.6.3/go.mod h1:3mjTaEx2Hm9VUs0rZIjCAFV6fToNxnVDj616NEc9Y0I=
git.gammaspectra.live/P2Pool/edwards25519 v0.0.0-20240405085108-e2f706cb5c00 h1:mDQY337iKB+kle5RYWL5CoAz+3DmnkAh/B2XD8B+PFk=
git.gammaspectra.live/P2Pool/edwards25519 v0.0.0-20240405085108-e2f706cb5c00/go.mod h1:FZsrMWGucMP3SZamzrd7m562geIs5zp1O/9MGoiAKH0=
git.gammaspectra.live/P2Pool/go-randomx v0.0.0-20221027085532-f46adfce03a7 h1:bzHDuu1IgETKqPBOlIdCE2LaZIJ+ZpROSprNn+fnzd8=
git.gammaspectra.live/P2Pool/go-randomx v0.0.0-20221027085532-f46adfce03a7/go.mod h1:3kT0v4AMwT/OdorfH2gRWPwoOrUX/LV03HEeBsaXG1c=
git.gammaspectra.live/P2Pool/go-randomx/v2 v2.1.0 h1:L1fV2XBYFmpFU+JKP/7fsgDm2Lfh9yFlS+800v+3OsM=
git.gammaspectra.live/P2Pool/go-randomx/v2 v2.1.0/go.mod h1:vNmHlEIRAcU/bA85mxbUKEiBYtrtS4MVwozf29KmoHM=
git.gammaspectra.live/P2Pool/monero-base58 v1.0.0 h1:s8LZxVNc93YEs2NCCNWZ7CKr8RbEb031y6Wkvhn+TS4=
git.gammaspectra.live/P2Pool/monero-base58 v1.0.0/go.mod h1:WWEJy/AdWKxKAruvlKI82brw+DtVlePy0ct3ZiBlc68=
git.gammaspectra.live/P2Pool/randomx-go-bindings v0.0.0-20230514082649-9c5f18cd5a71 h1:MgeHHcF+GnCJBWMSzq8XAbc8p/UhNwFruEKCPPJ74YQ=
git.gammaspectra.live/P2Pool/randomx-go-bindings v0.0.0-20230514082649-9c5f18cd5a71/go.mod h1:KQaYHIxGXNHNMQELC7xGLu8xouwvP/dN7iGk681BXmk=
git.gammaspectra.live/P2Pool/randomx-go-bindings v1.0.0 h1:tajr4QFSPrb8NtHmU14JaXdhr+z+0RbOBLIgUDI5Tow=
git.gammaspectra.live/P2Pool/randomx-go-bindings v1.0.0/go.mod h1:S17NNidG5hxqaVLsSykKqDBg/hTPSzP0KcSwXfH8WIA=
git.gammaspectra.live/P2Pool/sha3 v0.17.0 h1:CZpB466LPbNVQrUNjQTtQScGDc30xSMkZ6Bmw0W9VuM=
git.gammaspectra.live/P2Pool/sha3 v0.17.0/go.mod h1:HmrrYa97BZTKklUk2n/wAY+wrY0gHhoSGRd2+lIqXq8=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
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/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
github.com/dolthub/maphash v0.1.0/go.mod h1:gkg4Ch4CdCDu5h6PMriVLawB7koZ+5ijb9puGMV50a4=
github.com/dolthub/swiss v0.2.2-0.20240312182618-f4b2babd2bc1 h1:F7u1ZVCidajlPuJJzdL5REPHfLO/O6xLQRUw/YMxrkM=
github.com/dolthub/swiss v0.2.2-0.20240312182618-f4b2babd2bc1/go.mod h1:8AhKZZ1HK7g18j7v7k6c5cYIGEZJcPn0ARsai8cUrh0=
github.com/floatdrop/lru v1.3.0 h1:83abtaKjXcWrPmtzTAk2Ggq8DUKqI29YzrTrB8+vu0c=
github.com/floatdrop/lru v1.3.0/go.mod h1:83zlXKA06Bm32JImNINCiTr0ldadvdAjUe5jSwIaw0s=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8=
github.com/sclevine/spec v1.4.0/go.mod h1:LvpgJaFyvQzRvc1kaDs0bulYwzC70PbiYjC4QnFHkOM=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=