From 9768d79c987d19da103972ebbd6eff50a408cb82 Mon Sep 17 00:00:00 2001 From: Robin Lai Date: Mon, 9 Dec 2019 14:22:45 -0800 Subject: [PATCH 1/7] refactor ledger grpc client --- ledger/ledger.go | 52 +++++++++++------ ledger/sample/demo.go | 132 ++++++++++++++++++++---------------------- 2 files changed, 98 insertions(+), 86 deletions(-) diff --git a/ledger/ledger.go b/ledger/ledger.go index 4decf8c..542d8d7 100644 --- a/ledger/ledger.go +++ b/ledger/ledger.go @@ -2,18 +2,14 @@ package ledger import ( "context" + "github.com/tron-us/go-btfs-common/utils/grpc" "time" ic "github.com/libp2p/go-libp2p-core/crypto" ledgerpb "github.com/tron-us/go-btfs-common/protos/ledger" "github.com/tron-us/protobuf/proto" - "google.golang.org/grpc" ) -func NewClient(conn *grpc.ClientConn) ledgerpb.ChannelsClient { - return ledgerpb.NewChannelsClient(conn) -} - func NewAccount(pubKey ic.PubKey, amount int64) (*ledgerpb.Account, error) { addr, err := pubKey.Raw() if err != nil { @@ -59,19 +55,23 @@ func NewSignedChannelState(channelState *ledgerpb.ChannelState, fromSig []byte, } } -func ImportAccount(ctx context.Context, pubKey ic.PubKey, ledgerClient ledgerpb.ChannelsClient) (*ledgerpb.Account, error) { +func ImportAccount(ctx context.Context, pubKey ic.PubKey, addr string) (*ledgerpb.Account, error) { keyBytes, err := pubKey.Raw() if err != nil { return nil, err } - res, err := ledgerClient.CreateAccount(ctx, &ledgerpb.PublicKey{Key: keyBytes}) + var res *ledgerpb.CreateAccountResult + err = grpc.LedgerClient(addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error { + res, err = client.CreateAccount(ctx, &ledgerpb.PublicKey{Key: keyBytes}) + return err + }) if err != nil { return nil, err } return res.GetAccount(), nil } -func ImportSignedAccount(ctx context.Context, privKey ic.PrivKey, pubKey ic.PubKey, ledgerClient ledgerpb.ChannelsClient) (*ledgerpb.SignedCreateAccountResult, error) { +func ImportSignedAccount(ctx context.Context, privKey ic.PrivKey, pubKey ic.PubKey, addr string) (*ledgerpb.SignedCreateAccountResult, error) { pubKeyBytes, err := pubKey.Raw() if err != nil { return nil, err @@ -83,20 +83,36 @@ func ImportSignedAccount(ctx context.Context, privKey ic.PrivKey, pubKey ic.PubK return nil, err } signedPubkey := &ledgerpb.SignedPublicKey{Key: singedPubKey, Signature: signature} - return ledgerClient.SignedCreateAccount(ctx, signedPubkey) + + var result *ledgerpb.SignedCreateAccountResult + err = grpc.LedgerClient(addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error { + result, err = client.SignedCreateAccount(ctx, signedPubkey) + return err + }) + if err != nil { + return nil, err + } + return result, nil } -func CreateChannel(ctx context.Context, ledgerClient ledgerpb.ChannelsClient, channelCommit *ledgerpb.ChannelCommit, sig []byte) (*ledgerpb.ChannelID, error) { - return ledgerClient.CreateChannel(ctx, &ledgerpb.SignedChannelCommit{ - Channel: channelCommit, - Signature: sig, +func CreateChannel(ctx context.Context, addr string, channelCommit *ledgerpb.ChannelCommit, sig []byte) (*ledgerpb.ChannelID, error) { + var ( + channelId *ledgerpb.ChannelID + err error + ) + err = grpc.LedgerClient(addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error { + channelId, err = client.CreateChannel(ctx, &ledgerpb.SignedChannelCommit{ + Channel: channelCommit, + Signature: sig, + }) + return err }) + return channelId, err } -func CloseChannel(ctx context.Context, ledgerClient ledgerpb.ChannelsClient, signedChannelState *ledgerpb.SignedChannelState) error { - _, err := ledgerClient.CloseChannel(ctx, signedChannelState) - if err != nil { +func CloseChannel(ctx context.Context, addr string, signedChannelState *ledgerpb.SignedChannelState) error { + return grpc.LedgerClient(addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error { + _, err := client.CloseChannel(ctx, signedChannelState) return err - } - return nil + }) } diff --git a/ledger/sample/demo.go b/ledger/sample/demo.go index 4b6691c..6fa0f8e 100644 --- a/ledger/sample/demo.go +++ b/ledger/sample/demo.go @@ -2,11 +2,8 @@ package main import ( "context" - "github.com/tron-us/go-btfs-common/crypto" "github.com/tron-us/go-btfs-common/ledger" - ledgerpb "github.com/tron-us/go-btfs-common/protos/ledger" - "github.com/tron-us/go-btfs-common/utils/grpc" "github.com/tron-us/go-common/v2/log" "go.uber.org/zap" @@ -18,71 +15,70 @@ const ( ) func main() { - err := grpc.LedgerClient("https://ledger-dev.bt.co:443").WithContext(context.Background(), - func(ctx context.Context, ledgerClient ledgerpb.ChannelsClient) error { - // create payer Account - payerPrivKey, err := crypto.ToPrivKey(PayerPrivKeyString) - if err != nil { - log.Panic("can not convert to private key", zap.Error(err)) - } - payerPubKey := payerPrivKey.GetPublic() - _, err = ledger.ImportSignedAccount(ctx, payerPrivKey, payerPubKey, ledgerClient) - if err != nil { - log.Panic("can not create account on ledger", zap.Error(err)) - } - // create receiver account - recvPrivKey, err := crypto.ToPrivKey(ReceiverPrivKeyString) - if err != nil { - log.Panic("can not convert to private key", zap.Error(err)) - } - recvPubKey := recvPrivKey.GetPublic() - _, err = ledger.ImportSignedAccount(ctx, recvPrivKey, recvPubKey, ledgerClient) - if err != nil { - log.Panic("can not create account on ledger", zap.Error(err)) - } - // prepare channel commit - amount := int64(1) - channelCommit, err := ledger.NewChannelCommit(payerPubKey, recvPubKey, amount) - if err != nil { - log.Panic("can not create channel commit", zap.Error(err)) - } - // sign for the channel commit - fromSig, err := crypto.Sign(payerPrivKey, channelCommit) - if err != nil { - log.Panic("fail to sign channel commit", zap.Error(err)) - } - // create channel: payer start the channel - channelID, err := ledger.CreateChannel(ctx, ledgerClient, channelCommit, fromSig) - if err != nil { - log.Panic("fail to create channel", zap.Error(err)) - } - // channel state: transfer money from -> to - fromAcc, err := ledger.NewAccount(payerPubKey, 0) - if err != nil { - log.Panic("wrong account on channel", zap.Error(err)) - } - toAcc, err := ledger.NewAccount(recvPubKey, amount) - if err != nil { - log.Panic("wrong account on channel", zap.Error(err)) - } - channelState := ledger.NewChannelState(channelID, 1, fromAcc, toAcc) - // need permission from both account, get signature from both - fromSigState, err := crypto.Sign(payerPrivKey, channelState) - if err != nil { - log.Panic("error when signing the channel state", zap.Error(err)) - } - toSigState, err := crypto.Sign(recvPrivKey, channelState) - if err != nil { - log.Panic("error when signing the channel state", zap.Error(err)) - } - signedChannelState := ledger.NewSignedChannelState(channelState, fromSigState, toSigState) - // close channel - err = ledger.CloseChannel(ctx, ledgerClient, signedChannelState) - if err != nil { - log.Panic("fail to close channel", zap.Error(err)) - } - return nil - }) + ctx := context.Background() + addr := "https://ledger-dev.bt.co:443" + + // create payer Account + payerPrivKey, err := crypto.ToPrivKey(PayerPrivKeyString) + if err != nil { + log.Panic("can not convert to private key", zap.Error(err)) + } + payerPubKey := payerPrivKey.GetPublic() + _, err = ledger.ImportSignedAccount(ctx, payerPrivKey, payerPubKey, addr) + if err != nil { + log.Panic("can not create account on ledger", zap.Error(err)) + } + // create receiver account + recvPrivKey, err := crypto.ToPrivKey(ReceiverPrivKeyString) + if err != nil { + log.Panic("can not convert to private key", zap.Error(err)) + } + recvPubKey := recvPrivKey.GetPublic() + _, err = ledger.ImportSignedAccount(ctx, recvPrivKey, recvPubKey, addr) + if err != nil { + log.Panic("can not create account on ledger", zap.Error(err)) + } + // prepare channel commit + amount := int64(1) + channelCommit, err := ledger.NewChannelCommit(payerPubKey, recvPubKey, amount) + if err != nil { + log.Panic("can not create channel commit", zap.Error(err)) + } + // sign for the channel commit + fromSig, err := crypto.Sign(payerPrivKey, channelCommit) + if err != nil { + log.Panic("fail to sign channel commit", zap.Error(err)) + } + // create channel: payer start the channel + channelID, err := ledger.CreateChannel(ctx, addr, channelCommit, fromSig) + if err != nil { + log.Panic("fail to create channel", zap.Error(err)) + } + // channel state: transfer money from -> to + fromAcc, err := ledger.NewAccount(payerPubKey, 0) + if err != nil { + log.Panic("wrong account on channel", zap.Error(err)) + } + toAcc, err := ledger.NewAccount(recvPubKey, amount) + if err != nil { + log.Panic("wrong account on channel", zap.Error(err)) + } + channelState := ledger.NewChannelState(channelID, 1, fromAcc, toAcc) + // need permission from both account, get signature from both + fromSigState, err := crypto.Sign(payerPrivKey, channelState) + if err != nil { + log.Panic("error when signing the channel state", zap.Error(err)) + } + toSigState, err := crypto.Sign(recvPrivKey, channelState) + if err != nil { + log.Panic("error when signing the channel state", zap.Error(err)) + } + signedChannelState := ledger.NewSignedChannelState(channelState, fromSigState, toSigState) + // close channel + err = ledger.CloseChannel(ctx, addr, signedChannelState) + if err != nil { + log.Panic("fail to close channel", zap.Error(err)) + } if err != nil { log.Panic(err.Error()) } From 306a5862ca5b6ede5e5e3fd4ce2e3b5a0c10c895 Mon Sep 17 00:00:00 2001 From: Robin Lai Date: Mon, 9 Dec 2019 14:24:39 -0800 Subject: [PATCH 2/7] sort imports --- ledger/ledger.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ledger/ledger.go b/ledger/ledger.go index 542d8d7..8085095 100644 --- a/ledger/ledger.go +++ b/ledger/ledger.go @@ -2,12 +2,13 @@ package ledger import ( "context" - "github.com/tron-us/go-btfs-common/utils/grpc" "time" - ic "github.com/libp2p/go-libp2p-core/crypto" ledgerpb "github.com/tron-us/go-btfs-common/protos/ledger" + "github.com/tron-us/go-btfs-common/utils/grpc" "github.com/tron-us/protobuf/proto" + + ic "github.com/libp2p/go-libp2p-core/crypto" ) func NewAccount(pubKey ic.PubKey, amount int64) (*ledgerpb.Account, error) { From 835ef8adfe9320d7e3ec8e6d240c35ab1b6c3329 Mon Sep 17 00:00:00 2001 From: Robin Lai Date: Mon, 9 Dec 2019 14:34:55 -0800 Subject: [PATCH 3/7] handle unchecked err --- ledger/ledger.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ledger/ledger.go b/ledger/ledger.go index 8085095..0c20435 100644 --- a/ledger/ledger.go +++ b/ledger/ledger.go @@ -79,6 +79,9 @@ func ImportSignedAccount(ctx context.Context, privKey ic.PrivKey, pubKey ic.PubK } singedPubKey := &ledgerpb.PublicKey{Key: pubKeyBytes} sigBytes, err := proto.Marshal(singedPubKey) + if err != nil { + return nil, err + } signature, err := privKey.Sign(sigBytes) if err != nil { return nil, err From 41705de79f2534f4e041480e93a3097e3a36ffa0 Mon Sep 17 00:00:00 2001 From: Robin Lai Date: Mon, 9 Dec 2019 15:11:01 -0800 Subject: [PATCH 4/7] refactor --- ledger/ledger.go | 32 ++++++++++++++++++++------------ ledger/sample/demo.go | 20 ++++++++++---------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/ledger/ledger.go b/ledger/ledger.go index 0c20435..5b33eb3 100644 --- a/ledger/ledger.go +++ b/ledger/ledger.go @@ -11,7 +11,15 @@ import ( ic "github.com/libp2p/go-libp2p-core/crypto" ) -func NewAccount(pubKey ic.PubKey, amount int64) (*ledgerpb.Account, error) { +type Client struct { + addr string +} + +func NewClient(addr string) *Client { + return &Client{addr: addr} +} + +func (c *Client) NewAccount(pubKey ic.PubKey, amount int64) (*ledgerpb.Account, error) { addr, err := pubKey.Raw() if err != nil { return nil, err @@ -22,7 +30,7 @@ func NewAccount(pubKey ic.PubKey, amount int64) (*ledgerpb.Account, error) { }, nil } -func NewChannelCommit(fromKey ic.PubKey, toKey ic.PubKey, amount int64) (*ledgerpb.ChannelCommit, error) { +func (c *Client) NewChannelCommit(fromKey ic.PubKey, toKey ic.PubKey, amount int64) (*ledgerpb.ChannelCommit, error) { fromAddr, err := fromKey.Raw() if err != nil { return nil, err @@ -39,7 +47,7 @@ func NewChannelCommit(fromKey ic.PubKey, toKey ic.PubKey, amount int64) (*ledger }, err } -func NewChannelState(id *ledgerpb.ChannelID, sequence int64, fromAccount *ledgerpb.Account, toAccount *ledgerpb.Account) *ledgerpb.ChannelState { +func (c *Client) NewChannelState(id *ledgerpb.ChannelID, sequence int64, fromAccount *ledgerpb.Account, toAccount *ledgerpb.Account) *ledgerpb.ChannelState { return &ledgerpb.ChannelState{ Id: id, Sequence: sequence, @@ -48,7 +56,7 @@ func NewChannelState(id *ledgerpb.ChannelID, sequence int64, fromAccount *ledger } } -func NewSignedChannelState(channelState *ledgerpb.ChannelState, fromSig []byte, toSig []byte) *ledgerpb.SignedChannelState { +func (c *Client) NewSignedChannelState(channelState *ledgerpb.ChannelState, fromSig []byte, toSig []byte) *ledgerpb.SignedChannelState { return &ledgerpb.SignedChannelState{ Channel: channelState, FromSignature: fromSig, @@ -56,13 +64,13 @@ func NewSignedChannelState(channelState *ledgerpb.ChannelState, fromSig []byte, } } -func ImportAccount(ctx context.Context, pubKey ic.PubKey, addr string) (*ledgerpb.Account, error) { +func (c *Client) ImportAccount(ctx context.Context, pubKey ic.PubKey) (*ledgerpb.Account, error) { keyBytes, err := pubKey.Raw() if err != nil { return nil, err } var res *ledgerpb.CreateAccountResult - err = grpc.LedgerClient(addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error { + err = grpc.LedgerClient(c.addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error { res, err = client.CreateAccount(ctx, &ledgerpb.PublicKey{Key: keyBytes}) return err }) @@ -72,7 +80,7 @@ func ImportAccount(ctx context.Context, pubKey ic.PubKey, addr string) (*ledgerp return res.GetAccount(), nil } -func ImportSignedAccount(ctx context.Context, privKey ic.PrivKey, pubKey ic.PubKey, addr string) (*ledgerpb.SignedCreateAccountResult, error) { +func (c *Client) ImportSignedAccount(ctx context.Context, privKey ic.PrivKey, pubKey ic.PubKey) (*ledgerpb.SignedCreateAccountResult, error) { pubKeyBytes, err := pubKey.Raw() if err != nil { return nil, err @@ -89,7 +97,7 @@ func ImportSignedAccount(ctx context.Context, privKey ic.PrivKey, pubKey ic.PubK signedPubkey := &ledgerpb.SignedPublicKey{Key: singedPubKey, Signature: signature} var result *ledgerpb.SignedCreateAccountResult - err = grpc.LedgerClient(addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error { + err = grpc.LedgerClient(c.addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error { result, err = client.SignedCreateAccount(ctx, signedPubkey) return err }) @@ -99,12 +107,12 @@ func ImportSignedAccount(ctx context.Context, privKey ic.PrivKey, pubKey ic.PubK return result, nil } -func CreateChannel(ctx context.Context, addr string, channelCommit *ledgerpb.ChannelCommit, sig []byte) (*ledgerpb.ChannelID, error) { +func (c *Client) CreateChannel(ctx context.Context, channelCommit *ledgerpb.ChannelCommit, sig []byte) (*ledgerpb.ChannelID, error) { var ( channelId *ledgerpb.ChannelID err error ) - err = grpc.LedgerClient(addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error { + err = grpc.LedgerClient(c.addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error { channelId, err = client.CreateChannel(ctx, &ledgerpb.SignedChannelCommit{ Channel: channelCommit, Signature: sig, @@ -114,8 +122,8 @@ func CreateChannel(ctx context.Context, addr string, channelCommit *ledgerpb.Cha return channelId, err } -func CloseChannel(ctx context.Context, addr string, signedChannelState *ledgerpb.SignedChannelState) error { - return grpc.LedgerClient(addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error { +func (c *Client) CloseChannel(ctx context.Context, signedChannelState *ledgerpb.SignedChannelState) error { + return grpc.LedgerClient(c.addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error { _, err := client.CloseChannel(ctx, signedChannelState) return err }) diff --git a/ledger/sample/demo.go b/ledger/sample/demo.go index 6fa0f8e..d67cbc2 100644 --- a/ledger/sample/demo.go +++ b/ledger/sample/demo.go @@ -16,7 +16,7 @@ const ( func main() { ctx := context.Background() - addr := "https://ledger-dev.bt.co:443" + c := ledger.NewClient("https://ledger-dev.bt.co:443") // create payer Account payerPrivKey, err := crypto.ToPrivKey(PayerPrivKeyString) @@ -24,7 +24,7 @@ func main() { log.Panic("can not convert to private key", zap.Error(err)) } payerPubKey := payerPrivKey.GetPublic() - _, err = ledger.ImportSignedAccount(ctx, payerPrivKey, payerPubKey, addr) + _, err = c.ImportSignedAccount(ctx, payerPrivKey, payerPubKey) if err != nil { log.Panic("can not create account on ledger", zap.Error(err)) } @@ -34,13 +34,13 @@ func main() { log.Panic("can not convert to private key", zap.Error(err)) } recvPubKey := recvPrivKey.GetPublic() - _, err = ledger.ImportSignedAccount(ctx, recvPrivKey, recvPubKey, addr) + _, err = c.ImportSignedAccount(ctx, recvPrivKey, recvPubKey) if err != nil { log.Panic("can not create account on ledger", zap.Error(err)) } // prepare channel commit amount := int64(1) - channelCommit, err := ledger.NewChannelCommit(payerPubKey, recvPubKey, amount) + channelCommit, err := c.NewChannelCommit(payerPubKey, recvPubKey, amount) if err != nil { log.Panic("can not create channel commit", zap.Error(err)) } @@ -50,20 +50,20 @@ func main() { log.Panic("fail to sign channel commit", zap.Error(err)) } // create channel: payer start the channel - channelID, err := ledger.CreateChannel(ctx, addr, channelCommit, fromSig) + channelID, err := c.CreateChannel(ctx, channelCommit, fromSig) if err != nil { log.Panic("fail to create channel", zap.Error(err)) } // channel state: transfer money from -> to - fromAcc, err := ledger.NewAccount(payerPubKey, 0) + fromAcc, err := c.NewAccount(payerPubKey, 0) if err != nil { log.Panic("wrong account on channel", zap.Error(err)) } - toAcc, err := ledger.NewAccount(recvPubKey, amount) + toAcc, err := c.NewAccount(recvPubKey, amount) if err != nil { log.Panic("wrong account on channel", zap.Error(err)) } - channelState := ledger.NewChannelState(channelID, 1, fromAcc, toAcc) + channelState := c.NewChannelState(channelID, 1, fromAcc, toAcc) // need permission from both account, get signature from both fromSigState, err := crypto.Sign(payerPrivKey, channelState) if err != nil { @@ -73,9 +73,9 @@ func main() { if err != nil { log.Panic("error when signing the channel state", zap.Error(err)) } - signedChannelState := ledger.NewSignedChannelState(channelState, fromSigState, toSigState) + signedChannelState := c.NewSignedChannelState(channelState, fromSigState, toSigState) // close channel - err = ledger.CloseChannel(ctx, addr, signedChannelState) + err = c.CloseChannel(ctx, signedChannelState) if err != nil { log.Panic("fail to close channel", zap.Error(err)) } From eb8ea39ffe8d004339c7f40f6725c72eaf382607 Mon Sep 17 00:00:00 2001 From: Robin Lai Date: Mon, 9 Dec 2019 17:46:33 -0800 Subject: [PATCH 5/7] refactor: remove receiver from some functions --- ledger/ledger.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ledger/ledger.go b/ledger/ledger.go index 5b33eb3..dd8e9a8 100644 --- a/ledger/ledger.go +++ b/ledger/ledger.go @@ -19,7 +19,7 @@ func NewClient(addr string) *Client { return &Client{addr: addr} } -func (c *Client) NewAccount(pubKey ic.PubKey, amount int64) (*ledgerpb.Account, error) { +func NewAccount(pubKey ic.PubKey, amount int64) (*ledgerpb.Account, error) { addr, err := pubKey.Raw() if err != nil { return nil, err @@ -30,7 +30,7 @@ func (c *Client) NewAccount(pubKey ic.PubKey, amount int64) (*ledgerpb.Account, }, nil } -func (c *Client) NewChannelCommit(fromKey ic.PubKey, toKey ic.PubKey, amount int64) (*ledgerpb.ChannelCommit, error) { +func NewChannelCommit(fromKey ic.PubKey, toKey ic.PubKey, amount int64) (*ledgerpb.ChannelCommit, error) { fromAddr, err := fromKey.Raw() if err != nil { return nil, err @@ -47,7 +47,7 @@ func (c *Client) NewChannelCommit(fromKey ic.PubKey, toKey ic.PubKey, amount int }, err } -func (c *Client) NewChannelState(id *ledgerpb.ChannelID, sequence int64, fromAccount *ledgerpb.Account, toAccount *ledgerpb.Account) *ledgerpb.ChannelState { +func NewChannelState(id *ledgerpb.ChannelID, sequence int64, fromAccount *ledgerpb.Account, toAccount *ledgerpb.Account) *ledgerpb.ChannelState { return &ledgerpb.ChannelState{ Id: id, Sequence: sequence, @@ -56,7 +56,7 @@ func (c *Client) NewChannelState(id *ledgerpb.ChannelID, sequence int64, fromAcc } } -func (c *Client) NewSignedChannelState(channelState *ledgerpb.ChannelState, fromSig []byte, toSig []byte) *ledgerpb.SignedChannelState { +func NewSignedChannelState(channelState *ledgerpb.ChannelState, fromSig []byte, toSig []byte) *ledgerpb.SignedChannelState { return &ledgerpb.SignedChannelState{ Channel: channelState, FromSignature: fromSig, From 70ad25ab0e009e3d2fabca39f54582741cca073b Mon Sep 17 00:00:00 2001 From: Robin Lai Date: Mon, 9 Dec 2019 17:48:34 -0800 Subject: [PATCH 6/7] refactor --- ledger/sample/demo.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ledger/sample/demo.go b/ledger/sample/demo.go index d67cbc2..9796d90 100644 --- a/ledger/sample/demo.go +++ b/ledger/sample/demo.go @@ -40,7 +40,7 @@ func main() { } // prepare channel commit amount := int64(1) - channelCommit, err := c.NewChannelCommit(payerPubKey, recvPubKey, amount) + channelCommit, err := ledger.NewChannelCommit(payerPubKey, recvPubKey, amount) if err != nil { log.Panic("can not create channel commit", zap.Error(err)) } @@ -55,15 +55,15 @@ func main() { log.Panic("fail to create channel", zap.Error(err)) } // channel state: transfer money from -> to - fromAcc, err := c.NewAccount(payerPubKey, 0) + fromAcc, err := ledger.NewAccount(payerPubKey, 0) if err != nil { log.Panic("wrong account on channel", zap.Error(err)) } - toAcc, err := c.NewAccount(recvPubKey, amount) + toAcc, err := ledger.NewAccount(recvPubKey, amount) if err != nil { log.Panic("wrong account on channel", zap.Error(err)) } - channelState := c.NewChannelState(channelID, 1, fromAcc, toAcc) + channelState := ledger.NewChannelState(channelID, 1, fromAcc, toAcc) // need permission from both account, get signature from both fromSigState, err := crypto.Sign(payerPrivKey, channelState) if err != nil { @@ -73,7 +73,7 @@ func main() { if err != nil { log.Panic("error when signing the channel state", zap.Error(err)) } - signedChannelState := c.NewSignedChannelState(channelState, fromSigState, toSigState) + signedChannelState := ledger.NewSignedChannelState(channelState, fromSigState, toSigState) // close channel err = c.CloseChannel(ctx, signedChannelState) if err != nil { From a68d816007639d375d7cd859e3571101ebc314a6 Mon Sep 17 00:00:00 2001 From: Robin Lai Date: Mon, 9 Dec 2019 17:55:53 -0800 Subject: [PATCH 7/7] delete useless code. --- ledger/sample/demo.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/ledger/sample/demo.go b/ledger/sample/demo.go index 9796d90..1bcef98 100644 --- a/ledger/sample/demo.go +++ b/ledger/sample/demo.go @@ -79,7 +79,4 @@ func main() { if err != nil { log.Panic("fail to close channel", zap.Error(err)) } - if err != nil { - log.Panic(err.Error()) - } }