github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/consensus/tribe/tribe_test.go (about)

     1  package tribe
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"crypto/elliptic"
     6  	"crypto/rand"
     7  	"encoding/hex"
     8  	"math/big"
     9  	mrand "math/rand"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/SmartMeshFoundation/Spectrum/core/types"
    14  
    15  	"github.com/SmartMeshFoundation/Spectrum/core/state"
    16  	"github.com/SmartMeshFoundation/Spectrum/params"
    17  
    18  	"github.com/SmartMeshFoundation/Spectrum/accounts/keystore"
    19  	"github.com/SmartMeshFoundation/Spectrum/common"
    20  	"github.com/SmartMeshFoundation/Spectrum/crypto"
    21  	"github.com/SmartMeshFoundation/Spectrum/ethdb"
    22  )
    23  
    24  func TestNormal(t *testing.T) {
    25  	prv, _ := ecdsa.GenerateKey(crypto.S256(), rand.Reader)
    26  	t.Log(prv)
    27  	t.Log(prv.PublicKey)
    28  	pub := prv.PublicKey
    29  	pk := crypto.PubkeyToAddress(prv.PublicKey)
    30  	t.Log(pk.Hex())
    31  	m := elliptic.Marshal(crypto.S256(), pub.X, pub.Y)
    32  	t.Log("m=", m)
    33  	x, y := elliptic.Unmarshal(crypto.S256(), m)
    34  	t.Log("x,y=", x, y)
    35  
    36  }
    37  
    38  func TestDelay(t *testing.T) {
    39  	for signerLen := 1; signerLen < 11; signerLen += 1 {
    40  		wiggle := time.Duration(signerLen/2+1) * wiggleTime
    41  		delay := time.Duration(mrand.Int63n(int64(wiggle)))
    42  		t.Log(signerLen, wiggle, "\t", delay)
    43  	}
    44  	for signerLen := 10; signerLen < 110; signerLen += 10 {
    45  		wiggle := time.Duration(signerLen/2+1) * wiggleTime
    46  		delay := time.Duration(mrand.Int63n(int64(wiggle)))
    47  		t.Log(signerLen, wiggle, "\t", delay)
    48  	}
    49  }
    50  
    51  func TestNodekey(t *testing.T) {
    52  	prv, _ := ecdsa.GenerateKey(crypto.S256(), rand.Reader)
    53  	k := hex.EncodeToString(crypto.FromECDSA(prv))
    54  	t.Log(k)
    55  
    56  	json := `{"address":"4110bd1ff0b73fa12c259acf39c950277f266787","crypto":{"cipher":"aes-128-ctr","ciphertext":"3c12a4bb0048503ea6874c9357b046f3585cdb3af3219618e56220e69d01e5f8","cipherparams":{"iv":"e3e516858fa657437ab53aca9bd48b30"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"63795963bf80c8acff4c4c7f2f33f83b2ebf8f82747c057992cab92ba80beca6"},"mac":"12672d255fbbf0ef9374d9f37da7d25b39ea65b9dec3e63ff6055950dbb48103"},"id":"d541af26-402e-4cdb-841d-8bb700c09887","version":3}`
    57  	key, _ := keystore.DecryptKey([]byte(json), "123456")
    58  	prv2 := key.PrivateKey
    59  	t.Log(crypto.SaveECDSA("/tmp/nodekey", prv2))
    60  
    61  	prv3, _ := crypto.HexToECDSA("4755839cb237126c4317a858d85d3837745615b1f67eb20de260416658c43b19")
    62  	t.Log(&prv2)
    63  	t.Log(&prv3)
    64  
    65  }
    66  
    67  func TestHexToPrv(t *testing.T) {
    68  	h := "4755839cb237126c4317a858d85d3837745615b1f67eb20de260416658c43b19"
    69  	prv, e := crypto.HexToECDSA(h)
    70  	t.Log(e)
    71  	pub := prv.PublicKey
    72  	pbytes := elliptic.Marshal(pub.Curve, pub.X, pub.Y)
    73  	nodeid := pbytes[1:]
    74  	t.Log(crypto.PubkeyToAddress(pub).Hex())
    75  	t.Log(h)
    76  	t.Log(hex.EncodeToString(nodeid))
    77  	//f9cf4fdc53ce5ecf84e7c26fbe262bddd7cbf3d17593328f74816a1c646a0ccfac9a85d81f7d51b59bc02dc8f0e8c5dada4db081efd79698a820faf9384773c0
    78  }
    79  
    80  func TestKeyAddressExange(t *testing.T) {
    81  	nodekey := "6188edbba7ba30e7c29f314fea016937816598c8d43a903dabea58bef189d09a"
    82  	prv, _ := crypto.HexToECDSA(nodekey)
    83  	target := "bff277e91e8adeff5c39bb5969f7c8d9be27db76cb61080dc1f1b98601029eb69912731717580dfc140fe331a7953fcba211d59b1d2b086fb669dbdedbf1f717"
    84  	pub := prv.PublicKey
    85  	pbytes := elliptic.Marshal(pub.Curve, pub.X, pub.Y)
    86  	t.Log(pbytes)
    87  	nodeid := pbytes[1:]
    88  	t.Log("---------------")
    89  	t.Log(target)
    90  	t.Log(hex.EncodeToString(nodeid))
    91  	t.Logf("%x\n", nodeid)
    92  	t.Log(hex.EncodeToString(nodeid) == target)
    93  	pub2 := new(ecdsa.PublicKey)
    94  	pub2.Curve = pub.Curve
    95  	tb, _ := hex.DecodeString(target)
    96  	tb = append([]byte{4}, tb...)
    97  	t.Log(tb)
    98  	pub2.X, pub2.Y = elliptic.Unmarshal(pub.Curve, tb)
    99  	t.Log(pub)
   100  	t.Log(pub2)
   101  }
   102  
   103  func TestExtra(t *testing.T) {
   104  	extra := []byte("0xd68301000083736d6386676f312e31328664617277696e00000000000000000009b5bd71d0cababfc178e89f63210d322675d53d16bdb58ce5741e06d78f60bd4e2e517089c35d9e278bd6bbf52b6f77746ff314c6cf3237a27209a3e922035300")
   105  	t.Log(len(extra))
   106  }
   107  
   108  func TestAccumulateRewards(t *testing.T) {
   109  	db, _ := ethdb.NewMemDatabase()
   110  	state, _ := state.New(common.Hash{}, state.NewDatabase(db))
   111  	config := params.DevnetChainConfig
   112  	header := &types.Header{
   113  		Coinbase: common.HexToAddress("0x01"),
   114  	}
   115  	//noreward
   116  	expectReward := big.NewInt(0)
   117  	header.Number = new(big.Int).Set(config.Chief100Block)
   118  	header.Number.Sub(header.Number, big.NewInt(1))
   119  	accumulateRewards(config, state, header)
   120  	if state.GetBalance(header.Coinbase).Cmp(expectReward) != 0 {
   121  		t.Error("should no reward before chief100Block")
   122  		return
   123  	}
   124  
   125  	//full reward
   126  	expectReward.Add(expectReward, Chief100BlockReward)
   127  	header.Number = new(big.Int).Set(config.Chief100Block)
   128  	accumulateRewards(config, state, header)
   129  	if state.GetBalance(header.Coinbase).Cmp(expectReward) != 0 {
   130  		t.Errorf("should get total reward=%s,but got=%s", expectReward, state.GetBalance(header.Coinbase))
   131  		return
   132  	}
   133  
   134  	//half reward
   135  	currentReward := new(big.Int).Set(Chief100BlockReward)
   136  	currentReward = currentReward.Div(currentReward, big.NewInt(2))
   137  	expectReward = expectReward.Add(expectReward, currentReward)
   138  	header.Number = new(big.Int).Set(config.Chief100Block)
   139  	header.Number = header.Number.Add(header.Number, big.NewInt(int64(BlockRewardReducedInterval)))
   140  	accumulateRewards(config, state, header)
   141  	if state.GetBalance(header.Coinbase).Cmp(expectReward) != 0 {
   142  		t.Errorf("should get total reward=%s,but got=%s", expectReward, state.GetBalance(header.Coinbase))
   143  		return
   144  	}
   145  
   146  	//1/4 reward
   147  	currentReward = new(big.Int).Set(Chief100BlockReward)
   148  	currentReward.Div(currentReward, big.NewInt(4))
   149  	expectReward.Add(expectReward, currentReward)
   150  	header.Number = new(big.Int).Set(config.Chief100Block)
   151  	header.Number = header.Number.Add(header.Number, big.NewInt(int64(BlockRewardReducedInterval*2)))
   152  	accumulateRewards(config, state, header)
   153  	if state.GetBalance(header.Coinbase).Cmp(expectReward) != 0 {
   154  		t.Errorf("should get total reward=%s,but got=%s", expectReward, state.GetBalance(header.Coinbase))
   155  		return
   156  	}
   157  }
   158  
   159  func TestTribeStatus_destroySmartMeshFoundation12Balance(t *testing.T) {
   160  	db, _ := ethdb.NewMemDatabase()
   161  	state, _ := state.New(common.Hash{}, state.NewDatabase(db))
   162  	config := params.MainnetChainConfig
   163  	header := &types.Header{
   164  		Coinbase: common.HexToAddress("0x01"),
   165  		Number:   config.Chief100Block,
   166  	}
   167  	b := new(big.Int).Set(SmartMeshFoundationAccountDestroyBalance)
   168  	curBalance := b.Mul(b, big.NewInt(2))
   169  
   170  	config.Chief100Block = big.NewInt(300000)
   171  	header.Number = config.Chief100Block
   172  	state.AddBalance(SmartMeshFoundationAccount, big.NewInt(3000))
   173  	destroySmartMeshFoundation12Balance(config, state, header)
   174  	nb := state.GetBalance(SmartMeshFoundationAccount)
   175  	if nb.Cmp(big.NewInt(0)) != 0 {
   176  		t.Error("destroy all inf not enough")
   177  		return
   178  	}
   179  
   180  	state.AddBalance(SmartMeshFoundationAccount, curBalance)
   181  	destroySmartMeshFoundation12Balance(config, state, header)
   182  	nb = state.GetBalance(SmartMeshFoundationAccount)
   183  	left := curBalance.Div(curBalance, big.NewInt(2))
   184  	if nb.Cmp(left) != 0 {
   185  		t.Error("should only half left")
   186  		return
   187  	}
   188  }