github.com/devwanda/aphelion-staking@v0.33.9/p2p/key_test.go (about)

     1  package p2p
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	tmrand "github.com/devwanda/aphelion-staking/libs/rand"
    12  )
    13  
    14  func TestLoadOrGenNodeKey(t *testing.T) {
    15  	filePath := filepath.Join(os.TempDir(), tmrand.Str(12)+"_peer_id.json")
    16  
    17  	nodeKey, err := LoadOrGenNodeKey(filePath)
    18  	assert.Nil(t, err)
    19  
    20  	nodeKey2, err := LoadOrGenNodeKey(filePath)
    21  	assert.Nil(t, err)
    22  
    23  	assert.Equal(t, nodeKey, nodeKey2)
    24  }
    25  
    26  //----------------------------------------------------------
    27  
    28  func padBytes(bz []byte, targetBytes int) []byte {
    29  	return append(bz, bytes.Repeat([]byte{0xFF}, targetBytes-len(bz))...)
    30  }
    31  
    32  func TestPoWTarget(t *testing.T) {
    33  
    34  	targetBytes := 20
    35  	cases := []struct {
    36  		difficulty uint
    37  		target     []byte
    38  	}{
    39  		{0, padBytes([]byte{}, targetBytes)},
    40  		{1, padBytes([]byte{127}, targetBytes)},
    41  		{8, padBytes([]byte{0}, targetBytes)},
    42  		{9, padBytes([]byte{0, 127}, targetBytes)},
    43  		{10, padBytes([]byte{0, 63}, targetBytes)},
    44  		{16, padBytes([]byte{0, 0}, targetBytes)},
    45  		{17, padBytes([]byte{0, 0, 127}, targetBytes)},
    46  	}
    47  
    48  	for _, c := range cases {
    49  		assert.Equal(t, MakePoWTarget(c.difficulty, 20*8), c.target)
    50  	}
    51  }