Commit 540ac865 authored by QM's avatar QM

add unit test

parent bcf40d40
......@@ -200,12 +200,67 @@ func (c *suiteContracts) Test_CreateBridgeToken() {
addr, err := GetToken2address(c.x2EthContracts.BridgeBank, "bty")
require.Nil(c.T(), err)
assert.Equal(c.T(), addr, tokenAddr)
}
func (c *suiteContracts) Test_GetBalance() {
balance, err := GetBalance(c.sim, "", c.para.InitValidators[0].String())
chain33Sender := []byte("14KEKbYtKKQm4wMthSK9J4La4nAiidGozt")
amount := int64(100)
ethReceiver := c.para.InitValidators[2]
claimID := crypto.Keccak256Hash(chain33Sender, ethReceiver.Bytes(), big.NewInt(amount).Bytes())
authOracle, err := PrepareAuth(c.sim, c.para.ValidatorPriKey[0], c.para.InitValidators[0])
require.Nil(c.T(), err)
signature, err := SignClaim4Eth(claimID, c.para.ValidatorPriKey[0])
require.Nil(c.T(), err)
_, err = c.x2EthContracts.Oracle.NewOracleClaim(
authOracle,
events.ClaimTypeLock,
chain33Sender,
ethReceiver,
common.HexToAddress(tokenAddr),
"bty",
big.NewInt(amount),
claimID,
signature)
require.Nil(c.T(), err)
c.sim.Commit()
balanceNew, err := GetBalance(c.sim, tokenAddr, ethReceiver.String())
require.Nil(c.T(), err)
require.Equal(c.T(), balanceNew, "100")
chain33Receiver := "1GTxrmuWiXavhcvsaH5w9whgVxUrWsUMdV"
{
amount := "10"
bn := big.NewInt(1)
bn, _ = bn.SetString(x2ethTypes.TrimZeroAndDot(amount), 10)
txhash, err := Burn(hexutil.Encode(crypto.FromECDSA(c.para.ValidatorPriKey[2])), tokenAddr, chain33Receiver, c.x2EthDeployInfo.BridgeBank.Address, bn, c.x2EthContracts.BridgeBank, c.sim)
require.NoError(c.T(), err)
c.sim.Commit()
balanceNew, err = GetBalance(c.sim, tokenAddr, ethReceiver.String())
require.Nil(c.T(), err)
require.Equal(c.T(), balanceNew, "90")
status := GetEthTxStatus(c.sim, common.HexToHash(txhash))
fmt.Println()
fmt.Println(status)
}
{
amount := "10"
bn := big.NewInt(1)
bn, _ = bn.SetString(x2ethTypes.TrimZeroAndDot(amount), 10)
_, err := ApproveAllowance(hexutil.Encode(crypto.FromECDSA(c.para.ValidatorPriKey[2])), tokenAddr, c.x2EthDeployInfo.BridgeBank.Address, bn, c.sim)
require.Nil(c.T(), err)
c.sim.Commit()
_, err = BurnAsync(hexutil.Encode(crypto.FromECDSA(c.para.ValidatorPriKey[2])), tokenAddr, chain33Receiver, bn, c.x2EthContracts.BridgeBank, c.sim)
require.Nil(c.T(), err)
assert.Equal(c.T(), balance, "10000000000")
c.sim.Commit()
balanceNew, err = GetBalance(c.sim, tokenAddr, ethReceiver.String())
require.Nil(c.T(), err)
require.Equal(c.T(), balanceNew, "80")
}
}
func (c *suiteContracts) Test_CreateERC20Token() {
......@@ -283,16 +338,6 @@ func (c *suiteContracts) Test_GetLockedFunds() {
assert.Equal(c.T(), balance, "0")
}
//func (c *suiteContracts) Test_GetEthTxStatus() {
// hash := "0xc0c22aa6198fdde0dbe47ddadbe449f736b82ed4a498871de5d5f4ad9ae122a0"
// status := GetEthTxStatus(c.sim, common.HexToHash(hash))
// fmt.Println(status)
//}
func Test_All(t *testing.T) {
}
func PrepareTestEnv() (*ethinterface.SimExtend, *DeployPara) {
genesiskey, _ := crypto.GenerateKey()
alloc := make(core.GenesisAlloc)
......
......@@ -87,6 +87,13 @@ func Test_RelayOracleClaimToEthereum(t *testing.T) {
//hash := "0xc0c22aa6198fdde0dbe47ddadbe449f736b82ed4a498871de5d5f4ad9ae122a0"
//status := GetEthTxStatus(sim, common.HexToHash(hash))
//fmt.Println(status)
_, err = revokeNonce(para.Operator)
require.Nil(t, err)
}
func Test_revokeNonce(t *testing.T) {
}
func deployContracts() (*DeployPara, *ethinterface.SimExtend, *X2EthContracts, *X2EthDeployInfo, error) {
......
......@@ -36,15 +36,6 @@ const (
EthTxPending = EthTxStatus(2)
)
// GenerateClaimHash : Generates an OracleClaim hash from a ProphecyClaim's event data
//func GenerateClaimHash(prophecyID []byte, sender []byte, recipient []byte, token []byte, amount []byte, validator []byte) common.Hash {
// // Generate a hash containing the information
// rawHash := crypto.Keccak256Hash(prophecyID, sender, recipient, token, amount, validator)
//
// // Cast hash to hex encoded string
// return rawHash
//}
func SignClaim4Eth(hash common.Hash, privateKey *ecdsa.PrivateKey) ([]byte, error) {
rawSignature, _ := prefixMessage(hash, privateKey)
signature := hexutil.Bytes(rawSignature)
......
......@@ -12,27 +12,3 @@ var EventRecords = make(map[string]LockEvent)
func NewEventWrite(txHash string, event LockEvent) {
EventRecords[txHash] = event
}
// IsEventRecorded : checks the sessions stored events for this transaction hash
//func IsEventRecorded(txHash string) bool {
// return EventRecords[txHash].Nonce != nil
//}
//// PrintLockEventByTx : prints any witnessed events associated with a given transaction hash
//func PrintLockEventByTx(txHash string) {
// if IsEventRecorded(txHash) {
// event := EventRecords[txHash]
// //printLockEvent(&event)
// } else {
// log.Printf("\nNo records from this session for tx: %v\n", txHash)
// }
//}
//
//// PrintLockEvents : prints all the claims made on this event
//func PrintLockEvents() {
// // For each claim, print the validator which submitted the claim
// for txHash, event := range EventRecords {
// log.Printf("\nTransaction: %v\n", txHash)
// printLockEvent(&event)
// }
//}
package events
import (
"math/big"
"strings"
"testing"
"github.com/33cn/plugin/plugin/dapp/x2ethereum/ebrelayer/ethcontract/generated"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_UnpackLogLock(t *testing.T) {
abiJSON := generated.BridgeBankABI
contractABI, err := abi.JSON(strings.NewReader(abiJSON))
require.Nil(t, err)
eventName := LogLock.String()
eventData := []byte("nil")
_, err = UnpackLogLock(contractABI, eventName, eventData)
require.NotNil(t, err)
_, err = UnpackLogBurn(contractABI, eventName, eventData)
require.NotNil(t, err)
}
func Test_NewEventWrite(t *testing.T) {
event := LockEvent{
Symbol: "bty",
}
NewEventWrite("1", event)
assert.Equal(t, EventRecords["1"].Symbol, "bty")
}
func Test_NewChain33Msg(t *testing.T) {
_ = NewChain33Msg(MsgBurn, []byte("12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv"), common.HexToAddress("0x0C05bA5c230fDaA503b53702aF1962e08D0C60BF"),
"eth", big.NewInt(100000000000000000), common.HexToAddress("0x0000000000000000000000000000000000000000"))
}
func Test_Chain33MsgAttributeKey(t *testing.T) {
assert.Equal(t, UnsupportedAttributeKey.String(), "unsupported")
assert.Equal(t, Chain33Sender.String(), "chain33_sender")
assert.Equal(t, EthereumReceiver.String(), "ethereum_receiver")
assert.Equal(t, Coin.String(), "amount")
assert.Equal(t, TokenContractAddress.String(), "token_contract_address")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment