Skip to content

Commit

Permalink
move common/ crypto/ p2p/ dependency from Utility to ELA project issue
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpan0610 authored and bocheng0000 committed Feb 25, 2019
1 parent 01280b3 commit 09dd6b6
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 216 deletions.
30 changes: 3 additions & 27 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 6 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -17,7 +19,7 @@ const (
ConfigFilename = "./config.json"
)

var config = loadConfig()
var cfg = loadConfig()

type Config struct {
Magic uint32
Expand All @@ -29,7 +31,7 @@ type Config struct {
MaxPerLogSize int64
JsonRpcPort uint16

foundation *common.Uint168
genesisBlock *types.Block
}

func loadConfig() *Config {
Expand All @@ -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
}
Expand Down
78 changes: 2 additions & 76 deletions interface/config.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions interface/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ type TxListener struct {
log elalog.Logger
service SPVService
address string
txType types.TransactionType
txType types.TxType
flags uint64
}

func (l *TxListener) Address() string {
return l.address
}

func (l *TxListener) Type() types.TransactionType {
func (l *TxListener) Type() types.TxType {
return l.txType
}

Expand Down
6 changes: 3 additions & 3 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion sdk/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ func createCheckSigRedeemScript(publicKey *crypto.PublicKey) ([]byte, error) {
buf.WriteByte(byte(common.STANDARD))

return buf.Bytes(), nil
}
}
13 changes: 0 additions & 13 deletions sdk/protocal.go

This file was deleted.

5 changes: 3 additions & 2 deletions sdk/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
84 changes: 5 additions & 79 deletions spvwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/hex"
"fmt"
"time"

"github.com/elastos/Elastos.ELA.SPV/bloom"
"github.com/elastos/Elastos.ELA.SPV/database"
Expand All @@ -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 (
Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand All @@ -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)
}
12 changes: 6 additions & 6 deletions wallet/client/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 09dd6b6

Please sign in to comment.