Skip to content

Commit d5f2482

Browse files
committed
Some insertion.
1 parent ded14b2 commit d5f2482

File tree

2 files changed

+159
-14
lines changed

2 files changed

+159
-14
lines changed

linera-execution/solidity/Linera.sol

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ library Linera {
2727
return ChainId(entry.value.value);
2828
}
2929

30+
function chainid_to(Linera.ChainId memory entry)
31+
internal
32+
pure
33+
returns (LineraTypes.ChainId memory)
34+
{
35+
LineraTypes.CryptoHash memory hash = LineraTypes.CryptoHash(entry.value);
36+
return LineraTypes.ChainId(hash);
37+
}
38+
3039
struct AccountOwner {
3140
uint8 choice;
3241
// choice=0 corresponds to Reserved
@@ -54,6 +63,16 @@ library Linera {
5463
return LineraTypes.AccountOwner(owner.choice, owner.reserved, hash, owner.address20);
5564
}
5665

66+
function account_to(Linera.Account memory account)
67+
internal
68+
pure
69+
returns (LineraTypes.Account memory)
70+
{
71+
LineraTypes.ChainId memory chain_id = chainid_to(account.chain_id);
72+
LineraTypes.AccountOwner memory owner = accountowner_to(account.owner);
73+
return LineraTypes.Account(chain_id, owner);
74+
}
75+
5776
struct AccountOwnerBalance {
5877
AccountOwner account_owner;
5978
uint256 balance;
@@ -551,6 +570,18 @@ library Linera {
551570
return opt_uint32_from(output2);
552571
}
553572

573+
function transfer(Linera.AccountOwner account, uint256 amount) internal {
574+
address precompile = address(0x0b);
575+
LineraTypes.AccountOwner account2 = account_to(account);
576+
LineraTypes.ContractRuntimePrecompile_Transfer memory transfer_ = LineraTypes.ContractRuntimePrecompile_Transfer(account2, amount);
577+
LineraTypes.ContractRuntimePrecompile memory contract_ = LineraTypes.ContractRuntimePrecompile_case_transfer(transfer_);
578+
LineraTypes.RuntimePrecompile memory input1 = LineraTypes.RuntimePrecompile_case_contract(contract_);
579+
bytes memory input2 = LineraTypes.bcs_serialize_RuntimePrecompile(input1);
580+
(bool success, bytes memory output) = precompile.call(input2);
581+
require(success);
582+
require(output.length == 0);
583+
}
584+
554585
// ServiceRuntime functions.
555586

556587
function try_query_application(bytes32 universal_address, bytes memory argument) internal returns (bytes memory) {

linera-execution/solidity/LineraTypes.sol

Lines changed: 128 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,45 @@ library LineraTypes {
5151
return (0,0);
5252
}
5353

54+
struct Account {
55+
ChainId chain_id;
56+
AccountOwner owner;
57+
}
58+
59+
function bcs_serialize_Account(Account memory input)
60+
internal
61+
pure
62+
returns (bytes memory)
63+
{
64+
bytes memory result = bcs_serialize_ChainId(input.chain_id);
65+
return abi.encodePacked(result, bcs_serialize_AccountOwner(input.owner));
66+
}
67+
68+
function bcs_deserialize_offset_Account(uint256 pos, bytes memory input)
69+
internal
70+
pure
71+
returns (uint256, Account memory)
72+
{
73+
uint256 new_pos;
74+
ChainId memory chain_id;
75+
(new_pos, chain_id) = bcs_deserialize_offset_ChainId(pos, input);
76+
AccountOwner memory owner;
77+
(new_pos, owner) = bcs_deserialize_offset_AccountOwner(new_pos, input);
78+
return (new_pos, Account(chain_id, owner));
79+
}
80+
81+
function bcs_deserialize_Account(bytes memory input)
82+
internal
83+
pure
84+
returns (Account memory)
85+
{
86+
uint256 new_pos;
87+
Account memory value;
88+
(new_pos, value) = bcs_deserialize_offset_Account(0, input);
89+
require(new_pos == input.length, "incomplete deserialization");
90+
return value;
91+
}
92+
5493
struct AccountOwner {
5594
uint8 choice;
5695
// choice=0 corresponds to Reserved
@@ -584,6 +623,8 @@ library LineraTypes {
584623
// choice=10 corresponds to QueryService
585624
ContractRuntimePrecompile_QueryService query_service;
586625
// choice=11 corresponds to ValidationRound
626+
// choice=12 corresponds to Transfer
627+
ContractRuntimePrecompile_Transfer transfer_;
587628
}
588629

589630
function ContractRuntimePrecompile_case_authenticated_owner()
@@ -598,7 +639,8 @@ library LineraTypes {
598639
ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events;
599640
ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events;
600641
ContractRuntimePrecompile_QueryService memory query_service;
601-
return ContractRuntimePrecompile(uint8(0), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service);
642+
ContractRuntimePrecompile_Transfer memory transfer_;
643+
return ContractRuntimePrecompile(uint8(0), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
602644
}
603645

604646
function ContractRuntimePrecompile_case_message_origin_chain_id()
@@ -613,7 +655,8 @@ library LineraTypes {
613655
ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events;
614656
ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events;
615657
ContractRuntimePrecompile_QueryService memory query_service;
616-
return ContractRuntimePrecompile(uint8(1), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service);
658+
ContractRuntimePrecompile_Transfer memory transfer_;
659+
return ContractRuntimePrecompile(uint8(1), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
617660
}
618661

619662
function ContractRuntimePrecompile_case_message_is_bouncing()
@@ -628,7 +671,8 @@ library LineraTypes {
628671
ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events;
629672
ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events;
630673
ContractRuntimePrecompile_QueryService memory query_service;
631-
return ContractRuntimePrecompile(uint8(2), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service);
674+
ContractRuntimePrecompile_Transfer memory transfer_;
675+
return ContractRuntimePrecompile(uint8(2), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
632676
}
633677

634678
function ContractRuntimePrecompile_case_authenticated_caller_id()
@@ -643,7 +687,8 @@ library LineraTypes {
643687
ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events;
644688
ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events;
645689
ContractRuntimePrecompile_QueryService memory query_service;
646-
return ContractRuntimePrecompile(uint8(3), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service);
690+
ContractRuntimePrecompile_Transfer memory transfer_;
691+
return ContractRuntimePrecompile(uint8(3), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
647692
}
648693

649694
function ContractRuntimePrecompile_case_send_message(ContractRuntimePrecompile_SendMessage memory send_message)
@@ -657,7 +702,8 @@ library LineraTypes {
657702
ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events;
658703
ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events;
659704
ContractRuntimePrecompile_QueryService memory query_service;
660-
return ContractRuntimePrecompile(uint8(4), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service);
705+
ContractRuntimePrecompile_Transfer memory transfer_;
706+
return ContractRuntimePrecompile(uint8(4), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
661707
}
662708

663709
function ContractRuntimePrecompile_case_try_call_application(ContractRuntimePrecompile_TryCallApplication memory try_call_application)
@@ -671,7 +717,8 @@ library LineraTypes {
671717
ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events;
672718
ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events;
673719
ContractRuntimePrecompile_QueryService memory query_service;
674-
return ContractRuntimePrecompile(uint8(5), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service);
720+
ContractRuntimePrecompile_Transfer memory transfer_;
721+
return ContractRuntimePrecompile(uint8(5), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
675722
}
676723

677724
function ContractRuntimePrecompile_case_emit(ContractRuntimePrecompile_Emit memory emit_)
@@ -685,7 +732,8 @@ library LineraTypes {
685732
ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events;
686733
ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events;
687734
ContractRuntimePrecompile_QueryService memory query_service;
688-
return ContractRuntimePrecompile(uint8(6), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service);
735+
ContractRuntimePrecompile_Transfer memory transfer_;
736+
return ContractRuntimePrecompile(uint8(6), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
689737
}
690738

691739
function ContractRuntimePrecompile_case_read_event(ContractRuntimePrecompile_ReadEvent memory read_event)
@@ -699,7 +747,8 @@ library LineraTypes {
699747
ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events;
700748
ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events;
701749
ContractRuntimePrecompile_QueryService memory query_service;
702-
return ContractRuntimePrecompile(uint8(7), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service);
750+
ContractRuntimePrecompile_Transfer memory transfer_;
751+
return ContractRuntimePrecompile(uint8(7), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
703752
}
704753

705754
function ContractRuntimePrecompile_case_subscribe_to_events(ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events)
@@ -713,7 +762,8 @@ library LineraTypes {
713762
ContractRuntimePrecompile_ReadEvent memory read_event;
714763
ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events;
715764
ContractRuntimePrecompile_QueryService memory query_service;
716-
return ContractRuntimePrecompile(uint8(8), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service);
765+
ContractRuntimePrecompile_Transfer memory transfer_;
766+
return ContractRuntimePrecompile(uint8(8), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
717767
}
718768

719769
function ContractRuntimePrecompile_case_unsubscribe_from_events(ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events)
@@ -727,7 +777,8 @@ library LineraTypes {
727777
ContractRuntimePrecompile_ReadEvent memory read_event;
728778
ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events;
729779
ContractRuntimePrecompile_QueryService memory query_service;
730-
return ContractRuntimePrecompile(uint8(9), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service);
780+
ContractRuntimePrecompile_Transfer memory transfer_;
781+
return ContractRuntimePrecompile(uint8(9), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
731782
}
732783

733784
function ContractRuntimePrecompile_case_query_service(ContractRuntimePrecompile_QueryService memory query_service)
@@ -741,7 +792,8 @@ library LineraTypes {
741792
ContractRuntimePrecompile_ReadEvent memory read_event;
742793
ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events;
743794
ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events;
744-
return ContractRuntimePrecompile(uint8(10), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service);
795+
ContractRuntimePrecompile_Transfer memory transfer_;
796+
return ContractRuntimePrecompile(uint8(10), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
745797
}
746798

747799
function ContractRuntimePrecompile_case_validation_round()
@@ -756,7 +808,23 @@ library LineraTypes {
756808
ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events;
757809
ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events;
758810
ContractRuntimePrecompile_QueryService memory query_service;
759-
return ContractRuntimePrecompile(uint8(11), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service);
811+
ContractRuntimePrecompile_Transfer memory transfer_;
812+
return ContractRuntimePrecompile(uint8(11), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
813+
}
814+
815+
function ContractRuntimePrecompile_case_transfer(ContractRuntimePrecompile_Transfer memory transfer_)
816+
internal
817+
pure
818+
returns (ContractRuntimePrecompile memory)
819+
{
820+
ContractRuntimePrecompile_SendMessage memory send_message;
821+
ContractRuntimePrecompile_TryCallApplication memory try_call_application;
822+
ContractRuntimePrecompile_Emit memory emit_;
823+
ContractRuntimePrecompile_ReadEvent memory read_event;
824+
ContractRuntimePrecompile_SubscribeToEvents memory subscribe_to_events;
825+
ContractRuntimePrecompile_UnsubscribeFromEvents memory unsubscribe_from_events;
826+
ContractRuntimePrecompile_QueryService memory query_service;
827+
return ContractRuntimePrecompile(uint8(12), send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_);
760828
}
761829

762830
function bcs_serialize_ContractRuntimePrecompile(ContractRuntimePrecompile memory input)
@@ -785,6 +853,9 @@ library LineraTypes {
785853
if (input.choice == 10) {
786854
return abi.encodePacked(input.choice, bcs_serialize_ContractRuntimePrecompile_QueryService(input.query_service));
787855
}
856+
if (input.choice == 12) {
857+
return abi.encodePacked(input.choice, bcs_serialize_ContractRuntimePrecompile_Transfer(input.transfer_));
858+
}
788859
return abi.encodePacked(input.choice);
789860
}
790861

@@ -824,8 +895,12 @@ library LineraTypes {
824895
if (choice == 10) {
825896
(new_pos, query_service) = bcs_deserialize_offset_ContractRuntimePrecompile_QueryService(new_pos, input);
826897
}
827-
require(choice < 12);
828-
return (new_pos, ContractRuntimePrecompile(choice, send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service));
898+
ContractRuntimePrecompile_Transfer memory transfer_;
899+
if (choice == 12) {
900+
(new_pos, transfer_) = bcs_deserialize_offset_ContractRuntimePrecompile_Transfer(new_pos, input);
901+
}
902+
require(choice < 13);
903+
return (new_pos, ContractRuntimePrecompile(choice, send_message, try_call_application, emit_, read_event, subscribe_to_events, unsubscribe_from_events, query_service, transfer_));
829904
}
830905

831906
function bcs_deserialize_ContractRuntimePrecompile(bytes memory input)
@@ -1043,6 +1118,45 @@ library LineraTypes {
10431118
return value;
10441119
}
10451120

1121+
struct ContractRuntimePrecompile_Transfer {
1122+
Account account;
1123+
Amount amount;
1124+
}
1125+
1126+
function bcs_serialize_ContractRuntimePrecompile_Transfer(ContractRuntimePrecompile_Transfer memory input)
1127+
internal
1128+
pure
1129+
returns (bytes memory)
1130+
{
1131+
bytes memory result = bcs_serialize_Account(input.account);
1132+
return abi.encodePacked(result, bcs_serialize_Amount(input.amount));
1133+
}
1134+
1135+
function bcs_deserialize_offset_ContractRuntimePrecompile_Transfer(uint256 pos, bytes memory input)
1136+
internal
1137+
pure
1138+
returns (uint256, ContractRuntimePrecompile_Transfer memory)
1139+
{
1140+
uint256 new_pos;
1141+
Account memory account;
1142+
(new_pos, account) = bcs_deserialize_offset_Account(pos, input);
1143+
Amount memory amount;
1144+
(new_pos, amount) = bcs_deserialize_offset_Amount(new_pos, input);
1145+
return (new_pos, ContractRuntimePrecompile_Transfer(account, amount));
1146+
}
1147+
1148+
function bcs_deserialize_ContractRuntimePrecompile_Transfer(bytes memory input)
1149+
internal
1150+
pure
1151+
returns (ContractRuntimePrecompile_Transfer memory)
1152+
{
1153+
uint256 new_pos;
1154+
ContractRuntimePrecompile_Transfer memory value;
1155+
(new_pos, value) = bcs_deserialize_offset_ContractRuntimePrecompile_Transfer(0, input);
1156+
require(new_pos == input.length, "incomplete deserialization");
1157+
return value;
1158+
}
1159+
10461160
struct ContractRuntimePrecompile_TryCallApplication {
10471161
ApplicationId target;
10481162
bytes argument;

0 commit comments

Comments
 (0)