Skip to content

Commit

Permalink
Merge pull request #222 from bandprotocol/add-max-delay-on-v2.3.0
Browse files Browse the repository at this point in the history
cherry pick from branch fix-verify-endpoint to v.2.3.x
  • Loading branch information
satawatnack authored Aug 8, 2022
2 parents 25e7141 + 5cfe923 commit d858a31
Show file tree
Hide file tree
Showing 13 changed files with 592 additions and 299 deletions.
5 changes: 5 additions & 0 deletions proto/oracle/v1/oracle.proto
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ message RequestVerification {
(gogoproto.customname) = "ExternalID",
(gogoproto.casttype) = "ExternalID"
];
// DataSourceID is the ID of data source
uint64 data_source_id = 5 [
(gogoproto.customname) = "DataSourceID",
(gogoproto.casttype) = "DataSourceID"
];
}

// PriceResult is a result from standard price reference
Expand Down
12 changes: 10 additions & 2 deletions proto/oracle/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,17 @@ message QueryRequestVerificationRequest {
uint64 request_id = 3;
// ExternalID is an oracle's external ID
uint64 external_id = 4;
// DataSourceID is the ID of data source
uint64 data_source_id = 5;
// Reporter is an bech32-encoded public key of the reporter authorized by the
// validator
string reporter = 5;
string reporter = 6;
// Signature is a signature signed by the reporter using reporter's private
// key
bytes signature = 6;
bytes signature = 7;
// MaxDelay is a max number of RequestID delay acceptable in case of node
// delay
uint64 max_delay = 8;
}

// QueryRequestVerificationResponse is response type for the
Expand All @@ -311,4 +316,7 @@ message QueryRequestVerificationResponse {
// DataSourceID is the ID of a data source that relates to the targeted
// external ID
uint64 data_source_id = 5;
// IsDelay is a flag to tell that this request is not exist yet but in
// MaxDelay range
bool is_delay = 6;
}
12 changes: 6 additions & 6 deletions testing/testapp/wasm_1_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ var Wasm1 []byte = wat2wasm([]byte(`
i64.const 1
i64.const 1
i32.const 1024
i64.extend_u/i32
tee_local $l0
i64.extend_i32_u
local.tee $l0
i64.const 4
call $ask_external_data
i64.const 2
i64.const 2
get_local $l0
local.get $l0
i64.const 4
call $ask_external_data
i64.const 3
i64.const 3
get_local $l0
local.get $l0
i64.const 4
call $ask_external_data)
(func $execute (export "execute") (type $t0)
i32.const 1024
i64.extend_u/i32
i64.extend_i32_u
i64.const 4
call $set_return_data)
(table $T0 1 1 anyfunc)
(table $T0 1 1 funcref)
(memory $memory (export "memory") 17)
(data (i32.const 1024) "beeb"))
`))
2 changes: 1 addition & 1 deletion testing/testapp/wasm_3_do_nothing.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var Wasm3 []byte = wat2wasm([]byte(`
(import "env" "set_return_data" (func $set_return_data (type $t2)))
(func $prepare (export "prepare") (type $t0))
(func $execute (export "execute") (type $t0))
(table $T0 1 1 anyfunc)
(table $T0 1 1 funcref)
(memory $memory (export "memory") 17)
(data (i32.const 1024) "beeb"))
`))
20 changes: 10 additions & 10 deletions testing/testapp/wasm_56_computation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@ func Wasm56(time int) []byte {
(func $prepare (export "prepare") (type $t0)
(local $l0 i64)
(local $idx i32)
(set_local $idx (i32.const 0))
(local.set $idx (i32.const 0))
(block
(loop
(set_local $idx (get_local $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (get_local $idx) (i32.const %d)))
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (local.get $idx) (i32.const %d)))
)
)
i64.const 1
i64.const 1
i32.const 1024
i64.extend_u/i32
tee_local $l0
i64.extend_i32_u
local.tee $l0
i64.const 4
call $ask_external_data)
(func $execute (export "execute") (type $t0)
(local $idx i32)
(set_local $idx (i32.const 0))
(local.set $idx (i32.const 0))
(block
(loop
(set_local $idx (get_local $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (get_local $idx) (i32.const %d)))
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
(br_if 0 (i32.lt_u (local.get $idx) (i32.const %d)))
)
)
i32.const 1024
i64.extend_u/i32
i64.extend_i32_u
i64.const 4
call $set_return_data)
(table $T0 1 1 anyfunc)
(table $T0 1 1 funcref)
(memory $memory (export "memory") 17)
(data (i32.const 1024) "new beeb"))`, time, time)))
}
8 changes: 4 additions & 4 deletions testing/testapp/wasm_78_large_calldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ func Wasm78(time int) []byte {
i64.const 1
i64.const 1
i32.const 1024
i64.extend_u/i32
tee_local $l0
i64.extend_i32_u
local.tee $l0
i64.const %d
call $ask_external_data)
(func $execute (export "execute") (type $t0)
i32.const 1024
i64.extend_u/i32
i64.extend_i32_u
i64.const %d
call $set_return_data)
(table $T0 1 1 anyfunc)
(table $T0 1 1 funcref)
(memory $memory (export "memory") 200)
(data (i32.const 1024) "%s"))
`, time, time, b.String())))
Expand Down
6 changes: 3 additions & 3 deletions testing/testapp/wasm_9_set_data_several_times.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ var Wasm9 []byte = wat2wasm([]byte(`
(func $prepare (export "prepare") (type $t0))
(func $execute (export "execute") (type $t0)
i32.const 1024
i64.extend_u/i32
i64.extend_i32_u
i64.const 4
call $set_return_data
i32.const 1024
i64.extend_u/i32
i64.extend_i32_u
i64.const 4
call $set_return_data)
(table $T0 1 1 anyfunc)
(table $T0 1 1 funcref)
(memory $memory (export "memory") 17)
(data (i32.const 1024) "beeb"))
`))
17 changes: 16 additions & 1 deletion x/oracle/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (k Querier) RequestVerification(c context.Context, req *types.QueryRequestV
}
reporterPubKey := secp256k1.PubKey(pk[:])

requestVerificationContent := types.NewRequestVerification(req.ChainId, validator, types.RequestID(req.RequestId), types.ExternalID(req.ExternalId))
requestVerificationContent := types.NewRequestVerification(req.ChainId, validator, types.RequestID(req.RequestId), types.ExternalID(req.ExternalId), types.DataSourceID(req.DataSourceId))
signByte := requestVerificationContent.GetSignBytes()
if !reporterPubKey.VerifySignature(signByte, req.Signature) {
return nil, status.Error(codes.Unauthenticated, "invalid reporter's signature")
Expand All @@ -292,6 +292,17 @@ func (k Querier) RequestVerification(c context.Context, req *types.QueryRequestV
// Provided request should exist on chain
request, err := k.GetRequest(ctx, types.RequestID(req.RequestId))
if err != nil {
// return uncertain result if request id is in range of max delay
if req.RequestId-k.GetRequestCount(ctx) > 0 && req.RequestId-k.GetRequestCount(ctx) <= req.MaxDelay {
return &types.QueryRequestVerificationResponse{
ChainId: req.ChainId,
Validator: req.Validator,
RequestId: req.RequestId,
ExternalId: req.ExternalId,
DataSourceId: req.DataSourceId,
IsDelay: true,
}, nil
}
return nil, status.Error(codes.NotFound, fmt.Sprintf("unable to get request from chain: %s", err.Error()))
}

Expand Down Expand Up @@ -319,6 +330,9 @@ func (k Querier) RequestVerification(c context.Context, req *types.QueryRequestV
if dataSourceID == nil {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("no data source required by the request %d found which relates to the external data source with ID %d.", req.RequestId, req.ExternalId))
}
if *dataSourceID != types.DataSourceID(req.DataSourceId) {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("data source required by the request %d which relates to the external data source with ID %d is not match with data source id provided in request.", req.RequestId, req.ExternalId))
}

// Provided validator should not have reported data for the request
reports := k.GetReports(ctx, types.RequestID(req.RequestId))
Expand All @@ -345,5 +359,6 @@ func (k Querier) RequestVerification(c context.Context, req *types.QueryRequestV
RequestId: req.RequestId,
ExternalId: req.ExternalId,
DataSourceId: uint64(*dataSourceID),
IsDelay: false,
}, nil
}
Loading

0 comments on commit d858a31

Please sign in to comment.