2022-10-26 16:46:41 +00:00
package sidechain
2022-10-08 18:55:01 +00:00
import (
2023-03-05 14:06:49 +00:00
"context"
2022-10-08 18:55:01 +00:00
"encoding/hex"
2022-11-05 05:29:12 +00:00
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/address"
2022-10-26 16:46:41 +00:00
block2 "git.gammaspectra.live/P2Pool/p2pool-observer/monero/block"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/client"
2023-03-05 14:06:49 +00:00
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/randomx"
2022-10-08 18:55:01 +00:00
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"io"
"log"
"os"
"testing"
)
2022-11-01 11:22:00 +00:00
func TestPoolBlockDecode ( t * testing . T ) {
2022-12-16 19:32:57 +00:00
client . SetDefaultClientSettings ( os . Getenv ( "MONEROD_RPC_URL" ) )
2022-10-26 16:46:41 +00:00
2022-10-08 18:55:01 +00:00
f , err := os . Open ( "testdata/1783223a701d16192ce9ff83c603b48b3e1785e3779b42079ede6e52ea7f0d2d.hex" )
if err != nil {
t . Fatal ( err )
}
defer f . Close ( )
buf , err := io . ReadAll ( f )
if err != nil {
t . Fatal ( err )
}
contents , err := hex . DecodeString ( string ( buf ) )
if err != nil {
t . Fatal ( err )
}
2023-03-07 17:57:06 +00:00
block , err := NewShareFromExportedBytes ( contents , NetworkMainnet )
2022-10-08 18:55:01 +00:00
if err != nil {
t . Fatal ( err )
}
2022-10-28 09:47:14 +00:00
mainId := block . MainId ( )
if hex . EncodeToString ( mainId [ : ] ) != "05892769e709b6cfebd5d71e5cadf38ba0abde8048a0eea3792d981861ad9a69" {
t . Fatalf ( "expected main id 05892769e709b6cfebd5d71e5cadf38ba0abde8048a0eea3792d981861ad9a69, got %s" , mainId . String ( ) )
2022-10-08 18:55:01 +00:00
}
2022-10-28 09:47:14 +00:00
if hex . EncodeToString ( block . CoinbaseExtra ( SideTemplateId ) ) != "1783223a701d16192ce9ff83c603b48b3e1785e3779b42079ede6e52ea7f0d2d" {
t . Fatalf ( "expected side id 1783223a701d16192ce9ff83c603b48b3e1785e3779b42079ede6e52ea7f0d2d, got %s" , hex . EncodeToString ( block . CoinbaseExtra ( SideTemplateId ) ) )
2022-10-26 16:46:41 +00:00
}
b1 , _ := block . Main . MarshalBinary ( )
main2 := & block2 . Block { }
if err = main2 . UnmarshalBinary ( b1 ) ; err != nil {
t . Fatal ( err )
}
if main2 . Id ( ) != block . Main . Id ( ) {
t . Fatalf ( "expected main id %s, got %s" , block . Main . Id ( ) . String ( ) , main2 . Id ( ) )
2022-10-08 18:55:01 +00:00
}
2022-10-26 16:46:41 +00:00
b2 , _ := block . Main . MarshalBinary ( )
side2 := & SideData { }
2023-03-07 17:57:06 +00:00
if err = side2 . UnmarshalBinary ( b2 , block . ShareVersion ( ) ) ; err != nil {
2022-10-26 16:46:41 +00:00
t . Fatal ( err )
}
2022-10-28 09:47:14 +00:00
t . Log ( block . SideTemplateId ( ConsensusDefault ) . String ( ) )
2022-10-26 16:46:41 +00:00
2022-10-28 09:47:14 +00:00
t . Log ( block . Side . CoinbasePrivateKey . String ( ) )
2023-03-07 17:57:06 +00:00
t . Log ( address . GetDeterministicTransactionPrivateKey ( block . GetPrivateKeySeed ( ) , block . Main . PreviousId ) . String ( ) )
2022-10-26 16:46:41 +00:00
2022-10-08 18:55:01 +00:00
txId := block . Main . Coinbase . Id ( )
2022-11-05 05:29:12 +00:00
if txId . String ( ) != "41e8976fafbf9263996733b8f857a11ca385a78798c33617af8c77cfd989da60" {
t . Fatalf ( "expected coinbase id 41e8976fafbf9263996733b8f857a11ca385a78798c33617af8c77cfd989da60, got %s" , txId . String ( ) )
2022-10-08 18:55:01 +00:00
}
proofResult , _ := types . DifficultyFromString ( "00000000000000000000006ef6334490" )
2023-03-05 14:06:49 +00:00
getSeedByHeight := func ( height uint64 ) ( hash types . Hash ) {
seedHeight := randomx . SeedHeight ( height )
if h , err := client . GetDefaultClient ( ) . GetBlockHeaderByHeight ( seedHeight , context . Background ( ) ) ; err != nil {
return types . ZeroHash
} else {
hash , _ := types . HashFromString ( h . BlockHeader . Hash )
return hash
}
}
getDifficultyByHeight := func ( height uint64 ) types . Difficulty {
if h , err := client . GetDefaultClient ( ) . GetBlockHeaderByHeight ( height , context . Background ( ) ) ; err != nil {
return types . ZeroDifficulty
} else {
return types . DifficultyFrom64 ( h . BlockHeader . Difficulty )
}
}
if types . DifficultyFromPoW ( block . PowHash ( getSeedByHeight ) ) . Cmp ( proofResult ) != 0 {
t . Fatalf ( "expected PoW difficulty %s, got %s" , proofResult . String ( ) , types . DifficultyFromPoW ( block . PowHash ( getSeedByHeight ) ) . String ( ) )
2022-10-08 18:55:01 +00:00
}
2022-10-26 16:46:41 +00:00
t . Log ( block . Main . Id ( ) . String ( ) )
//t.Log(block.Main.PowHash().String())
//t.Log(block.Main.PowHash().String())
2023-03-05 14:06:49 +00:00
if ! block . IsProofHigherThanMainDifficulty ( getDifficultyByHeight , getSeedByHeight ) {
2022-10-08 18:55:01 +00:00
t . Fatal ( "expected proof higher than difficulty" )
}
2022-11-03 11:32:07 +00:00
block . cache . powHash [ 31 ] = 1
2022-10-08 18:55:01 +00:00
2023-03-05 14:06:49 +00:00
if block . IsProofHigherThanMainDifficulty ( getDifficultyByHeight , getSeedByHeight ) {
2022-10-08 18:55:01 +00:00
t . Fatal ( "expected proof lower than difficulty" )
}
2023-03-05 14:06:49 +00:00
log . Print ( types . DifficultyFromPoW ( block . PowHash ( getSeedByHeight ) ) . String ( ) )
log . Print ( block . MainDifficulty ( getDifficultyByHeight ) . String ( ) )
2022-10-08 18:55:01 +00:00
}