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/api/client/service/server_test.go

95 lines
2.4 KiB

package client
import (
"bytes"
"context"
"math/big"
"testing"
blockfactory "github.com/harmony-one/harmony/block/factory"
"github.com/harmony-one/harmony/internal/chain"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
client "github.com/harmony-one/harmony/api/client/service/proto"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/state"
"github.com/harmony-one/harmony/core/vm"
"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.NewFactory(chainConfig)
)
func TestGetFreeToken(test *testing.T) {
hash := common.Hash{}
hash.SetBytes([]byte("hello"))
server := NewServer(func() (*state.DB, error) {
return nil, nil
}, func(common.Address) common.Hash {
return hash
})
testBankKey, _ := crypto.GenerateKey()
testBankAddress := crypto.PubkeyToAddress(testBankKey.PublicKey)
response, err := server.GetFreeToken(
context.TODO(),
&client.GetFreeTokenRequest{Address: testBankAddress.Bytes()},
)
if err != nil {
test.Errorf("Failed to get free token")
}
if !bytes.Equal(response.TxId, hash.Bytes()) {
test.Errorf("Wrong transaction id is returned")
}
}
func TestFetchAccountState(test *testing.T) {
var (
database = ethdb.NewMemDatabase()
gspec = core.Genesis{
Config: chainConfig,
Factory: blockFactory,
Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
ShardID: 10,
}
)
genesis := gspec.MustCommit(database)
_ = genesis
chain, _ := core.NewBlockChain(database, nil, gspec.Config, chain.Engine, vm.Config{}, nil)
hash := common.Hash{}
hash.SetBytes([]byte("hello"))
server := NewServer(func() (*state.DB, error) {
return chain.State()
}, func(common.Address) common.Hash {
return hash
})
response, err := server.FetchAccountState(
context.TODO(),
&client.FetchAccountStateRequest{Address: testBankAddress.Bytes()},
)
if err != nil {
test.Errorf("Failed to get free token")
}
if !bytes.Equal(response.Balance, testBankFunds.Bytes()) {
test.Errorf("Wrong balance is returned")
}
if response.Nonce != 0 {
test.Errorf("Wrong nonce is returned")
}
}