|
@@ -6,6 +6,7 @@ import (
|
|
|
"testing"
|
|
"testing"
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
|
|
|
+ "github.com/libp2p/go-libp2p/core/peer"
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
node_common "github.com/certusone/wormhole/node/pkg/common"
|
|
node_common "github.com/certusone/wormhole/node/pkg/common"
|
|
@@ -21,6 +22,8 @@ func TestSignedHeartbeat(t *testing.T) {
|
|
|
timestamp int64
|
|
timestamp int64
|
|
|
gk *ecdsa.PrivateKey
|
|
gk *ecdsa.PrivateKey
|
|
|
heartbeatGuardianAddr string
|
|
heartbeatGuardianAddr string
|
|
|
|
|
+ fromP2pId peer.ID
|
|
|
|
|
+ p2pNodeId []byte
|
|
|
expectSuccess bool
|
|
expectSuccess bool
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -29,10 +32,18 @@ func TestSignedHeartbeat(t *testing.T) {
|
|
|
gk, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
gk, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
|
gAddr := ethcrypto.PubkeyToAddress(gk.PublicKey)
|
|
gAddr := ethcrypto.PubkeyToAddress(gk.PublicKey)
|
|
|
|
|
+ fromP2pId, err := peer.Decode("12D3KooWSgMXkhzTbKTeupHYmyG7sFJ5LpVreQcwVnX8RD7LBpy9")
|
|
|
|
|
+ assert.NoError(t, err)
|
|
|
|
|
+ p2pNodeId, err := fromP2pId.Marshal()
|
|
|
|
|
+ assert.NoError(t, err)
|
|
|
|
|
|
|
|
gk2, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
gk2, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
|
gAddr2 := ethcrypto.PubkeyToAddress(gk2.PublicKey)
|
|
gAddr2 := ethcrypto.PubkeyToAddress(gk2.PublicKey)
|
|
|
|
|
+ fromP2pId2, err := peer.Decode("12D3KooWDZVv7BhZ8yFLkarNdaSWaB43D6UbQwExJ8nnGAEmfHcU")
|
|
|
|
|
+ assert.NoError(t, err)
|
|
|
|
|
+ p2pNodeId2, err := fromP2pId2.Marshal()
|
|
|
|
|
+ assert.NoError(t, err)
|
|
|
|
|
|
|
|
tests := []testCase{
|
|
tests := []testCase{
|
|
|
// happy case
|
|
// happy case
|
|
@@ -40,6 +51,8 @@ func TestSignedHeartbeat(t *testing.T) {
|
|
|
timestamp: time.Now().UnixNano(),
|
|
timestamp: time.Now().UnixNano(),
|
|
|
gk: gk,
|
|
gk: gk,
|
|
|
heartbeatGuardianAddr: gAddr.String(),
|
|
heartbeatGuardianAddr: gAddr.String(),
|
|
|
|
|
+ fromP2pId: fromP2pId,
|
|
|
|
|
+ p2pNodeId: p2pNodeId,
|
|
|
expectSuccess: true,
|
|
expectSuccess: true,
|
|
|
},
|
|
},
|
|
|
// guardian signed a heartbeat for another guardian
|
|
// guardian signed a heartbeat for another guardian
|
|
@@ -47,20 +60,35 @@ func TestSignedHeartbeat(t *testing.T) {
|
|
|
timestamp: time.Now().UnixNano(),
|
|
timestamp: time.Now().UnixNano(),
|
|
|
gk: gk,
|
|
gk: gk,
|
|
|
heartbeatGuardianAddr: gAddr2.String(),
|
|
heartbeatGuardianAddr: gAddr2.String(),
|
|
|
|
|
+ fromP2pId: fromP2pId,
|
|
|
|
|
+ p2pNodeId: p2pNodeId,
|
|
|
expectSuccess: false,
|
|
expectSuccess: false,
|
|
|
},
|
|
},
|
|
|
// old heartbeat
|
|
// old heartbeat
|
|
|
{
|
|
{
|
|
|
timestamp: time.Now().Add(-time.Hour).UnixNano(),
|
|
timestamp: time.Now().Add(-time.Hour).UnixNano(),
|
|
|
gk: gk,
|
|
gk: gk,
|
|
|
- heartbeatGuardianAddr: gAddr2.String(),
|
|
|
|
|
|
|
+ heartbeatGuardianAddr: gAddr.String(),
|
|
|
|
|
+ fromP2pId: fromP2pId,
|
|
|
|
|
+ p2pNodeId: p2pNodeId,
|
|
|
expectSuccess: false,
|
|
expectSuccess: false,
|
|
|
},
|
|
},
|
|
|
// heartbeat from the distant future
|
|
// heartbeat from the distant future
|
|
|
{
|
|
{
|
|
|
timestamp: time.Now().Add(time.Hour).UnixNano(),
|
|
timestamp: time.Now().Add(time.Hour).UnixNano(),
|
|
|
gk: gk,
|
|
gk: gk,
|
|
|
- heartbeatGuardianAddr: gAddr2.String(),
|
|
|
|
|
|
|
+ heartbeatGuardianAddr: gAddr.String(),
|
|
|
|
|
+ fromP2pId: fromP2pId,
|
|
|
|
|
+ p2pNodeId: p2pNodeId,
|
|
|
|
|
+ expectSuccess: false,
|
|
|
|
|
+ },
|
|
|
|
|
+ // mismatched peer id
|
|
|
|
|
+ {
|
|
|
|
|
+ timestamp: time.Now().UnixNano(),
|
|
|
|
|
+ gk: gk,
|
|
|
|
|
+ heartbeatGuardianAddr: gAddr.String(),
|
|
|
|
|
+ fromP2pId: fromP2pId,
|
|
|
|
|
+ p2pNodeId: p2pNodeId2,
|
|
|
expectSuccess: false,
|
|
expectSuccess: false,
|
|
|
},
|
|
},
|
|
|
}
|
|
}
|
|
@@ -79,6 +107,7 @@ func TestSignedHeartbeat(t *testing.T) {
|
|
|
GuardianAddr: tc.heartbeatGuardianAddr,
|
|
GuardianAddr: tc.heartbeatGuardianAddr,
|
|
|
BootTimestamp: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano(),
|
|
BootTimestamp: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano(),
|
|
|
Features: []string{},
|
|
Features: []string{},
|
|
|
|
|
+ P2PNodeId: tc.p2pNodeId,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
s := createSignedHeartbeat(gk, heartbeat)
|
|
s := createSignedHeartbeat(gk, heartbeat)
|
|
@@ -89,7 +118,7 @@ func TestSignedHeartbeat(t *testing.T) {
|
|
|
|
|
|
|
|
gst := node_common.NewGuardianSetState(nil)
|
|
gst := node_common.NewGuardianSetState(nil)
|
|
|
|
|
|
|
|
- heartbeatResult, err := processSignedHeartbeat("someone", s, gs, gst, false)
|
|
|
|
|
|
|
+ heartbeatResult, err := processSignedHeartbeat(tc.fromP2pId, s, gs, gst, false)
|
|
|
|
|
|
|
|
if tc.expectSuccess {
|
|
if tc.expectSuccess {
|
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|