Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send new height with postBlock response #4482

Merged
merged 7 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Include status into postBlock response
  • Loading branch information
rainydio committed Sep 7, 2021
commit 845822e3f10edf0f837fdda22f59644ea617883b
4 changes: 4 additions & 0 deletions packages/core-kernel/src/contracts/p2p/blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type PostBlockResponse = {
status: boolean;
height: number;
};
1 change: 1 addition & 0 deletions packages/core-kernel/src/contracts/p2p/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./blocks";
export * from "./chunk-cache";
export * from "./nes-client";
export * from "./network-monitor";
Expand Down
3 changes: 1 addition & 2 deletions packages/core-p2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
"@types/fs-extra": "^8.0.1",
"@types/hapi__sntp": "^3.1.0",
"@types/ip": "^1.1.0",
"@types/semver": "^6.2.0",
"protobufjs": "^6.11.2"
"@types/semver": "^6.2.0"
},
"engines": {
"node": ">=10.x"
Expand Down
3 changes: 2 additions & 1 deletion packages/core-p2p/src/socket-server/codecs/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Contracts } from "@arkecosystem/core-kernel";
import { Utils } from "@arkecosystem/crypto";
import { blocks } from "./proto/protos";

Expand Down Expand Up @@ -87,7 +88,7 @@ export const postBlock = {
serialize: (obj: blocks.IPostBlockResponse): Buffer => {
return Buffer.from(blocks.PostBlockResponse.encode(obj).finish());
},
deserialize: (payload: Buffer): blocks.IPostBlockResponse => {
deserialize: (payload: Buffer): Contracts.P2P.PostBlockResponse => {
return blocks.PostBlockResponse.decode(payload);
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ message PostBlockRequest {
}

message PostBlockResponse {
uint32 height = 1;
bool status = 1;
uint32 height = 2;
}

message GetBlocksRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export namespace blocks {
/** Properties of a PostBlockResponse. */
interface IPostBlockResponse {

/** PostBlockResponse status */
status?: (boolean|null);

/** PostBlockResponse height */
height?: (number|null);
}
Expand All @@ -114,6 +117,9 @@ export namespace blocks {
*/
constructor(properties?: blocks.IPostBlockResponse);

/** PostBlockResponse status. */
public status: boolean;

/** PostBlockResponse height. */
public height: number;

Expand Down
27 changes: 25 additions & 2 deletions packages/core-p2p/src/socket-server/codecs/proto/protos.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ $root.blocks = (function() {
* Properties of a PostBlockResponse.
* @memberof blocks
* @interface IPostBlockResponse
* @property {boolean|null} [status] PostBlockResponse status
* @property {number|null} [height] PostBlockResponse height
*/

Expand All @@ -266,6 +267,14 @@ $root.blocks = (function() {
this[keys[i]] = properties[keys[i]];
}

/**
* PostBlockResponse status.
* @member {boolean} status
* @memberof blocks.PostBlockResponse
* @instance
*/
PostBlockResponse.prototype.status = false;

/**
* PostBlockResponse height.
* @member {number} height
Expand Down Expand Up @@ -298,8 +307,10 @@ $root.blocks = (function() {
PostBlockResponse.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.status != null && Object.hasOwnProperty.call(message, "status"))
writer.uint32(/* id 1, wireType 0 =*/8).bool(message.status);
if (message.height != null && Object.hasOwnProperty.call(message, "height"))
writer.uint32(/* id 1, wireType 0 =*/8).uint32(message.height);
writer.uint32(/* id 2, wireType 0 =*/16).uint32(message.height);
return writer;
};

Expand Down Expand Up @@ -335,6 +346,9 @@ $root.blocks = (function() {
var tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.status = reader.bool();
break;
case 2:
message.height = reader.uint32();
break;
default:
Expand Down Expand Up @@ -372,6 +386,9 @@ $root.blocks = (function() {
PostBlockResponse.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.status != null && message.hasOwnProperty("status"))
if (typeof message.status !== "boolean")
return "status: boolean expected";
if (message.height != null && message.hasOwnProperty("height"))
if (!$util.isInteger(message.height))
return "height: integer expected";
Expand All @@ -390,6 +407,8 @@ $root.blocks = (function() {
if (object instanceof $root.blocks.PostBlockResponse)
return object;
var message = new $root.blocks.PostBlockResponse();
if (object.status != null)
message.status = Boolean(object.status);
if (object.height != null)
message.height = object.height >>> 0;
return message;
Expand All @@ -408,8 +427,12 @@ $root.blocks = (function() {
if (!options)
options = {};
var object = {};
if (options.defaults)
if (options.defaults) {
object.status = false;
object.height = 0;
}
if (message.status != null && message.hasOwnProperty("status"))
object.status = message.status;
if (message.height != null && message.hasOwnProperty("height"))
object.height = message.height;
return object;
Expand Down
14 changes: 7 additions & 7 deletions packages/core-p2p/src/socket-server/controllers/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ export class BlocksController extends Controller {
@Container.tagged("plugin", "@arkecosystem/core-p2p")
private readonly configuration!: Providers.PluginConfiguration;

@Container.inject(Container.Identifiers.StateStore)
private readonly stateStore!: Contracts.State.StateStore;

@Container.inject(Container.Identifiers.BlockchainService)
private readonly blockchain!: Contracts.Blockchain.Blockchain;

@Container.inject(Container.Identifiers.DatabaseService)
private readonly database!: DatabaseService;

public async postBlock(request: Hapi.Request): Promise<{ status: boolean; height: number }> {
public async postBlock(
request: Hapi.Request,
h: Hapi.ResponseToolkit,
): Promise<{ status: boolean; height: number }> {
const blockBuffer: Buffer = request.payload.block;

const deserializedHeader = Blocks.Deserializer.deserialize(blockBuffer, true);
Expand Down Expand Up @@ -50,15 +50,15 @@ export class BlocksController extends Controller {

if (!fromForger) {
if (this.blockchain.pingBlock(block)) {
return { status: true, height: this.stateStore.getLastHeight() };
return { status: true, height: this.blockchain.getLastHeight() };
}

const lastDownloadedBlock: Interfaces.IBlockData = this.blockchain.getLastDownloadedBlock();

const blockTimeLookup = await Utils.forgingInfoCalculator.getBlockTimeLookup(this.app, block.height);

if (!Utils.isBlockChained(lastDownloadedBlock, block, blockTimeLookup)) {
return { status: false, height: this.stateStore.getLastHeight() };
return { status: false, height: this.blockchain.getLastHeight() };
}
}

Expand All @@ -72,7 +72,7 @@ export class BlocksController extends Controller {

await this.blockchain.handleIncomingBlock(block, fromForger);

return { status: true, height: this.stateStore.getLastHeight() };
return { status: true, height: this.blockchain.getLastHeight() };
}

public async getBlocks(
Expand Down