From 8b2ff8fe554f99c3bdf709153926446111509e75 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Wed, 1 May 2019 22:45:32 -0700 Subject: [PATCH] Add restapi for play and payout --- .../restclientsupport/puzzle_service.go | 69 +++++++++++++++++++ api/service/restclientsupport/service.go | 66 ++---------------- contracts/Puzzle.go | 4 +- contracts/Puzzle.sol | 4 +- node/node_genesis.go | 21 +++--- node/puzzle_contract.go | 29 ++++---- 6 files changed, 109 insertions(+), 84 deletions(-) create mode 100644 api/service/restclientsupport/puzzle_service.go diff --git a/api/service/restclientsupport/puzzle_service.go b/api/service/restclientsupport/puzzle_service.go new file mode 100644 index 000000000..158b0b3c7 --- /dev/null +++ b/api/service/restclientsupport/puzzle_service.go @@ -0,0 +1,69 @@ +package restclientsupport + +import ( + "encoding/json" + "fmt" + "github.com/ethereum/go-ethereum/common" + "github.com/harmony-one/harmony/internal/utils" + "net/http" + "strconv" +) + +// Play triggers play method of puzzle smart contract. +func (s *Service) Play(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + key := r.FormValue("key") + amount := r.FormValue("amount") + amountInt, err := strconv.ParseInt(amount, 10, 64) + fmt.Println("Play: key", key, "amount", amountInt) + + res := &Response{ + Success: false, + } + + if err != nil { + json.NewEncoder(w).Encode(res) + return + } + + if s.CreateTransactionForPlayMethod == nil { + fmt.Println("puzzle-play no method", key) + json.NewEncoder(w).Encode(res) + return + } + + + if err := s.CreateTransactionForPlayMethod(key, amountInt); err != nil { + utils.GetLogInstance().Error("puzzle-play, error", err) + json.NewEncoder(w).Encode(res) + return + } + res.Success = true + json.NewEncoder(w).Encode(res) +} + +// Payout triggers play payout of puzzle smart contract. +func (s *Service) Payout(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + address := r.FormValue("address") + newLevel := r.FormValue("new_level") + newLevelInt, err := strconv.Atoi(newLevel) + + fmt.Println("Payout: address", address, "new_level", newLevelInt) + res := &Response{ + Success: false, + } + + if s.CreateTransactionForPayoutMethod == nil { + json.NewEncoder(w).Encode(res) + return + } + + if err = s.CreateTransactionForPayoutMethod(common.HexToAddress(address), uint8(newLevelInt), "" /* place holder for steps*/); err != nil { + utils.GetLogInstance().Error("Payout error", err) + json.NewEncoder(w).Encode(res) + return + } + res.Success = true + json.NewEncoder(w).Encode(res) +} diff --git a/api/service/restclientsupport/service.go b/api/service/restclientsupport/service.go index a30b4bb55..fa7612369 100644 --- a/api/service/restclientsupport/service.go +++ b/api/service/restclientsupport/service.go @@ -35,8 +35,8 @@ type Service struct { messageChan chan *msg_pb.Message CallFaucetContract func(common.Address) common.Hash GetAccountBalance func(common.Address) (*big.Int, error) - CreateTransactionForPlayMethod func(string) error - CreateTransactionForPayoutMethod func(string, int) error + CreateTransactionForPlayMethod func(string, int64) error + CreateTransactionForPayoutMethod func(common.Address, uint8, string) error } // New returns new client support service. @@ -45,8 +45,8 @@ func New( GetResult func(string) ([]string, []*big.Int), CreateTransactionForPickWinner func() error, CallFaucetContract func(common.Address) common.Hash, GetAccountBalance func(common.Address) (*big.Int, error), - CreateTransactionForPlayMethod func(string) error, - CreateTransactionForPayoutMethod func(string, int) error) *Service { + CreateTransactionForPlayMethod func(string, int64) error, + CreateTransactionForPayoutMethod func(common.Address, uint8, string) error) *Service { return &Service{ CreateTransactionForEnterMethod: CreateTransactionForEnterMethod, GetResult: GetResult, @@ -100,11 +100,11 @@ func (s *Service) Run() *http.Server { s.router.Path("/winner").HandlerFunc(s.Winner) // Routing for puzzle app. - // Set up router for play. + s.router.Path("/play").Queries("address", "{[0-9A-Fa-fx]*?}", "amount", "{[0-9]*?}").HandlerFunc(s.Play).Methods("GET") s.router.Path("/play").HandlerFunc(s.Play) // Set up router for payout. - s.router.Path("/payout").Queries("key", "{[0-9A-Fa-fx]*?}", "new_level", "{[0-9]*?}").HandlerFunc(s.Payout).Methods("GET") + s.router.Path("/payout").Queries("address", "{[0-9A-Fa-fx]*?}", "new_level", "{[0-9]*?}").HandlerFunc(s.Payout).Methods("GET") s.router.Path("/payout").HandlerFunc(s.Payout) // Do serving now. utils.GetLogInstance().Info("Listening on ", "port: ", Port) @@ -124,7 +124,7 @@ type Response struct { func (s *Service) GetBalance(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") addressHex := r.FormValue("key") - fmt.Println("fundMe: address", addressHex) + fmt.Println("GetBalance: address", addressHex) res := &Response{Success: false} if s.GetAccountBalance == nil { @@ -239,58 +239,6 @@ func (s *Service) Winner(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(res) } -// Play triggers play method of puzzle smart contract. -func (s *Service) Play(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - key := r.FormValue("key") - - fmt.Println("puzzle-play", key) - res := &Response{ - Success: false, - } - - if s.CreateTransactionForPlayMethod == nil { - fmt.Println("puzzle-play no method", key) - json.NewEncoder(w).Encode(res) - return - } - - if err := s.CreateTransactionForPlayMethod(key); err != nil { - utils.GetLogInstance().Error("puzzle-play, error", err) - json.NewEncoder(w).Encode(res) - return - } - res.Success = true - json.NewEncoder(w).Encode(res) -} - -// Payout triggers play payout of puzzle smart contract. -func (s *Service) Payout(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - key := r.FormValue("key") - newLevel := r.FormValue("new_level") - fmt.Println("payout: key", key, "new_level", newLevel) - newLevelInt, err := strconv.Atoi(newLevel) - - fmt.Println("play") - res := &Response{ - Success: false, - } - - if s.CreateTransactionForPayoutMethod == nil { - json.NewEncoder(w).Encode(res) - return - } - - if err = s.CreateTransactionForPayoutMethod(key, newLevelInt); err != nil { - utils.GetLogInstance().Error("error", err) - json.NewEncoder(w).Encode(res) - return - } - res.Success = true - json.NewEncoder(w).Encode(res) -} - // NotifyService notify service func (s *Service) NotifyService(params map[string]interface{}) { return diff --git a/contracts/Puzzle.go b/contracts/Puzzle.go index 2b58d7382..30cfecc31 100644 --- a/contracts/Puzzle.go +++ b/contracts/Puzzle.go @@ -28,10 +28,10 @@ var ( ) // PuzzleABI is the input ABI used to generate the binding from. -const PuzzleABI = "[{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"}],\"name\":\"endGame\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"manager\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"},{\"name\":\"level\",\"type\":\"uint256\"},{\"name\":\"\",\"type\":\"string\"}],\"name\":\"payout\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"},{\"name\":\"level\",\"type\":\"uint256\"}],\"name\":\"setLevel\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"getPlayers\",\"outputs\":[{\"name\":\"\",\"type\":\"address[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"play\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"}],\"name\":\"resetPlayer\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"reset\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"players\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]" +const PuzzleABI = "[{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"}],\"name\":\"endGame\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"manager\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"},{\"name\":\"level\",\"type\":\"uint256\"},{\"name\":\"\",\"type\":\"string\"}],\"name\":\"payout\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"},{\"name\":\"level\",\"type\":\"uint256\"}],\"name\":\"setLevel\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"getPlayers\",\"outputs\":[{\"name\":\"\",\"type\":\"address[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"play\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"}],\"name\":\"resetPlayer\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"reset\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"players\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"constructor\"}]" // PuzzleBin is the compiled bytecode used for deploying new contracts. -const PuzzleBin = `0x608060405234801561001057600080fd5b50600480546001600160a01b03191633179055610977806100326000396000f3fe6080604052600436106100865760003560e01c80638b5b9ccc116100595780638b5b9ccc146101e557806393e84cd91461024a578063c95e090914610252578063d826f88f14610285578063f71d96cb1461029a57610086565b80632a035b6c1461008b578063481c6a75146100c057806352bcd7c8146100f1578063722dcd8f146101ac575b600080fd5b34801561009757600080fd5b506100be600480360360208110156100ae57600080fd5b50356001600160a01b03166102c4565b005b3480156100cc57600080fd5b506100d56103ad565b604080516001600160a01b039092168252519081900360200190f35b6100be6004803603606081101561010757600080fd5b6001600160a01b038235169160208101359181019060608101604082013564010000000081111561013757600080fd5b82018360208201111561014957600080fd5b8035906020019184600183028401116401000000008311171561016b57600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295506103bc945050505050565b3480156101b857600080fd5b506100be600480360360408110156101cf57600080fd5b506001600160a01b038135169060200135610569565b3480156101f157600080fd5b506101fa610585565b60408051602080825283518183015283519192839290830191858101910280838360005b8381101561023657818101518382015260200161021e565b505050509050019250505060405180910390f35b6100be6105e8565b34801561025e57600080fd5b506100be6004803603602081101561027557600080fd5b50356001600160a01b0316610722565b34801561029157600080fd5b506100be6107f6565b3480156102a657600080fd5b506100d5600480360360208110156102bd57600080fd5b50356108dc565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461038b57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360005b83811015610350578181015183820152602001610338565b50505050905090810190601f16801561037d5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b506001600160a01b03166000908152600260205260409020805460ff19169055565b6004546001600160a01b031681565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461044757604051600160e51b62461bcd02815260040180806020018281038252838181518152602001915080519060200190808383600083811015610350578181015183820152602001610338565b506001600160a01b038316600090815260026020908152604091829020548251808401909352601f83527f506c61796572206973206e6f7420696e20616e206163746976652067616d65009183019190915260ff1615156001146104f057604051600160e51b62461bcd02815260040180806020018281038252838181518152602001915080519060200190808383600083811015610350578181015183820152602001610338565b506001600160a01b03831660009081526020819052604090205482036105168484610569565b6001600160a01b03841660008181526001602052604080822054905160059091048402929183156108fc02918491818181858888f19350505050158015610561573d6000803e3d6000fd5b505050505050565b6001600160a01b03909116600090815260208190526040902055565b606060058054806020026020016040519081016040528092919081815260200182805480156105dd57602002820191906000526020600020905b81546001600160a01b031681526001909101906020018083116105bf575b505050505090505b90565b60408051808201909152601181527f496e73756666696369656e742046756e6400000000000000000000000000000060208201526801158e460913d0000034101561067857604051600160e51b62461bcd02815260040180806020018281038252838181518152602001915080519060200190808383600083811015610350578181015183820152602001610338565b503360009081526003602052604090205460ff1615156106ef57336000818152600360205260408120805460ff191660019081179091556005805491820181559091527f036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db00180546001600160a01b03191690911790555b3360009081526020818152604080832083905560018083528184203490556002909252909120805460ff19169091179055565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b031633146107ad57604051600160e51b62461bcd02815260040180806020018281038252838181518152602001915080519060200190808383600083811015610350578181015183820152602001610338565b506001600160a01b031660009081526020818152604080832083905560028252808320805460ff1990811690915560018352818420849055600390925290912080549091169055565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461088157604051600160e51b62461bcd02815260040180806020018281038252838181518152602001915080519060200190808383600083811015610350578181015183820152602001610338565b5060055460005b818110156108ca5760006005828154811015156108a157fe5b6000918252602090912001546001600160a01b031690506108c181610722565b50600101610888565b5060006108d8600582610904565b5050565b60058054829081106108ea57fe5b6000918252602090912001546001600160a01b0316905081565b8154818355818111156109285760008381526020902061092891810190830161092d565b505050565b6105e591905b808211156109475760008155600101610933565b509056fea165627a7a72305820d77629ee8b56472dfdca022f06934c4f4dfcf700ce46dccd736c4eef81d380bc0029` +const PuzzleBin = `0x6080604052600480546001600160a01b03191633179055610984806100256000396000f3fe6080604052600436106100865760003560e01c80638b5b9ccc116100595780638b5b9ccc146101f257806393e84cd914610257578063c95e09091461025f578063d826f88f14610292578063f71d96cb146102a757610086565b80632a035b6c1461008b578063481c6a75146100c057806352bcd7c8146100f1578063722dcd8f146101b9575b600080fd5b34801561009757600080fd5b506100be600480360360208110156100ae57600080fd5b50356001600160a01b03166102d1565b005b3480156100cc57600080fd5b506100d56103ba565b604080516001600160a01b039092168252519081900360200190f35b3480156100fd57600080fd5b506100be6004803603606081101561011457600080fd5b6001600160a01b038235169160208101359181019060608101604082013564010000000081111561014457600080fd5b82018360208201111561015657600080fd5b8035906020019184600183028401116401000000008311171561017857600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295506103c9945050505050565b3480156101c557600080fd5b506100be600480360360408110156101dc57600080fd5b506001600160a01b038135169060200135610576565b3480156101fe57600080fd5b50610207610592565b60408051602080825283518183015283519192839290830191858101910280838360005b8381101561024357818101518382015260200161022b565b505050509050019250505060405180910390f35b6100be6105f5565b34801561026b57600080fd5b506100be6004803603602081101561028257600080fd5b50356001600160a01b031661072f565b34801561029e57600080fd5b506100be610803565b3480156102b357600080fd5b506100d5600480360360208110156102ca57600080fd5b50356108e9565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461039857604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561035d578181015183820152602001610345565b50505050905090810190601f16801561038a5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b506001600160a01b03166000908152600260205260409020805460ff19169055565b6004546001600160a01b031681565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461045457604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b506001600160a01b038316600090815260026020908152604091829020548251808401909352601f83527f506c61796572206973206e6f7420696e20616e206163746976652067616d65009183019190915260ff1615156001146104fd57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b506001600160a01b03831660009081526020819052604090205482036105238484610576565b6001600160a01b03841660008181526001602052604080822054905160059091048402929183156108fc02918491818181858888f1935050505015801561056e573d6000803e3d6000fd5b505050505050565b6001600160a01b03909116600090815260208190526040902055565b606060058054806020026020016040519081016040528092919081815260200182805480156105ea57602002820191906000526020600020905b81546001600160a01b031681526001909101906020018083116105cc575b505050505090505b90565b60408051808201909152601181527f496e73756666696369656e742046756e6400000000000000000000000000000060208201526801158e460913d0000034101561068557604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b503360009081526003602052604090205460ff1615156106fc57336000818152600360205260408120805460ff191660019081179091556005805491820181559091527f036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db00180546001600160a01b03191690911790555b3360009081526020818152604080832083905560018083528184203490556002909252909120805460ff19169091179055565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b031633146107ba57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b506001600160a01b031660009081526020818152604080832083905560028252808320805460ff1990811690915560018352818420849055600390925290912080549091169055565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461088e57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b5060055460005b818110156108d75760006005828154811015156108ae57fe5b6000918252602090912001546001600160a01b031690506108ce8161072f565b50600101610895565b5060006108e5600582610911565b5050565b60058054829081106108f757fe5b6000918252602090912001546001600160a01b0316905081565b8154818355818111156109355760008381526020902061093591810190830161093a565b505050565b6105f291905b808211156109545760008155600101610940565b509056fea165627a7a723058204706efee8388d70aabafd388a42b5b54a6268e521a1d1401aec9adf14b0e6f980029` // DeployPuzzle deploys a new Ethereum contract, binding an instance of Puzzle to it. func DeployPuzzle(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *Puzzle, error) { diff --git a/contracts/Puzzle.sol b/contracts/Puzzle.sol index ac0512e88..bf2ce4620 100644 --- a/contracts/Puzzle.sol +++ b/contracts/Puzzle.sol @@ -15,7 +15,7 @@ contract Puzzle { address public manager; // The adress of the owner of this contract address payable[] public players; // all players - constructor() public { + constructor() public payable { manager = msg.sender; } @@ -46,7 +46,7 @@ contract Puzzle { /** * @dev pay the player if they have crossed their last best level. */ - function payout(address payable player, uint level, string memory /*sequence*/) public payable restricted { + function payout(address payable player, uint level, string memory /*sequence*/) public restricted { require(playerInGame[player] == true, NOT_IN_GAME); uint progress = level - playerLevel[player]; //if a later transaction for a higher level comes earlier. diff --git a/node/node_genesis.go b/node/node_genesis.go index fd6b8ec39..0fbd991ab 100644 --- a/node/node_genesis.go +++ b/node/node_genesis.go @@ -20,9 +20,9 @@ const ( // FakeAddressNumber is the number of fake address. FakeAddressNumber = 100 // TotalInitFund is the initial total fund for the contract deployer. - TotalInitFund = 9000000 + TotalInitFund = 1000000100 // InitFreeFundInEther is the initial fund for sample accounts. - InitFreeFundInEther = 1000 + InitFreeFundInEther = 100 ) // GenesisBlockSetup setups a genesis blockchain. @@ -39,6 +39,11 @@ func (node *Node) GenesisBlockSetup(db ethdb.Database, shardID uint32, isArchiva genesisAlloc[contractDeployerAddress] = core.GenesisAccount{Balance: contractDeployerFunds} node.ContractDeployerKey = contractDeployerKey + // Add puzzle fund + puzzleFunds := big.NewInt(TotalInitFund) + puzzleFunds = puzzleFunds.Mul(puzzleFunds, big.NewInt(params.Ether)) + genesisAlloc[common.HexToAddress(contract.PuzzleAccounts[0].Address)] = core.GenesisAccount{Balance: puzzleFunds} + if shardID == 0 { // Accounts used by validator/nodes to stake and participate in the network. AddNodeAddressesToGenesisAlloc(genesisAlloc) @@ -94,12 +99,12 @@ func AddNodeAddressesToGenesisAlloc(genesisAlloc core.GenesisAlloc) { address := common.HexToAddress(account.Address) genesisAlloc[address] = core.GenesisAccount{Balance: testBankFunds} } - for _, account := range contract.NewNodeAccounts { - testBankFunds := big.NewInt(InitFreeFundInEther) - testBankFunds = testBankFunds.Mul(testBankFunds, big.NewInt(params.Ether)) - address := common.HexToAddress(account.Address) - genesisAlloc[address] = core.GenesisAccount{Balance: testBankFunds} - } + //for _, account := range contract.NewNodeAccounts { + // testBankFunds := big.NewInt(InitFreeFundInEther) + // testBankFunds = testBankFunds.Mul(testBankFunds, big.NewInt(params.Ether)) + // address := common.HexToAddress(account.Address) + // genesisAlloc[address] = core.GenesisAccount{Balance: testBankFunds} + //} for _, account := range contract.DemoAccounts { testBankFunds := big.NewInt(InitFreeFundInEther) testBankFunds = testBankFunds.Mul(testBankFunds, big.NewInt(params.Ether)) diff --git a/node/puzzle_contract.go b/node/puzzle_contract.go index 839019ae1..1958607e9 100644 --- a/node/puzzle_contract.go +++ b/node/puzzle_contract.go @@ -42,7 +42,7 @@ func (node *Node) AddPuzzleContract() { contractFunds := big.NewInt(PuzzleFund) contractFunds = contractFunds.Mul(contractFunds, big.NewInt(params.Ether)) demoContract, _ := types.SignTx( - types.NewContractCreation(uint64(0), node.Consensus.ShardID, contractFunds, params.TxGasContractCreation*10, nil, dataEnc), + types.NewContractCreation(uint64(0), node.Consensus.ShardID, contractFunds, params.TxGasContractCreation*1000, nil, dataEnc), types.HomesteadSigner{}, priKey) node.PuzzleContractAddress = crypto.CreateAddress(crypto.PubkeyToAddress(priKey.PublicKey), uint64(0)) @@ -50,8 +50,8 @@ func (node *Node) AddPuzzleContract() { node.addPendingTransactions(types.Transactions{demoContract}) } -// CreateTransactionForPlayMethod generates transaction for enter method and add it into pending tx list. -func (node *Node) CreateTransactionForPlayMethod(priKey string) error { +// CreateTransactionForPlayMethod generates transaction for play method and add it into pending tx list. +func (node *Node) CreateTransactionForPlayMethod(priKey string, amount int64) error { var err error toAddress := node.PuzzleContractAddress @@ -66,22 +66,25 @@ func (node *Node) CreateTransactionForPlayMethod(priKey string) error { return err } + Stake := big.NewInt(0) + Stake = Stake.Mul(OneEther, big.NewInt(amount)) + key, err := crypto.HexToECDSA(priKey) address := crypto.PubkeyToAddress(key.PublicKey) balance, err := node.GetBalanceOfAddress(address) if err != nil { utils.GetLogInstance().Error("puzzle-play: can not get address", "error", err) return err - } else if balance.Cmp(OneEther) == -1 { - utils.GetLogInstance().Error("puzzle-play: insufficient fund", "error", err) + } else if balance.Cmp(Stake) == -1 { + utils.GetLogInstance().Error("puzzle-play: insufficient fund", "error", err, "stake", Stake, "balance", balance) return ErrPuzzleInsufficientFund } nonce := node.GetNonceOfAddress(address) tx := types.NewTransaction( nonce, toAddress, - 0, - OneEther, + node.NodeConfig.ShardID, + Stake, params.TxGas*10, nil, bytesData, @@ -100,7 +103,7 @@ func (node *Node) CreateTransactionForPlayMethod(priKey string) error { } // CreateTransactionForPayoutMethod generates transaction for payout method and add it into pending tx list. -func (node *Node) CreateTransactionForPayoutMethod(address string, newLevel int) error { +func (node *Node) CreateTransactionForPayoutMethod(address common.Address, newLevel uint8, steps string) error { var err error toAddress := node.PuzzleContractAddress @@ -109,9 +112,9 @@ func (node *Node) CreateTransactionForPayoutMethod(address string, newLevel int) utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err) return err } - // add params for address payable player, uint8 new_level - // TODO(minh, rj) - bytesData, err := abi.Pack(Payout) + // add params for address payable player, uint8 new_level, steps string + fmt.Println("Payout: address", address) + bytesData, err := abi.Pack(Payout, address, big.NewInt(int64(newLevel)), steps) if err != nil { utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err) return err @@ -126,9 +129,9 @@ func (node *Node) CreateTransactionForPayoutMethod(address string, newLevel int) tx := types.NewTransaction( nonce, toAddress, - 0, + node.NodeConfig.ShardID, Amount, - params.TxGas*10000, + params.TxGas*10, nil, bytesData, )