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
426 changes: 88 additions & 338 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 {

@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