Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
422 changes: 85 additions & 337 deletions framework/src/main/java/org/tron/core/services/RpcApiService.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.tron.core.services.filter;

import io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener;
import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.tron.core.db.Manager;
import org.tron.core.db2.core.Chainbase;

/**
* Switches the current thread's read cursor around the synchronous handler callback and restores it
* afterwards, so the handler reads from the snapshot its subclass selects (HEAD / SOLIDITY / PBFT);
* the services behind it never touch the cursor.
*
* <p>Two invariants it relies on:
* <ul>
* <li>The bracket wraps {@code onHalfClose()} — where gRPC runs the unary handler inline — not
* {@code interceptCall}, which may land on another pool thread; the cursor is a
* {@link ThreadLocal}, so setting it elsewhere fails silently and the port serves HEAD.
* <li>Handlers must be synchronous: the cursor is reset when {@code onHalfClose} returns, so a read
* deferred to another thread would read HEAD.
* </ul>
*
* <p>The {@code finally} reset is mandatory: the fixed thread pool is reused, so a leftover cursor
* leaks into the next call on that thread.
*/
public abstract class CursorServerInterceptor implements ServerInterceptor {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NIT] Method-set parity is now implicit — record the new contract semantics

After this dedup, any method added to protocol.WalletSolidity / protocol.Database in the future automatically appears on all three ports (HEAD / SOLIDITY / PBFT). The per-port opt-out that the mirrored delegations provided — intentionally or not, e.g. the two methods the PBFT port lacked until now — no longer exists; each port's method set is now defined solely by the proto service. That is a reasonable semantics, but it should be recorded: one sentence in this class's javadoc or the PR description, e.g. "new proto methods default to all-port parity; per-port exclusion must be implemented explicitly".

Suggestion: add the one-line contract note to the class javadoc (or PR description); no code change needed.


@Autowired
protected Manager dbManager;

/** Snapshot every call on this server reads from; set by the subclass. */
protected Chainbase.Cursor cursor;

@Override
public <Q, A> ServerCall.Listener<Q> interceptCall(
ServerCall<Q, A> call, Metadata headers, ServerCallHandler<Q, A> next) {
return new SimpleForwardingServerCallListener<Q>(next.startCall(call, headers)) {
@Override
public void onHalfClose() {
try {
dbManager.setCursor(cursor);
super.onHalfClose();
} finally {
dbManager.resetCursor();
}
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.tron.core.services.filter;

import org.springframework.stereotype.Component;
import org.tron.core.db2.core.Chainbase;

/**
* Makes every call on the PBFT gRPC server read the PBFT-confirmed state view.
*/
@Component
public class PbftCursorInterceptor extends CursorServerInterceptor {

public PbftCursorInterceptor() {
this.cursor = Chainbase.Cursor.PBFT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.tron.core.services.filter;

import org.springframework.stereotype.Component;
import org.tron.core.db2.core.Chainbase;

/**
* Makes every call on the Solidity gRPC server read the solidified state view.
*/
@Component
public class SolidityCursorInterceptor extends CursorServerInterceptor {

public SolidityCursorInterceptor() {
this.cursor = Chainbase.Cursor.SOLIDITY;
}
}
Loading
Loading