77
88public class OperationRegistry {
99
10+ private static final Operation DEFAULT_MLOAD = new Operation (
11+ Op .MLOAD , 1 , 1 , EnergyCost ::getMloadCost , OperationActions ::mLoadAction );
12+
13+ private static final Operation DEFAULT_MSTORE = new Operation (
14+ Op .MSTORE , 2 , 0 , EnergyCost ::getMStoreCost , OperationActions ::mStoreAction );
15+
16+ private static final Operation DEFAULT_MSTORE8 = new Operation (
17+ Op .MSTORE8 , 2 , 0 , EnergyCost ::getMStore8Cost , OperationActions ::mStore8Action );
18+
19+ private static final Operation ADJUSTED_MLOAD =
20+ DEFAULT_MLOAD .adjustCost (EnergyCost ::getMloadCost2 );
21+
22+ private static final Operation ADJUSTED_MSTORE =
23+ DEFAULT_MSTORE .adjustCost (EnergyCost ::getMStoreCost2 );
24+
25+ private static final Operation ADJUSTED_MSTORE8 =
26+ DEFAULT_MSTORE8 .adjustCost (EnergyCost ::getMStore8Cost2 );
27+
28+ private static final Operation DEFAULT_VOTEWITNESS = new Operation (
29+ Op .VOTEWITNESS , 4 , 1 , EnergyCost ::getVoteWitnessCost ,
30+ OperationActions ::voteWitnessAction , VMConfig ::allowTvmVote );
31+
32+ private static final Operation ADJUSTED_VOTEWITNESS =
33+ DEFAULT_VOTEWITNESS .adjustCost (EnergyCost ::getVoteWitnessCost2 );
34+
35+ private static final Operation OSAKA_VOTEWITNESS =
36+ DEFAULT_VOTEWITNESS .adjustCost (EnergyCost ::getVoteWitnessCost3 );
37+
38+ private static final Operation DEFAULT_SUICIDE = new Operation (
39+ Op .SUICIDE , 1 , 0 , EnergyCost ::getSuicideCost , OperationActions ::suicideAction );
40+
41+ private static final Operation ADJUSTED_SUICIDE =
42+ DEFAULT_SUICIDE .adjustCost (EnergyCost ::getSuicideCost2 );
43+
44+ private static final Operation RESTRICTED_SUICIDE =
45+ DEFAULT_SUICIDE .adjustCost (EnergyCost ::getSuicideCost3 )
46+ .adjustAction (OperationActions ::suicideAction2 );
47+
1048 public enum Version {
11- TRON_V1_0 ,
12- TRON_V1_1 ,
13- TRON_V1_2 ,
14- TRON_V1_3 ,
15- TRON_V1_4 ,
1649 TRON_V1_5 ,
1750 // add more
1851 // TRON_V2,
@@ -21,13 +54,21 @@ public enum Version {
2154
2255 private static final Map <Version , JumpTable > tableMap = new HashMap <>();
2356
57+ // The newest version in use. Bump this when a newer operation set is added,
58+ // together with newLatestOperationSet() below.
59+ private static final Version LATEST_VERSION = Version .TRON_V1_5 ;
60+
2461 static {
25- tableMap .put (Version .TRON_V1_0 , newTronV10OperationSet ());
26- tableMap .put (Version .TRON_V1_1 , newTronV11OperationSet ());
27- tableMap .put (Version .TRON_V1_2 , newTronV12OperationSet ());
28- tableMap .put (Version .TRON_V1_3 , newTronV13OperationSet ());
29- tableMap .put (Version .TRON_V1_4 , newTronV14OperationSet ());
30- tableMap .put (Version .TRON_V1_5 , newTronV15OperationSet ());
62+ tableMap .put (LATEST_VERSION , newLatestOperationSet ());
63+ }
64+
65+ // Constant calls get a dedicated instance of the newest table, isolated from
66+ // the shared consensus table above.
67+ private static final JumpTable CONSTANT_CALL_TABLE = newLatestOperationSet ();
68+
69+ // The single place that decides which operation set is the newest.
70+ private static JumpTable newLatestOperationSet () {
71+ return newTronV15OperationSet ();
3172 }
3273
3374 public static JumpTable newTronV10OperationSet () {
@@ -74,28 +115,22 @@ public static JumpTable newTronV15OperationSet() {
74115 // Just for warming up class to avoid out_of_time
75116 public static void init () {}
76117
77- public static JumpTable getTable () {
78- // always get the table which has the newest version
79- JumpTable table = tableMap .get (Version .TRON_V1_5 );
80-
81- // next make the corresponding changes, exclude activating opcode
82- if (VMConfig .allowHigherLimitForMaxCpuTimeOfOneTx ()) {
83- adjustMemOperations (table );
84- }
85-
86- if (VMConfig .allowEnergyAdjustment ()) {
87- adjustForFairEnergy (table );
88- }
89-
90- if (VMConfig .allowTvmSelfdestructRestriction ()) {
91- adjustSelfdestruct (table );
92- }
118+ public static JumpTable prepareAndGetTable (boolean isConstantCall ) {
119+ JumpTable table = getTable (isConstantCall );
120+ // Apply configuration-dependent changes once at the top level.
121+ adjustTable (table );
122+ return table ;
123+ }
93124
94- if ( VMConfig . allowTvmOsaka () ) {
95- adjustVoteWitnessCost ( table );
96- }
125+ public static JumpTable getTable ( boolean isConstantCall ) {
126+ return isConstantCall ? CONSTANT_CALL_TABLE : tableMap . get ( LATEST_VERSION );
127+ }
97128
98- return table ;
129+ private static void adjustTable (JumpTable table ) {
130+ // Make the corresponding changes, excluding opcode activation.
131+ adjustMemOperations (table );
132+ adjustVoteWitness (table );
133+ adjustSelfdestruct (table );
99134 }
100135
101136 public static JumpTable newBaseOperationSet () {
@@ -331,20 +366,11 @@ public static JumpTable newBaseOperationSet() {
331366 EnergyCost ::getBaseTierCost ,
332367 OperationActions ::popAction ));
333368
334- table .set (new Operation (
335- Op .MLOAD , 1 , 1 ,
336- EnergyCost ::getMloadCost ,
337- OperationActions ::mLoadAction ));
369+ table .set (DEFAULT_MLOAD );
338370
339- table .set (new Operation (
340- Op .MSTORE , 2 , 0 ,
341- EnergyCost ::getMStoreCost ,
342- OperationActions ::mStoreAction ));
371+ table .set (DEFAULT_MSTORE );
343372
344- table .set (new Operation (
345- Op .MSTORE8 , 2 , 0 ,
346- EnergyCost ::getMStore8Cost ,
347- OperationActions ::mStore8Action ));
373+ table .set (DEFAULT_MSTORE8 );
348374
349375 table .set (new Operation (
350376 Op .SLOAD , 1 , 1 ,
@@ -449,10 +475,7 @@ public static JumpTable newBaseOperationSet() {
449475 EnergyCost ::getRevertCost ,
450476 OperationActions ::revertAction ));
451477
452- table .set (new Operation (
453- Op .SUICIDE , 1 , 0 ,
454- EnergyCost ::getSuicideCost ,
455- OperationActions ::suicideAction ));
478+ table .set (DEFAULT_SUICIDE );
456479
457480 return table ;
458481 }
@@ -570,11 +593,7 @@ public static void appendFreezeOperations(JumpTable table) {
570593 public static void appendVoteOperations (JumpTable table ) {
571594 BooleanSupplier proposal = VMConfig ::allowTvmVote ;
572595
573- table .set (new Operation (
574- Op .VOTEWITNESS , 4 , 1 ,
575- EnergyCost ::getVoteWitnessCost ,
576- OperationActions ::voteWitnessAction ,
577- proposal ));
596+ table .set (DEFAULT_VOTEWITNESS );
578597
579598 table .set (new Operation (
580599 Op .WITHDRAWREWARD , 0 , 1 ,
@@ -593,23 +612,6 @@ public static void appendLondonOperations(JumpTable table) {
593612 proposal ));
594613 }
595614
596- public static void adjustMemOperations (JumpTable table ) {
597- table .set (new Operation (
598- Op .MLOAD , 1 , 1 ,
599- EnergyCost ::getMloadCost2 ,
600- OperationActions ::mLoadAction ));
601-
602- table .set (new Operation (
603- Op .MSTORE , 2 , 0 ,
604- EnergyCost ::getMStoreCost2 ,
605- OperationActions ::mStoreAction ));
606-
607- table .set (new Operation (
608- Op .MSTORE8 , 2 , 0 ,
609- EnergyCost ::getMStore8Cost2 ,
610- OperationActions ::mStore8Action ));
611- }
612-
613615 public static void appendFreezeV2Operations (JumpTable table ) {
614616 BooleanSupplier proposal = VMConfig ::allowTvmFreezeV2 ;
615617
@@ -664,19 +666,6 @@ public static void appendShangHaiOperations(JumpTable table) {
664666 proposal ));
665667 }
666668
667- public static void adjustForFairEnergy (JumpTable table ) {
668- table .set (new Operation (
669- Op .VOTEWITNESS , 4 , 1 ,
670- EnergyCost ::getVoteWitnessCost2 ,
671- OperationActions ::voteWitnessAction ,
672- VMConfig ::allowTvmVote ));
673-
674- table .set (new Operation (
675- Op .SUICIDE , 1 , 0 ,
676- EnergyCost ::getSuicideCost2 ,
677- OperationActions ::suicideAction ));
678- }
679-
680669 public static void appendCancunOperations (JumpTable table ) {
681670 BooleanSupplier proposal = VMConfig ::allowTvmCancun ;
682671 BooleanSupplier tvmBlobProposal = VMConfig ::allowTvmBlob ;
@@ -722,18 +711,30 @@ public static void appendOsakaOperations(JumpTable table) {
722711 proposal ));
723712 }
724713
725- public static void adjustSelfdestruct (JumpTable table ) {
726- table . set ( new Operation (
727- Op . SUICIDE , 1 , 0 ,
728- EnergyCost :: getSuicideCost3 ,
729- OperationActions :: suicideAction2 ) );
714+ public static void adjustMemOperations (JumpTable table ) {
715+ boolean adjusted = VMConfig . allowHigherLimitForMaxCpuTimeOfOneTx ();
716+ table . set ( adjusted ? ADJUSTED_MLOAD : DEFAULT_MLOAD );
717+ table . set ( adjusted ? ADJUSTED_MSTORE : DEFAULT_MSTORE );
718+ table . set ( adjusted ? ADJUSTED_MSTORE8 : DEFAULT_MSTORE8 );
730719 }
731720
732- public static void adjustVoteWitnessCost (JumpTable table ) {
733- table .set (new Operation (
734- Op .VOTEWITNESS , 4 , 1 ,
735- EnergyCost ::getVoteWitnessCost3 ,
736- OperationActions ::voteWitnessAction ,
737- VMConfig ::allowTvmVote ));
721+ public static void adjustVoteWitness (JumpTable table ) {
722+ if (VMConfig .allowTvmOsaka ()) {
723+ table .set (OSAKA_VOTEWITNESS );
724+ } else if (VMConfig .allowEnergyAdjustment ()) {
725+ table .set (ADJUSTED_VOTEWITNESS );
726+ } else {
727+ table .set (DEFAULT_VOTEWITNESS );
728+ }
729+ }
730+
731+ public static void adjustSelfdestruct (JumpTable table ) {
732+ if (VMConfig .allowTvmSelfdestructRestriction ()) {
733+ table .set (RESTRICTED_SUICIDE );
734+ } else if (VMConfig .allowEnergyAdjustment ()) {
735+ table .set (ADJUSTED_SUICIDE );
736+ } else {
737+ table .set (DEFAULT_SUICIDE );
738+ }
738739 }
739740}
0 commit comments