The core protocol of WoopChain
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.
woop/node/worker/worker_test.go

103 lines
3.0 KiB

package worker
import (
"math/big"
"math/rand"
"testing"
3 years ago
"github.com/harmony-one/harmony/core/state"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
blockfactory "github.com/harmony-one/harmony/block/factory"
"github.com/harmony-one/harmony/common/denominations"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm"
chain2 "github.com/harmony-one/harmony/internal/chain"
"github.com/harmony-one/harmony/internal/params"
)
var (
// Test accounts
testBankKey, _ = crypto.GenerateKey()
testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey)
testBankFunds = big.NewInt(8000000000000000000)
chainConfig = params.TestChainConfig
blockFactory = blockfactory.ForTest
)
func TestNewWorker(t *testing.T) {
6 years ago
// Setup a new blockchain with genesis block containing test token on test address
var (
database = rawdb.NewMemoryDatabase()
gspec = core.Genesis{
Config: chainConfig,
Factory: blockFactory,
Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
ShardID: 10,
}
engine = chain2.NewEngine()
)
genesis := gspec.MustCommit(database)
_ = genesis
chain, err := core.NewBlockChain(database, state.NewDatabase(database), &core.BlockChainImpl{}, nil, gspec.Config, engine, vm.Config{})
if err != nil {
t.Error(err)
}
6 years ago
// Create a new worker
worker := New(params.TestChainConfig, chain, nil, engine)
if worker.GetCurrentState().GetBalance(crypto.PubkeyToAddress(testBankKey.PublicKey)).Cmp(testBankFunds) != 0 {
t.Error("Worker state is not setup correctly")
}
}
func TestCommitTransactions(t *testing.T) {
6 years ago
// Setup a new blockchain with genesis block containing test token on test address
var (
database = rawdb.NewMemoryDatabase()
gspec = core.Genesis{
Config: chainConfig,
Factory: blockFactory,
Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
ShardID: 0,
}
engine = chain2.NewEngine()
)
6 years ago
gspec.MustCommit(database)
chain, _ := core.NewBlockChain(database, state.NewDatabase(database), nil, nil, gspec.Config, engine, vm.Config{})
6 years ago
// Create a new worker
worker := New(params.TestChainConfig, chain, nil, engine)
6 years ago
// Generate a test tx
baseNonce := worker.GetCurrentState().GetNonce(crypto.PubkeyToAddress(testBankKey.PublicKey))
randAmount := rand.Float32()
tx, _ := types.SignTx(types.NewTransaction(baseNonce, testBankAddress, uint32(0), big.NewInt(int64(denominations.One*randAmount)), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey)
6 years ago
// Commit the tx to the worker
txs := make(map[common.Address]types.Transactions)
txs[testBankAddress] = types.Transactions{tx}
err := worker.CommitTransactions(
txs, nil, testBankAddress,
)
if err != nil {
t.Error(err)
}
if len(worker.GetCurrentReceipts()) == 0 {
t.Error("No receipt is created for new transactions")
}
if len(worker.current.txs) != 1 {
t.Error("Transaction is not committed")
}
}