Skip to content

Commit

Permalink
Merge pull request #510 from bandprotocol/timestamp-tunnel
Browse files Browse the repository at this point in the history
[fix][tunnel] use default timestamp from blocktime instead of 0, cleanup test
  • Loading branch information
RogerKSI authored Dec 3, 2024
2 parents 21710ba + d6fe0f6 commit 774ab71
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 67 deletions.
4 changes: 2 additions & 2 deletions scripts/tunnel/signal_deviations.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"signal_deviations": [
{
"signal_id": "CS:BTC-USD",
"deviation_bps": 2000
"deviation_bps": 200
},
{
"signal_id": "CS:ETH-USD",
"deviation_bps": 3000
"deviation_bps": 400
}
]
}
2 changes: 1 addition & 1 deletion x/feeds/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func GetTxCmdVote() *cobra.Command {
fmt.Sprintf(
`Vote signal ids and their power.
Example:
$ %s tx feeds vote BTC,1000000 --from mykey
$ %s tx feeds vote CS:BAND-USD,1000000 --from mykey
`,
version.AppName,
),
Expand Down
6 changes: 6 additions & 0 deletions x/feeds/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,19 @@ func (suite *KeeperTestSuite) TestQueryPrices() {
// Setup multiple prices
prices := []types.Price{
{
Status: types.PRICE_STATUS_AVAILABLE,
SignalID: "CS:BAND-USD",
Price: 100000000,
Timestamp: 1234567890,
},
{
Status: types.PRICE_STATUS_AVAILABLE,
SignalID: "CS:ATOM-USD",
Price: 200000000,
Timestamp: 1234567890,
},
{
Status: types.PRICE_STATUS_AVAILABLE,
SignalID: "CS:BTC-USD",
Price: 300000000,
Timestamp: 1234567890,
Expand Down Expand Up @@ -173,16 +176,19 @@ func (suite *KeeperTestSuite) TestQueryAllPrices() {
// Setup multiple prices
prices := []types.Price{
{
Status: types.PRICE_STATUS_AVAILABLE,
SignalID: "CS:ATOM-USD",
Price: 200000000,
Timestamp: 1234567891,
},
{
Status: types.PRICE_STATUS_AVAILABLE,
SignalID: "CS:BAND-USD",
Price: 100000000,
Timestamp: 1234567890,
},
{
Status: types.PRICE_STATUS_AVAILABLE,
SignalID: "CS:BTC-USD",
Price: 300000000,
Timestamp: 1234567892,
Expand Down
4 changes: 2 additions & 2 deletions x/tunnel/client/cli/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

func TestParseSignalDeviations(t *testing.T) {
signalDeviations := []SignalDeviation{
{SignalID: "BTC", DeviationBPS: 2000},
{SignalID: "ETH", DeviationBPS: 4000},
{SignalID: "CS:BTC-USD", DeviationBPS: 2000},
{SignalID: "CS:ETH-USD", DeviationBPS: 4000},
}
file, cleanup := createTempSignalDeviationFile(signalDeviations)
defer cleanup()
Expand Down
7 changes: 6 additions & 1 deletion x/tunnel/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ func TestPacketDataUnmarshalerInterface(t *testing.T) {
TunnelID: 1,
Sequence: 1,
Prices: []feedstypes.Price{
{Status: feedstypes.PRICE_STATUS_NOT_IN_CURRENT_FEEDS, SignalID: "BTC", Price: 50000},
{
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:BAND-USD",
Price: 50000,
Timestamp: 1733000000,
},
},
CreatedAt: 1633024800,
}
Expand Down
3 changes: 2 additions & 1 deletion x/tunnel/keeper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func GenerateNewPrices(
signalDeviations []types.SignalDeviation,
latestPricesMap map[string]feedstypes.Price,
feedsPricesMap map[string]feedstypes.Price,
timestamp int64,
sendAll bool,
) []feedstypes.Price {
shouldSend := false
Expand All @@ -36,7 +37,7 @@ func GenerateNewPrices(

feedPrice, ok := feedsPricesMap[sd.SignalID]
if !ok {
feedPrice = feedstypes.NewPrice(feedstypes.PRICE_STATUS_NOT_IN_CURRENT_FEEDS, sd.SignalID, 0, 0)
feedPrice = feedstypes.NewPrice(feedstypes.PRICE_STATUS_NOT_IN_CURRENT_FEEDS, sd.SignalID, 0, timestamp)
}

// calculate deviation between old price and new price and compare with the threshold.
Expand Down
83 changes: 68 additions & 15 deletions x/tunnel/keeper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,27 @@ func (s *KeeperTestSuite) TestGeneratePricesSendAll() {

tunnelID := uint64(1)
pricesMap := map[string]feedstypes.Price{
"BTC/USD": {Status: feedstypes.PRICE_STATUS_AVAILABLE, SignalID: "BTC/USD", Price: 50000, Timestamp: 0},
"CS:BAND-USD": {
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:BAND-USD",
Price: 50000,
Timestamp: 1733000000,
},
}
sendAll := true
tunnel := types.Tunnel{
ID: tunnelID,
SignalDeviations: []types.SignalDeviation{
{SignalID: "BTC/USD", SoftDeviationBPS: 1000, HardDeviationBPS: 1000},
{SignalID: "CS:BAND-USD", SoftDeviationBPS: 1000, HardDeviationBPS: 1000},
},
}
latestPrices := types.NewLatestPrices(tunnelID, []feedstypes.Price{
{Status: feedstypes.PRICE_STATUS_NOT_IN_CURRENT_FEEDS, SignalID: "BTC/USD", Price: 0},
{
Status: feedstypes.PRICE_STATUS_NOT_IN_CURRENT_FEEDS,
SignalID: "CS:BAND-USD",
Price: 0,
Timestamp: 1733000000,
},
}, 0)

latestPricesMap := keeper.CreatePricesMap(latestPrices.Prices)
Expand All @@ -33,6 +43,7 @@ func (s *KeeperTestSuite) TestGeneratePricesSendAll() {
tunnel.SignalDeviations,
latestPricesMap,
pricesMap,
1733000000,
sendAll,
)
s.Require().Len(newPrices, 1)
Expand All @@ -43,20 +54,40 @@ func (s *KeeperTestSuite) TestGeneratePricesMeetHardDeviation() {

tunnelID := uint64(1)
pricesMap := map[string]feedstypes.Price{
"BTC/USD": {Status: feedstypes.PRICE_STATUS_AVAILABLE, SignalID: "BTC/USD", Price: 50000, Timestamp: 0},
"ETH/USD": {Status: feedstypes.PRICE_STATUS_AVAILABLE, SignalID: "BTC/USD", Price: 2000, Timestamp: 0},
"CS:BAND-USD": {
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:BAND-USD",
Price: 50000,
Timestamp: 1733000000,
},
"CS:ETH-USD": {
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:ETH-USD",
Price: 2000,
Timestamp: 1733000000,
},
}
sendAll := false
tunnel := types.Tunnel{
ID: tunnelID,
SignalDeviations: []types.SignalDeviation{
{SignalID: "BTC/USD", SoftDeviationBPS: 100, HardDeviationBPS: 300},
{SignalID: "ETH/USD", SoftDeviationBPS: 100, HardDeviationBPS: 300},
{SignalID: "CS:BAND-USD", SoftDeviationBPS: 100, HardDeviationBPS: 300},
{SignalID: "CS:ETH-USD", SoftDeviationBPS: 100, HardDeviationBPS: 300},
},
}
latestPrices := types.NewLatestPrices(tunnelID, []feedstypes.Price{
{Status: feedstypes.PRICE_STATUS_AVAILABLE, SignalID: "BTC/USD", Price: 48500}, // 3%
{Status: feedstypes.PRICE_STATUS_AVAILABLE, SignalID: "ETH/USD", Price: 1980}, // 1%
{
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:BAND-USD",
Price: 48500,
Timestamp: 1732000000,
}, // 3%
{
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:ETH-USD",
Price: 1980,
Timestamp: 1732000000,
}, // 1%
}, 0)
latestPricesMap := keeper.CreatePricesMap(latestPrices.Prices)

Expand All @@ -67,6 +98,7 @@ func (s *KeeperTestSuite) TestGeneratePricesMeetHardDeviation() {
tunnel.SignalDeviations,
latestPricesMap,
pricesMap,
1733000000,
sendAll,
)
s.Require().Len(newPrices, 2)
Expand All @@ -77,20 +109,40 @@ func (s *KeeperTestSuite) TestGeneratePricesNotMeetHardDeviation() {

tunnelID := uint64(1)
pricesMap := map[string]feedstypes.Price{
"BTC/USD": {Status: feedstypes.PRICE_STATUS_AVAILABLE, SignalID: "BTC/USD", Price: 50000, Timestamp: 0},
"ETH/USD": {Status: feedstypes.PRICE_STATUS_AVAILABLE, SignalID: "BTC/USD", Price: 2000, Timestamp: 0},
"CS:BAND-USD": {
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:BAND-USD",
Price: 50000,
Timestamp: 1733000000,
},
"CS:ETH-USD": {
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:ETH-USD",
Price: 2000,
Timestamp: 1733000000,
},
}
sendAll := false
tunnel := types.Tunnel{
ID: tunnelID,
SignalDeviations: []types.SignalDeviation{
{SignalID: "BTC/USD", SoftDeviationBPS: 100, HardDeviationBPS: 300},
{SignalID: "ETH/USD", SoftDeviationBPS: 100, HardDeviationBPS: 300},
{SignalID: "CS:BAND-USD", SoftDeviationBPS: 100, HardDeviationBPS: 300},
{SignalID: "CS:ETH-USD", SoftDeviationBPS: 100, HardDeviationBPS: 300},
},
}
latestPrices := types.NewLatestPrices(tunnelID, []feedstypes.Price{
{Status: feedstypes.PRICE_STATUS_AVAILABLE, SignalID: "BTC/USD", Price: 49000}, // 2%
{Status: feedstypes.PRICE_STATUS_AVAILABLE, SignalID: "ETH/USD", Price: 1950}, // 2.5%
{
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:BAND-USD",
Price: 49000,
Timestamp: 1732000000,
}, // 2%
{
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:ETH-USD",
Price: 1950,
Timestamp: 1732000000,
}, // 2.5%
}, 0)
latestPricesMap := keeper.CreatePricesMap(latestPrices.Prices)

Expand All @@ -101,6 +153,7 @@ func (s *KeeperTestSuite) TestGeneratePricesNotMeetHardDeviation() {
tunnel.SignalDeviations,
latestPricesMap,
pricesMap,
1733000000,
sendAll,
)
s.Require().Len(newPrices, 0)
Expand Down
21 changes: 18 additions & 3 deletions x/tunnel/keeper/keeper_latest_prices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ func (s *KeeperTestSuite) TestGetSetLatestPrices() {
latestPrices := types.LatestPrices{
TunnelID: tunnelID,
Prices: []feedstypes.Price{
{Status: feedstypes.PRICE_STATUS_NOT_IN_CURRENT_FEEDS, SignalID: "BTC", Price: 50000},
{
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:BAND-USD",
Price: 50000,
Timestamp: 1733000000,
},
},
}

Expand All @@ -29,13 +34,23 @@ func (s *KeeperTestSuite) TestGetAllLatestPrices() {
latestPrices1 := types.LatestPrices{
TunnelID: 1,
Prices: []feedstypes.Price{
{Status: feedstypes.PRICE_STATUS_NOT_IN_CURRENT_FEEDS, SignalID: "BTC", Price: 50000},
{
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:BAND-USD",
Price: 50000,
Timestamp: 1733000000,
},
},
}
latestPrices2 := types.LatestPrices{
TunnelID: 2,
Prices: []feedstypes.Price{
{Status: feedstypes.PRICE_STATUS_NOT_IN_CURRENT_FEEDS, SignalID: "ETH", Price: 3000},
{
Status: feedstypes.PRICE_STATUS_AVAILABLE,
SignalID: "CS:ETH-USD",
Price: 3000,
Timestamp: 1733000000,
},
},
}

Expand Down
8 changes: 7 additions & 1 deletion x/tunnel/keeper/keeper_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ func (k Keeper) ProducePacket(
sendAll := unixNow >= int64(tunnel.Interval)+latestPrices.LastInterval

// generate newPrices; if no newPrices, stop the process.
newPrices := GenerateNewPrices(tunnel.SignalDeviations, latestPricesMap, feedsPricesMap, sendAll)
newPrices := GenerateNewPrices(
tunnel.SignalDeviations,
latestPricesMap,
feedsPricesMap,
ctx.BlockTime().Unix(),
sendAll,
)
if len(newPrices) == 0 {
return nil
}
Expand Down
Loading

0 comments on commit 774ab71

Please sign in to comment.