From 7feec02902d52a3abf722613eb9e218e015b723c Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 30 Jun 2020 17:05:50 -0400 Subject: make mc tx work --- examples/multicoin/main.go | 218 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 examples/multicoin/main.go (limited to 'examples/multicoin/main.go') diff --git a/examples/multicoin/main.go b/examples/multicoin/main.go new file mode 100644 index 0000000..3e42010 --- /dev/null +++ b/examples/multicoin/main.go @@ -0,0 +1,218 @@ +package main + +import ( + "crypto/rand" + //"encoding/hex" + "encoding/json" + "fmt" + "github.com/ava-labs/coreth" + "github.com/ava-labs/coreth/core" + "github.com/ava-labs/coreth/eth" + //"github.com/ava-labs/coreth/accounts/abi" + "github.com/ava-labs/coreth/core/types" + "github.com/ava-labs/coreth/params" + "github.com/ava-labs/go-ethereum/common" + "github.com/ava-labs/go-ethereum/common/compiler" + "github.com/ava-labs/go-ethereum/crypto" + "github.com/ava-labs/go-ethereum/log" + "go/build" + "math/big" + "os" + "os/signal" + "path/filepath" + //"strings" + "syscall" + "time" +) + +var ( + codeHex = "6101c7610026600b82828239805160001a60731461001957fe5b30600052607381538281f3fe730000000000000000000000000000000000000000301460806040526004361061004b5760003560e01c80631e01043914610050578063abb24ba014610092578063b6510bb3146100a9575b600080fd5b61007c6004803603602081101561006657600080fd5b8101908080359060200190929190505050610118565b6040518082815260200191505060405180910390f35b81801561009e57600080fd5b506100a761013b565b005b8180156100b557600080fd5b50610116600480360360808110156100cc57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001909291908035906020019092919050505061013e565b005b60003373ffffffffffffffffffffffffffffffffffffffff1682905d9050919050565b5c565b8373ffffffffffffffffffffffffffffffffffffffff1681836108fc8690811502906040516000604051808303818888878c8af69550505050505015801561018a573d6000803e3d6000fd5b505050505056fea26469706673582212204e4689067358d8e82903012dcaca0c004d36e39fc99360919fb824c4ce718bdd64736f6c634300060a0033" + codeABI = `[{"inputs":[{"internalType":"uint256","name":"coinid","type":"uint256"}],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]` +) + +func checkError(err error) { + if err != nil { + panic(err) + } +} + +func main() { + // configure the chain + config := eth.DefaultConfig + config.ManualCanonical = true + chainConfig := ¶ms.ChainConfig{ + ChainID: big.NewInt(1), + HomesteadBlock: big.NewInt(0), + DAOForkBlock: big.NewInt(0), + DAOForkSupport: true, + EIP150Block: big.NewInt(0), + EIP150Hash: common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: nil, + Ethash: nil, + } + + // configure the genesis block + //genBalance := big.NewInt(100000000000000000) + genKey, _ := coreth.NewKey(rand.Reader) + bob, _ := coreth.NewKey(rand.Reader) + + g := new(core.Genesis) + b := `{"config":{"chainId":1,"homesteadBlock":0,"daoForkBlock":0,"daoForkSupport":true,"eip150Block":0,"eip150Hash":"0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0","eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x00","gasLimit":"0x5f5e100","difficulty":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"751a0b96e1042bee789452ecb20253fba40dbe85":{"balance":"0x1000000000000000", "mcbalance": {"0x0000000000000000000000000000000000000000000000000000000000000000": 1000000000000000000}}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}` + k := "0xabd71b35d559563fea757f0f5edbde286fb8c043105b15abb7cd57189306d7d1" + err := json.Unmarshal([]byte(b), g) + checkError(err) + config.Genesis = g + hk, _ := crypto.HexToECDSA(k[2:]) + genKey = coreth.NewKeyFromECDSA(hk) + //config.Genesis = &core.Genesis{ + // Config: chainConfig, + // Nonce: 0, + // Number: 0, + // ExtraData: hexutil.MustDecode("0x00"), + // GasLimit: 100000000, + // Difficulty: big.NewInt(0), + // Alloc: core.GenesisAlloc{genKey.Address: {Balance: genBalance}}, + //} + + // grab the control of block generation and disable auto uncle + config.Miner.ManualMining = true + config.Miner.ManualUncle = true + + // compile the smart contract + gopath := os.Getenv("GOPATH") + if gopath == "" { + gopath = build.Default.GOPATH + } + + // info required to generate a transaction + chainID := chainConfig.ChainID + nonce := uint64(0) + gasLimit := 10000000 + gasPrice := big.NewInt(1000000000) + + blockCount := 0 + chain := coreth.NewETHChain(&config, nil, nil, nil) + newTxPoolHeadChan := make(chan core.NewTxPoolHeadEvent, 1) + log.Info(chain.GetGenesisBlock().Hash().Hex()) + firstBlock := false + var contractAddr common.Address + coin0 := common.HexToHash("0x0") + //var calls [][]byte + postGen := func(block *types.Block) bool { + if blockCount == 15 { + state, err := chain.CurrentState() + checkError(err) + log.Info(fmt.Sprintf("genesis balance = %s", state.GetBalance(genKey.Address))) + log.Info(fmt.Sprintf("genesis balance2 = %s", state.GetBalanceMultiCoin(genKey.Address, coin0))) + log.Info(fmt.Sprintf("contract balance = %s", state.GetBalance(contractAddr))) + log.Info(fmt.Sprintf("bob's balance = %s", state.GetBalance(bob.Address))) + log.Info(fmt.Sprintf("bob's balance2 = %s", state.GetBalanceMultiCoin(bob.Address, coin0))) + log.Info(fmt.Sprintf("state = %s", state.Dump(true, false, true))) + log.Info(fmt.Sprintf("x = %s", state.GetState(contractAddr, common.BigToHash(big.NewInt(0))).String())) + return true + } + if !firstBlock { + firstBlock = true + receipts := chain.GetReceiptsByHash(block.Hash()) + if len(receipts) != 1 { + panic(fmt.Sprintf("# receipts is %d != 1", len(receipts))) + } + contractAddr = receipts[0].ContractAddress + txHash := receipts[0].TxHash + log.Info(fmt.Sprintf("deploy tx = %s", txHash.String())) + log.Info(fmt.Sprintf("contract addr = %s", contractAddr.String())) + //var call []byte + //if len(calls) > 0 { + // call = calls[0] + // calls = calls[1:] + //} else { + // call = nil + //} + //state, _ := chain.CurrentState() + //call := common.Hex2Bytes("1003e2d20000000000000000000000000000000000000000000000000000000000000001") + //log.Info(fmt.Sprintf("code = %s", hex.EncodeToString(state.GetCode(contractAddr)))) + go func() { + tx := types.NewTransaction(nonce, bob.Address, big.NewInt(300000000000000000), uint64(gasLimit), gasPrice, nil) + signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) + checkError(err) + chain.AddRemoteTxs([]*types.Transaction{signedTx}) + nonce++ + time.Sleep(20 * time.Millisecond) + + // self-loop tx to enable MC + tx = types.NewTransaction(0, bob.Address, big.NewInt(1), uint64(gasLimit), gasPrice, nil) + tx.SetMultiCoinValue(&coin0, big.NewInt(0)) + signedTx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), bob.PrivateKey) + checkError(err) + chain.AddRemoteTxs([]*types.Transaction{signedTx}) + time.Sleep(20 * time.Millisecond) + + for i := 0; i < 10; i++ { + tx := types.NewTransaction(nonce, bob.Address, big.NewInt(10000000000000000), uint64(gasLimit), gasPrice, nil) + tx.SetMultiCoinValue(&coin0, big.NewInt(100000000000000000)) + signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) + checkError(err) + chain.AddRemoteTxs([]*types.Transaction{signedTx}) + nonce++ + } + }() + } + return false + } + chain.SetOnHeaderNew(func(header *types.Header) { + hid := make([]byte, 32) + _, err := rand.Read(hid) + if err != nil { + panic("cannot generate hid") + } + header.Extra = append(header.Extra, hid...) + }) + chain.SetOnSealFinish(func(block *types.Block) error { + blockCount++ + if postGen(block) { + return nil + } + go func() { + <-newTxPoolHeadChan + time.Sleep(10 * time.Millisecond) + chain.GenBlock() + }() + return nil + }) + + // start the chain + chain.GetTxPool().SubscribeNewHeadEvent(newTxPoolHeadChan) + chain.Start() + + //code := common.Hex2Bytes(codeHex) + counterSrc, err := filepath.Abs(gopath + "/src/github.com/ava-labs/coreth/examples/counter/counter.sol") + checkError(err) + contracts, err := compiler.CompileSolidity("", counterSrc) + checkError(err) + contract, _ := contracts[fmt.Sprintf("%s:%s", counterSrc, "Counter")] + code := common.Hex2Bytes(contract.Code[2:]) + + //abi, err := abi.JSON(strings.NewReader(codeABI)) + //cc, err := abi.Pack("getBalance", big.NewInt(0)) + //checkError(err) + //calls = append(calls, cc) + tx := types.NewContractCreation(nonce, big.NewInt(0), uint64(gasLimit), gasPrice, code) + signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) + checkError(err) + chain.AddRemoteTxs([]*types.Transaction{signedTx}) + time.Sleep(1000 * time.Millisecond) + nonce++ + + chain.GenBlock() + + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + signal.Notify(c, os.Interrupt, syscall.SIGINT) + <-c + chain.Stop() +} -- cgit v1.2.3 From fbe0f2d6b14d7b6fc43068e224412d4e65176550 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 30 Jun 2020 20:56:12 -0400 Subject: test multi-coin smart contracts --- examples/multicoin/main.go | 87 ++++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 33 deletions(-) (limited to 'examples/multicoin/main.go') diff --git a/examples/multicoin/main.go b/examples/multicoin/main.go index 3e42010..b226703 100644 --- a/examples/multicoin/main.go +++ b/examples/multicoin/main.go @@ -6,28 +6,30 @@ import ( "encoding/json" "fmt" "github.com/ava-labs/coreth" + "github.com/ava-labs/coreth/accounts/abi" "github.com/ava-labs/coreth/core" - "github.com/ava-labs/coreth/eth" - //"github.com/ava-labs/coreth/accounts/abi" "github.com/ava-labs/coreth/core/types" + "github.com/ava-labs/coreth/eth" "github.com/ava-labs/coreth/params" "github.com/ava-labs/go-ethereum/common" - "github.com/ava-labs/go-ethereum/common/compiler" + //"github.com/ava-labs/go-ethereum/common/compiler" "github.com/ava-labs/go-ethereum/crypto" "github.com/ava-labs/go-ethereum/log" "go/build" "math/big" "os" "os/signal" - "path/filepath" - //"strings" + //"path/filepath" + "strings" "syscall" "time" ) var ( - codeHex = "6101c7610026600b82828239805160001a60731461001957fe5b30600052607381538281f3fe730000000000000000000000000000000000000000301460806040526004361061004b5760003560e01c80631e01043914610050578063abb24ba014610092578063b6510bb3146100a9575b600080fd5b61007c6004803603602081101561006657600080fd5b8101908080359060200190929190505050610118565b6040518082815260200191505060405180910390f35b81801561009e57600080fd5b506100a761013b565b005b8180156100b557600080fd5b50610116600480360360808110156100cc57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001909291908035906020019092919050505061013e565b005b60003373ffffffffffffffffffffffffffffffffffffffff1682905d9050919050565b5c565b8373ffffffffffffffffffffffffffffffffffffffff1681836108fc8690811502906040516000604051808303818888878c8af69550505050505015801561018a573d6000803e3d6000fd5b505050505056fea26469706673582212204e4689067358d8e82903012dcaca0c004d36e39fc99360919fb824c4ce718bdd64736f6c634300060a0033" - codeABI = `[{"inputs":[{"internalType":"uint256","name":"coinid","type":"uint256"}],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]` + mcLibCode = "6101c7610026600b82828239805160001a60731461001957fe5b30600052607381538281f3fe730000000000000000000000000000000000000000301460806040526004361061004b5760003560e01c80631e01043914610050578063abb24ba014610092578063b6510bb3146100a9575b600080fd5b61007c6004803603602081101561006657600080fd5b8101908080359060200190929190505050610118565b6040518082815260200191505060405180910390f35b81801561009e57600080fd5b506100a761013b565b005b8180156100b557600080fd5b50610116600480360360808110156100cc57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001909291908035906020019092919050505061013e565b005b60003373ffffffffffffffffffffffffffffffffffffffff1682905d9050919050565b5c565b8373ffffffffffffffffffffffffffffffffffffffff1681836108fc8690811502906040516000604051808303818888878c8af69550505050505015801561018a573d6000803e3d6000fd5b505050505056fea2646970667358221220477c0e55c25708d36d55abae8a51496c16bc5d28bc1ee6a9963c304afbacdc3464736f6c634300060a0033" + mcLibAddr = "0xDCb165540502E5d32F89382175150013D6644A69" + testContract = "608060405234801561001057600080fd5b5073dcb165540502e5d32f89382175150013d6644a6963abb24ba06040518163ffffffff1660e01b815260040160006040518083038186803b15801561005557600080fd5b505af4158015610069573d6000803e3d6000fd5b505050506101528061007c6000396000f3fe6080604052600436106100295760003560e01c80631e0104391461002e578063d0e30db01461007d575b600080fd5b34801561003a57600080fd5b506100676004803603602081101561005157600080fd5b8101908080359060200190929190505050610087565b6040518082815260200191505060405180910390f35b61008561011a565b005b600073dcb165540502e5d32f89382175150013d6644a69631e010439836040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b1580156100d857600080fd5b505af41580156100ec573d6000803e3d6000fd5b505050506040513d602081101561010257600080fd5b81019080805190602001909291905050509050919050565b56fea26469706673582212204e41dc04c9409d8c06804429d6a03f2d6bca46de6073522300ea08f8ed4a90ac64736f6c634300060a0033" + testContractABI = `[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"deposit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"coinid","type":"uint256"}],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]` ) func checkError(err error) { @@ -99,25 +101,23 @@ func main() { chain := coreth.NewETHChain(&config, nil, nil, nil) newTxPoolHeadChan := make(chan core.NewTxPoolHeadEvent, 1) log.Info(chain.GetGenesisBlock().Hash().Hex()) - firstBlock := false var contractAddr common.Address + var contractAddr1 common.Address coin0 := common.HexToHash("0x0") - //var calls [][]byte postGen := func(block *types.Block) bool { if blockCount == 15 { state, err := chain.CurrentState() checkError(err) log.Info(fmt.Sprintf("genesis balance = %s", state.GetBalance(genKey.Address))) log.Info(fmt.Sprintf("genesis balance2 = %s", state.GetBalanceMultiCoin(genKey.Address, coin0))) - log.Info(fmt.Sprintf("contract balance = %s", state.GetBalance(contractAddr))) + log.Info(fmt.Sprintf("contract balance2 = %s", state.GetBalanceMultiCoin(contractAddr1, coin0))) log.Info(fmt.Sprintf("bob's balance = %s", state.GetBalance(bob.Address))) log.Info(fmt.Sprintf("bob's balance2 = %s", state.GetBalanceMultiCoin(bob.Address, coin0))) log.Info(fmt.Sprintf("state = %s", state.Dump(true, false, true))) log.Info(fmt.Sprintf("x = %s", state.GetState(contractAddr, common.BigToHash(big.NewInt(0))).String())) return true } - if !firstBlock { - firstBlock = true + if blockCount == 1 { receipts := chain.GetReceiptsByHash(block.Hash()) if len(receipts) != 1 { panic(fmt.Sprintf("# receipts is %d != 1", len(receipts))) @@ -126,14 +126,20 @@ func main() { txHash := receipts[0].TxHash log.Info(fmt.Sprintf("deploy tx = %s", txHash.String())) log.Info(fmt.Sprintf("contract addr = %s", contractAddr.String())) - //var call []byte - //if len(calls) > 0 { - // call = calls[0] - // calls = calls[1:] - //} else { - // call = nil - //} - //state, _ := chain.CurrentState() + code := common.Hex2Bytes(testContract) + tx := types.NewContractCreation(nonce, big.NewInt(0), uint64(gasLimit), gasPrice, code) + signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) + checkError(err) + chain.AddRemoteTxs([]*types.Transaction{signedTx}) + nonce++ + } else if blockCount == 2 { + receipts := chain.GetReceiptsByHash(block.Hash()) + if len(receipts) != 1 { + panic(fmt.Sprintf("# receipts is %d != 1", len(receipts))) + } + contractAddr1 = receipts[0].ContractAddress + fmt.Println("contract", contractAddr1.Hex()) + //call := common.Hex2Bytes("1003e2d20000000000000000000000000000000000000000000000000000000000000001") //log.Info(fmt.Sprintf("code = %s", hex.EncodeToString(state.GetCode(contractAddr)))) go func() { @@ -152,13 +158,26 @@ func main() { chain.AddRemoteTxs([]*types.Transaction{signedTx}) time.Sleep(20 * time.Millisecond) - for i := 0; i < 10; i++ { - tx := types.NewTransaction(nonce, bob.Address, big.NewInt(10000000000000000), uint64(gasLimit), gasPrice, nil) + var code []byte + abi, err := abi.JSON(strings.NewReader(testContractABI)) + checkError(err) + code, err = abi.Pack("deposit") + checkError(err) + + for i := 0; i < 5; i++ { + tx := types.NewTransaction(nonce, bob.Address, big.NewInt(0), uint64(gasLimit), gasPrice, nil) tx.SetMultiCoinValue(&coin0, big.NewInt(100000000000000000)) signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) chain.AddRemoteTxs([]*types.Transaction{signedTx}) nonce++ + + tx = types.NewTransaction(nonce, contractAddr1, big.NewInt(0), uint64(gasLimit), gasPrice, code) + tx.SetMultiCoinValue(&coin0, big.NewInt(100000000000000000)) + signedTx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) + checkError(err) + chain.AddRemoteTxs([]*types.Transaction{signedTx}) + nonce++ } }() } @@ -189,25 +208,27 @@ func main() { chain.GetTxPool().SubscribeNewHeadEvent(newTxPoolHeadChan) chain.Start() - //code := common.Hex2Bytes(codeHex) - counterSrc, err := filepath.Abs(gopath + "/src/github.com/ava-labs/coreth/examples/counter/counter.sol") - checkError(err) - contracts, err := compiler.CompileSolidity("", counterSrc) - checkError(err) - contract, _ := contracts[fmt.Sprintf("%s:%s", counterSrc, "Counter")] - code := common.Hex2Bytes(contract.Code[2:]) - - //abi, err := abi.JSON(strings.NewReader(codeABI)) - //cc, err := abi.Pack("getBalance", big.NewInt(0)) + //cc, err := abi.Pack("enableMultiCoin") + //checkError(err) + //calls = append(calls, cc) + //cc, err = abi.Pack("getBalance", big.NewInt(0)) //checkError(err) //calls = append(calls, cc) + + code := common.Hex2Bytes(mcLibCode) tx := types.NewContractCreation(nonce, big.NewInt(0), uint64(gasLimit), gasPrice, code) signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) chain.AddRemoteTxs([]*types.Transaction{signedTx}) - time.Sleep(1000 * time.Millisecond) nonce++ + //counterSrc, err := filepath.Abs(gopath + "/src/github.com/ava-labs/coreth/examples/counter/counter.sol") + //checkError(err) + //contracts, err := compiler.CompileSolidity("", counterSrc) + //checkError(err) + //contract, _ := contracts[fmt.Sprintf("%s:%s", counterSrc, "Counter")] + //code := common.Hex2Bytes(contract.Code[2:]) + time.Sleep(10 * time.Millisecond) chain.GenBlock() c := make(chan os.Signal, 1) -- cgit v1.2.3 From a00cf4e6318e0dcda72725995653011c545e13ff Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 1 Jul 2020 16:34:08 -0400 Subject: embed mc lib into genesis --- examples/multicoin/main.go | 76 +++++++++------------------------------------- 1 file changed, 15 insertions(+), 61 deletions(-) (limited to 'examples/multicoin/main.go') diff --git a/examples/multicoin/main.go b/examples/multicoin/main.go index b226703..ad23e32 100644 --- a/examples/multicoin/main.go +++ b/examples/multicoin/main.go @@ -2,7 +2,6 @@ package main import ( "crypto/rand" - //"encoding/hex" "encoding/json" "fmt" "github.com/ava-labs/coreth" @@ -12,26 +11,19 @@ import ( "github.com/ava-labs/coreth/eth" "github.com/ava-labs/coreth/params" "github.com/ava-labs/go-ethereum/common" - //"github.com/ava-labs/go-ethereum/common/compiler" + "github.com/ava-labs/go-ethereum/common/compiler" "github.com/ava-labs/go-ethereum/crypto" "github.com/ava-labs/go-ethereum/log" "go/build" "math/big" "os" "os/signal" - //"path/filepath" + "path/filepath" "strings" "syscall" "time" ) -var ( - mcLibCode = "6101c7610026600b82828239805160001a60731461001957fe5b30600052607381538281f3fe730000000000000000000000000000000000000000301460806040526004361061004b5760003560e01c80631e01043914610050578063abb24ba014610092578063b6510bb3146100a9575b600080fd5b61007c6004803603602081101561006657600080fd5b8101908080359060200190929190505050610118565b6040518082815260200191505060405180910390f35b81801561009e57600080fd5b506100a761013b565b005b8180156100b557600080fd5b50610116600480360360808110156100cc57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001909291908035906020019092919050505061013e565b005b60003373ffffffffffffffffffffffffffffffffffffffff1682905d9050919050565b5c565b8373ffffffffffffffffffffffffffffffffffffffff1681836108fc8690811502906040516000604051808303818888878c8af69550505050505015801561018a573d6000803e3d6000fd5b505050505056fea2646970667358221220477c0e55c25708d36d55abae8a51496c16bc5d28bc1ee6a9963c304afbacdc3464736f6c634300060a0033" - mcLibAddr = "0xDCb165540502E5d32F89382175150013D6644A69" - testContract = "608060405234801561001057600080fd5b5073dcb165540502e5d32f89382175150013d6644a6963abb24ba06040518163ffffffff1660e01b815260040160006040518083038186803b15801561005557600080fd5b505af4158015610069573d6000803e3d6000fd5b505050506101528061007c6000396000f3fe6080604052600436106100295760003560e01c80631e0104391461002e578063d0e30db01461007d575b600080fd5b34801561003a57600080fd5b506100676004803603602081101561005157600080fd5b8101908080359060200190929190505050610087565b6040518082815260200191505060405180910390f35b61008561011a565b005b600073dcb165540502e5d32f89382175150013d6644a69631e010439836040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b1580156100d857600080fd5b505af41580156100ec573d6000803e3d6000fd5b505050506040513d602081101561010257600080fd5b81019080805190602001909291905050509050919050565b56fea26469706673582212204e41dc04c9409d8c06804429d6a03f2d6bca46de6073522300ea08f8ed4a90ac64736f6c634300060a0033" - testContractABI = `[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"deposit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"coinid","type":"uint256"}],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]` -) - func checkError(err error) { if err != nil { panic(err) @@ -59,27 +51,17 @@ func main() { } // configure the genesis block - //genBalance := big.NewInt(100000000000000000) genKey, _ := coreth.NewKey(rand.Reader) bob, _ := coreth.NewKey(rand.Reader) g := new(core.Genesis) - b := `{"config":{"chainId":1,"homesteadBlock":0,"daoForkBlock":0,"daoForkSupport":true,"eip150Block":0,"eip150Hash":"0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0","eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x00","gasLimit":"0x5f5e100","difficulty":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"751a0b96e1042bee789452ecb20253fba40dbe85":{"balance":"0x1000000000000000", "mcbalance": {"0x0000000000000000000000000000000000000000000000000000000000000000": 1000000000000000000}}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}` + b := `{"config":{"chainId":1,"homesteadBlock":0,"daoForkBlock":0,"daoForkSupport":true,"eip150Block":0,"eip150Hash":"0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0","eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x00","gasLimit":"0x5f5e100","difficulty":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"751a0b96e1042bee789452ecb20253fba40dbe85":{"balance":"0x1000000000000000", "mcbalance": {"0x0000000000000000000000000000000000000000000000000000000000000000": 1000000000000000000}}, "0100000000000000000000000000000000000000": {"code": "0x730000000000000000000000000000000000000000301460806040526004361061004b5760003560e01c80631e01043914610050578063abb24ba014610092578063b6510bb3146100a9575b600080fd5b61007c6004803603602081101561006657600080fd5b8101908080359060200190929190505050610118565b6040518082815260200191505060405180910390f35b81801561009e57600080fd5b506100a761013b565b005b8180156100b557600080fd5b50610116600480360360808110156100cc57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001909291908035906020019092919050505061013e565b005b60003373ffffffffffffffffffffffffffffffffffffffff1682905d9050919050565b5c565b8373ffffffffffffffffffffffffffffffffffffffff1681836108fc8690811502906040516000604051808303818888878c8af69550505050505015801561018a573d6000803e3d6000fd5b505050505056fea2646970667358221220477c0e55c25708d36d55abae8a51496c16bc5d28bc1ee6a9963c304afbacdc3464736f6c634300060a0033", "balance": "0x0", "mcbalance": {}}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}` k := "0xabd71b35d559563fea757f0f5edbde286fb8c043105b15abb7cd57189306d7d1" err := json.Unmarshal([]byte(b), g) checkError(err) config.Genesis = g hk, _ := crypto.HexToECDSA(k[2:]) genKey = coreth.NewKeyFromECDSA(hk) - //config.Genesis = &core.Genesis{ - // Config: chainConfig, - // Nonce: 0, - // Number: 0, - // ExtraData: hexutil.MustDecode("0x00"), - // GasLimit: 100000000, - // Difficulty: big.NewInt(0), - // Alloc: core.GenesisAlloc{genKey.Address: {Balance: genBalance}}, - //} // grab the control of block generation and disable auto uncle config.Miner.ManualMining = true @@ -90,6 +72,11 @@ func main() { if gopath == "" { gopath = build.Default.GOPATH } + counterSrc, err := filepath.Abs(gopath + "/src/github.com/ava-labs/coreth/examples/multicoin/mc_test.sol") + checkError(err) + contracts, err := compiler.CompileSolidity("", counterSrc) + checkError(err) + contract, _ := contracts[fmt.Sprintf("%s:%s", counterSrc, "MCTest")] // info required to generate a transaction chainID := chainConfig.ChainID @@ -102,7 +89,6 @@ func main() { newTxPoolHeadChan := make(chan core.NewTxPoolHeadEvent, 1) log.Info(chain.GetGenesisBlock().Hash().Hex()) var contractAddr common.Address - var contractAddr1 common.Address coin0 := common.HexToHash("0x0") postGen := func(block *types.Block) bool { if blockCount == 15 { @@ -110,11 +96,10 @@ func main() { checkError(err) log.Info(fmt.Sprintf("genesis balance = %s", state.GetBalance(genKey.Address))) log.Info(fmt.Sprintf("genesis balance2 = %s", state.GetBalanceMultiCoin(genKey.Address, coin0))) - log.Info(fmt.Sprintf("contract balance2 = %s", state.GetBalanceMultiCoin(contractAddr1, coin0))) + log.Info(fmt.Sprintf("contract balance2 = %s", state.GetBalanceMultiCoin(contractAddr, coin0))) log.Info(fmt.Sprintf("bob's balance = %s", state.GetBalance(bob.Address))) log.Info(fmt.Sprintf("bob's balance2 = %s", state.GetBalanceMultiCoin(bob.Address, coin0))) log.Info(fmt.Sprintf("state = %s", state.Dump(true, false, true))) - log.Info(fmt.Sprintf("x = %s", state.GetState(contractAddr, common.BigToHash(big.NewInt(0))).String())) return true } if blockCount == 1 { @@ -123,25 +108,8 @@ func main() { panic(fmt.Sprintf("# receipts is %d != 1", len(receipts))) } contractAddr = receipts[0].ContractAddress - txHash := receipts[0].TxHash - log.Info(fmt.Sprintf("deploy tx = %s", txHash.String())) log.Info(fmt.Sprintf("contract addr = %s", contractAddr.String())) - code := common.Hex2Bytes(testContract) - tx := types.NewContractCreation(nonce, big.NewInt(0), uint64(gasLimit), gasPrice, code) - signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) - checkError(err) - chain.AddRemoteTxs([]*types.Transaction{signedTx}) - nonce++ - } else if blockCount == 2 { - receipts := chain.GetReceiptsByHash(block.Hash()) - if len(receipts) != 1 { - panic(fmt.Sprintf("# receipts is %d != 1", len(receipts))) - } - contractAddr1 = receipts[0].ContractAddress - fmt.Println("contract", contractAddr1.Hex()) - //call := common.Hex2Bytes("1003e2d20000000000000000000000000000000000000000000000000000000000000001") - //log.Info(fmt.Sprintf("code = %s", hex.EncodeToString(state.GetCode(contractAddr)))) go func() { tx := types.NewTransaction(nonce, bob.Address, big.NewInt(300000000000000000), uint64(gasLimit), gasPrice, nil) signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) @@ -159,9 +127,11 @@ func main() { time.Sleep(20 * time.Millisecond) var code []byte - abi, err := abi.JSON(strings.NewReader(testContractABI)) + var a abi.ABI + abiStr, err := json.Marshal(contract.Info.AbiDefinition) + a, err = abi.JSON(strings.NewReader(string(abiStr))) checkError(err) - code, err = abi.Pack("deposit") + code, err = a.Pack("deposit") checkError(err) for i := 0; i < 5; i++ { @@ -172,7 +142,7 @@ func main() { chain.AddRemoteTxs([]*types.Transaction{signedTx}) nonce++ - tx = types.NewTransaction(nonce, contractAddr1, big.NewInt(0), uint64(gasLimit), gasPrice, code) + tx = types.NewTransaction(nonce, contractAddr, big.NewInt(0), uint64(gasLimit), gasPrice, code) tx.SetMultiCoinValue(&coin0, big.NewInt(100000000000000000)) signedTx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) @@ -207,28 +177,12 @@ func main() { // start the chain chain.GetTxPool().SubscribeNewHeadEvent(newTxPoolHeadChan) chain.Start() - - //cc, err := abi.Pack("enableMultiCoin") - //checkError(err) - //calls = append(calls, cc) - //cc, err = abi.Pack("getBalance", big.NewInt(0)) - //checkError(err) - //calls = append(calls, cc) - - code := common.Hex2Bytes(mcLibCode) + code := common.Hex2Bytes(contract.Code[2:]) tx := types.NewContractCreation(nonce, big.NewInt(0), uint64(gasLimit), gasPrice, code) signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) chain.AddRemoteTxs([]*types.Transaction{signedTx}) nonce++ - //counterSrc, err := filepath.Abs(gopath + "/src/github.com/ava-labs/coreth/examples/counter/counter.sol") - //checkError(err) - //contracts, err := compiler.CompileSolidity("", counterSrc) - //checkError(err) - //contract, _ := contracts[fmt.Sprintf("%s:%s", counterSrc, "Counter")] - //code := common.Hex2Bytes(contract.Code[2:]) - - time.Sleep(10 * time.Millisecond) chain.GenBlock() c := make(chan os.Signal, 1) -- cgit v1.2.3 From 2d396b3c69a26e4096ccb376c012911d299c68c7 Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 1 Jul 2020 17:29:39 -0400 Subject: make sure getBalance() works --- examples/multicoin/main.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'examples/multicoin/main.go') diff --git a/examples/multicoin/main.go b/examples/multicoin/main.go index ad23e32..9d1970a 100644 --- a/examples/multicoin/main.go +++ b/examples/multicoin/main.go @@ -55,7 +55,7 @@ func main() { bob, _ := coreth.NewKey(rand.Reader) g := new(core.Genesis) - b := `{"config":{"chainId":1,"homesteadBlock":0,"daoForkBlock":0,"daoForkSupport":true,"eip150Block":0,"eip150Hash":"0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0","eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x00","gasLimit":"0x5f5e100","difficulty":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"751a0b96e1042bee789452ecb20253fba40dbe85":{"balance":"0x1000000000000000", "mcbalance": {"0x0000000000000000000000000000000000000000000000000000000000000000": 1000000000000000000}}, "0100000000000000000000000000000000000000": {"code": "0x730000000000000000000000000000000000000000301460806040526004361061004b5760003560e01c80631e01043914610050578063abb24ba014610092578063b6510bb3146100a9575b600080fd5b61007c6004803603602081101561006657600080fd5b8101908080359060200190929190505050610118565b6040518082815260200191505060405180910390f35b81801561009e57600080fd5b506100a761013b565b005b8180156100b557600080fd5b50610116600480360360808110156100cc57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001909291908035906020019092919050505061013e565b005b60003373ffffffffffffffffffffffffffffffffffffffff1682905d9050919050565b5c565b8373ffffffffffffffffffffffffffffffffffffffff1681836108fc8690811502906040516000604051808303818888878c8af69550505050505015801561018a573d6000803e3d6000fd5b505050505056fea2646970667358221220477c0e55c25708d36d55abae8a51496c16bc5d28bc1ee6a9963c304afbacdc3464736f6c634300060a0033", "balance": "0x0", "mcbalance": {}}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}` + b := `{"config":{"chainId":1,"homesteadBlock":0,"daoForkBlock":0,"daoForkSupport":true,"eip150Block":0,"eip150Hash":"0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0","eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x00","gasLimit":"0x5f5e100","difficulty":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"751a0b96e1042bee789452ecb20253fba40dbe85":{"balance":"0x1000000000000000", "mcbalance": {"0x0000000000000000000000000000000000000000000000000000000000000000": 1000000000000000000}}, "0100000000000000000000000000000000000000": {"code": "0x730000000000000000000000000000000000000000301460806040526004361061004b5760003560e01c80631e01043914610050578063abb24ba014610092578063b6510bb3146100a9575b600080fd5b61007c6004803603602081101561006657600080fd5b8101908080359060200190929190505050610118565b6040518082815260200191505060405180910390f35b81801561009e57600080fd5b506100a761013b565b005b8180156100b557600080fd5b50610116600480360360808110156100cc57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001909291908035906020019092919050505061013e565b005b60003073ffffffffffffffffffffffffffffffffffffffff1682905d9050919050565b5c565b8373ffffffffffffffffffffffffffffffffffffffff1681836108fc8690811502906040516000604051808303818888878c8af69550505050505015801561018a573d6000803e3d6000fd5b505050505056fea26469706673582212200bc9fda886770285cc0f33cf0c674c080debc2ad79572ecf0c9f20d729b9487264736f6c634300060a0033", "balance": "0x0", "mcbalance": {}}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}` k := "0xabd71b35d559563fea757f0f5edbde286fb8c043105b15abb7cd57189306d7d1" err := json.Unmarshal([]byte(b), g) checkError(err) @@ -149,6 +149,15 @@ func main() { chain.AddRemoteTxs([]*types.Transaction{signedTx}) nonce++ } + code, err = a.Pack("updateBalance", big.NewInt(0)) + tx = types.NewTransaction(nonce, contractAddr, big.NewInt(0), uint64(gasLimit), gasPrice, code) + signedTx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) + checkError(err) + chain.AddRemoteTxs([]*types.Transaction{signedTx}) + nonce++ + + checkError(err) + }() } return false -- cgit v1.2.3 From ca6847998d867dc5d66e76a03bda9c72ae8d287b Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 2 Jul 2020 01:36:18 -0400 Subject: make MultiCoin.transfer work --- examples/multicoin/main.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'examples/multicoin/main.go') diff --git a/examples/multicoin/main.go b/examples/multicoin/main.go index 9d1970a..403db4d 100644 --- a/examples/multicoin/main.go +++ b/examples/multicoin/main.go @@ -55,7 +55,7 @@ func main() { bob, _ := coreth.NewKey(rand.Reader) g := new(core.Genesis) - b := `{"config":{"chainId":1,"homesteadBlock":0,"daoForkBlock":0,"daoForkSupport":true,"eip150Block":0,"eip150Hash":"0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0","eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x00","gasLimit":"0x5f5e100","difficulty":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"751a0b96e1042bee789452ecb20253fba40dbe85":{"balance":"0x1000000000000000", "mcbalance": {"0x0000000000000000000000000000000000000000000000000000000000000000": 1000000000000000000}}, "0100000000000000000000000000000000000000": {"code": "0x730000000000000000000000000000000000000000301460806040526004361061004b5760003560e01c80631e01043914610050578063abb24ba014610092578063b6510bb3146100a9575b600080fd5b61007c6004803603602081101561006657600080fd5b8101908080359060200190929190505050610118565b6040518082815260200191505060405180910390f35b81801561009e57600080fd5b506100a761013b565b005b8180156100b557600080fd5b50610116600480360360808110156100cc57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001909291908035906020019092919050505061013e565b005b60003073ffffffffffffffffffffffffffffffffffffffff1682905d9050919050565b5c565b8373ffffffffffffffffffffffffffffffffffffffff1681836108fc8690811502906040516000604051808303818888878c8af69550505050505015801561018a573d6000803e3d6000fd5b505050505056fea26469706673582212200bc9fda886770285cc0f33cf0c674c080debc2ad79572ecf0c9f20d729b9487264736f6c634300060a0033", "balance": "0x0", "mcbalance": {}}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}` + b := `{"config":{"chainId":1,"homesteadBlock":0,"daoForkBlock":0,"daoForkSupport":true,"eip150Block":0,"eip150Hash":"0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0","eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x00","gasLimit":"0x5f5e100","difficulty":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"751a0b96e1042bee789452ecb20253fba40dbe85":{"balance":"0x1000000000000000", "mcbalance": {"0x0000000000000000000000000000000000000000000000000000000000000000": 1000000000000000000}}, "0100000000000000000000000000000000000000": {"code": "0x730000000000000000000000000000000000000000301460806040526004361061004b5760003560e01c80631e01043914610050578063abb24ba014610092578063b6510bb3146100a9575b600080fd5b61007c6004803603602081101561006657600080fd5b8101908080359060200190929190505050610118565b6040518082815260200191505060405180910390f35b81801561009e57600080fd5b506100a761013b565b005b8180156100b557600080fd5b50610116600480360360808110156100cc57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001909291908035906020019092919050505061013e565b005b60003073ffffffffffffffffffffffffffffffffffffffff1682905d9050919050565b5c565b8373ffffffffffffffffffffffffffffffffffffffff1681836108fc8690811502906040516000604051808303818888878c8af69550505050505015801561018a573d6000803e3d6000fd5b505050505056fea2646970667358221220ed2100d6623a884d196eceefabe5e03da4309a2562bb25262f3874f1acb31cd764736f6c634300060a0033", "balance": "0x0", "mcbalance": {}}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}` k := "0xabd71b35d559563fea757f0f5edbde286fb8c043105b15abb7cd57189306d7d1" err := json.Unmarshal([]byte(b), g) checkError(err) @@ -149,15 +149,19 @@ func main() { chain.AddRemoteTxs([]*types.Transaction{signedTx}) nonce++ } - code, err = a.Pack("updateBalance", big.NewInt(0)) + code, err = a.Pack("withdraw", big.NewInt(0), big.NewInt(0), big.NewInt(10000000000000000)) tx = types.NewTransaction(nonce, contractAddr, big.NewInt(0), uint64(gasLimit), gasPrice, code) signedTx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) chain.AddRemoteTxs([]*types.Transaction{signedTx}) nonce++ + code, err = a.Pack("updateBalance", big.NewInt(0)) + tx = types.NewTransaction(nonce, contractAddr, big.NewInt(0), uint64(gasLimit), gasPrice, code) + signedTx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) - + chain.AddRemoteTxs([]*types.Transaction{signedTx}) + nonce++ }() } return false -- cgit v1.2.3 From 1f49826de2bb8bb4f5f99f69fd2beb039b1172d9 Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 29 Jul 2020 12:01:05 -0400 Subject: clean up --- examples/multicoin/main.go | 77 +++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 32 deletions(-) (limited to 'examples/multicoin/main.go') diff --git a/examples/multicoin/main.go b/examples/multicoin/main.go index 403db4d..3b221b1 100644 --- a/examples/multicoin/main.go +++ b/examples/multicoin/main.go @@ -8,6 +8,7 @@ import ( "github.com/ava-labs/coreth/accounts/abi" "github.com/ava-labs/coreth/core" "github.com/ava-labs/coreth/core/types" + "github.com/ava-labs/coreth/core/vm" "github.com/ava-labs/coreth/eth" "github.com/ava-labs/coreth/params" "github.com/ava-labs/go-ethereum/common" @@ -51,18 +52,18 @@ func main() { } // configure the genesis block - genKey, _ := coreth.NewKey(rand.Reader) - bob, _ := coreth.NewKey(rand.Reader) + genesisJSON := `{"config":{"chainId":1,"homesteadBlock":0,"daoForkBlock":0,"daoForkSupport":true,"eip150Block":0,"eip150Hash":"0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0","eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x00","gasLimit":"0x5f5e100","difficulty":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"751a0b96e1042bee789452ecb20253fba40dbe85":{"balance":"0x1000000000000000", "mcbalance": {"0x0000000000000000000000000000000000000000000000000000000000000000": 1000000000000000000}}, "0100000000000000000000000000000000000000": {"code": "0x730000000000000000000000000000000000000000301460806040526004361061004b5760003560e01c80631e01043914610050578063abb24ba014610092578063b6510bb3146100a9575b600080fd5b61007c6004803603602081101561006657600080fd5b8101908080359060200190929190505050610118565b6040518082815260200191505060405180910390f35b81801561009e57600080fd5b506100a761013b565b005b8180156100b557600080fd5b50610116600480360360808110156100cc57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001909291908035906020019092919050505061013e565b005b60003073ffffffffffffffffffffffffffffffffffffffff1682905d9050919050565b5c565b8373ffffffffffffffffffffffffffffffffffffffff1681836108fc8690811502906040516000604051808303818888878c8af69550505050505015801561018a573d6000803e3d6000fd5b505050505056fea2646970667358221220ed2100d6623a884d196eceefabe5e03da4309a2562bb25262f3874f1acb31cd764736f6c634300060a0033", "balance": "0x0", "mcbalance": {}}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}` + mcAbiJSON := `[{"inputs":[],"name":"enableMultiCoin","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"coinid","type":"uint256"}],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address payable","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"coinid","type":"uint256"},{"internalType":"uint256","name":"amount2","type":"uint256"}],"name":"transfer","outputs":[],"stateMutability":"nonpayable","type":"function"}]` + genesisKey := "0xabd71b35d559563fea757f0f5edbde286fb8c043105b15abb7cd57189306d7d1" - g := new(core.Genesis) - b := `{"config":{"chainId":1,"homesteadBlock":0,"daoForkBlock":0,"daoForkSupport":true,"eip150Block":0,"eip150Hash":"0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0","eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x00","gasLimit":"0x5f5e100","difficulty":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"751a0b96e1042bee789452ecb20253fba40dbe85":{"balance":"0x1000000000000000", "mcbalance": {"0x0000000000000000000000000000000000000000000000000000000000000000": 1000000000000000000}}, "0100000000000000000000000000000000000000": {"code": "0x730000000000000000000000000000000000000000301460806040526004361061004b5760003560e01c80631e01043914610050578063abb24ba014610092578063b6510bb3146100a9575b600080fd5b61007c6004803603602081101561006657600080fd5b8101908080359060200190929190505050610118565b6040518082815260200191505060405180910390f35b81801561009e57600080fd5b506100a761013b565b005b8180156100b557600080fd5b50610116600480360360808110156100cc57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001909291908035906020019092919050505061013e565b005b60003073ffffffffffffffffffffffffffffffffffffffff1682905d9050919050565b5c565b8373ffffffffffffffffffffffffffffffffffffffff1681836108fc8690811502906040516000604051808303818888878c8af69550505050505015801561018a573d6000803e3d6000fd5b505050505056fea2646970667358221220ed2100d6623a884d196eceefabe5e03da4309a2562bb25262f3874f1acb31cd764736f6c634300060a0033", "balance": "0x0", "mcbalance": {}}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}` - k := "0xabd71b35d559563fea757f0f5edbde286fb8c043105b15abb7cd57189306d7d1" - err := json.Unmarshal([]byte(b), g) + bobKey, _ := coreth.NewKey(rand.Reader) + genesisBlock := new(core.Genesis) + err := json.Unmarshal([]byte(genesisJSON), genesisBlock) checkError(err) - config.Genesis = g - hk, _ := crypto.HexToECDSA(k[2:]) - genKey = coreth.NewKeyFromECDSA(hk) + hk, _ := crypto.HexToECDSA(genesisKey[2:]) + genKey := coreth.NewKeyFromECDSA(hk) + config.Genesis = genesisBlock // grab the control of block generation and disable auto uncle config.Miner.ManualMining = true config.Miner.ManualUncle = true @@ -88,17 +89,27 @@ func main() { chain := coreth.NewETHChain(&config, nil, nil, nil) newTxPoolHeadChan := make(chan core.NewTxPoolHeadEvent, 1) log.Info(chain.GetGenesisBlock().Hash().Hex()) + + mcAbi, err := abi.JSON(strings.NewReader(mcAbiJSON)) + checkError(err) + enableCode, err := mcAbi.Pack("enableMultiCoin") + checkError(err) + + abiStr, err := json.Marshal(contract.Info.AbiDefinition) + contractAbi, err := abi.JSON(strings.NewReader(string(abiStr))) + checkError(err) + var contractAddr common.Address - coin0 := common.HexToHash("0x0") postGen := func(block *types.Block) bool { if blockCount == 15 { + coin0 := common.HexToHash("0x0") state, err := chain.CurrentState() checkError(err) log.Info(fmt.Sprintf("genesis balance = %s", state.GetBalance(genKey.Address))) - log.Info(fmt.Sprintf("genesis balance2 = %s", state.GetBalanceMultiCoin(genKey.Address, coin0))) - log.Info(fmt.Sprintf("contract balance2 = %s", state.GetBalanceMultiCoin(contractAddr, coin0))) - log.Info(fmt.Sprintf("bob's balance = %s", state.GetBalance(bob.Address))) - log.Info(fmt.Sprintf("bob's balance2 = %s", state.GetBalanceMultiCoin(bob.Address, coin0))) + log.Info(fmt.Sprintf("genesis mcbalance(0) = %s", state.GetBalanceMultiCoin(genKey.Address, coin0))) + log.Info(fmt.Sprintf("bob's balance = %s", state.GetBalance(bobKey.Address))) + log.Info(fmt.Sprintf("bob's mcbalance(0) = %s", state.GetBalanceMultiCoin(bobKey.Address, coin0))) + log.Info(fmt.Sprintf("contract mcbalance(0) = %s", state.GetBalanceMultiCoin(contractAddr, coin0))) log.Info(fmt.Sprintf("state = %s", state.Dump(true, false, true))) return true } @@ -111,53 +122,54 @@ func main() { log.Info(fmt.Sprintf("contract addr = %s", contractAddr.String())) go func() { - tx := types.NewTransaction(nonce, bob.Address, big.NewInt(300000000000000000), uint64(gasLimit), gasPrice, nil) + // give Bob some initial balance + tx := types.NewTransaction(nonce, bobKey.Address, big.NewInt(300000000000000000), uint64(gasLimit), gasPrice, nil) signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) chain.AddRemoteTxs([]*types.Transaction{signedTx}) nonce++ time.Sleep(20 * time.Millisecond) - // self-loop tx to enable MC - tx = types.NewTransaction(0, bob.Address, big.NewInt(1), uint64(gasLimit), gasPrice, nil) - tx.SetMultiCoinValue(&coin0, big.NewInt(0)) - signedTx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), bob.PrivateKey) + // enable MC for Bob + tx = types.NewTransaction(0, vm.BuiltinAddr, big.NewInt(0), uint64(gasLimit), gasPrice, enableCode) + signedTx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), bobKey.PrivateKey) checkError(err) chain.AddRemoteTxs([]*types.Transaction{signedTx}) + time.Sleep(20 * time.Millisecond) - var code []byte - var a abi.ABI - abiStr, err := json.Marshal(contract.Info.AbiDefinition) - a, err = abi.JSON(strings.NewReader(string(abiStr))) + bobTransferInput, err := mcAbi.Pack("transfer", bobKey.Address, big.NewInt(0), big.NewInt(0), big.NewInt(100000000000000000)) checkError(err) - code, err = a.Pack("deposit") + contractTransferInput, err := mcAbi.Pack("transfer", contractAddr, big.NewInt(0), big.NewInt(0), big.NewInt(100000000000000000)) checkError(err) for i := 0; i < 5; i++ { - tx := types.NewTransaction(nonce, bob.Address, big.NewInt(0), uint64(gasLimit), gasPrice, nil) - tx.SetMultiCoinValue(&coin0, big.NewInt(100000000000000000)) + // transfer some coin0 balance to Bob + tx := types.NewTransaction(nonce, vm.BuiltinAddr, big.NewInt(0), uint64(gasLimit), gasPrice, bobTransferInput) signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) chain.AddRemoteTxs([]*types.Transaction{signedTx}) nonce++ - tx = types.NewTransaction(nonce, contractAddr, big.NewInt(0), uint64(gasLimit), gasPrice, code) - tx.SetMultiCoinValue(&coin0, big.NewInt(100000000000000000)) + // transfer some coin0 balance to the contract + tx = types.NewTransaction(nonce, vm.BuiltinAddr, big.NewInt(0), uint64(gasLimit), gasPrice, contractTransferInput) signedTx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) chain.AddRemoteTxs([]*types.Transaction{signedTx}) nonce++ } - code, err = a.Pack("withdraw", big.NewInt(0), big.NewInt(0), big.NewInt(10000000000000000)) - tx = types.NewTransaction(nonce, contractAddr, big.NewInt(0), uint64(gasLimit), gasPrice, code) + + // test contract methods + + input, err := contractAbi.Pack("withdraw", big.NewInt(0), big.NewInt(0), big.NewInt(10000000000000000)) + tx = types.NewTransaction(nonce, contractAddr, big.NewInt(0), uint64(gasLimit), gasPrice, input) signedTx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) chain.AddRemoteTxs([]*types.Transaction{signedTx}) nonce++ - code, err = a.Pack("updateBalance", big.NewInt(0)) - tx = types.NewTransaction(nonce, contractAddr, big.NewInt(0), uint64(gasLimit), gasPrice, code) + input, err = contractAbi.Pack("updateBalance", big.NewInt(0)) + tx = types.NewTransaction(nonce, contractAddr, big.NewInt(0), uint64(gasLimit), gasPrice, input) signedTx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) chain.AddRemoteTxs([]*types.Transaction{signedTx}) @@ -191,6 +203,7 @@ func main() { chain.GetTxPool().SubscribeNewHeadEvent(newTxPoolHeadChan) chain.Start() code := common.Hex2Bytes(contract.Code[2:]) + tx := types.NewContractCreation(nonce, big.NewInt(0), uint64(gasLimit), gasPrice, code) signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) -- cgit v1.2.3