Skip to content

Commit 8235149

Browse files
[FAB-13795] Contract definition
Change-Id: I848cd04986a3c3d70d7d1930021c7bfa8075d51e Signed-off-by: gennady <[email protected]>
1 parent 762a955 commit 8235149

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright IBM Corp., DTCC All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package org.hyperledger.fabric.contract;
8+
9+
import org.hyperledger.fabric.shim.ChaincodeStub;
10+
11+
/**
12+
* Interface all contracts should implement
13+
*/
14+
public interface ContractInterface {
15+
/**
16+
* Returns instance of context associated with current invocation
17+
* @return
18+
*/
19+
default Context getContext() { throw new IllegalStateException("getContext default implementation can't be directly invoked"); }
20+
21+
/**
22+
* Create context from {@link ChaincodeStub}, default impl provided, but can be overwritten by contract
23+
* @param stub
24+
* @return
25+
*/
26+
default Context createContext(ChaincodeStub stub) { return ContextFactory.getInstance().createContext(stub); }
27+
28+
/**
29+
* Invoked once method for transaction not exist in contract
30+
*/
31+
default void unknownTransaction() {
32+
throw new IllegalStateException("Undefined contract method called");
33+
}
34+
35+
/**
36+
* Invoke before each transaction method
37+
*/
38+
default void beforeTransaction() {}
39+
40+
/**
41+
* Invoke after each transaction method
42+
*/
43+
default void afterTransaction() {}
44+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright IBM Corp., DTCC All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package org.hyperledger.fabric.contract.annotation;
8+
9+
import io.swagger.v3.oas.annotations.info.Info;
10+
11+
import java.lang.annotation.ElementType;
12+
import java.lang.annotation.Retention;
13+
import java.lang.annotation.Target;
14+
15+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
16+
17+
/**
18+
* Class level annotation that identifies this class as being a contract.
19+
*/
20+
@Retention(RUNTIME)
21+
@Target(ElementType.TYPE)
22+
public @interface Contract {
23+
Info info();
24+
String namespace() default "";
25+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
package org.hyperledger.fabric.contract;
7+
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.junit.rules.ExpectedException;
11+
12+
import static org.hamcrest.Matchers.*;
13+
import static org.junit.Assert.*;
14+
15+
public class ContractInterfaceTest {
16+
@Rule
17+
public ExpectedException thrown = ExpectedException.none();
18+
19+
@Test
20+
public void getContext() {
21+
thrown.expect(IllegalStateException.class);
22+
thrown.expectMessage("getContext default implementation can't be directly invoked");
23+
new ContractInterface() {}.getContext();
24+
}
25+
26+
@Test
27+
public void createContext() {
28+
assertThat((new ContractInterface(){}).createContext(new ChaincodeStubNaiveImpl()), is(instanceOf(Context.class)));
29+
}
30+
31+
@Test
32+
public void unknownTransaction() {
33+
thrown.expect(IllegalStateException.class);
34+
thrown.expectMessage("Undefined contract method called");
35+
new ContractInterface() {}.unknownTransaction();
36+
}
37+
38+
@Test
39+
public void beforeTransaction() {
40+
new ContractInterface() {}.beforeTransaction();
41+
}
42+
43+
@Test
44+
public void afterTransaction() {
45+
new ContractInterface() {}.afterTransaction();
46+
}
47+
}

0 commit comments

Comments
 (0)