Skip to content

Commit

Permalink
Add chord id to each sigchain element
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Sep 14, 2018
1 parent 5ae7eee commit 4994d77
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 21 deletions.
4 changes: 2 additions & 2 deletions api/common/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,10 @@ func sigchaintest(s Serverer, params map[string]interface{}) map[string]interfac
if err != nil {
return respPacking(nil, INTERNAL_ERROR)
}
if err := sigChain.Sign(encodedPublickKey, mining, account); err != nil {
if err := sigChain.Sign(srcID, encodedPublickKey, mining, account); err != nil {
return respPacking(nil, INTERNAL_ERROR)
}
if err := sigChain.Sign(encodedPublickKey, mining, account); err != nil {
if err := sigChain.Sign(srcID, encodedPublickKey, mining, account); err != nil {
return respPacking(nil, INTERNAL_ERROR)
}
buf, err := proto.Marshal(sigChain)
Expand Down
2 changes: 1 addition & 1 deletion nknd.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func nknMain(c *cli.Context) error {
// if InitLedger return err, ledger.DefaultLedger is uninitialized.
defer ledger.DefaultLedger.Store.Close()

err = por.InitPorServer(account)
err = por.InitPorServer(account, ring)
if err != nil {
return errors.New("PorServer initialization error")
}
Expand Down
4 changes: 2 additions & 2 deletions por/porpackage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func TestPorPackage(t *testing.T) {
t.Error("sigchain created failed")
}

err = sc.Sign(toPk, true, rel)
err = sc.Sign(srcID, toPk, true, rel)
if err != nil || sc.Verify() != nil {
t.Error("'rel' sign in error")
}

err = sc.Sign(toPk, true, to)
err = sc.Sign(srcID, toPk, true, to)
if err != nil || sc.Verify() != nil {
t.Error("'to' sign in error")
}
Expand Down
19 changes: 15 additions & 4 deletions por/porserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,37 @@ import (

"github.com/nknorg/nkn/common"
"github.com/nknorg/nkn/core/transaction"
"github.com/nknorg/nkn/net/chord"
"github.com/nknorg/nkn/util/log"
"github.com/nknorg/nkn/vault"
)

type PorServer struct {
sync.RWMutex
account *vault.Account
ring *chord.Ring
pors map[uint32][]*PorPackage
}

var porServer *PorServer

func NewPorServer(account *vault.Account) *PorServer {
func NewPorServer(account *vault.Account, ring *chord.Ring) *PorServer {
ps := &PorServer{
account: account,
ring: ring,
pors: make(map[uint32][]*PorPackage),
}
return ps
}

func InitPorServer(account *vault.Account) error {
func InitPorServer(account *vault.Account, ring *chord.Ring) error {
if porServer != nil {
return errors.New("PorServer already initialized")
}
porServer = NewPorServer(account)
if ring == nil {
return errors.New("Ring is not initialized")
}
porServer = NewPorServer(account, ring)
return nil
}

Expand Down Expand Up @@ -60,7 +66,12 @@ func (ps *PorServer) Sign(sc *SigChain, nextPubkey []byte, mining bool) error {
return errors.New("it's not the right signer")
}

err = sc.Sign(nextPubkey, mining, ps.account)
vnode, err := ps.ring.GetFirstVnode()
if err != nil {
return err
}

err = sc.Sign(vnode.Id, nextPubkey, mining, ps.account)
if err != nil {
log.Error("Signature chain signing error:", err)
return err
Expand Down
6 changes: 3 additions & 3 deletions por/porserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func TestPorServer(t *testing.T) {
from, _ := vault.NewAccount()
rel, _ := vault.NewAccount()
to, _ := vault.NewAccount()
pmFrom := NewPorServer(from)
pmRel := NewPorServer(rel)
pmTo := NewPorServer(to)
pmFrom := NewPorServer(from, ring)
pmRel := NewPorServer(rel, ring)
pmTo := NewPorServer(to, ring)
toPk, _ := to.PubKey().EncodePoint(true)
relPk, _ := rel.PubKey().EncodePoint(true)

Expand Down
11 changes: 6 additions & 5 deletions por/sigchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,18 @@ func NewSigChain(srcAccount *vault.Account, dataSize uint32, dataHash, blockHash
return sc, nil
}

func NewSigChainElem(nextPubkey []byte, mining bool) *SigChainElem {
func NewSigChainElem(addr, nextPubkey []byte, mining bool) *SigChainElem {
return &SigChainElem{
Addr: addr,
SigAlgo: sigAlgo,
NextPubkey: nextPubkey,
Mining: mining,
Signature: nil,
}
}

func (sc *SigChain) ExtendElement(nextPubkey []byte, mining bool) ([]byte, error) {
elem := NewSigChainElem(nextPubkey, mining)
func (sc *SigChain) ExtendElement(addr, nextPubkey []byte, mining bool) ([]byte, error) {
elem := NewSigChainElem(addr, nextPubkey, mining)
lastElem, err := sc.lastSigElem()
if err != nil {
return nil, err
Expand Down Expand Up @@ -197,7 +198,7 @@ func (sc *SigChain) AddLastSignature(signature []byte) error {
}

// Sign new created signature chain with local wallet.
func (sc *SigChain) Sign(nextPubkey []byte, mining bool, signer *vault.Account) error {
func (sc *SigChain) Sign(addr, nextPubkey []byte, mining bool, signer *vault.Account) error {
sigNum := sc.Length()
if sigNum < 1 {
return errors.New("there are not enough signatures")
Expand Down Expand Up @@ -225,7 +226,7 @@ func (sc *SigChain) Sign(nextPubkey []byte, mining bool, signer *vault.Account)
return errors.New("signer is not the right one")
}

digest, err := sc.ExtendElement(nextPubkey, mining)
digest, err := sc.ExtendElement(addr, nextPubkey, mining)
if err != nil {
log.Error("Signature chain extent element error:", err)
return err
Expand Down
6 changes: 3 additions & 3 deletions por/sigchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ func TestSigChain(t *testing.T) {
t.Error("[TestSigChain] 'from' create new SigChain in error")
}

err = sc.Sign(relay2Pk, true, relay1)
err = sc.Sign(srcID, relay2Pk, true, relay1)
if err != nil || sc.Verify() != nil {
t.Error("[TestSigChain] 'relay1' sign in error")
}

err = sc.Sign(toPk, true, relay2)
err = sc.Sign(srcID, toPk, true, relay2)
if err != nil || sc.Verify() != nil {
t.Error("[TestSigChain] 'relay2' sign in error")
}

err = sc.Sign(toPk, true, to)
err = sc.Sign(srcID, toPk, true, to)
if err != nil || sc.Verify() != nil {
t.Error("[TestSigChain] 'to' sign in error")
}
Expand Down
2 changes: 1 addition & 1 deletion relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (rs *RelayService) SendPacketToClients(clients []*session.Session, packet *

// TODO: only pick sigchain to sign when threshold is smaller than

_, err = packet.SigChain.ExtendElement(destPubKey, false)
_, err = packet.SigChain.ExtendElement(packet.DestID, destPubKey, false)
if err != nil {
return err
}
Expand Down

0 comments on commit 4994d77

Please sign in to comment.