11const web3Utils = require ( "web3-utils" ) ;
22
33class Injector {
4- constructor ( ) {
4+ constructor ( viaIR ) {
5+ this . viaIR = viaIR ;
56 this . hashCounter = 0 ;
67 this . modifierCounter = 0 ;
78 this . modifiers = { } ;
@@ -23,7 +24,9 @@ class Injector {
2324 case 'modifier' :
2425 return ` ${ this . _getModifierIdentifier ( id ) } ` ;
2526 default :
26- return `${ this . _getDefaultMethodIdentifier ( id ) } (${ hash } ); /* ${ type } */ \n` ;
27+ return ( this . viaIR )
28+ ? `${ this . _getAbiEncodeStatementHash ( hash ) } /* ${ type } */ \n`
29+ : `${ this . _getDefaultMethodIdentifier ( id ) } (${ hash } ); /* ${ type } */ \n` ;
2730 }
2831 }
2932
@@ -51,6 +54,16 @@ class Injector {
5154 return `c_mod${ web3Utils . keccak256 ( id ) . slice ( 2 , 10 ) } `
5255 }
5356
57+ // Way to get hash on the stack with viaIR (which seems to ignore abi.encode builtin)
58+ // Tested with v0.8.17, v0.8.24
59+ _getAbiEncodeStatementHash ( hash ) {
60+ return `abi.encode(${ hash } ); `
61+ }
62+
63+ _getAbiEncodeStatementVar ( hash ) {
64+ return `abi.encode(c__${ hash } ); `
65+ }
66+
5467 _getInjectionComponents ( contract , injectionPoint , id , type ) {
5568 const { start, end } = this . _split ( contract , injectionPoint ) ;
5669 const hash = this . _getHash ( id )
@@ -73,7 +86,10 @@ class Injector {
7386 _getDefaultMethodDefinition ( id ) {
7487 const hash = web3Utils . keccak256 ( id ) . slice ( 2 , 10 ) ;
7588 const method = this . _getDefaultMethodIdentifier ( id ) ;
76- return `\nfunction ${ method } (bytes8 c__${ hash } ) internal pure {}\n` ;
89+
90+ return ( this . viaIR )
91+ ? ``
92+ : `\nfunction ${ method } (bytes8 c__${ hash } ) internal pure {}\n` ;
7793 }
7894
7995 /**
@@ -85,7 +101,11 @@ class Injector {
85101 _getFileScopedHashMethodDefinition ( id , contract ) {
86102 const hash = web3Utils . keccak256 ( id ) . slice ( 2 , 10 ) ;
87103 const method = this . _getDefaultMethodIdentifier ( id ) ;
88- return `\nfunction ${ method } (bytes8 c__${ hash } ) pure {}\n` ;
104+ const abi = this . _getAbiEncodeStatementVar ( hash ) ;
105+
106+ return ( this . viaIR )
107+ ? `\nfunction ${ method } (bytes8 c__${ hash } ) pure { ${ abi } }\n`
108+ : `\nfunction ${ method } (bytes8 c__${ hash } ) pure {}\n` ;
89109 }
90110
91111 /**
@@ -97,7 +117,11 @@ class Injector {
97117 _getTrueMethodDefinition ( id ) {
98118 const hash = web3Utils . keccak256 ( id ) . slice ( 2 , 10 ) ;
99119 const method = this . _getTrueMethodIdentifier ( id ) ;
100- return `function ${ method } (bytes8 c__${ hash } ) internal pure returns (bool){ return true; }\n` ;
120+ const abi = this . _getAbiEncodeStatementVar ( hash ) ;
121+
122+ return ( this . viaIR )
123+ ? `function ${ method } (bytes8 c__${ hash } ) internal pure returns (bool){ ${ abi } return true; }\n`
124+ : `function ${ method } (bytes8 c__${ hash } ) internal pure returns (bool){ return true; }\n` ;
101125 }
102126
103127 /**
@@ -110,7 +134,11 @@ class Injector {
110134 _getFileScopeTrueMethodDefinition ( id ) {
111135 const hash = web3Utils . keccak256 ( id ) . slice ( 2 , 10 ) ;
112136 const method = this . _getTrueMethodIdentifier ( id ) ;
113- return `function ${ method } (bytes8 c__${ hash } ) pure returns (bool){ return true; }\n` ;
137+ const abi = this . _getAbiEncodeStatementVar ( hash ) ;
138+
139+ return ( this . viaIR )
140+ ? `function ${ method } (bytes8 c__${ hash } ) pure returns (bool){ ${ abi } return true; }\n`
141+ : `function ${ method } (bytes8 c__${ hash } ) pure returns (bool){ return true; }\n` ;
114142 }
115143
116144 /**
@@ -122,7 +150,11 @@ class Injector {
122150 _getFalseMethodDefinition ( id ) {
123151 const hash = web3Utils . keccak256 ( id ) . slice ( 2 , 10 ) ;
124152 const method = this . _getFalseMethodIdentifier ( id ) ;
125- return `function ${ method } (bytes8 c__${ hash } ) internal pure returns (bool){ return false; }\n` ;
153+ const abi = this . _getAbiEncodeStatementVar ( hash ) ;
154+
155+ return ( this . viaIR )
156+ ? `function ${ method } (bytes8 c__${ hash } ) internal pure returns (bool){ ${ abi } return false; }\n`
157+ : `function ${ method } (bytes8 c__${ hash } ) internal pure returns (bool){ return false; }\n` ;
126158 }
127159
128160 /**
@@ -135,7 +167,11 @@ class Injector {
135167 _getFileScopedFalseMethodDefinition ( id ) {
136168 const hash = web3Utils . keccak256 ( id ) . slice ( 2 , 10 ) ;
137169 const method = this . _getFalseMethodIdentifier ( id ) ;
138- return `function ${ method } (bytes8 c__${ hash } ) pure returns (bool){ return false; }\n` ;
170+ const abi = this . _getAbiEncodeStatementVar ( hash ) ;
171+
172+ return ( this . viaIR )
173+ ? `function ${ method } (bytes8 c__${ hash } ) pure returns (bool){ ${ abi } return false; }\n`
174+ : `function ${ method } (bytes8 c__${ hash } ) pure returns (bool){ return false; }\n` ;
139175 }
140176
141177 _getModifierDefinitions ( contractId , instrumentation ) {
0 commit comments