From 09dd6b6fe94d7f1b4ac99dd410a8cb3543502403 Mon Sep 17 00:00:00 2001 From: AlexPan Date: Tue, 25 Dec 2018 15:54:27 +0800 Subject: [PATCH] move common/ crypto/ p2p/ dependency from Utility to ELA project issue #51 --- client.go | 30 ++---------- config.go | 9 ++-- interface/config.go | 78 +---------------------------- interface/interface.go | 2 +- interface/interface_test.go | 4 +- log.go | 6 +-- sdk/account.go | 2 +- sdk/protocal.go | 13 ----- sdk/service.go | 5 +- spvwallet.go | 84 ++------------------------------ wallet/client/account/account.go | 12 ++--- wallet/client/wallet.go | 7 +-- 12 files changed, 36 insertions(+), 216 deletions(-) delete mode 100644 sdk/protocal.go diff --git a/client.go b/client.go index da91ff2..24813f8 100644 --- a/client.go +++ b/client.go @@ -5,36 +5,12 @@ import ( "github.com/elastos/Elastos.ELA.SPV/wallet" - "github.com/elastos/Elastos.ELA/common" - "github.com/elastos/Elastos.ELA/core/contract/program" - "github.com/elastos/Elastos.ELA/core/types" - "github.com/elastos/Elastos.ELA/core/types/payload" + "github.com/elastos/Elastos.ELA/common/config" ) var Version string func main() { - url := fmt.Sprint("http://127.0.0.1:", config.JsonRpcPort, "/spvwallet") - wallet.RunClient(Version, dataDir, url, getSystemAssetId()) -} - -func getSystemAssetId() common.Uint256 { - systemToken := &types.Transaction{ - TxType: types.RegisterAsset, - PayloadVersion: 0, - Payload: &payload.PayloadRegisterAsset{ - Asset: payload.Asset{ - Name: "ELA", - Precision: 0x08, - AssetType: 0x00, - }, - Amount: 0 * 100000000, - Controller: common.Uint168{}, - }, - Attributes: []*types.Attribute{}, - Inputs: []*types.Input{}, - Outputs: []*types.Output{}, - Programs: []*program.Program{}, - } - return systemToken.Hash() + url := fmt.Sprint("http://127.0.0.1:", cfg.JsonRpcPort, "/spvwallet") + wallet.RunClient(Version, dataDir, url, config.ELAAssetID) } diff --git a/config.go b/config.go index c2ca233..fbb8cb7 100644 --- a/config.go +++ b/config.go @@ -9,6 +9,8 @@ import ( "os" "github.com/elastos/Elastos.ELA/common" + "github.com/elastos/Elastos.ELA/common/config" + "github.com/elastos/Elastos.ELA/core/types" ) const ( @@ -17,7 +19,7 @@ const ( ConfigFilename = "./config.json" ) -var config = loadConfig() +var cfg = loadConfig() type Config struct { Magic uint32 @@ -29,7 +31,7 @@ type Config struct { MaxPerLogSize int64 JsonRpcPort uint16 - foundation *common.Uint168 + genesisBlock *types.Block } func loadConfig() *Config { @@ -54,11 +56,12 @@ func loadConfig() *Config { c.Foundation = "8VYXVxKKSAxkmRrfmGpQR2Kc66XhG6m3ta" } - c.foundation, err = common.Uint168FromAddress(c.Foundation) + foundation, err := common.Uint168FromAddress(c.Foundation) if err != nil { fmt.Printf("Parse foundation address error %s", err) os.Exit(-1) } + c.genesisBlock = config.GenesisBlock(foundation) return &c } diff --git a/interface/config.go b/interface/config.go index 76969bf..f48bdf5 100644 --- a/interface/config.go +++ b/interface/config.go @@ -1,16 +1,12 @@ package _interface import ( - "time" - "github.com/elastos/Elastos.ELA.SPV/interface/iutil" "github.com/elastos/Elastos.ELA.SPV/util" "github.com/elastos/Elastos.ELA/common" - "github.com/elastos/Elastos.ELA/core/contract/program" + "github.com/elastos/Elastos.ELA/common/config" "github.com/elastos/Elastos.ELA/core/types" - "github.com/elastos/Elastos.ELA/core/types/payload" - "github.com/elastos/Elastos.ELA/crypto" ) func newBlockHeader() util.BlockHeader { @@ -24,75 +20,5 @@ func newTransaction() util.Transaction { // GenesisHeader creates a specific genesis header by the given // foundation address. func GenesisHeader(foundation *common.Uint168) util.BlockHeader { - // Genesis time - genesisTime := time.Date(2017, time.December, 22, 10, 0, 0, 0, time.UTC) - - // header - header := types.Header{ - Version: 0, - Previous: common.EmptyHash, - MerkleRoot: common.EmptyHash, - Timestamp: uint32(genesisTime.Unix()), - Bits: 0x1d03ffff, - Nonce: types.GenesisNonce, - Height: uint32(0), - } - - // ELA coin - elaCoin := &types.Transaction{ - TxType: types.RegisterAsset, - PayloadVersion: 0, - Payload: &payload.PayloadRegisterAsset{ - Asset: payload.Asset{ - Name: "ELA", - Precision: 0x08, - AssetType: 0x00, - }, - Amount: 0 * 100000000, - Controller: common.Uint168{}, - }, - Attributes: []*types.Attribute{}, - Inputs: []*types.Input{}, - Outputs: []*types.Output{}, - Programs: []*program.Program{}, - } - - coinBase := &types.Transaction{ - TxType: types.CoinBase, - PayloadVersion: payload.PayloadCoinBaseVersion, - Payload: new(payload.PayloadCoinBase), - Inputs: []*types.Input{ - { - Previous: types.OutPoint{ - TxID: common.EmptyHash, - Index: 0x0000, - }, - Sequence: 0x00000000, - }, - }, - Attributes: []*types.Attribute{}, - LockTime: 0, - Programs: []*program.Program{}, - } - - coinBase.Outputs = []*types.Output{ - { - AssetID: elaCoin.Hash(), - Value: 3300 * 10000 * 100000000, - ProgramHash: *foundation, - }, - } - - nonce := []byte{0x4d, 0x65, 0x82, 0x21, 0x07, 0xfc, 0xfd, 0x52} - txAttr := types.NewAttribute(types.Nonce, nonce) - coinBase.Attributes = append(coinBase.Attributes, &txAttr) - - transactions := []*types.Transaction{coinBase, elaCoin} - hashes := make([]common.Uint256, 0, len(transactions)) - for _, tx := range transactions { - hashes = append(hashes, tx.Hash()) - } - header.MerkleRoot, _ = crypto.ComputeRoot(hashes) - - return iutil.NewHeader(&header) + return iutil.NewHeader(&config.GenesisBlock(foundation).Header) } diff --git a/interface/interface.go b/interface/interface.go index a4e7797..f1e40d5 100644 --- a/interface/interface.go +++ b/interface/interface.go @@ -97,7 +97,7 @@ type TransactionListener interface { Address() string // Type() indicates which transaction type this listener are interested - Type() types.TransactionType + Type() types.TxType // Flags control the notification actions by the given flag Flags() uint64 diff --git a/interface/interface_test.go b/interface/interface_test.go index 810a3a5..1fd341f 100644 --- a/interface/interface_test.go +++ b/interface/interface_test.go @@ -28,7 +28,7 @@ type TxListener struct { log elalog.Logger service SPVService address string - txType types.TransactionType + txType types.TxType flags uint64 } @@ -36,7 +36,7 @@ func (l *TxListener) Address() string { return l.address } -func (l *TxListener) Type() types.TransactionType { +func (l *TxListener) Type() types.TxType { return l.txType } diff --git a/log.go b/log.go index d4dbddf..db09a4b 100644 --- a/log.go +++ b/log.go @@ -25,10 +25,10 @@ import ( var ( fileWriter = elalog.NewFileWriter( filepath.Join(dataDir, "logs"), - config.MaxPerLogSize, - config.MaxLogsSize, + cfg.MaxPerLogSize, + cfg.MaxLogsSize, ) - level = elalog.Level(config.PrintLevel) + level = elalog.Level(cfg.PrintLevel) backend = elalog.NewBackend(io.MultiWriter(os.Stdout, fileWriter), elalog.Llongfile) diff --git a/sdk/account.go b/sdk/account.go index f4a2d47..ac3557a 100644 --- a/sdk/account.go +++ b/sdk/account.go @@ -91,4 +91,4 @@ func createCheckSigRedeemScript(publicKey *crypto.PublicKey) ([]byte, error) { buf.WriteByte(byte(common.STANDARD)) return buf.Bytes(), nil -} \ No newline at end of file +} diff --git a/sdk/protocal.go b/sdk/protocal.go deleted file mode 100644 index 070590c..0000000 --- a/sdk/protocal.go +++ /dev/null @@ -1,13 +0,0 @@ -package sdk - -import ( - "github.com/elastos/Elastos.ELA/p2p" -) - -const ( - // The protocol version implemented SPV protocol - ProtocolVersion = p2p.EIP001Version - - // OpenService is a flag used to indicate a peer provides open service. - OpenService = 1 << 2 -) diff --git a/sdk/service.go b/sdk/service.go index 1436e07..655a804 100644 --- a/sdk/service.go +++ b/sdk/service.go @@ -12,6 +12,7 @@ import ( "github.com/elastos/Elastos.ELA.SPV/util" "github.com/elastos/Elastos.ELA/common" + "github.com/elastos/Elastos.ELA/elanet/pact" "github.com/elastos/Elastos.ELA/p2p" "github.com/elastos/Elastos.ELA/p2p/msg" "github.com/elastos/Elastos.ELA/p2p/peer" @@ -104,8 +105,8 @@ func newService(cfg *Config) (*service, error) { serverCfg := server.NewDefaultConfig( cfg.Magic, - p2p.EIP001Version, - OpenService, + pact.EBIP001Version, + 0, cfg.DefaultPort, cfg.SeedList, nil, diff --git a/spvwallet.go b/spvwallet.go index e0db186..5443b24 100644 --- a/spvwallet.go +++ b/spvwallet.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/hex" "fmt" - "time" "github.com/elastos/Elastos.ELA.SPV/bloom" "github.com/elastos/Elastos.ELA.SPV/database" @@ -17,10 +16,7 @@ import ( "github.com/elastos/Elastos.ELA.Utility/http/jsonrpc" httputil "github.com/elastos/Elastos.ELA.Utility/http/util" "github.com/elastos/Elastos.ELA/common" - "github.com/elastos/Elastos.ELA/core/contract/program" "github.com/elastos/Elastos.ELA/core/types" - "github.com/elastos/Elastos.ELA/core/types/payload" - "github.com/elastos/Elastos.ELA/crypto" ) const ( @@ -339,9 +335,9 @@ func NewWallet(dataDir string) (*spvwallet, error) { // Initialize spv service w.IService, err = sdk.NewService( &sdk.Config{ - Magic: config.Magic, - SeedList: config.SeedList, - DefaultPort: config.DefaultPort, + Magic: cfg.Magic, + SeedList: cfg.SeedList, + DefaultPort: cfg.DefaultPort, MaxPeers: MaxPeers, GenesisHeader: GenesisHeader(), ChainStore: chainStore, @@ -356,7 +352,7 @@ func NewWallet(dataDir string) (*spvwallet, error) { s := jsonrpc.NewServer(&jsonrpc.Config{ Path: "/spvwallet", - ServePort: config.JsonRpcPort, + ServePort: cfg.JsonRpcPort, }) s.RegisterAction("notifynewaddress", w.notifyNewAddress, "addr") s.RegisterAction("sendrawtransaction", w.sendTransaction, "data") @@ -372,75 +368,5 @@ func newTransaction() util.Transaction { // GenesisHeader creates a specific genesis header by the given // foundation address. func GenesisHeader() util.BlockHeader { - // Genesis time - genesisTime := time.Date(2017, time.December, 22, 10, 0, 0, 0, time.UTC) - - // header - header := types.Header{ - Version: 0, - Previous: common.EmptyHash, - MerkleRoot: common.EmptyHash, - Timestamp: uint32(genesisTime.Unix()), - Bits: 0x1d03ffff, - Nonce: types.GenesisNonce, - Height: uint32(0), - } - - // ELA coin - elaCoin := &types.Transaction{ - TxType: types.RegisterAsset, - PayloadVersion: 0, - Payload: &payload.PayloadRegisterAsset{ - Asset: payload.Asset{ - Name: "ELA", - Precision: 0x08, - AssetType: 0x00, - }, - Amount: 0 * 100000000, - Controller: common.Uint168{}, - }, - Attributes: []*types.Attribute{}, - Inputs: []*types.Input{}, - Outputs: []*types.Output{}, - Programs: []*program.Program{}, - } - - coinBase := &types.Transaction{ - TxType: types.CoinBase, - PayloadVersion: payload.PayloadCoinBaseVersion, - Payload: new(payload.PayloadCoinBase), - Inputs: []*types.Input{ - { - Previous: types.OutPoint{ - TxID: common.EmptyHash, - Index: 0x0000, - }, - Sequence: 0x00000000, - }, - }, - Attributes: []*types.Attribute{}, - LockTime: 0, - Programs: []*program.Program{}, - } - - coinBase.Outputs = []*types.Output{ - { - AssetID: elaCoin.Hash(), - Value: 3300 * 10000 * 100000000, - ProgramHash: *config.foundation, - }, - } - - nonce := []byte{0x4d, 0x65, 0x82, 0x21, 0x07, 0xfc, 0xfd, 0x52} - txAttr := types.NewAttribute(types.Nonce, nonce) - coinBase.Attributes = append(coinBase.Attributes, &txAttr) - - transactions := []*types.Transaction{coinBase, elaCoin} - hashes := make([]common.Uint256, 0, len(transactions)) - for _, tx := range transactions { - hashes = append(hashes, tx.Hash()) - } - header.MerkleRoot, _ = crypto.ComputeRoot(hashes) - - return sutil.NewHeader(&header) + return sutil.NewHeader(&cfg.genesisBlock.Header) } diff --git a/wallet/client/account/account.go b/wallet/client/account/account.go index a943157..2427e19 100644 --- a/wallet/client/account/account.go +++ b/wallet/client/account/account.go @@ -58,16 +58,16 @@ func addMultiSignAccount(context *cli.Context, wallet *client.Wallet, content st return errors.New(fmt.Sprint("multi sign account require at lest ", MinMultiSignKeys, " public keys")) } - // Get M value - M := context.Int("m") - if M == 0 { // Use default M greater than half - M = len(publicKeys)/2 + 1 + // Get m value + m := context.Int("m") + if m == 0 { // Use default m greater than half + m = len(publicKeys)/2 + 1 } - if M < len(publicKeys)/2+1 || M > len(publicKeys) { + if m < len(publicKeys)/2+1 || m > len(publicKeys) { return errors.New("M must be greater than half number of public keys, less than number of public keys") } - programHash, err := wallet.AddMultiSignAccount(uint(M), publicKeys...) + programHash, err := wallet.AddMultiSignAccount(m, publicKeys...) if err != nil { return err } diff --git a/wallet/client/wallet.go b/wallet/client/wallet.go index 5a67ee3..2d87b2e 100644 --- a/wallet/client/wallet.go +++ b/wallet/client/wallet.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/hex" "errors" + "github.com/elastos/Elastos.ELA/core/contract" "math" "math/rand" "strconv" @@ -96,8 +97,8 @@ func (wallet *Wallet) NewSubAccount(password []byte) (*common.Uint168, error) { return account.ProgramHash(), nil } -func (wallet *Wallet) AddMultiSignAccount(M uint, publicKeys ...*crypto.PublicKey) (*common.Uint168, error) { - redeemScript, err := crypto.CreateMultiSignRedeemScript(M, publicKeys) +func (wallet *Wallet) AddMultiSignAccount(m int, publicKeys ...*crypto.PublicKey) (*common.Uint168, error) { + redeemScript, err := contract.CreateMultiSigRedeemScript(m, publicKeys) if err != nil { return nil, errors.New("[Wallet], CreateStandardRedeemScript failed") } @@ -343,7 +344,7 @@ func InputFromUTXO(utxo *sutil.UTXO) *types.Input { func (wallet *Wallet) newTransaction(redeemScript []byte, inputs []*types.Input, outputs []*types.Output) *types.Transaction { // Create payload - txPayload := &payload.PayloadTransferAsset{} + txPayload := &payload.TransferAsset{} // Create attributes txAttr := types.NewAttribute(types.Nonce, []byte(strconv.FormatInt(rand.Int63(), 10))) attributes := make([]*types.Attribute, 0)