private_state_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package core
  2. import (
  3. "fmt"
  4. "math/big"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/core/types"
  8. "github.com/ethereum/go-ethereum/crypto"
  9. )
  10. // callmsg is the message type used for call transactions in the private state test
  11. type callmsg struct {
  12. addr common.Address
  13. to *common.Address
  14. gas uint64
  15. gasPrice *big.Int
  16. value *big.Int
  17. data []byte
  18. accessList types.AccessList // EIP-2930 access list.
  19. }
  20. // accessor boilerplate to implement core.Message
  21. func (m callmsg) From() common.Address { return m.addr }
  22. func (m callmsg) FromFrontier() common.Address { return m.addr }
  23. func (m callmsg) Nonce() uint64 { return 0 }
  24. func (m callmsg) To() *common.Address { return m.to }
  25. func (m callmsg) GasPrice() *big.Int { return m.gasPrice }
  26. func (m callmsg) Gas() uint64 { return m.gas }
  27. func (m callmsg) Value() *big.Int { return m.value }
  28. func (m callmsg) Data() []byte { return m.data }
  29. func (m callmsg) CheckNonce() bool { return true }
  30. func (m callmsg) AccessList() types.AccessList { return m.accessList }
  31. func ExampleMakeCallHelper() {
  32. var (
  33. // setup new pair of keys for the calls
  34. key, _ = crypto.GenerateKey()
  35. // create a new helper
  36. helper = MakeCallHelper()
  37. )
  38. // Private contract address
  39. prvContractAddr := common.Address{1}
  40. // Initialise custom code for private contract
  41. helper.PrivateState.SetCode(prvContractAddr, common.Hex2Bytes("600a60005500"))
  42. // Public contract address
  43. pubContractAddr := common.Address{2}
  44. // Initialise custom code for public contract
  45. helper.PublicState.SetCode(pubContractAddr, common.Hex2Bytes("601460005500"))
  46. // Make a call to the private contract
  47. err := helper.MakeCall(true, key, prvContractAddr, nil)
  48. if err != nil {
  49. fmt.Println(err)
  50. }
  51. // Make a call to the public contract
  52. err = helper.MakeCall(false, key, pubContractAddr, nil)
  53. if err != nil {
  54. fmt.Println(err)
  55. }
  56. // Output:
  57. // Private: 10
  58. // Public: 20
  59. fmt.Println("Private:", helper.PrivateState.GetState(prvContractAddr, common.Hash{}).Big())
  60. fmt.Println("Public:", helper.PublicState.GetState(pubContractAddr, common.Hash{}).Big())
  61. }
  62. // 600a600055600060006001a1
  63. // 60 0a, 60 00, 55, 60 00, 60 00, 60 01, a1
  64. // [1] (0x60) PUSH1 0x0a (store value)
  65. // [3] (0x60) PUSH1 0x00 (store addr)
  66. // [4] (0x55) SSTORE (Store (k-00,v-a))
  67. // [6] (0x60) PUSH1 0x00
  68. // [8] (0x60) PUSH1 0x00
  69. // [10](0x60) PUSH1 0x01
  70. // [11](0xa1) LOG1 offset(0x01), len(0x00), topic(0x00)
  71. //
  72. // Store then log
  73. func TestPrivateTransaction(t *testing.T) {
  74. var (
  75. key, _ = crypto.GenerateKey()
  76. helper = MakeCallHelper()
  77. privateState = helper.PrivateState
  78. publicState = helper.PublicState
  79. )
  80. prvContractAddr := common.Address{1}
  81. pubContractAddr := common.Address{2}
  82. // SSTORE (K,V) SSTORE(0, 10): 600a600055
  83. // +
  84. // LOG1 OFFSET LEN TOPIC, LOG1 (a1) 01, 00, 00: 600060006001a1
  85. privateState.SetCode(prvContractAddr, common.Hex2Bytes("600a600055600060006001a1"))
  86. // SSTORE (K,V) SSTORE(0, 14): 6014600055
  87. publicState.SetCode(pubContractAddr, common.Hex2Bytes("6014600055"))
  88. if publicState.Exist(prvContractAddr) {
  89. t.Error("didn't expect private contract address to exist on public state")
  90. }
  91. // Private transaction 1
  92. err := helper.MakeCall(true, key, prvContractAddr, nil)
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. stateEntry := privateState.GetState(prvContractAddr, common.Hash{}).Big()
  97. if stateEntry.Cmp(big.NewInt(10)) != 0 {
  98. t.Error("expected state to have 10, got", stateEntry)
  99. }
  100. if len(privateState.Logs()) != 1 {
  101. t.Error("expected private state to have 1 log, got", len(privateState.Logs()))
  102. }
  103. if len(publicState.Logs()) != 0 {
  104. t.Error("expected public state to have 0 logs, got", len(publicState.Logs()))
  105. }
  106. if publicState.Exist(prvContractAddr) {
  107. t.Error("didn't expect private contract address to exist on public state")
  108. }
  109. if !privateState.Exist(prvContractAddr) {
  110. t.Error("expected private contract address to exist on private state")
  111. }
  112. // Public transaction 1
  113. err = helper.MakeCall(false, key, pubContractAddr, nil)
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. stateEntry = publicState.GetState(pubContractAddr, common.Hash{}).Big()
  118. if stateEntry.Cmp(big.NewInt(20)) != 0 {
  119. t.Error("expected state to have 20, got", stateEntry)
  120. }
  121. // Private transaction 2
  122. helper.MakeCall(true, key, prvContractAddr, nil)
  123. stateEntry = privateState.GetState(prvContractAddr, common.Hash{}).Big()
  124. if stateEntry.Cmp(big.NewInt(10)) != 0 {
  125. t.Error("expected state to have 10, got", stateEntry)
  126. }
  127. if publicState.Exist(prvContractAddr) {
  128. t.Error("didn't expect private contract address to exist on public state")
  129. }
  130. if privateState.Exist(pubContractAddr) {
  131. t.Error("didn't expect public contract address to exist on private state")
  132. }
  133. }