Skip to content

Commit

Permalink
Make requested PR changes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Aug 1, 2021
1 parent 8e3b0a1 commit 96a5af8
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 75 deletions.
3 changes: 3 additions & 0 deletions consensus/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ func (d *Dev) writeNewBlock(parent *types.Header) error {

// Commit the changes
_, root := transition.Commit()
// Since the block transactions are firstly written, then the block is built (causing the txns to be written again),
// no need to double add staking / unstaking events that might have been added in the initial txn write,
// and have complicated logic in unstaking
staking.GetStakingHub().ClearEvents()

// Update the header
Expand Down
10 changes: 5 additions & 5 deletions e2e/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

"github.com/0xPolygon/minimal/crypto"
"github.com/0xPolygon/minimal/e2e/framework"
"github.com/0xPolygon/minimal/helper/currency"
"github.com/0xPolygon/minimal/helper/tests"
"github.com/0xPolygon/minimal/types"
"github.com/stretchr/testify/assert"
)

func TestBroadcast(t *testing.T) {
tests := []struct {
testTable := []struct {
name string
numNodes int
// Number of nodes that connects to left node
Expand All @@ -39,11 +39,11 @@ func TestBroadcast(t *testing.T) {

conf := func(config *framework.TestServerConfig) {
config.SetConsensus(framework.ConsensusDummy)
config.Premine(senderAddr, currency.EthToWei(10))
config.Premine(senderAddr, tests.EthToWei(10))
config.SetSeal(true)
}

for _, tt := range tests {
for _, tt := range testTable {
t.Run(tt.name, func(t *testing.T) {
srvs := framework.NewTestServers(t, tt.numNodes, conf)
framework.MultiJoinSerial(t, srvs[0:tt.numConnectedNodes])
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestBroadcast(t *testing.T) {
Nonce: 0,
From: senderAddr,
To: &receiverAddr,
Value: currency.EthToWei(1),
Value: tests.EthToWei(1),
Gas: 1000000,
GasPrice: big.NewInt(10000),
Input: []byte{},
Expand Down
4 changes: 2 additions & 2 deletions e2e/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/0xPolygon/minimal/e2e/framework"
"github.com/0xPolygon/minimal/helper/currency"
"github.com/0xPolygon/minimal/helper/tests"
"github.com/stretchr/testify/assert"
)

Expand All @@ -16,7 +16,7 @@ func TestEncoding(t *testing.T) {
srvs := framework.NewTestServers(t, 1, func(config *framework.TestServerConfig) {
config.SetConsensus(framework.ConsensusDev)
config.SetSeal(true)
config.Premine(from, currency.EthToWei(10))
config.Premine(from, tests.EthToWei(10))
})
srv := srvs[0]

Expand Down
14 changes: 7 additions & 7 deletions e2e/ibft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/0xPolygon/minimal/consensus/ibft"
"github.com/0xPolygon/minimal/helper/currency"
"github.com/0xPolygon/minimal/helper/tests"
"github.com/umbracle/go-web3"

"github.com/0xPolygon/minimal/e2e/framework"
Expand All @@ -20,8 +20,8 @@ func TestIbft_Transfer(t *testing.T) {
_, receiverAddr := framework.GenerateKeyAndAddr(t)

ibftManager := framework.NewIBFTServersManager(t, IBFTMinNodes, IBFTDirPrefix, func(i int, config *framework.TestServerConfig) {
config.Premine(senderAddr, currency.EthToWei(10))
config.PremineValidatorBalance(big.NewInt(0), currency.EthToWei(10))
config.Premine(senderAddr, tests.EthToWei(10))
config.PremineValidatorBalance(big.NewInt(0), tests.EthToWei(10))
config.SetSeal(true)
})

Expand All @@ -36,7 +36,7 @@ func TestIbft_Transfer(t *testing.T) {
To: &receiverAddr,
GasPrice: big.NewInt(10000),
Gas: 1000000,
Value: currency.EthToWei(1),
Value: tests.EthToWei(1),
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand All @@ -58,7 +58,7 @@ func TestIbft_TransactionFeeRecipient(t *testing.T) {
{
name: "transfer transaction",
contractCall: false,
txAmount: currency.EthToWei(1),
txAmount: tests.EthToWei(1),
},
{
name: "contract function execution",
Expand All @@ -73,8 +73,8 @@ func TestIbft_TransactionFeeRecipient(t *testing.T) {
_, receiverAddr := framework.GenerateKeyAndAddr(t)

ibftManager := framework.NewIBFTServersManager(t, IBFTMinNodes, IBFTDirPrefix, func(i int, config *framework.TestServerConfig) {
config.Premine(senderAddr, currency.EthToWei(10))
config.PremineValidatorBalance(big.NewInt(0), currency.EthToWei(10))
config.Premine(senderAddr, tests.EthToWei(10))
config.PremineValidatorBalance(big.NewInt(0), tests.EthToWei(10))
config.SetSeal(true)
})

Expand Down
6 changes: 3 additions & 3 deletions e2e/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/0xPolygon/minimal/e2e/framework"
"github.com/0xPolygon/minimal/helper/currency"
"github.com/0xPolygon/minimal/helper/tests"
"github.com/stretchr/testify/assert"
"github.com/umbracle/go-web3"
)
Expand All @@ -16,7 +16,7 @@ func TestNewFilter_Logs(t *testing.T) {
_, addr := framework.GenerateKeyAndAddr(t)
srvs := framework.NewTestServers(t, 1, func(config *framework.TestServerConfig) {
config.SetConsensus(framework.ConsensusDev)
config.Premine(addr, currency.EthToWei(10))
config.Premine(addr, tests.EthToWei(10))
config.SetSeal(true)
})
srv := srvs[0]
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestNewFilter_Block(t *testing.T) {

srvs := framework.NewTestServers(t, 1, func(config *framework.TestServerConfig) {
config.SetConsensus(framework.ConsensusDev)
config.Premine(from, currency.EthToWei(10))
config.Premine(from, tests.EthToWei(10))
config.SetSeal(true)
})
srv := srvs[0]
Expand Down
21 changes: 11 additions & 10 deletions e2e/pos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/0xPolygon/minimal/consensus/ibft/proto"
"github.com/0xPolygon/minimal/crypto"
"github.com/0xPolygon/minimal/e2e/framework"
"github.com/0xPolygon/minimal/helper/currency"
"github.com/0xPolygon/minimal/helper/tests"
"github.com/0xPolygon/minimal/state/runtime/system"
"github.com/0xPolygon/minimal/types"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -47,8 +47,8 @@ func TestPoS_Stake(t *testing.T) {

numGenesisValidators := IBFTMinNodes
ibftManager := framework.NewIBFTServersManager(t, numGenesisValidators, IBFTDirPrefix, func(i int, config *framework.TestServerConfig) {
config.PremineWithStake(stakerAddr, currency.EthToWei(10), currency.EthToWei(10))
config.PremineValidatorBalance(big.NewInt(0), currency.EthToWei(10))
config.PremineWithStake(stakerAddr, tests.EthToWei(10), tests.EthToWei(10))
config.PremineValidatorBalance(big.NewInt(0), tests.EthToWei(10))
config.SetSeal(true)
})

Expand All @@ -64,7 +64,7 @@ func TestPoS_Stake(t *testing.T) {
To: &stakingContractAddr,
GasPrice: big.NewInt(10000),
Gas: 1000000,
Value: currency.EthToWei(1),
Value: tests.EthToWei(1),
}
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
Expand All @@ -90,7 +90,7 @@ func TestPoS_Unstake(t *testing.T) {
numGenesisValidators := IBFTMinNodes + 1
ibftManager := framework.NewIBFTServersManager(t, numGenesisValidators, IBFTDirPrefix, func(i int, config *framework.TestServerConfig) {
// Premine to send unstake transaction
config.PremineValidatorBalance(currency.EthToWei(1), currency.EthToWei(10))
config.PremineValidatorBalance(tests.EthToWei(1), tests.EthToWei(10))
config.SetSeal(true)
})

Expand Down Expand Up @@ -121,7 +121,7 @@ func TestPoS_Unstake(t *testing.T) {
To: &unstakingContractAddr,
GasPrice: big.NewInt(10000),
Gas: 1000000,
Value: currency.EthToWei(0),
Value: tests.EthToWei(0),
}
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
Expand All @@ -146,7 +146,7 @@ func TestPoS_UnstakeAttack(t *testing.T) {
numGenesisValidators := IBFTMinNodes + 1
gasPrice := big.NewInt(10000)
receiptArr := make([]*web3.Receipt, numGenesisValidators)
defaultBalance := currency.EthToWei(10)
defaultBalance := tests.EthToWei(10)

// Set up the manager
ibftManager := framework.NewIBFTServersManager(t, numGenesisValidators, IBFTDirPrefix, func(i int, config *framework.TestServerConfig) {
Expand Down Expand Up @@ -178,7 +178,7 @@ func TestPoS_UnstakeAttack(t *testing.T) {
To: &unstakingContractAddr,
GasPrice: gasPrice,
Gas: 1000000,
Value: currency.EthToWei(0),
Value: tests.EthToWei(0),
}

ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
Expand Down Expand Up @@ -222,8 +222,9 @@ func TestPoS_UnstakeAttack(t *testing.T) {
fee,
)

actualStakedBalance := getStakedBalance(referenceAddr, srv.JSONRPC(), t)
actualBalance := getAccountBalance(referenceAddr, srv.JSONRPC(), t)
client := srv.JSONRPC()
actualStakedBalance := getStakedBalance(referenceAddr, client, t)
actualBalance := getAccountBalance(referenceAddr, client, t)

// Make sure the account balance matches up
assert.Equalf(t,
Expand Down
24 changes: 12 additions & 12 deletions e2e/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/0xPolygon/minimal/e2e/framework"
"github.com/0xPolygon/minimal/helper/currency"
"github.com/0xPolygon/minimal/helper/tests"
"github.com/0xPolygon/minimal/state/runtime/system"
"github.com/0xPolygon/minimal/types"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestSystem_StakeAmount(t *testing.T) {
// Valid account #1
{
addressKeyPairs[0].address,
currency.EthToWei(50), // 50 ETH
tests.EthToWei(50), // 50 ETH
},
// Empty account
{
Expand All @@ -105,13 +105,13 @@ func TestSystem_StakeAmount(t *testing.T) {
{
"Valid stake",
validAccounts[0].address,
currency.EthToWei(10),
tests.EthToWei(10),
true,
},
{
"Invalid stake",
validAccounts[1].address,
currency.EthToWei(100),
tests.EthToWei(100),
false,
},
}
Expand Down Expand Up @@ -210,20 +210,20 @@ func TestSystem_UnstakeAmount(t *testing.T) {
// Staking address initialization
{
stakingAddress,
currency.EthToWei(10), // 10 ETH has been staked in the past
currency.EthToWei(0),
tests.EthToWei(10), // 10 ETH has been staked in the past
tests.EthToWei(0),
},
// Valid account with stake
{
addressKeyPairs[0].address,
currency.EthToWei(50), // 50 ETH
currency.EthToWei(10), // 10 ETH
tests.EthToWei(50), // 50 ETH
tests.EthToWei(10), // 10 ETH
},
// Valid account without stake
{
addressKeyPairs[1].address,
currency.EthToWei(0),
currency.EthToWei(0),
tests.EthToWei(0),
tests.EthToWei(0),
},
}

Expand All @@ -236,13 +236,13 @@ func TestSystem_UnstakeAmount(t *testing.T) {
{
"Valid unstake",
validAccounts[1].address,
currency.EthToWei(10),
tests.EthToWei(10),
true,
},
{
"Invalid unstake",
validAccounts[2].address,
currency.EthToWei(100),
tests.EthToWei(100),
false,
},
}
Expand Down
16 changes: 8 additions & 8 deletions e2e/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/0xPolygon/minimal/e2e/framework"
"github.com/0xPolygon/minimal/helper/currency"
"github.com/0xPolygon/minimal/helper/tests"
"github.com/0xPolygon/minimal/types"
"github.com/stretchr/testify/assert"
"github.com/umbracle/go-web3"
Expand All @@ -17,10 +17,10 @@ func TestSignedTransaction(t *testing.T) {
senderKey, senderAddr := framework.GenerateKeyAndAddr(t)
_, receiverAddr := framework.GenerateKeyAndAddr(t)

preminedAmount := currency.EthToWei(10)
preminedAmount := tests.EthToWei(10)
ibftManager := framework.NewIBFTServersManager(t, IBFTMinNodes, IBFTDirPrefix, func(i int, config *framework.TestServerConfig) {
config.Premine(senderAddr, preminedAmount)
config.PremineValidatorBalance(big.NewInt(0), currency.EthToWei(10))
config.PremineValidatorBalance(big.NewInt(0), tests.EthToWei(10))
config.SetSeal(true)
})

Expand Down Expand Up @@ -111,7 +111,7 @@ func TestEthTransfer(t *testing.T) {
// Valid account #1
{
types.StringToAddress("1"),
currency.EthToWei(50), // 50 ETH
tests.EthToWei(50), // 50 ETH
},
// Empty account
{
Expand All @@ -121,7 +121,7 @@ func TestEthTransfer(t *testing.T) {
// Valid account #2
{
types.StringToAddress("3"),
currency.EthToWei(10), // 10 ETH
tests.EthToWei(10), // 10 ETH
},
}

Expand All @@ -137,23 +137,23 @@ func TestEthTransfer(t *testing.T) {
"Valid ETH transfer #1",
validAccounts[0].address,
validAccounts[2].address,
currency.EthToWei(10),
tests.EthToWei(10),
true,
},
{
// ACC #2 -> ACC #3
"Invalid ETH transfer",
validAccounts[1].address,
validAccounts[2].address,
currency.EthToWei(100),
tests.EthToWei(100),
false,
},
{
// ACC #3 -> ACC #2
"Valid ETH transfer #2",
validAccounts[2].address,
validAccounts[1].address,
currency.EthToWei(5),
tests.EthToWei(5),
true,
},
}
Expand Down
2 changes: 1 addition & 1 deletion helper/currency/currency.go → helper/tests/testing.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package currency
package tests

import "math/big"

Expand Down
Loading

0 comments on commit 96a5af8

Please sign in to comment.