You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
5.5 KiB
165 lines
5.5 KiB
package payouts |
|
|
|
import ( |
|
"github.com/yuriy0803/open-etc-pool-friends/rpc" |
|
"github.com/yuriy0803/open-etc-pool-friends/storage" |
|
"math/big" |
|
"os" |
|
"testing" |
|
) |
|
|
|
func TestMain(m *testing.M) { |
|
os.Exit(m.Run()) |
|
} |
|
|
|
func TestCalculateRewards(t *testing.T) { |
|
blockReward, _ := new(big.Rat).SetString("5000000000000000000") |
|
shares := map[string]int64{"0x0": 1000000, "0x1": 20000, "0x2": 5000, "0x3": 10, "0x4": 1} |
|
expectedRewards := map[string]int64{"0x0": 4877996431, "0x1": 97559929, "0x2": 24389982, "0x3": 48780, "0x4": 4878} |
|
totalShares := int64(1025011) |
|
|
|
rewards := calculateRewardsForShares(shares, totalShares, blockReward) |
|
expectedTotalAmount := int64(5000000000) |
|
|
|
totalAmount := int64(0) |
|
for login, amount := range rewards { |
|
totalAmount += amount |
|
|
|
if expectedRewards[login] != amount { |
|
t.Errorf("Amount for %v must be equal to %v vs %v", login, expectedRewards[login], amount) |
|
} |
|
} |
|
if totalAmount != expectedTotalAmount { |
|
t.Errorf("Total reward must be equal to block reward in Shannon: %v vs %v", expectedTotalAmount, totalAmount) |
|
} |
|
} |
|
|
|
func TestChargeFee(t *testing.T) { |
|
orig, _ := new(big.Rat).SetString("5000000000000000000") |
|
value, _ := new(big.Rat).SetString("5000000000000000000") |
|
expectedNewValue, _ := new(big.Rat).SetString("3750000000000000000") |
|
expectedFee, _ := new(big.Rat).SetString("1250000000000000000") |
|
newValue, fee := chargeFee(orig, 25.0) |
|
|
|
if orig.Cmp(value) != 0 { |
|
t.Error("Must not change original value") |
|
} |
|
if newValue.Cmp(expectedNewValue) != 0 { |
|
t.Error("Must charge and deduct correct fee") |
|
} |
|
if fee.Cmp(expectedFee) != 0 { |
|
t.Error("Must charge fee") |
|
} |
|
} |
|
|
|
func TestWeiToShannonInt64(t *testing.T) { |
|
wei, _ := new(big.Rat).SetString("1000000000000000000") |
|
origWei, _ := new(big.Rat).SetString("1000000000000000000") |
|
shannon := int64(1000000000) |
|
|
|
if weiToShannonInt64(wei) != shannon { |
|
t.Error("Must convert to Shannon") |
|
} |
|
if wei.Cmp(origWei) != 0 { |
|
t.Error("Must charge original value") |
|
} |
|
} |
|
|
|
func TestGetBlockEra(t *testing.T) { |
|
blockNum := big.NewInt(11700000) |
|
eraLength := big.NewInt(5000000) |
|
era := GetBlockEra(blockNum, eraLength) |
|
if era.Cmp(big.NewInt(2)) != 0 { |
|
t.Error("Should return Era 2", "era", era) |
|
} |
|
// handle negative blockNum |
|
blockNum = big.NewInt(-50000) |
|
era = GetBlockEra(blockNum, eraLength) |
|
if era.Cmp(big.NewInt(0)) != 0 { |
|
t.Error("Should return Era 0", "era", era) |
|
} |
|
// handle negative blockNum |
|
blockNum = big.NewInt(5000001) |
|
era = GetBlockEra(blockNum, eraLength) |
|
if era.Cmp(big.NewInt(1)) != 0 { |
|
t.Error("Should return Era 1", "era", era) |
|
} |
|
} |
|
|
|
func TestGetBlockWinnerRewardByEra(t *testing.T) { |
|
baseReward := big.NewInt(5000000000000000000) |
|
era := big.NewInt(0) |
|
blockReward := GetBlockWinnerRewardByEra(era, baseReward) |
|
if blockReward.Cmp(big.NewInt(5000000000000000000)) != 0 { |
|
t.Error("Should return blockReward 5000000000000000000", "reward", blockReward) |
|
} |
|
era = big.NewInt(1) |
|
blockReward = GetBlockWinnerRewardByEra(era, baseReward) |
|
if blockReward.Cmp(big.NewInt(4000000000000000000)) != 0 { |
|
t.Error("Should return blockReward 4000000000000000000", "reward", blockReward) |
|
} |
|
era = big.NewInt(2) |
|
blockReward = GetBlockWinnerRewardByEra(era, baseReward) |
|
if blockReward.Cmp(big.NewInt(3200000000000000000)) != 0 { |
|
t.Error("Should return blockReward 3200000000000000000", "reward", blockReward) |
|
} |
|
era = big.NewInt(3) |
|
blockReward = GetBlockWinnerRewardByEra(era, baseReward) |
|
if blockReward.Cmp(big.NewInt(2560000000000000000)) != 0 { |
|
t.Error("Should return blockReward 2560000000000000000", "reward", blockReward) |
|
} |
|
era = big.NewInt(4) |
|
blockReward = GetBlockWinnerRewardByEra(era, baseReward) |
|
if blockReward.Cmp(big.NewInt(2048000000000000000)) != 0 { |
|
t.Error("Should return blockReward 2048000000000000000", "reward", blockReward) |
|
} |
|
} |
|
|
|
func TestGetRewardForUncle(t *testing.T) { |
|
baseReward := big.NewInt(4000000000000000000) |
|
uncleReward := getRewardForUncle(baseReward) |
|
if uncleReward.Cmp(big.NewInt(125000000000000000)) != 0 { |
|
t.Error("Should return uncleReward 125000000000000000", "reward", uncleReward) |
|
} |
|
baseReward = big.NewInt(3200000000000000000) |
|
uncleReward = getRewardForUncle(baseReward) |
|
if uncleReward.Cmp(big.NewInt(100000000000000000)) != 0 { |
|
t.Error("Should return uncleReward 100000000000000000", "reward", uncleReward) |
|
} |
|
baseReward = big.NewInt(2560000000000000000) |
|
uncleReward = getRewardForUncle(baseReward) |
|
if uncleReward.Cmp(big.NewInt(80000000000000000)) != 0 { |
|
t.Error("Should return uncleReward 80000000000000000", "reward", uncleReward) |
|
} |
|
baseReward = big.NewInt(2048000000000000000) |
|
uncleReward = getRewardForUncle(baseReward) |
|
if uncleReward.Cmp(big.NewInt(64000000000000000)) != 0 { |
|
t.Error("Should return uncleReward 64000000000000000", "reward", uncleReward) |
|
} |
|
} |
|
|
|
func TestMatchCandidate(t *testing.T) { |
|
gethBlock := &rpc.GetBlockReply{Hash: "0x12345A", Nonce: "0x1A"} |
|
parityBlock := &rpc.GetBlockReply{Hash: "0x12345A", SealFields: []string{"0x0A", "0x1A"}} |
|
candidate := &storage.BlockData{Nonce: "0x1a"} |
|
orphan := &storage.BlockData{Nonce: "0x1abc"} |
|
|
|
if !matchCandidate(gethBlock, candidate) { |
|
t.Error("Must match with nonce") |
|
} |
|
if !matchCandidate(parityBlock, candidate) { |
|
t.Error("Must match with seal fields") |
|
} |
|
if matchCandidate(gethBlock, orphan) { |
|
t.Error("Must not match with orphan with nonce") |
|
} |
|
if matchCandidate(parityBlock, orphan) { |
|
t.Error("Must not match orphan with seal fields") |
|
} |
|
|
|
block := &rpc.GetBlockReply{Hash: "0x12345A"} |
|
immature := &storage.BlockData{Hash: "0x12345a", Nonce: "0x0"} |
|
if !matchCandidate(block, immature) { |
|
t.Error("Must match with hash") |
|
} |
|
}
|
|
|