test(mining): integration test against C++ testnet daemon
Build-tagged //go:build integration. Fetches a real template, decodes it, computes header mining hash. Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
parent
340aa99db6
commit
f9ff8ad877
1 changed files with 58 additions and 0 deletions
58
mining/integration_test.go
Normal file
58
mining/integration_test.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
//go:build integration
|
||||
|
||||
// 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 mining
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"forge.lthn.ai/core/go-blockchain/rpc"
|
||||
"forge.lthn.ai/core/go-blockchain/wire"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIntegration_GetBlockTemplate(t *testing.T) {
|
||||
client := rpc.NewClient("http://localhost:46941")
|
||||
|
||||
// Get daemon info to check it's running.
|
||||
info, err := client.GetInfo()
|
||||
require.NoError(t, err, "daemon must be running on localhost:46941")
|
||||
t.Logf("daemon height: %d, pow_difficulty: %d", info.Height, info.PowDifficulty)
|
||||
|
||||
// Get a block template.
|
||||
// Use the testnet genesis coinbase address (all zeros — won't receive real
|
||||
// coins but the daemon accepts it for template generation).
|
||||
tmpl, err := client.GetBlockTemplate("iTHNtestaddr")
|
||||
if err != nil {
|
||||
t.Skipf("getblocktemplate failed (may need valid address): %v", err)
|
||||
}
|
||||
|
||||
assert.Greater(t, tmpl.Height, uint64(0))
|
||||
assert.NotEmpty(t, tmpl.Difficulty)
|
||||
assert.NotEmpty(t, tmpl.BlockTemplateBlob)
|
||||
|
||||
// Decode the template blob.
|
||||
blobBytes, err := hex.DecodeString(tmpl.BlockTemplateBlob)
|
||||
require.NoError(t, err)
|
||||
|
||||
dec := wire.NewDecoder(bytes.NewReader(blobBytes))
|
||||
block := wire.DecodeBlock(dec)
|
||||
require.NoError(t, dec.Err())
|
||||
|
||||
t.Logf("template: height=%d, major=%d, timestamp=%d, txs=%d",
|
||||
tmpl.Height, block.MajorVersion, block.Timestamp, len(block.TxHashes))
|
||||
|
||||
// Compute header mining hash.
|
||||
headerHash := HeaderMiningHash(&block)
|
||||
t.Logf("header mining hash: %s", hex.EncodeToString(headerHash[:]))
|
||||
|
||||
// Verify the header hash is non-zero.
|
||||
assert.False(t, headerHash == [32]byte{})
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue