Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Sep 4, 2023
2 parents 679755a + 80a6f98 commit a8bacfb
Show file tree
Hide file tree
Showing 21 changed files with 11,620 additions and 2,489 deletions.
15 changes: 6 additions & 9 deletions contracts/contractsv02.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ func (p *RPCProvider) deployAndWaitWithWallet(ctx context.Context, compiledClass
if err := json.Unmarshal(compiledClass, &class); err != nil {
return nil, err
}
fmt.Println("a")

saltFelt, err := utils.HexToFelt(salt)
if err != nil {
return nil, err
Expand All @@ -125,15 +123,14 @@ func (p *RPCProvider) deployAndWaitWithWallet(ctx context.Context, compiledClass
}

// TODO: use UDC via account
tx, err := provider.AddDeployTransaction(ctx, rpc.BroadcastedDeployTxn{
DeployTransactionProperties: rpc.DeployTransactionProperties{
Version: rpc.TransactionV1,
ContractAddressSalt: saltFelt,
ConstructorCalldata: inputsFelt,
tx, err := provider.AddDeployAccountTransaction(ctx, rpc.BroadcastedDeployAccountTransaction{
BroadcastedTxnCommonProperties: rpc.BroadcastedTxnCommonProperties{
Version: rpc.TransactionV1,
},
DeprecatedContractClass: class,
ContractAddressSalt: saltFelt,
ConstructorCalldata: inputsFelt,
})
fmt.Println("b")

if err != nil {
return nil, err
}
Expand Down
21 changes: 20 additions & 1 deletion rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func WithBlockTag(tag string) BlockID {
}

// BlockWithTxHashes gets block information given the block id.
// TODO: add support for PendingBlock
func (provider *Provider) BlockWithTxHashes(ctx context.Context, blockID BlockID) (interface{}, error) {
var result Block
if err := do(ctx, provider.c, "starknet_getBlockWithTxHashes", &result, blockID); err != nil {
Expand All @@ -59,6 +58,17 @@ func (provider *Provider) BlockWithTxHashes(ctx context.Context, blockID BlockID
}
return nil, err
}

// if header.Hash == nil it's a pending block
if result.BlockHeader.BlockHash == nil {
return PendingBlock{
ParentHash: result.ParentHash,
Timestamp: result.Timestamp,
SequencerAddress: result.SequencerAddress,
Transactions: result.Transactions,
}, nil
}

return &result, nil
}

Expand Down Expand Up @@ -95,5 +105,14 @@ func (provider *Provider) BlockWithTxs(ctx context.Context, blockID BlockID) (in
}
return nil, err
}
// if header.Hash == nil it's a pending block
if result.BlockHeader.BlockHash == nil {
return PendingBlock{
ParentHash: result.ParentHash,
Timestamp: result.Timestamp,
SequencerAddress: result.SequencerAddress,
Transactions: result.Transactions,
}, nil
}
return &result, nil
}
154 changes: 69 additions & 85 deletions rpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ func TestBlockWithTxHashes(t *testing.T) {
testConfig := beforeEach(t)

type testSetType struct {
BlockID BlockID
ExpectedError error
ExpectedBlockWithTxHashes *Block
BlockID BlockID
ExpectedError error
ExpectedBlockWithTxHashes *Block
ExpectedPendingBlockWithTxHashes *PendingBlock
}

var blockGoerli310370 = Block{
Expand Down Expand Up @@ -118,7 +119,24 @@ func TestBlockWithTxHashes(t *testing.T) {
}

testSet := map[string][]testSetType{
"mock": {},
"mock": {{
BlockID: BlockID{Tag: "latest"},
ExpectedPendingBlockWithTxHashes: &PendingBlock{
ParentHash: &felt.Zero,
Timestamp: 123,
SequencerAddress: &felt.Zero,
},
},
{
BlockID: BlockID{Hash: &felt.Zero},
ExpectedBlockWithTxHashes: &Block{
BlockHeader: BlockHeader{
ParentHash: &felt.Zero,
Timestamp: 123,
SequencerAddress: &felt.Zero},
Status: BlockStatus_AcceptedOnL1,
},
}},
"testnet": {
{
BlockID: WithBlockTag("latest"),
Expand All @@ -145,29 +163,36 @@ func TestBlockWithTxHashes(t *testing.T) {
if err != test.ExpectedError {
t.Fatal("BlockWithTxHashes match the expected error:", err)
}
block, ok := result.(*Block)
if !ok {
t.Fatalf("should return *Block, instead: %T\n", result)
}
if test.ExpectedError != nil {
continue
}
if !strings.HasPrefix(block.BlockHash.String(), "0x") {
t.Fatal("Block Hash should start with \"0x\", instead", block.BlockHash)
}

if len(block.Transactions) == 0 {
t.Fatal("the number of transaction should not be 0")
}

if test.ExpectedBlockWithTxHashes != nil {
if (*test.ExpectedBlockWithTxHashes).BlockHash == &felt.Zero {
switch resultBlock := result.(type) {
case Block:
block, ok := result.(*Block)
if !ok {
t.Fatalf("should return *Block, instead: %T\n", result)
}
if test.ExpectedError != nil {
continue
}
if !strings.HasPrefix(block.BlockHash.String(), "0x") {
t.Fatal("Block Hash should start with \"0x\", instead", block.BlockHash)
}

if !cmp.Equal(*test.ExpectedBlockWithTxHashes, *block) {
t.Fatalf("the expected transaction blocks to match, instead: %s", cmp.Diff(test.ExpectedBlockWithTxHashes, block))
if len(block.Transactions) == 0 {
t.Fatal("the number of transaction should not be 0")
}

if test.ExpectedBlockWithTxHashes != nil {
if (*test.ExpectedBlockWithTxHashes).BlockHash == &felt.Zero {
continue
}

if !cmp.Equal(*test.ExpectedBlockWithTxHashes, *block) {
t.Fatalf("the expected transaction blocks to match, instead: %s", cmp.Diff(test.ExpectedBlockWithTxHashes, block))
}
}
case PendingBlock:
require.Equal(t, resultBlock.ParentHash, test.ExpectedPendingBlockWithTxHashes.ParentHash, "Error in PendingBlock ParentHash")
require.Equal(t, resultBlock.SequencerAddress, test.ExpectedPendingBlockWithTxHashes.SequencerAddress, "Error in PendingBlock SequencerAddress")
require.Equal(t, resultBlock.Timestamp, test.ExpectedPendingBlockWithTxHashes.Timestamp, "Error in PendingBlock Timestamp")
}

}
Expand Down Expand Up @@ -312,58 +337,32 @@ func TestBlockWithTxsAndDeployOrDeclare(t *testing.T) {
ExpectedBlockWithTxs *Block
}

var fullBlockGoerli310843 = Block{
BlockHeader: BlockHeader{
BlockHash: utils.TestHexToFelt(t, "0x424fba26a7760b63895abe0c366c2d254cb47090c6f9e91ba2b3fa0824d4fc9"),
ParentHash: utils.TestHexToFelt(t, "0x30e34dedf00bb35a9076b2b0f50a5a74fd2501f62094b6e687277be6ef3d444"),
SequencerAddress: utils.TestHexToFelt(t, "0x46a89ae102987331d369645031b49c27738ed096f2789c24449966da4c6de6b"),
BlockNumber: 310843,
NewRoot: utils.TestHexToFelt(t, "0x32bd4ff21288c898d4d3b6a7aea4ebdb3f1c7089cd52bde98316b4ecb8a50be"),
Timestamp: 1661486036,
},
Status: "ACCEPTED_ON_L1",
Transactions: []Transaction{
DeployTxn{
TransactionHash: utils.TestHexToFelt(t, "0x35bd2978d2061b3463498f83c09322ed6a82e4b2a188506525e272a7adcdf6a"),
ClassHash: utils.TestHexToFelt(t, "0x1ca349f9721a2bf05012bb475b404313c497ca7d6d5f80c03e98ff31e9867f5"),
DeployTransactionProperties: DeployTransactionProperties{
ConstructorCalldata: []*felt.Felt{
utils.TestHexToFelt(t, "0x31ad196615d50956d98be085eb1774624106a6936c7c38696e730d2a6df735a"),
utils.TestHexToFelt(t, "0x736affc32af71f8d361c855b38ffef58ec151bd8361a3b160017b90ada1068e"),
},
ContractAddressSalt: utils.TestHexToFelt(t, "0x4241e90ee6a33a1e2e70b088f7e4acfb3d6630964c1a85e96fa96acd56dcfcf"),
// To do : re-add test for deploy account transaction

Type: "DEPLOY",
Version: TransactionV0,
},
},
},
}

var fullBlockGoerli300114 = Block{
var fullBlockGoerli848634 = Block{
BlockHeader: BlockHeader{
BlockHash: utils.TestHexToFelt(t, "0x184268bfbce24766fa53b65c9c8b30b295e145e8281d543a015b46308e27fdf"),
ParentHash: utils.TestHexToFelt(t, "0x7307cb0d7fa65c111e71cdfb6209bdc90d2454d4c0f34d8bf5a3fe477826c3c"),
SequencerAddress: utils.TestHexToFelt(t, "0x46a89ae102987331d369645031b49c27738ed096f2789c24449966da4c6de6b"),
BlockNumber: 300114,
NewRoot: utils.TestHexToFelt(t, "0x239a44410e78665f41f7a65ef3b5ed244ce411965498a83f80f904e22df1045"),
Timestamp: 1660701246,
BlockHash: utils.TestHexToFelt(t, "0x684f3d864e91543bc02e8f1cad53750faf91dae7a7d8a5337d3710cbf7afb3a"),
ParentHash: utils.TestHexToFelt(t, "0x1848e859df691858f9ef2a84d0060de81080f2a5873f76827002512c87eeb96"),
SequencerAddress: utils.TestHexToFelt(t, "0x1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8"),
BlockNumber: 848634,
NewRoot: utils.TestHexToFelt(t, "00edc1cee04b4c7d4ff301151be08aea818ab03edcfe871de824eb3e8b0a729f"),
Timestamp: 1692418503,
},
Status: "ACCEPTED_ON_L1",
Transactions: []Transaction{
DeclareTxn{
CommonTransaction: CommonTransaction{
TransactionHash: utils.TestHexToFelt(t, "0x46a9f52a96b2d226407929e04cb02507e531f7c78b9196fc8c910351d8c33f3"),
TransactionHash: utils.TestHexToFelt(t, "0x23ea110ef578b24c2c2e7f5a25ee2983e9d430dc801c01302c1191fd7bf5948"),
BroadcastedTxnCommonProperties: BroadcastedTxnCommonProperties{
Type: TransactionType_Declare,
MaxFee: &felt.Zero,
Version: TransactionV0,
Signature: []*felt.Felt{},
Nonce: &felt.Zero,
MaxFee: utils.TestHexToFelt(t, "0x27a64c6eeca"),
Version: TransactionV1,
Signature: []*felt.Felt{utils.TestHexToFelt(t, "0x63c588e0e543458fb66d9aee3e1acc8ecd9694502533234b0680c9148bab82c"), utils.TestHexToFelt(t, "0xc7b2e56f0e9990edbe4b86be9ea515c46ec831e0b32cf475f37beb00459ebe")},
Nonce: utils.TestHexToFelt(t, "0x350"),
},
},
ClassHash: utils.TestHexToFelt(t, "0x6feb117d1c3032b0ae7bd3b50cd8ec4a78c621dca0d63ddc17890b78a6c3b49"),
SenderAddress: utils.TestHexToFelt(t, "0x1"),
ClassHash: utils.TestHexToFelt(t, "0x398f9b04d99ace4cf04d0739dd6649e5e73a3d089e59aef3711f8b079b38849"),
SenderAddress: utils.TestHexToFelt(t, "0x45dba6ce6a4dc3d2f31aa6da5f51007f1e43e84a1e62c4481bac5454dea4e6d"),
},
},
}
Expand All @@ -376,25 +375,11 @@ func TestBlockWithTxsAndDeployOrDeclare(t *testing.T) {
ExpectedError: nil,
},
{
BlockID: WithBlockHash(utils.TestHexToFelt(t, "0x424fba26a7760b63895abe0c366c2d254cb47090c6f9e91ba2b3fa0824d4fc9")),
ExpectedError: nil,
LookupTxnPositionInOriginal: 14,
LookupTxnPositionInExpected: 0,
ExpectedBlockWithTxs: &fullBlockGoerli310843,
},
{
BlockID: WithBlockNumber(310843),
ExpectedError: nil,
LookupTxnPositionInOriginal: 14,
LookupTxnPositionInExpected: 0,
ExpectedBlockWithTxs: &fullBlockGoerli310843,
},
{
BlockID: WithBlockNumber(300114),
BlockID: WithBlockNumber(848634),
ExpectedError: nil,
LookupTxnPositionInOriginal: 3,
LookupTxnPositionInOriginal: 0,
LookupTxnPositionInExpected: 0,
ExpectedBlockWithTxs: &fullBlockGoerli300114,
ExpectedBlockWithTxs: &fullBlockGoerli848634,
},
},
"mainnet": {},
Expand Down Expand Up @@ -495,8 +480,8 @@ func TestCaptureUnsupportedBlockTxn(t *testing.T) {
"mock": {},
"testnet": {
{
StartBlock: 375919,
EndBlock: 376000,
StartBlock: 848634,
EndBlock: 848635,
},
},
"mainnet": {},
Expand All @@ -516,9 +501,8 @@ func TestCaptureUnsupportedBlockTxn(t *testing.T) {
_, okv0 := v.(InvokeTxnV0)
_, okl1 := v.(L1HandlerTxn)
_, okdec := v.(DeclareTxn)
_, okdep := v.(DeployTxn)
_, okdepac := v.(DeployAccountTxn)
if !okv0 && !okv1 && !okl1 && !okdec && !okdep && !okdepac {
if !okv0 && !okv1 && !okl1 && !okdec && !okdepac {
t.Fatalf("New Type Detected %T at Block(%d)/Txn(%d)", v, i, k)
}
}
Expand Down
10 changes: 5 additions & 5 deletions rpc/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// Class gets the contract class definition associated with the given hash.
func (provider *Provider) Class(ctx context.Context, blockID BlockID, classHash string) (GetClassOutput, error) {
func (provider *Provider) Class(ctx context.Context, blockID BlockID, classHash string) (ClassOutput, error) {
var rawClass map[string]any
if err := do(ctx, provider.c, "starknet_getClass", &rawClass, blockID, classHash); err != nil {
switch {
Expand All @@ -23,12 +23,12 @@ func (provider *Provider) Class(ctx context.Context, blockID BlockID, classHash
return nil, err
}

return typecastClassOutut(&rawClass)
return typecastClassOutput(&rawClass)

}

// ClassAt get the contract class definition at the given address.
func (provider *Provider) ClassAt(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (GetClassOutput, error) {
func (provider *Provider) ClassAt(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (ClassOutput, error) {
var rawClass map[string]any
if err := do(ctx, provider.c, "starknet_getClassAt", &rawClass, blockID, contractAddress); err != nil {
switch {
Expand All @@ -39,10 +39,10 @@ func (provider *Provider) ClassAt(ctx context.Context, blockID BlockID, contract
}
return nil, err
}
return typecastClassOutut(&rawClass)
return typecastClassOutput(&rawClass)
}

func typecastClassOutut(rawClass *map[string]any) (GetClassOutput, error) {
func typecastClassOutput(rawClass *map[string]any) (ClassOutput, error) {
rawClassByte, err := json.Marshal(rawClass)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions rpc/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

// Events returns all events matching the given filter
func (provider *Provider) Events(ctx context.Context, input EventsInput) (*EventsOutput, error) {
var result EventsOutput
func (provider *Provider) Events(ctx context.Context, input EventsInput) (*EventChunk, error) {
var result EventChunk
if err := do(ctx, provider.c, "starknet_getEvents", &result, input); err != nil {
switch {
case errors.Is(err, ErrPageSizeTooBig):
Expand Down
Loading

0 comments on commit a8bacfb

Please sign in to comment.