move setup staking contract

pull/573/head
Minh Doan 6 years ago committed by Minh Doan
parent 6099b8e575
commit 4845384266
  1. 201
      test/chain/main.go

@ -191,7 +191,7 @@ func playFaucetContract(chain *core.BlockChain) {
callFaucetContractToFundAnAddress(chain) callFaucetContractToFundAnAddress(chain)
} }
func playStakingContract(chain *core.BlockChain) { func playSetupStakingContract(chain *core.BlockChain) {
fmt.Println() fmt.Println()
fmt.Println("--------- ************************** ---------") fmt.Println("--------- ************************** ---------")
fmt.Println() fmt.Println()
@ -220,105 +220,108 @@ func playStakingContract(chain *core.BlockChain) {
fmt.Println(state.GetBalance(StakeContractAddress)) fmt.Println(state.GetBalance(StakeContractAddress))
fmt.Println("stake address balance :") fmt.Println("stake address balance :")
fmt.Println(state.GetBalance(StakingAddress)) fmt.Println(state.GetBalance(StakingAddress))
}
depositFnSignature := []byte("deposit()") func playStakingContract(chain *core.BlockChain) {
//hash = sha3.NewKeccak256() playSetupStakingContract(chain)
hash := sha3.NewLegacyKeccak256() // fmt.Println()
hash.Write(depositFnSignature) // fmt.Println("--------- Staking Contract added to txns ---------")
methodID := hash.Sum(nil)[:4] // fmt.Println()
// fmt.Printf("-- Now Staking with stake: %d --\n", stake)
var callEncl []byte // fmt.Println()
callEncl = append(callEncl, methodID...) // depositFnSignature := []byte("deposit()")
stake := 100000 // //hash = sha3.NewKeccak256()
fmt.Println() // hash := sha3.NewLegacyKeccak256()
fmt.Println("--------- Staking Contract added to txns ---------") // hash.Write(depositFnSignature)
fmt.Println() // methodID := hash.Sum(nil)[:4]
fmt.Printf("-- Now Staking with stake: %d --\n", stake)
fmt.Println() // var callEncl []byte
for i := 0; i <= 2; i++ { // callEncl = append(callEncl, methodID...)
// stake := 100000
//Deposit Does not take a argument, stake is transferred via amount. // for i := 0; i <= 2; i++ {
tx, _ := types.SignTx(types.NewTransaction(0, StakeContractAddress, 0, big.NewInt(int64(stake)), params.TxGas*5, nil, callEncl), types.HomesteadSigner{}, AllRandomUserKey[i])
stakingtxns = append(stakingtxns, tx) // //Deposit Does not take a argument, stake is transferred via amount.
} // tx, _ := types.SignTx(types.NewTransaction(0, StakeContractAddress, 0, big.NewInt(int64(stake)), params.TxGas*5, nil, callEncl), types.HomesteadSigner{}, AllRandomUserKey[i])
err = contractworker.CommitTransactions(stakingtxns) // stakingtxns = append(stakingtxns, tx)
// }
if err != nil { // err = contractworker.CommitTransactions(stakingtxns)
fmt.Println(err)
} // if err != nil {
block, _ = contractworker.Commit() // fmt.Println(err)
_, err = chain.InsertChain(types.Blocks{block}) // }
if err != nil { // block, _ = contractworker.Commit()
fmt.Println(err) // _, err = chain.InsertChain(types.Blocks{block})
} // if err != nil {
// receipts := contractworker.GetCurrentReceipts() // fmt.Println(err)
// fmt.Println(receipts[len(receipts)-4].ContractAddress) // }
state = contractworker.GetCurrentState() // // receipts := contractworker.GetCurrentReceipts()
// // fmt.Println(receipts[len(receipts)-4].ContractAddress)
fmt.Printf("After Staking Balances (should be less by %d)\n", stake) // state = contractworker.GetCurrentState()
fmt.Println("user address balance")
fmt.Println(state.GetBalance(AllRandomUserAddress[0])) // fmt.Printf("After Staking Balances (should be less by %d)\n", stake)
fmt.Println("The balances for 2 more users:") // fmt.Println("user address balance")
fmt.Println(state.GetBalance(AllRandomUserAddress[1])) // fmt.Println(state.GetBalance(AllRandomUserAddress[0]))
fmt.Println(state.GetBalance(AllRandomUserAddress[2])) // fmt.Println("The balances for 2 more users:")
fmt.Println("faucet contract balance (unchanged):") // fmt.Println(state.GetBalance(AllRandomUserAddress[1]))
fmt.Println(state.GetBalance(FaucetContractAddress)) // fmt.Println(state.GetBalance(AllRandomUserAddress[2]))
fmt.Println("stake contract balance :") // fmt.Println("faucet contract balance (unchanged):")
fmt.Println(state.GetBalance(StakeContractAddress)) // fmt.Println(state.GetBalance(FaucetContractAddress))
fmt.Println("stake address balance :") // fmt.Println("stake contract balance :")
fmt.Println(state.GetBalance(StakingAddress)) // fmt.Println(state.GetBalance(StakeContractAddress))
fmt.Println() // fmt.Println("stake address balance :")
fmt.Println("--------- Now Setting up Withdrawing Stakes ---------") // fmt.Println(state.GetBalance(StakingAddress))
// fmt.Println()
withdrawFnSignature := []byte("withdraw(uint256)") // fmt.Println("--------- Now Setting up Withdrawing Stakes ---------")
//hash = sha3.NewKeccak256()
hash = sha3.NewLegacyKeccak256() // withdrawFnSignature := []byte("withdraw(uint256)")
hash.Write(withdrawFnSignature) // //hash = sha3.NewKeccak256()
methodID = hash.Sum(nil)[:4] // hash = sha3.NewLegacyKeccak256()
// hash.Write(withdrawFnSignature)
withdraw := "5000" // methodID = hash.Sum(nil)[:4]
withdrawstake := new(big.Int)
withdrawstake.SetString(withdraw, 10) // withdraw := "5000"
paddedAmount := common.LeftPadBytes(withdrawstake.Bytes(), 32) // withdrawstake := new(big.Int)
// withdrawstake.SetString(withdraw, 10)
var dataEncl []byte // paddedAmount := common.LeftPadBytes(withdrawstake.Bytes(), 32)
dataEncl = append(dataEncl, methodID...)
dataEncl = append(dataEncl, paddedAmount...) // var dataEncl []byte
// dataEncl = append(dataEncl, methodID...)
var withdrawstakingtxns []*types.Transaction // dataEncl = append(dataEncl, paddedAmount...)
fmt.Println() // var withdrawstakingtxns []*types.Transaction
fmt.Printf("-- Withdrawing Stake by amount: %s --\n", withdraw)
fmt.Println() // fmt.Println()
// fmt.Printf("-- Withdrawing Stake by amount: %s --\n", withdraw)
for i := 0; i <= 2; i++ { // fmt.Println()
cnonce := contractworker.GetCurrentState().GetNonce(AllRandomUserAddress[i])
tx, _ := types.SignTx(types.NewTransaction(cnonce, StakeContractAddress, 0, big.NewInt(0), params.TxGas*5, nil, dataEncl), types.HomesteadSigner{}, AllRandomUserKey[i]) // for i := 0; i <= 2; i++ {
withdrawstakingtxns = append(withdrawstakingtxns, tx) // cnonce := contractworker.GetCurrentState().GetNonce(AllRandomUserAddress[i])
} // tx, _ := types.SignTx(types.NewTransaction(cnonce, StakeContractAddress, 0, big.NewInt(0), params.TxGas*5, nil, dataEncl), types.HomesteadSigner{}, AllRandomUserKey[i])
// withdrawstakingtxns = append(withdrawstakingtxns, tx)
err = contractworker.CommitTransactions(withdrawstakingtxns) // }
if err != nil {
fmt.Println("error:") // err = contractworker.CommitTransactions(withdrawstakingtxns)
fmt.Println(err) // if err != nil {
} // fmt.Println("error:")
// fmt.Println(err)
block, _ = contractworker.Commit() // }
_, err = chain.InsertChain(types.Blocks{block})
if err != nil { // block, _ = contractworker.Commit()
fmt.Println(err) // _, err = chain.InsertChain(types.Blocks{block})
} // if err != nil {
state = contractworker.GetCurrentState() // fmt.Println(err)
fmt.Printf("Withdraw Staking Balances (should be up by %s)\n", withdraw) // }
fmt.Println(state.GetBalance(AllRandomUserAddress[0])) // state = contractworker.GetCurrentState()
fmt.Println(state.GetBalance(AllRandomUserAddress[1])) // fmt.Printf("Withdraw Staking Balances (should be up by %s)\n", withdraw)
fmt.Println(state.GetBalance(AllRandomUserAddress[2])) // fmt.Println(state.GetBalance(AllRandomUserAddress[0]))
fmt.Println("faucet contract balance (unchanged):") // fmt.Println(state.GetBalance(AllRandomUserAddress[1]))
fmt.Println(state.GetBalance(FaucetContractAddress)) // fmt.Println(state.GetBalance(AllRandomUserAddress[2]))
fmt.Printf("stake contract balance (should downup by %s)\n", withdraw) // fmt.Println("faucet contract balance (unchanged):")
fmt.Println(state.GetBalance(StakeContractAddress)) // fmt.Println(state.GetBalance(FaucetContractAddress))
fmt.Println("stake address balance :") // fmt.Printf("stake contract balance (should downup by %s)\n", withdraw)
fmt.Println(state.GetBalance(StakingAddress)) // fmt.Println(state.GetBalance(StakeContractAddress))
// fmt.Println("stake address balance :")
// fmt.Println(state.GetBalance(StakingAddress))
} }

Loading…
Cancel
Save